jayn95 commited on
Commit
a13af90
·
verified ·
1 Parent(s): ea1465c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -38
app.py CHANGED
@@ -44,44 +44,49 @@ def base64_to_pil(b64_str):
44
 
45
 
46
  # --- Main detection ---
47
- def detect_gingivitis(image, conf=0.25, iou=0.5):
48
- image = preprocess(image)
49
-
50
- sw_res = model_swelling.predict(image, conf=conf, iou=iou)
51
- rd_res = model_redness.predict(image, conf=conf, iou=iou)
52
- bl_res = model_bleeding.predict(image, conf=conf, iou=iou)
53
-
54
- # Draw boxes only (no labels)
55
- img_sw = sw_res[0].plot(labels=False)[:, :, ::-1]
56
- img_rd = rd_res[0].plot(labels=False)[:, :, ::-1]
57
- img_bl = bl_res[0].plot(labels=False)[:, :, ::-1]
58
-
59
- # Encode images to Base64 (for backend API consumption)
60
- sw_b64 = np_to_base64(img_sw)
61
- rd_b64 = np_to_base64(img_rd)
62
- bl_b64 = np_to_base64(img_bl)
63
-
64
- # Convert Base64 back to PIL for Gradio display
65
- sw_pil = base64_to_pil(sw_b64)
66
- rd_pil = base64_to_pil(rd_b64)
67
- bl_pil = base64_to_pil(bl_b64)
68
-
69
- # Determine diagnosis
70
- has_sw = len(sw_res[0].boxes) > 0
71
- has_rd = len(rd_res[0].boxes) > 0
72
- has_bl = len(bl_res[0].boxes) > 0
73
-
74
- if has_sw and has_rd and has_bl:
75
- diagnosis = (
76
- "🦷 You have gingivitis.\n\n"
77
- "For accurate assessment and guidance, we recommend visiting your dentist.\n"
78
- "If you have a periapical X-ray, you may try the *Detect Periodontitis* tool."
79
- )
80
- else:
81
- diagnosis = "🟢 You don't have gingivitis."
82
-
83
- # Return PIL for Gradio + Base64 is available for backend
84
- return [sw_pil, rd_pil, bl_pil, diagnosis]
 
 
 
 
 
85
 
86
 
87
  # --- Gradio Interface ---
 
44
 
45
 
46
  # --- Main detection ---
47
+ def detect_gingivitis_safe(image, conf=0.25, iou=0.5):
48
+ try:
49
+ if image is None:
50
+ return [None, None, None, "❌ No image uploaded"]
51
+
52
+ # Lazy-load models
53
+ model_swelling = get_swelling_model()
54
+ model_redness = get_redness_model()
55
+ model_bleeding = get_bleeding_model()
56
+
57
+ # Preprocess image
58
+ image = preprocess(image)
59
+
60
+ # Run models
61
+ sw_res = model_swelling.predict(image, conf=conf, iou=iou)
62
+ rd_res = model_redness.predict(image, conf=conf, iou=iou)
63
+ bl_res = model_bleeding.predict(image, conf=conf, iou=iou)
64
+
65
+ # Convert to images
66
+ img_sw = sw_res[0].plot(labels=False)[:, :, ::-1]
67
+ img_rd = rd_res[0].plot(labels=False)[:, :, ::-1]
68
+ img_bl = bl_res[0].plot(labels=False)[:, :, ::-1]
69
+
70
+ sw_pil = base64_to_pil(np_to_base64(img_sw))
71
+ rd_pil = base64_to_pil(np_to_base64(img_rd))
72
+ bl_pil = base64_to_pil(np_to_base64(img_bl))
73
+
74
+ # Determine diagnosis
75
+ has_sw = len(sw_res[0].boxes) > 0
76
+ has_rd = len(rd_res[0].boxes) > 0
77
+ has_bl = len(bl_res[0].boxes) > 0
78
+
79
+ if has_sw and has_rd and has_bl:
80
+ diagnosis = "🦷 You have gingivitis."
81
+ else:
82
+ diagnosis = "🟢 You don't have gingivitis."
83
+
84
+ return [sw_pil, rd_pil, bl_pil, diagnosis]
85
+
86
+ except Exception as e:
87
+ # Catch all errors and return a friendly message
88
+ return [None, None, None, f"❌ Error during processing: {str(e)}"]
89
+
90
 
91
 
92
  # --- Gradio Interface ---