RichardLu commited on
Commit
6e38a14
·
verified ·
1 Parent(s): 096a3dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -5
app.py CHANGED
@@ -1,6 +1,5 @@
1
  """
2
  Hugging Face Spaces - Pneumonia Detection App
3
-
4
  This is a self-contained version for Hugging Face Spaces deployment.
5
  Copy this file as 'app.py' to your HF Spaces repository.
6
  """
@@ -13,6 +12,7 @@ from PIL import Image
13
  import numpy as np
14
  from pytorch_grad_cam import GradCAM
15
  from pytorch_grad_cam.utils.image import show_cam_on_image
 
16
 
17
  # =============================================================================
18
  # Configuration
@@ -192,14 +192,16 @@ if model_loaded:
192
  image = Image.open(uploaded_file).convert("RGB")
193
 
194
  with col1:
195
- st.image(image, caption="Uploaded X-Ray", width="stretch")
196
- analyze = st.button("🔬 Analyze Image", type="primary", width="stretch")
197
 
198
  if analyze:
199
  with col2:
200
  with st.spinner("Analyzing..."):
 
201
  pred_class, confidence, img_tensor = predict(model, image, device)
202
  cam_image, original = generate_gradcam(model, img_tensor, device)
 
203
 
204
  # Results
205
  if pred_class == "PNEUMONIA":
@@ -217,11 +219,17 @@ if model_loaded:
217
  </div>
218
  """, unsafe_allow_html=True)
219
 
 
 
 
 
 
 
220
  # Grad-CAM
221
  st.subheader("🔥 Grad-CAM")
222
  gcol1, gcol2 = st.columns(2)
223
- gcol1.image(original, caption="Original", width="stretch")
224
- gcol2.image(cam_image, caption="Heatmap", width="stretch")
225
 
226
  st.warning("**Disclaimer:** For educational purposes only. Consult a healthcare professional.")
227
 
 
1
  """
2
  Hugging Face Spaces - Pneumonia Detection App
 
3
  This is a self-contained version for Hugging Face Spaces deployment.
4
  Copy this file as 'app.py' to your HF Spaces repository.
5
  """
 
12
  import numpy as np
13
  from pytorch_grad_cam import GradCAM
14
  from pytorch_grad_cam.utils.image import show_cam_on_image
15
+ import time
16
 
17
  # =============================================================================
18
  # Configuration
 
192
  image = Image.open(uploaded_file).convert("RGB")
193
 
194
  with col1:
195
+ st.image(image, caption="Uploaded X-Ray", use_container_width=True)
196
+ analyze = st.button("🔬 Analyze Image", type="primary", use_container_width=True)
197
 
198
  if analyze:
199
  with col2:
200
  with st.spinner("Analyzing..."):
201
+ start_time = time.time()
202
  pred_class, confidence, img_tensor = predict(model, image, device)
203
  cam_image, original = generate_gradcam(model, img_tensor, device)
204
+ inference_time = (time.time() - start_time) * 1000
205
 
206
  # Results
207
  if pred_class == "PNEUMONIA":
 
219
  </div>
220
  """, unsafe_allow_html=True)
221
 
222
+ # Metrics row
223
+ m1, m2, m3 = st.columns(3)
224
+ m1.metric("Prediction", pred_class)
225
+ m2.metric("Confidence", f"{confidence:.1%}")
226
+ m3.metric("Time", f"{inference_time:.0f}ms")
227
+
228
  # Grad-CAM
229
  st.subheader("🔥 Grad-CAM")
230
  gcol1, gcol2 = st.columns(2)
231
+ gcol1.image(original, caption="Original", use_container_width=True)
232
+ gcol2.image(cam_image, caption="Heatmap", use_container_width=True)
233
 
234
  st.warning("**Disclaimer:** For educational purposes only. Consult a healthcare professional.")
235