[python] FastAPI๋กœ ๋”ฅ๋Ÿฌ๋‹ ๋ชจ๋ธ API ๊ตฌ์ถ•ํ•˜๊ธฐ | ๋ฌธ์ž์—ด ํŒŒ์ผ๊ฒฝ๋กœ, Bytes ํƒ€์ž… ์ž…๋ ฅ๋ฐ›๊ธฐ

2023. 8. 3. 09:27ยท๐Ÿ’ป Programming/Python
๋ฐ˜์‘ํ˜•

FastAPI๋ฅผ ์ด์šฉํ•œ API ์ƒ์„ฑ
from typing import List
from pydantic import BaseModel

from fastapi import FastAPI, Depends, File, Request
import uvicorn

app = FastAPI()

class InputData(BaseModel):
    data: str

@app.on_event("startup")
async def load_ai_model():
    global model
    model = My_Model()
    print("AI Model Loaded")

async def parse_body(request: Request):
    data: bytes = await request.body()
    return data

@app.post("/model_inference_str_input/")
def AI_model_inference(input: InputData):
    output = model(input.data)

    return output

@app.post("/model_inference_bytes_input/")
def AI_model_inference_2(image_bytes: bytes = Depends(parse_body)):
    output = model(image_bytes)
    
    return output
 

if __name__ == "__main__":
    uvicorn.run(app, host="localhost", port=8000)

API๋ฅผ ๋งŒ๋“ค ๋•Œ ์ž…๋ ฅ์œผ๋กœ ํŒŒ์ผ ๊ฒฝ๋กœ(๋ฌธ์ž์—ด)๋ฅผ ๋ฐ›๊ณ  ์‹ถ์„ ์ˆ˜๋„ ์žˆ๊ณ  Bytes ํฌ๋งท์˜ ๋ณ€์ˆ˜๋ฅผ ๋ฐ›๊ณ  ์‹ถ์„ ์ˆ˜๋„ ์žˆ๋‹ค. 

 

  • load_ai_model : 'on_event' ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์„œ๋ฒ„๊ฐ€ ์‹œ์ž‘๋  ๋•Œ ํ˜ธ์ถœํ•˜์—ฌ AI ๋ชจ๋ธ์„ ๋ฏธ๋ฆฌ ๋ถˆ๋Ÿฌ์˜จ๋‹ค
  • AI_model_inference : ํŒŒ์ผ ๊ฒฝ๋กœ(๋ฌธ์ž์—ด)์„ ์ž…๋ ฅ๋ฐ›๋Š” ํ•จ์ˆ˜
  • AI_model_inference_2 : Bytes ๋ณ€์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š” ํ•จ์ˆ˜

 

uvicorn.run()์„ ์‚ฌ์šฉํ•˜์—ฌ ์„œ๋ฒ„๋ฅผ ์‹คํ–‰ํ•˜๋ฉฐ, ์ด๋ฅผ ์‹คํ–‰ํ•˜๋ฉด API๊ฐ€ http://localhost:8000 ์ฃผ์†Œ์—์„œ ์‹คํ–‰๋œ๋‹ค.

 

์ด์ œ ์„œ๋ฒ„๋ฅผ ์‹คํ–‰ํ•˜๊ณ  http://localhost:8000/docs๋กœ ์ด๋™ํ•˜์—ฌ ์ž๋™ ์ƒ์„ฑ๋œ API ๋ฌธ์„œ์—์„œ ๋‘ ์—”๋“œํฌ์ธํŠธ์— ๋Œ€ํ•œ ์ƒ์„ธ ์ •๋ณด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ API ๋ฌธ์„œ๋ฅผ ํ†ตํ•ด ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์˜ ํ˜•์‹๊ณผ ์š”์ฒญ ๋ฐฉ๋ฒ•์„ ํ™•์ธํ•˜๊ณ  ํ…Œ์ŠคํŠธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

ํŒŒ์ผ ๊ฒฝ๋กœ(๋ฌธ์ž์—ด)๋ฅผ ์ž…๋ ฅํ•˜๋Š” API ํ˜ธ์ถœ
import requests
import numpy as np

url = 'http://localhost:8000/model_inference_str_input/'

input_image_path = 'image.jpg'

response = requests.post(url, json={'data': input_image_path})

if response.status_code == 200:
    result = response.json()
    print(result)
else:
    print("API ํ˜ธ์ถœ ์‹คํŒจ:", response.text)

 

Bytes ๋ณ€์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜๋Š” API ํ˜ธ์ถœ
import requests
import numpy as np

url = 'http://localhost:8000/model_inference_bytes_input/'

input_image_path = 'image.jpg'

# ์ด๋ฏธ์ง€ ํŒŒ์ผ์„ ๋ฐ”์ดํŠธ ํ˜•ํƒœ๋กœ ์ฝ์–ด์˜ต๋‹ˆ๋‹ค.
with open(input_image_path, "rb") as image_file:
    image_bytes = image_file.read()

response = requests.post(url, data = image_bytes)

if response.status_code == 200:
    result = response.json()
    print(result)
else:
    print("API ํ˜ธ์ถœ ์‹คํŒจ:", response.text)
๋ฐ˜์‘ํ˜•

