Spaces:
Configuration error
Configuration error
File size: 1,417 Bytes
2945bec |
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 39 40 41 42 43 44 45 46 47 48 |
from fastapi import FastAPI, File, UploadFile, Form
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware
from app.utils.predict import predict_logo
import shutil
import os
import uuid
app = FastAPI(title="Fake Logo Detector API")
# Allow frontend calls (camera/gallery)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
UPLOAD_DIR = "temp_uploads"
os.makedirs(UPLOAD_DIR, exist_ok=True)
@app.post("/scan-logo/")
async def scan_logo(image: UploadFile = File(...), brand: str = Form("Unknown")):
try:
# Save the uploaded image temporarily
temp_filename = os.path.join(UPLOAD_DIR, f"{uuid.uuid4()}.jpg")
with open(temp_filename, "wb") as buffer:
shutil.copyfileobj(image.file, buffer)
# Call prediction logic
verdict, confidence = predict_logo(temp_filename)
# Delete temp file
os.remove(temp_filename)
return JSONResponse(content={
"brand": brand,
"verdict": verdict,
"confidence": round(confidence * 100, 2)
})
except Exception as e:
return JSONResponse(status_code=500, content={"error": str(e)})
@app.get("/")
async def root():
return {"message": "Fake Logo Detector API is running!"} |