Spaces:
Sleeping
Sleeping
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") | |
def index() -> FileResponse: | |
return FileResponse(path="static/index.html", media_type="text/html") | |
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 |