Krishwall commited on
Commit
fd674f0
·
1 Parent(s): 9bf18c6

Fix image file pointer issue and add validation

Browse files
Files changed (1) hide show
  1. app.py +19 -3
app.py CHANGED
@@ -226,8 +226,15 @@ with col1:
226
  help="Upload a chest X-ray image in PNG, JPG, or JPEG format"
227
  )
228
  if uploaded_image:
229
- image_preview = Image.open(uploaded_image).convert("RGB")
230
- st.image(image_preview, caption="Uploaded X-ray", use_container_width=True)
 
 
 
 
 
 
 
231
  st.markdown('</div>', unsafe_allow_html=True)
232
 
233
  with col2:
@@ -270,13 +277,22 @@ st.markdown("<br>", unsafe_allow_html=True)
270
  analyze = st.button("🔍 Analyze Case", use_container_width=True, type="primary")
271
  st.markdown("<br>", unsafe_allow_html=True)
272
 
 
 
 
 
 
 
 
273
  # --------------------------------------------------
274
  # Inference + Explainability
275
  # --------------------------------------------------
276
- if analyze and uploaded_image and findings:
277
  with st.spinner("🔄 Analyzing case... This may take a few moments."):
278
  try:
279
  # ---- Preprocess inputs ----
 
 
280
  image = Image.open(uploaded_image).convert("RGB")
281
  image_tensor = image_transform(image).unsqueeze(0).to(device)
282
 
 
226
  help="Upload a chest X-ray image in PNG, JPG, or JPEG format"
227
  )
228
  if uploaded_image:
229
+ try:
230
+ # Reset file pointer to beginning
231
+ uploaded_image.seek(0)
232
+ image_preview = Image.open(uploaded_image).convert("RGB")
233
+ st.image(image_preview, caption="Uploaded X-ray", use_container_width=True)
234
+ # Reset again for later use
235
+ uploaded_image.seek(0)
236
+ except Exception as e:
237
+ st.error(f"Error loading image: {str(e)}")
238
  st.markdown('</div>', unsafe_allow_html=True)
239
 
240
  with col2:
 
277
  analyze = st.button("🔍 Analyze Case", use_container_width=True, type="primary")
278
  st.markdown("<br>", unsafe_allow_html=True)
279
 
280
+ # Validation message
281
+ if analyze:
282
+ if not uploaded_image:
283
+ st.warning("⚠️ Please upload a chest X-ray image")
284
+ if not findings or not findings.strip():
285
+ st.warning("⚠️ Please enter radiology findings")
286
+
287
  # --------------------------------------------------
288
  # Inference + Explainability
289
  # --------------------------------------------------
290
+ if analyze and uploaded_image and findings and findings.strip():
291
  with st.spinner("🔄 Analyzing case... This may take a few moments."):
292
  try:
293
  # ---- Preprocess inputs ----
294
+ # Reset file pointer to beginning
295
+ uploaded_image.seek(0)
296
  image = Image.open(uploaded_image).convert("RGB")
297
  image_tensor = image_transform(image).unsqueeze(0).to(device)
298