testing_spaces / app /main.py
Klasta's picture
First commit
5c6e546
raw
history blame contribute delete
654 Bytes
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}