Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -11,9 +11,9 @@ import json
|
|
11 |
# ================== MODEL LOADING ==================
|
12 |
try:
|
13 |
model = load_model('wound_classifier_model_googlenet.h5')
|
14 |
-
print("
|
15 |
except Exception as e:
|
16 |
-
raise RuntimeError(f"
|
17 |
|
18 |
# ================== CLASS LABELS ==================
|
19 |
CLASS_LABELS = [
|
@@ -37,14 +37,14 @@ def preprocess_image(image, target_size=(224, 224)):
|
|
37 |
"""Process and validate input images"""
|
38 |
try:
|
39 |
if not image:
|
40 |
-
raise ValueError("
|
41 |
|
42 |
image = image.convert("RGB").resize(target_size)
|
43 |
array = np.array(image) / 255.0
|
44 |
-
print(f"
|
45 |
return array
|
46 |
except Exception as e:
|
47 |
-
raise RuntimeError(f"
|
48 |
|
49 |
# ================== MEDICAL GUIDELINES ==================
|
50 |
def get_medical_guidelines(wound_type):
|
@@ -62,7 +62,7 @@ def get_medical_guidelines(wound_type):
|
|
62 |
Use clear, simple language without markdown."""
|
63 |
|
64 |
try:
|
65 |
-
print(f"
|
66 |
response = requests.post(
|
67 |
OPENROUTER_API_URL,
|
68 |
headers=headers,
|
@@ -76,15 +76,15 @@ def get_medical_guidelines(wound_type):
|
|
76 |
|
77 |
response.raise_for_status()
|
78 |
result = response.json()
|
79 |
-
print("
|
80 |
|
81 |
if not result.get("choices"):
|
82 |
-
return "
|
83 |
|
84 |
return result["choices"][0]["message"]["content"]
|
85 |
|
86 |
except Exception as e:
|
87 |
-
return f"
|
88 |
|
89 |
# ================== MAIN PREDICTION ==================
|
90 |
def predict(image):
|
@@ -111,44 +111,26 @@ def predict(image):
|
|
111 |
return results, guidelines
|
112 |
|
113 |
except Exception as e:
|
114 |
-
return {f"
|
115 |
|
116 |
# ================== GRADIO INTERFACE ==================
|
117 |
def create_interface():
|
118 |
with gr.Blocks(title="AI Wound Classifier") as demo:
|
119 |
-
gr.Markdown("#
|
120 |
gr.Markdown("Upload a wound image or take a photo using your camera")
|
121 |
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
with gr.TabItem("\ud83d\udcf7 Use Camera"):
|
127 |
-
webcam_input = gr.Video(
|
128 |
-
mirror_webcam=False,
|
129 |
-
label="Take Wound Photo"
|
130 |
-
)
|
131 |
-
|
132 |
-
with gr.Row():
|
133 |
-
submit_btn = gr.Button("Analyze Now", variant="primary")
|
134 |
-
|
135 |
-
with gr.Row():
|
136 |
-
output_label = gr.Label(label="Top Predictions", num_top_classes=3)
|
137 |
-
output_guidelines = gr.Textbox(label="Treatment Guidelines", lines=8)
|
138 |
|
139 |
-
# Connect
|
140 |
submit_btn.click(
|
141 |
fn=predict,
|
142 |
inputs=[file_input],
|
143 |
outputs=[output_label, output_guidelines]
|
144 |
)
|
145 |
-
|
146 |
-
webcam_input.change(
|
147 |
-
fn=predict,
|
148 |
-
inputs=[webcam_input],
|
149 |
-
outputs=[output_label, output_guidelines]
|
150 |
-
)
|
151 |
-
|
152 |
return demo
|
153 |
|
154 |
if __name__ == "__main__":
|
@@ -156,6 +138,5 @@ if __name__ == "__main__":
|
|
156 |
iface.launch(
|
157 |
server_name="0.0.0.0",
|
158 |
server_port=7860,
|
159 |
-
|
160 |
-
share=False # Set to True if you want a public link
|
161 |
)
|
|
|
11 |
# ================== MODEL LOADING ==================
|
12 |
try:
|
13 |
model = load_model('wound_classifier_model_googlenet.h5')
|
14 |
+
print("✅ Model loaded successfully")
|
15 |
except Exception as e:
|
16 |
+
raise RuntimeError(f"❌ Model loading failed: {str(e)}")
|
17 |
|
18 |
# ================== CLASS LABELS ==================
|
19 |
CLASS_LABELS = [
|
|
|
37 |
"""Process and validate input images"""
|
38 |
try:
|
39 |
if not image:
|
40 |
+
raise ValueError("🚨 No image provided")
|
41 |
|
42 |
image = image.convert("RGB").resize(target_size)
|
43 |
array = np.array(image) / 255.0
|
44 |
+
print(f"🖼️ Image processed: Shape {array.shape}")
|
45 |
return array
|
46 |
except Exception as e:
|
47 |
+
raise RuntimeError(f"🖼️ Image processing failed: {str(e)}")
|
48 |
|
49 |
# ================== MEDICAL GUIDELINES ==================
|
50 |
def get_medical_guidelines(wound_type):
|
|
|
62 |
Use clear, simple language without markdown."""
|
63 |
|
64 |
try:
|
65 |
+
print(f"📡 Sending API request for {wound_type}...")
|
66 |
response = requests.post(
|
67 |
OPENROUTER_API_URL,
|
68 |
headers=headers,
|
|
|
76 |
|
77 |
response.raise_for_status()
|
78 |
result = response.json()
|
79 |
+
print("🔧 Raw API response:", json.dumps(result, indent=2))
|
80 |
|
81 |
if not result.get("choices"):
|
82 |
+
return "⚠️ API response format unexpected"
|
83 |
|
84 |
return result["choices"][0]["message"]["content"]
|
85 |
|
86 |
except Exception as e:
|
87 |
+
return f"⚠️ Guidelines unavailable: {str(e)}"
|
88 |
|
89 |
# ================== MAIN PREDICTION ==================
|
90 |
def predict(image):
|
|
|
111 |
return results, guidelines
|
112 |
|
113 |
except Exception as e:
|
114 |
+
return {f"🚨 Error": str(e)}, ""
|
115 |
|
116 |
# ================== GRADIO INTERFACE ==================
|
117 |
def create_interface():
|
118 |
with gr.Blocks(title="AI Wound Classifier") as demo:
|
119 |
+
gr.Markdown("# 🩹 AI-Powered Wound Classification System")
|
120 |
gr.Markdown("Upload a wound image or take a photo using your camera")
|
121 |
|
122 |
+
file_input = gr.Image(type="pil", label="Upload Wound Image")
|
123 |
+
submit_btn = gr.Button("Analyze Now", variant="primary")
|
124 |
+
output_label = gr.Label(label="Top Predictions", num_top_classes=3)
|
125 |
+
output_guidelines = gr.Textbox(label="Treatment Guidelines", lines=8)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
|
127 |
+
# Connect input to processing
|
128 |
submit_btn.click(
|
129 |
fn=predict,
|
130 |
inputs=[file_input],
|
131 |
outputs=[output_label, output_guidelines]
|
132 |
)
|
133 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
134 |
return demo
|
135 |
|
136 |
if __name__ == "__main__":
|
|
|
138 |
iface.launch(
|
139 |
server_name="0.0.0.0",
|
140 |
server_port=7860,
|
141 |
+
share=True # Set to False if you do not want a public link
|
|
|
142 |
)
|