File size: 1,032 Bytes
62139b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, UploadFile, File
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
from fastapi.middleware.cors import CORSMiddleware
from inference import predict_hotdog
import io
from PIL import Image

app = FastAPI()

# Add CORS middleware
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # In production, specify your frontend URL
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Mount static files at /static, not at root
app.mount("/static", StaticFiles(directory="static"), name="static")

@app.get("/")
def index() -> FileResponse:
    return FileResponse(path="static/index.html", media_type="text/html")

@app.post("/predict")
async def predict_hotdog_endpoint(file: UploadFile = File(...)):
    # Read the uploaded file
    contents = await file.read()
    
    # Convert to PIL Image
    img = Image.open(io.BytesIO(contents))
    
    # Make prediction
    predictions = predict_hotdog(img)
    
    return predictions