Faysal4200 commited on
Commit
bf8fc9f
Β·
verified Β·
1 Parent(s): e9d107d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -42
app.py CHANGED
@@ -380,7 +380,7 @@ with st.sidebar:
380
 
381
 
382
  st.markdown("<div class='header-title'>Skin Cancer Image Classifier</div>", unsafe_allow_html=True)
383
- st.markdown("<div class='header-sub'>CNN Classifier β€’ Model Attention (Grad-CAM++) visualizations β€’ VLM explanations</div>", unsafe_allow_html=True)
384
 
385
  uploaded_file = st.file_uploader("Upload a skin lesion image", type=["jpg","jpeg","png"], key="uploaded_file" )
386
 
@@ -425,40 +425,8 @@ if 'selected_image' in st.session_state:
425
  st.session_state["conf"] = conf
426
  st.session_state["overlay_pil"] = overlay_pil
427
  st.session_state["last_image_bytes"] = img_bytes
428
- try:
429
- with st.spinner("Loading VLM Model. Please be patient..."):
430
- try:
431
- vlm_info = load_vlm_model()
432
- except Exception as e:
433
- st.error("VLM load failed. See logs above.")
434
- vlm_info = None
435
-
436
- if vlm_info is not None:
437
- try:
438
- img_for_vlm = overlay_pil.convert("RGB").resize((224, 224), Image.BILINEAR)
439
- except Exception:
440
- st.warning("Overlay image not available for VLM input; using original image.")
441
- img_for_vlm = pil_img.convert("RGB").resize((224, 224), Image.BILINEAR)
442
-
443
- with st.spinner("Generating Explanation...."):
444
- response = generate_vlm_response(
445
- vlm_info["processor"],
446
- vlm_info["model"],
447
- vlm_info["device"],
448
- img_for_vlm,
449
- pred_label,
450
- max_new_tokens=128
451
- )
452
- #response = "Debugging VLM response." # For debugging
453
- if response is None:
454
- st.error("VLM did not return a response.")
455
- else:
456
- st.session_state["vlm_response"] = response
457
- except Exception as e:
458
- st.error(f"Error in VLM generation flow: {e}")
459
- st.exception(traceback.format_exc())
460
-
461
-
462
  with attention_column:
463
  st.markdown("<div class='card'>", unsafe_allow_html=True)
464
  st.subheader("Model Attention Visualization")
@@ -478,12 +446,31 @@ else:
478
  c1.metric("Predicted", "β€”")
479
  c2.metric("Confidence", "β€”")
480
 
481
- # VLM Response placeholder
482
- st.subheader("Generated Explanation")
483
- if st.session_state.get("vlm_response"):
484
- st.info(st.session_state["vlm_response"])
485
- else:
486
- st.info("VLM explanation will appear here after selecting an image and running classification.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
487
 
488
  example_paths = [
489
  "images/ISIC_0025314.jpg",
@@ -563,7 +550,7 @@ if toggle:
563
  st.toast(f"βœ… Selected image: {selected_path}", icon="πŸ“Έ")
564
  except Exception:
565
  st.success(f"Selected image: {selected_path}")
566
- st.rerun()
567
  st.markdown("</div>", unsafe_allow_html=True)
568
 
569
  st.markdown("""
 
380
 
381
 
382
  st.markdown("<div class='header-title'>Skin Cancer Image Classifier</div>", unsafe_allow_html=True)
383
+ st.markdown("<div class='header-sub'>CNN inference β€’ Model Attention (Grad-CAM++) visualizations β€’ VLM explanations</div>", unsafe_allow_html=True)
384
 
385
  uploaded_file = st.file_uploader("Upload a skin lesion image", type=["jpg","jpeg","png"], key="uploaded_file" )
386
 
 
425
  st.session_state["conf"] = conf
426
  st.session_state["overlay_pil"] = overlay_pil
427
  st.session_state["last_image_bytes"] = img_bytes
428
+ st.session_state["vlm_response"] = None
429
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
430
  with attention_column:
431
  st.markdown("<div class='card'>", unsafe_allow_html=True)
432
  st.subheader("Model Attention Visualization")
 
446
  c1.metric("Predicted", "β€”")
447
  c2.metric("Confidence", "β€”")
448
 
449
+ if st.button("Generate VLM Explanation"):
450
+ if 'selected_image' in st.session_state:
451
+ if not st.session_state.get("vlm_response", False):
452
+ try:
453
+ with st.spinner("Loading VLM Model. First time load will take time. Please be patient..."):
454
+
455
+ response = "Debugging VLM response."
456
+ if response is None:
457
+ st.error("VLM did not return a response.")
458
+ else:
459
+ st.session_state["vlm_response"] = response
460
+
461
+ except Exception as e:
462
+ st.error(f"Error in VLM generation flow: {e}")
463
+ st.exception(traceback.format_exc())
464
+ else:
465
+ st.warning("Upload an image first or use the example images provided below!")
466
+
467
+
468
+ if st.session_state.get("vlm_response", False):
469
+ st.subheader("Generated Explanation")
470
+ if st.session_state.get("vlm_response"):
471
+ st.info(st.session_state["vlm_response"])
472
+ else:
473
+ st.info("VLM explanation will appear here after selecting an image and running classification.")
474
 
475
  example_paths = [
476
  "images/ISIC_0025314.jpg",
 
550
  st.toast(f"βœ… Selected image: {selected_path}", icon="πŸ“Έ")
551
  except Exception:
552
  st.success(f"Selected image: {selected_path}")
553
+ st.rerun()
554
  st.markdown("</div>", unsafe_allow_html=True)
555
 
556
  st.markdown("""