Implement lazy loading to avoid Hugging Face readiness timeout
Browse files
app.py
CHANGED
|
@@ -1,15 +1,19 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from solution import TrafficViolationDetector
|
| 3 |
|
| 4 |
-
# Initialize
|
| 5 |
-
|
| 6 |
-
detector = TrafficViolationDetector(model_dir="./models")
|
| 7 |
-
print("Models loaded successfully!")
|
| 8 |
|
| 9 |
def detect_violations(image_path):
|
|
|
|
| 10 |
if image_path is None:
|
| 11 |
return {"error": "No image provided"}
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
try:
|
| 14 |
# The detector.predict expects a path to the image
|
| 15 |
result = detector.predict(image_path)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from solution import TrafficViolationDetector
|
| 3 |
|
| 4 |
+
# Initialize globally but lazily to avoid Hugging Face 60s boot timeout
|
| 5 |
+
detector = None
|
|
|
|
|
|
|
| 6 |
|
| 7 |
def detect_violations(image_path):
|
| 8 |
+
global detector
|
| 9 |
if image_path is None:
|
| 10 |
return {"error": "No image provided"}
|
| 11 |
|
| 12 |
+
if detector is None:
|
| 13 |
+
print("Lazy loading models on first request...")
|
| 14 |
+
detector = TrafficViolationDetector(model_dir="./models")
|
| 15 |
+
print("Models loaded successfully!")
|
| 16 |
+
|
| 17 |
try:
|
| 18 |
# The detector.predict expects a path to the image
|
| 19 |
result = detector.predict(image_path)
|