File size: 654 Bytes
5c6e546
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import fastapi
import io
from fastapi import UploadFile, File
from PIL import Image
from .predict import predict_image

app = fastapi.FastAPI()

@app.get("/")
def root():
    return {"message": "API is working!"}

@app.post("/predict-image")
async def upload_image(file: UploadFile = File(...)):
    if not file.content_type.startswith("image/"):
        return {"error": "Please upload an image to proceed"}

    contents = await file.read()

    try:
        image = Image.open(io.BytesIO(contents))
    except Exception as e:
        return {"error": f"Failed to open image: {str(e)}"}

    pred = predict_image(image)
    return {"prediction": pred}