m4az19 commited on
Commit
f6b11b9
·
verified ·
1 Parent(s): d639e8e

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +41 -20
main.py CHANGED
@@ -7,46 +7,67 @@ import base64
7
  import traceback
8
 
9
  app = FastAPI()
 
 
10
  model = YOLO("best.pt")
11
 
 
12
  class ImageData(BaseModel):
13
  image_base64: str
14
 
15
- # This stops the 404 logs from Hugging Face's health checks
 
16
  @app.get("/")
17
  def read_root():
18
- return {"status": "SA'EF API is live and running!"}
 
 
 
19
 
20
  @app.post("/predict")
21
  async def predict(data: ImageData):
22
  try:
 
23
  image_bytes = base64.b64decode(data.image_base64)
24
- image = Image.open(io.BytesIO(image_bytes))
25
 
26
- # The model runs the image
 
 
 
27
  results = model(image)
28
 
29
- if len(results.boxes) > 0:
30
- box = results.boxes
31
-
32
- # Use .item() to safely convert PyTorch Tensors to standard JSON numbers
33
- confidence = float(box.conf.item())
34
- class_id = int(box.cls.item())
35
- class_name = results.names[class_id]
36
 
37
- return {
38
- "status": "success",
39
- "wound_type": class_name,
40
- "confidence": confidence
41
- }
42
- else:
43
  return {
44
  "status": "not_found",
45
  "message": "No wound detected"
46
  }
47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  except Exception as e:
49
- # If it ever crashes again, this will print the EXACT reason to your Hugging Face logs
50
- print("--- ERROR IN PREDICT ---")
51
  traceback.print_exc()
52
- raise HTTPException(status_code=500, detail=str(e))
 
 
 
 
 
7
  import traceback
8
 
9
  app = FastAPI()
10
+
11
+ # Load model once when server starts
12
  model = YOLO("best.pt")
13
 
14
+
15
  class ImageData(BaseModel):
16
  image_base64: str
17
 
18
+
19
+ # Prevent Hugging Face health-check 404 spam
20
  @app.get("/")
21
  def read_root():
22
+ return {
23
+ "status": "SA'EF API is live and running!"
24
+ }
25
+
26
 
27
  @app.post("/predict")
28
  async def predict(data: ImageData):
29
  try:
30
+ # Decode base64 image
31
  image_bytes = base64.b64decode(data.image_base64)
 
32
 
33
+ # Convert image safely to RGB
34
+ image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
35
+
36
+ # Run YOLO inference
37
  results = model(image)
38
 
39
+ # Get first result object
40
+ result = results[0]
 
 
 
 
 
41
 
42
+ # No detections found
43
+ if len(result.boxes) == 0:
 
 
 
 
44
  return {
45
  "status": "not_found",
46
  "message": "No wound detected"
47
  }
48
 
49
+ # Get first detected box
50
+ box = result.boxes[0]
51
+
52
+ # Convert tensors safely
53
+ confidence = float(box.conf.item())
54
+ class_id = int(box.cls.item())
55
+
56
+ # Get class name
57
+ class_name = result.names[class_id]
58
+
59
+ # Return successful response
60
+ return {
61
+ "status": "success",
62
+ "wound_type": class_name,
63
+ "confidence": confidence
64
+ }
65
+
66
  except Exception as e:
67
+ print("\n--- ERROR IN PREDICT ---")
 
68
  traceback.print_exc()
69
+
70
+ raise HTTPException(
71
+ status_code=500,
72
+ detail=str(e)
73
+ )