'๐Ÿ’ป Programming > Python' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[pandas] DataFrame ์„ค๋ช… | ๋ฐ์ดํ„ฐ ์กฐ์ž‘, ํ•„ํ„ฐ๋ง, ์‹œ๊ฐํ™”, ํ†ต๊ณ„ ๋ถ„์„  (0) 2023.11.16
[pandas] 2์ฐจ์› ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ | pd.DataFrame  (0) 2023.11.16
[python] ๊ตฌ๊ธ€ ๊ฒ€์ƒ‰ ์ด๋ฏธ์ง€ ํฌ๋กค๋ง/์Šคํฌ๋ž˜ํ•‘ํ•˜๊ธฐ  (0) 2023.08.15
curl ์ปค๋งจ๋“œ๋ฅผ python, javascript ๋“ฑ์˜ ์–ธ์–ด๋กœ ๋ณ€ํ™˜ | curl ์ปค๋งจ๋“œ api๋ฅผ ํŒŒ์ด์ฌ ์ฝ”๋“œ๋กœ ๋ณ€ํ™˜ํ•  ๋•Œ  (0) 2023.07.11
[python] ๋ฐ˜๋ณต๋ฌธ ํšจ๊ณผ์ ์œผ๋กœ ๋””๋ฒ„๊น…ํ•˜๊ธฐ: try-except ๋ธ”๋ก  (0) 2022.12.15
'๐Ÿ’ป Programming/Python' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • [pandas] 2์ฐจ์› ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ | pd.DataFrame
  • [python] ๊ตฌ๊ธ€ ๊ฒ€์ƒ‰ ์ด๋ฏธ์ง€ ํฌ๋กค๋ง/์Šคํฌ๋ž˜ํ•‘ํ•˜๊ธฐ
  • curl ์ปค๋งจ๋“œ๋ฅผ python, javascript ๋“ฑ์˜ ์–ธ์–ด๋กœ ๋ณ€ํ™˜ | curl ์ปค๋งจ๋“œ api๋ฅผ ํŒŒ์ด์ฌ ์ฝ”๋“œ๋กœ ๋ณ€ํ™˜ํ•  ๋•Œ
  • [python] ๋ฐ˜๋ณต๋ฌธ ํšจ๊ณผ์ ์œผ๋กœ ๋””๋ฒ„๊น…ํ•˜๊ธฐ: try-except ๋ธ”๋ก
๋ญ…์ฆค
๋ญ…์ฆค
AI ๊ธฐ์ˆ  ๋ธ”๋กœ๊ทธ
    ๋ฐ˜์‘ํ˜•
  • ๋ญ…์ฆค
    CV DOODLE
    ๋ญ…์ฆค
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
  • ๊ณต์ง€์‚ฌํ•ญ

    • โœจ About Me
    • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (198)
      • ๐Ÿ“– Fundamentals (33)
        • Computer Vision (9)
        • 3D vision & Graphics (6)
        • AI & ML (15)
        • NLP (2)
        • etc. (1)
      • ๐Ÿ› Research (64)
        • Deep Learning (7)
        • Image Classification (2)
        • Detection & Segmentation (17)
        • OCR (7)
        • Multi-modal (4)
        • Generative AI (6)
        • 3D Vision (2)
        • Material & Texture Recognit.. (8)
        • NLP & LLM (11)
        • etc. (0)
      • ๐ŸŒŸ AI & ML Tech (7)
        • AI & ML ์ธ์‚ฌ์ดํŠธ (7)
      • ๐Ÿ’ป Programming (85)
        • Python (18)
        • Computer Vision (12)
        • LLM (4)
        • AI & ML (17)
        • Database (3)
        • Apache Airflow (6)
        • Docker & Kubernetes (14)
        • ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ (4)
        • C++ (1)
        • etc. (6)
      • ๐Ÿ’ฌ ETC (3)
        • ์ฑ… ๋ฆฌ๋ทฐ (3)
  • ๋งํฌ

  • ์ธ๊ธฐ ๊ธ€

  • ํƒœ๊ทธ

    ์ปดํ“จํ„ฐ๋น„์ „
    ๋”ฅ๋Ÿฌ๋‹
    Image Classification
    VLP
    OpenAI
    ๊ฐ์ฒด๊ฒ€์ถœ
    LLM
    material recognition
    nlp
    deep learning
    AI
    CNN
    Computer Vision
    segmentation
    ChatGPT
    OpenCV
    object detection
    multi-modal
    Python
    ๋„์ปค
    ํ”„๋กฌํ”„ํŠธ์—”์ง€๋‹ˆ์–ด๋ง
    3D Vision
    airflow
    ํŒŒ์ด์ฌ
    pandas
    pytorch
    Text recognition
    ๊ฐ์ฒด ๊ฒ€์ถœ
    OCR
    GPT
  • ์ตœ๊ทผ ๋Œ“๊ธ€

  • ์ตœ๊ทผ ๊ธ€

  • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.3
๋ญ…์ฆค
[python] FastAPI๋กœ ๋”ฅ๋Ÿฌ๋‹ ๋ชจ๋ธ API ๊ตฌ์ถ•ํ•˜๊ธฐ | ๋ฌธ์ž์—ด ํŒŒ์ผ๊ฒฝ๋กœ, Bytes ํƒ€์ž… ์ž…๋ ฅ๋ฐ›๊ธฐ
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”