bombshelll commited on
Commit
24a5179
Β·
1 Parent(s): bfe2b83
Files changed (2) hide show
  1. app.py +37 -5
  2. setup.sh +2 -0
app.py CHANGED
@@ -2,8 +2,15 @@ import gradio as gr
2
  from PIL import Image
3
  import torch
4
  from transformers import VisionEncoderDecoderModel, AutoTokenizer, ViTFeatureExtractor, AutoImageProcessor, AutoModelForImageClassification
 
 
5
  import warnings
6
 
 
 
 
 
 
7
  warnings.filterwarnings("ignore", category=UserWarning)
8
 
9
  device = "cuda" if torch.cuda.is_available() else "cpu"
@@ -38,6 +45,17 @@ def classify_image(image):
38
  results[name] = label
39
  return results
40
 
 
 
 
 
 
 
 
 
 
 
 
41
  def generate_captions(image, keywords):
42
  pixel_values = feature_extractor(images=image, return_tensors="pt").pixel_values.to(device)
43
 
@@ -61,7 +79,7 @@ def generate_captions(image, keywords):
61
 
62
  return caption1, caption2
63
 
64
- def run_pipeline(image):
65
  classification = classify_image(image)
66
  keywords = list(classification.values())
67
  caption1, caption2 = generate_captions(image, keywords)
@@ -73,30 +91,44 @@ def run_pipeline(image):
73
  + (f"Tumor Type: {classification.get('tumor_type')}" if "tumor_type" in classification else "")
74
  )
75
 
76
- return classification_text, caption1, caption2
 
 
 
 
 
 
 
 
 
 
 
77
 
78
  with gr.Blocks(theme=gr.themes.Soft(primary_hue="pink"), css=custom_css) as demo:
79
  gr.Markdown(
80
  """
81
  <link href="https://fonts.googleapis.com/css2?family=Poppins&display=swap" rel="stylesheet">
82
  <h1 style='text-align: center;'>🧠 Brain Hierarchical Classification + Captioning</h1>
83
- <p style='text-align: center;'>Upload an MRI/CT brain image. The system will classify the image (plane, modality, abnormality, tumor type) and generate two captions.</p>
84
  """,
85
  elem_id="title"
86
  )
87
  with gr.Row():
88
  with gr.Column():
89
  image_input = gr.Image(type="pil", label="πŸ–ΌοΈ Upload Brain MRI/CT")
 
90
  btn = gr.Button("πŸš€ Submit")
91
  with gr.Column():
92
  cls_box = gr.Textbox(label="πŸ“‹ Classification Result", lines=4)
93
  cap1_box = gr.Textbox(label="πŸ“ Caption without Keyword Integration", lines=4)
94
  cap2_box = gr.Textbox(label="🧠 Caption with Keyword Integration", lines=4)
 
 
95
 
96
  btn.click(
97
  fn=run_pipeline,
98
- inputs=[image_input],
99
- outputs=[cls_box, cap1_box, cap2_box]
100
  )
101
 
102
  demo.launch()
 
2
  from PIL import Image
3
  import torch
4
  from transformers import VisionEncoderDecoderModel, AutoTokenizer, ViTFeatureExtractor, AutoImageProcessor, AutoModelForImageClassification
5
+ from nltk.translate.bleu_score import sentence_bleu, SmoothingFunction
6
+ import nltk
7
  import warnings
8
 
9
+ try:
10
+ nltk.data.find("tokenizers/punkt")
11
+ except LookupError:
12
+ nltk.download("punkt")
13
+
14
  warnings.filterwarnings("ignore", category=UserWarning)
15
 
16
  device = "cuda" if torch.cuda.is_available() else "cpu"
 
45
  results[name] = label
46
  return results
47
 
48
+ def preprocess_caption(text):
49
+ text = str(text).lower()
50
+ for term in ["magnetic resonance imaging", "magnetic resonance image"]:
51
+ text = text.replace(term, "mri")
52
+ for term in ["computed tomography"]:
53
+ text = text.replace(term, "ct")
54
+ text = text.replace("t1-weighted", "t1").replace("t1w1", "t1").replace("t1ce", "t1")
55
+ text = text.replace("t2-weighted", "t2").replace("t2w", "t2").replace("t2/flair", "flair")
56
+ text = text.replace("tumour", "tumor").replace("lesions", "lesion").replace("-", " ")
57
+ return text.split()
58
+
59
  def generate_captions(image, keywords):
60
  pixel_values = feature_extractor(images=image, return_tensors="pt").pixel_values.to(device)
61
 
 
79
 
80
  return caption1, caption2
81
 
82
+ def run_pipeline(image, actual_caption):
83
  classification = classify_image(image)
84
  keywords = list(classification.values())
85
  caption1, caption2 = generate_captions(image, keywords)
 
91
  + (f"Tumor Type: {classification.get('tumor_type')}" if "tumor_type" in classification else "")
92
  )
93
 
94
+ if actual_caption.strip():
95
+ ref = [preprocess_caption(actual_caption)]
96
+ hyp1 = preprocess_caption(caption1)
97
+ hyp2 = preprocess_caption(caption2)
98
+ smooth = SmoothingFunction().method1
99
+ bleu1 = f"{sentence_bleu(ref, hyp1, smoothing_function=smooth):.2f}"
100
+ bleu2 = f"{sentence_bleu(ref, hyp2, smoothing_function=smooth):.2f}"
101
+ else:
102
+ bleu1 = "-"
103
+ bleu2 = "-"
104
+
105
+ return classification_text, caption1, caption2, bleu1, bleu2
106
 
107
  with gr.Blocks(theme=gr.themes.Soft(primary_hue="pink"), css=custom_css) as demo:
108
  gr.Markdown(
109
  """
110
  <link href="https://fonts.googleapis.com/css2?family=Poppins&display=swap" rel="stylesheet">
111
  <h1 style='text-align: center;'>🧠 Brain Hierarchical Classification + Captioning</h1>
112
+ <p style='text-align: center;'>Upload an MRI/CT brain image. The system will classify the image and generate captions. Optionally, provide ground truth to see BLEU scores.</p>
113
  """,
114
  elem_id="title"
115
  )
116
  with gr.Row():
117
  with gr.Column():
118
  image_input = gr.Image(type="pil", label="πŸ–ΌοΈ Upload Brain MRI/CT")
119
+ actual_caption = gr.Textbox(label="πŸ’¬ Ground Truth Caption (optional)")
120
  btn = gr.Button("πŸš€ Submit")
121
  with gr.Column():
122
  cls_box = gr.Textbox(label="πŸ“‹ Classification Result", lines=4)
123
  cap1_box = gr.Textbox(label="πŸ“ Caption without Keyword Integration", lines=4)
124
  cap2_box = gr.Textbox(label="🧠 Caption with Keyword Integration", lines=4)
125
+ bleu1_box = gr.Textbox(label="πŸ“Š BLEU (No Keyword)", lines=1)
126
+ bleu2_box = gr.Textbox(label="πŸ“ˆ BLEU (With Keyword)", lines=1)
127
 
128
  btn.click(
129
  fn=run_pipeline,
130
+ inputs=[image_input, actual_caption],
131
+ outputs=[cls_box, cap1_box, cap2_box, bleu1_box, bleu2_box]
132
  )
133
 
134
  demo.launch()
setup.sh ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ #!/bin/bash
2
+ python -m nltk.downloader punkt