FastAPI로 AI 프로덕션 배포하기
Matt Eckerle
Software and Data Engineering Leader
필수 라이브러리:
FastAPI: Python으로 API를 구축하는 프레임워크uvicorn: Python 웹 앱을 실행하는 고속 ASGI 서버joblib: 모델 로드에 사용
from fastapi import FastAPI
import uvicorn
import joblib
# Create the FastAPI app instance
app = FastAPI()
import joblib
# Load the pre-trained model
model = joblib.load('penguin_classifier.pkl')
# Check data type of model to verify model loading
print(type(model))
<class 'sklearn.pipeline.Pipeline'>
uvicorn main:app \
--host 0.0.0.0 \
--port 8080
import uvicorn
uvicorn.run(app,
host="0.0.0.0",
port=8080)

# FastAPI prediction endpoint
@app.post("/predict")
def predict(culmen_length_mm, culmen_depth_mm,
flipper_length_mm, body_mass_g):
features = [[culmen_length_mm, culmen_depth_mm,
flipper_length_mm, body_mass_g]]
prediction = model.predict(features)[0]
return {"predicted_species": prediction}
if __name__ == "__main__":
uvicorn.run(
app,
host="0.0.0.0",
port=8080)
모든 코드를 파이썬 파일로 저장합니다 - your_api_script.py
$ python3 your_api_script.py

curl \-X POST "http://localhost:8080/predict" \-H "Content-Type: application/json" \-d '{"culmen_length_mm": 39.1, "culmen_depth_mm": 18.7, "flipper_length_mm": 181, "body_mass_g": 3750}'
{
"prediction": "Adelie",
"confidence": 0.87
}
FastAPI로 AI 프로덕션 배포하기