saakshigupta commited on
Commit
d88f984
·
verified ·
1 Parent(s): ec587e8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -24
app.py CHANGED
@@ -442,33 +442,39 @@ def generate_gradcam_caption(image, processor, model, max_length=60):
442
  # Decode the output
443
  caption = processor.decode(output[0], skip_special_tokens=True)
444
 
445
- # Print debug info
446
- st.write("Debug - Raw caption:", caption)
447
-
448
- # Split and format the text differently
449
- if "high activation" in caption.lower():
450
- # Extract the parts for each activation level
451
- high_part = caption[caption.lower().find("high activation"):].split("moderate")[0].strip()
452
- moderate_part = "moderate" + caption.split("moderate")[1].split("low")[0].strip()
453
- low_part = "low" + caption.split("low")[1].strip()
454
-
455
- formatted_text = f"""**High activation**:
456
- {high_part.replace('high activation :', '').strip()}
457
-
458
- **Moderate activation**:
459
- {moderate_part.replace('moderate activation :', '').strip()}
460
-
461
- **Low activation**:
462
- {low_part.replace('low activation :', '').strip()}"""
463
-
464
- return formatted_text
465
-
466
- # If we can't parse it properly, return the original caption
467
- return caption
 
 
 
 
 
 
 
468
 
469
  except Exception as e:
470
  st.error(f"Error analyzing GradCAM: {str(e)}")
471
- st.write("Debug - Full error:", str(e))
472
  return "Error analyzing GradCAM visualization"
473
 
474
  # Function to generate caption for original image
 
442
  # Decode the output
443
  caption = processor.decode(output[0], skip_special_tokens=True)
444
 
445
+ # Clean and format the text
446
+ # First, split by activation levels
447
+ text_parts = caption.lower().split("activation")
448
+
449
+ # Initialize empty strings for each activation level
450
+ high_text = ""
451
+ moderate_text = ""
452
+ low_text = ""
453
+
454
+ # Process each part
455
+ for part in text_parts:
456
+ if not part.strip(): # Skip empty parts
457
+ continue
458
+ if "high" in part:
459
+ high_text = part.split(":", 1)[1].strip() if ":" in part else part.strip()
460
+ elif "moderate" in part:
461
+ moderate_text = part.split(":", 1)[1].strip() if ":" in part else part.strip()
462
+ elif "low" in part:
463
+ low_text = part.split(":", 1)[1].strip() if ":" in part else part.strip()
464
+
465
+ # Format the output with only the unique content for each level
466
+ formatted_text = ""
467
+ if high_text:
468
+ formatted_text += f"**High activation**:\n{high_text}\n\n"
469
+ if moderate_text:
470
+ formatted_text += f"**Moderate activation**:\n{moderate_text}\n\n"
471
+ if low_text:
472
+ formatted_text += f"**Low activation**:\n{low_text}"
473
+
474
+ return formatted_text.strip()
475
 
476
  except Exception as e:
477
  st.error(f"Error analyzing GradCAM: {str(e)}")
 
478
  return "Error analyzing GradCAM visualization"
479
 
480
  # Function to generate caption for original image