Spaces:
Sleeping

File size: 1,121 Bytes
0aa7d43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, File, UploadFile
from fastapi.responses import JSONResponse
from src.config import Config
from src.inference import detector # Imports the loaded model instance

app = FastAPI(
    title=Config.API_TITLE,
    description=Config.API_DESC,
    version=Config.API_VERSION
)

@app.get("/health")
def health_check():
    return {"status": "operational", "model": Config.MODEL_PATH}

@app.post("/predict")
async def predict(file: UploadFile = File(...)):
    try:
        # 1. Read Image
        image_bytes = await file.read()
        img = detector.process_image(image_bytes)

        # 2. Inference
        raw_results = detector.run_inference(img)

        # 3. Formatting
        detections, base64_img = detector.format_results(raw_results)

        return JSONResponse(content={
            "meta": {
                "count": len(detections),
                "model": "YOLOv8-X"
            },
            "detections": detections,
            "annotated_image_base64": base64_img
        })

    except Exception as e:
        return JSONResponse(content={"error": str(e)}, status_code=500)