-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_module.py
55 lines (42 loc) · 1.74 KB
/
api_module.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# api_module.py
import os
from fastapi import FastAPI, Request, Form
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from data_processing import preprocess_canvas_images
from model import load_model, predict
from monitoring import log_info, log_error
from database import save_prediction
def create_app():
app = FastAPI()
# 모델 로드
model = load_model()
# 템플릿 설정
templates = Jinja2Templates(directory="templates")
@app.get("/", response_class=HTMLResponse)
async def home(request: Request):
return templates.TemplateResponse("index.html", {"request": request})
@app.post("/", response_class=HTMLResponse)
async def predict_route(
request: Request,
image1: str = Form(...),
image2: str = Form(...),
image3: str = Form(...),
image4: str = Form(...)
):
try:
images_data = [image1, image2, image3, image4]
# 이미지 전처리
input_data_list, raw_images = preprocess_canvas_images(images_data)
# 예측 수행
predictions = predict(model, input_data_list)
# 결과 저장 (이미지와 예측 결과를 함께 저장)
save_prediction(raw_images, predictions)
# 로그 정보
log_info(f"Predictions: {predictions}")
# 결과 반환
return templates.TemplateResponse("index.html", {"request": request, "predictions": predictions, "error": None})
except Exception as e:
log_error(f"Error in predict_route: {str(e)}")
return templates.TemplateResponse("result.html", {"request": request, "predictions": None, "error": "내부 서버 오류"})
return app