File size: 594 Bytes
1e5f3d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from fastapi import FastAPI, File, UploadFile, Form
from inference import predict
import shutil
import os

app = FastAPI()

UPLOAD_DIR = "temp"
os.makedirs(UPLOAD_DIR, exist_ok=True)

@app.post("/predict")
async def predict_api(file: UploadFile = File(...), question: str = Form(...)):
    try:
        file_path = os.path.join(UPLOAD_DIR, file.filename)

        with open(file_path, "wb") as buffer:
            shutil.copyfileobj(file.file, buffer)

        answer = predict(file_path, question)

        return {"answer": answer}

    except Exception as e:
        return {"error": str(e)}