jayn95 commited on
Commit
ddfb6ec
·
verified ·
1 Parent(s): f96dd8f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -9
app.py CHANGED
@@ -2,12 +2,29 @@ import gradio as gr
2
  from ultralytics import YOLO
3
  from PIL import Image, ImageOps, ImageEnhance
4
  import numpy as np
5
- import tempfile, io, base64
6
 
7
- # Load models
8
- model_swelling = YOLO("models/swelling/swelling_final.pt")
9
- model_redness = YOLO("models/redness/redness_final.pt")
10
- model_bleeding = YOLO("models/bleeding/bleeding_final.pt")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
 
13
  # --- Helpers ---
@@ -49,13 +66,16 @@ def detect_gingivitis(image, conf=0.25, iou=0.5):
49
  if image is None:
50
  return [None, None, None, "❌ No image uploaded"]
51
 
 
 
 
52
  # Preprocess image
53
  image = preprocess(image)
54
 
55
- # Run models
56
- sw_res = model_swelling.predict(image, conf=conf, iou=iou)
57
- rd_res = model_redness.predict(image, conf=conf, iou=iou)
58
- bl_res = model_bleeding.predict(image, conf=conf, iou=iou)
59
 
60
  # Convert to images
61
  img_sw = sw_res[0].plot(labels=False)[:, :, ::-1]
@@ -105,6 +125,13 @@ interface = gr.Interface(
105
  title="Gingivitis Detection"
106
  )
107
 
 
 
 
 
 
 
 
108
  # interface.launch()
109
  if __name__ == "__main__":
110
  interface.launch(server_name="0.0.0.0", server_port=7860, show_error=True)
 
2
  from ultralytics import YOLO
3
  from PIL import Image, ImageOps, ImageEnhance
4
  import numpy as np
5
+ import io, base64
6
 
7
+ # =========================================================
8
+ # Lazy-loaded global models (LOAD ONLY ON FIRST REQUEST)
9
+ # =========================================================
10
+ model_swelling = None
11
+ model_redness = None
12
+ model_bleeding = None
13
+
14
+ def get_models():
15
+ """Load YOLO models only once (lazy loading)."""
16
+ global model_swelling, model_redness, model_bleeding
17
+
18
+ if model_swelling is None:
19
+ model_swelling = YOLO("models/swelling/swelling_final.pt")
20
+
21
+ if model_redness is None:
22
+ model_redness = YOLO("models/redness/redness_final.pt")
23
+
24
+ if model_bleeding is None:
25
+ model_bleeding = YOLO("models/bleeding/bleeding_final.pt")
26
+
27
+ return model_swelling, model_redness, model_bleeding
28
 
29
 
30
  # --- Helpers ---
 
66
  if image is None:
67
  return [None, None, None, "❌ No image uploaded"]
68
 
69
+ # Load models (only once)
70
+ sw_model, rd_model, bl_model = get_models()
71
+
72
  # Preprocess image
73
  image = preprocess(image)
74
 
75
+ # Run detections
76
+ sw_res = sw_model.predict(image, conf=conf, iou=iou)
77
+ rd_res = rd_model.predict(image, conf=conf, iou=iou)
78
+ bl_res = bl_model.predict(image, conf=conf, iou=iou)
79
 
80
  # Convert to images
81
  img_sw = sw_res[0].plot(labels=False)[:, :, ::-1]
 
125
  title="Gingivitis Detection"
126
  )
127
 
128
+ # =========================================================
129
+ # Warm-start: preload models on startup
130
+ # =========================================================
131
+ print("🔥 Preloading models to reduce Render cold start...")
132
+ get_models()
133
+ print("✅ Gingivitis models ready")
134
+
135
  # interface.launch()
136
  if __name__ == "__main__":
137
  interface.launch(server_name="0.0.0.0", server_port=7860, show_error=True)