arittrabag commited on
Commit
cb2008f
·
verified ·
1 Parent(s): 9096d87

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, UploadFile, File
2
+ from fastapi.responses import JSONResponse
3
+ from ultralytics import YOLO
4
+ import shutil
5
+ import os
6
+ from pathlib import Path
7
+
8
+ # Load model at startup
9
+ MODEL_PATH = Path("best.pt")
10
+ model = YOLO(str(MODEL_PATH))
11
+
12
+ app = FastAPI(title="Yellow Disease Detection API")
13
+
14
+ @app.get("/")
15
+ def root():
16
+ return {"status": "ok", "message": "YOLOv8 Yellow Disease Detection API is running 🚀"}
17
+
18
+ @app.post("/predict/")
19
+ async def predict(file: UploadFile = File(...)):
20
+ # Save temp file
21
+ tmp_path = f"temp_{file.filename}"
22
+ with open(tmp_path, "wb") as buffer:
23
+ shutil.copyfileobj(file.file, buffer)
24
+
25
+ # Run YOLO inference
26
+ results = model.predict(source=tmp_path, conf=0.5, imgsz=640)
27
+
28
+ # Parse predictions
29
+ preds = []
30
+ for r in results:
31
+ for box in r.boxes:
32
+ preds.append({
33
+ "cls": int(box.cls.item()),
34
+ "label": model.names[int(box.cls.item())],
35
+ "confidence": float(box.conf.item()),
36
+ "bbox": box.xyxy[0].tolist()
37
+ })
38
+
39
+ # cleanup
40
+ os.remove(tmp_path)
41
+
42
+ return JSONResponse(content={"predictions": preds})