Update app.py
Browse files
app.py
CHANGED
@@ -2,8 +2,23 @@ import gradio as gr
|
|
2 |
from PIL import Image
|
3 |
import clipGPT
|
4 |
import vitGPT
|
5 |
-
import
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
|
9 |
# Caption generation functions
|
@@ -17,44 +32,117 @@ def generate_caption_vitgpt(image):
|
|
17 |
|
18 |
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
25 |
|
|
|
|
|
26 |
with gr.Row():
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
"CXR193_IM-0601-1001.png",
|
31 |
-
"CXR194_IM-0609-1001.png",
|
32 |
-
"CXR195_IM-0618-1001.png"
|
33 |
-
]
|
34 |
-
image = gr.Image(label="Upload Chest X-ray")
|
35 |
-
gr.Gallery(
|
36 |
-
value = sample_images,
|
37 |
-
label="Sample Images",
|
38 |
-
)
|
39 |
-
# sample_images_gallery = gr.Gallery(
|
40 |
-
# value = sample_images,
|
41 |
-
# label="Sample Images",
|
42 |
-
# )
|
43 |
-
with gr.Row():
|
44 |
model_choice = gr.Radio(["CLIP-GPT2", "ViT-GPT2", "ViT-CoAttention"], label="Select Model")
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
def predict(img, model_name):
|
49 |
if model_name == "CLIP-GPT2":
|
50 |
-
return generate_caption_clipgpt(img)
|
51 |
elif model_name == "ViT-GPT2":
|
52 |
return generate_caption_vitgpt(img)
|
53 |
else:
|
54 |
-
return "Caption generation for this model is not yet implemented."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
-
|
57 |
-
|
|
|
|
|
58 |
|
|
|
|
|
|
|
59 |
|
60 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
from PIL import Image
|
3 |
import clipGPT
|
4 |
import vitGPT
|
5 |
+
import difflib
|
6 |
+
|
7 |
+
|
8 |
+
def compare_and_highlight(text1, text2):
|
9 |
+
matcher = difflib.SequenceMatcher(None, text1, text2)
|
10 |
+
output = ''
|
11 |
+
for op, a1, a2, b1, b2 in matcher.get_opcodes():
|
12 |
+
if op == 'equal':
|
13 |
+
output += f"**{text1[a1:a2]}**" # Highlight matches in bold
|
14 |
+
elif op == 'insert':
|
15 |
+
output += f"<ins>{text2[b1:b2]}</ins>"
|
16 |
+
elif op == 'delete':
|
17 |
+
output += f"<del>{text1[a1:a2]}</del>"
|
18 |
+
elif op == 'replace':
|
19 |
+
# Handle replacements (more complex)
|
20 |
+
output += f"<del>{text1[a1:a2]}</del> <ins>{text2[b1:b2]}</ins>"
|
21 |
+
return output
|
22 |
|
23 |
|
24 |
# Caption generation functions
|
|
|
32 |
|
33 |
|
34 |
|
35 |
+
# Sample image paths
|
36 |
+
sample_images = [
|
37 |
+
"CXR191_IM-0591-1001.jpg",
|
38 |
+
"CXR191_IM-0598-1001.jpg",
|
39 |
+
"CXR191_IM-0601-1001.jpg",
|
40 |
+
"CXR191_IM-0609-1001.jpg",
|
41 |
+
"CXR191_IM-0618-1001.jpg"
|
42 |
+
]
|
43 |
|
44 |
+
# Gradio interface
|
45 |
+
with gr.Blocks() as demo:
|
46 |
with gr.Row():
|
47 |
+
image = gr.Image(label="Upload Chest X-ray", source="upload")
|
48 |
+
sample_image_gallery = gr.ImageGallery(sample_images, label="Sample Images")
|
49 |
+
with gr.Row():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
model_choice = gr.Radio(["CLIP-GPT2", "ViT-GPT2", "ViT-CoAttention"], label="Select Model")
|
51 |
+
with gr.Row():
|
52 |
+
caption = gr.Textbox(label="Generated Caption")
|
53 |
+
|
54 |
+
generated_captions = {
|
55 |
+
"CLIP-GPT2": "",
|
56 |
+
"ViT-GPT2": "",
|
57 |
+
}
|
58 |
|
59 |
def predict(img, model_name):
|
60 |
if model_name == "CLIP-GPT2":
|
61 |
+
return generate_caption_clipgpt(img)
|
62 |
elif model_name == "ViT-GPT2":
|
63 |
return generate_caption_vitgpt(img)
|
64 |
else:
|
65 |
+
return "Caption generation for this model is not yet implemented."
|
66 |
+
generated_captions[model_name] = caption
|
67 |
+
|
68 |
+
with gr.Row():
|
69 |
+
caption1 = gr.Textbox(label="CLIP-GPT2")
|
70 |
+
caption2 = gr.Textbox(label="ViT-GPT2")
|
71 |
+
compare_button = gr.Button("Compare Captions")
|
72 |
+
with gr.Row():
|
73 |
+
comparison_result = gr.Textbox(label="Comparison Result")
|
74 |
|
75 |
+
# Compare captions on button click
|
76 |
+
compare_button.click(lambda: compare_and_highlight(
|
77 |
+
generated_captions["CLIP-GPT2"], generated_captions["ViT-GPT2"]
|
78 |
+
), [], comparison_result)
|
79 |
|
80 |
+
# Handle changes for both uploaded and sample images
|
81 |
+
gr.Image.change(predict, [image, model_choice], caption)
|
82 |
+
sample_image_gallery.change(predict, [sample_image_gallery, model_choice], caption)
|
83 |
|
84 |
demo.launch()
|
85 |
+
|
86 |
+
|
87 |
+
|
88 |
+
# import gradio as gr
|
89 |
+
# from PIL import Image
|
90 |
+
# import clipGPT
|
91 |
+
# import vitGPT
|
92 |
+
# import skimage.io as io
|
93 |
+
# import PIL.Image
|
94 |
+
|
95 |
+
|
96 |
+
# # Caption generation functions
|
97 |
+
# def generate_caption_clipgpt(image):
|
98 |
+
# caption = clipGPT.generate_caption_clipgpt(image)
|
99 |
+
# return caption
|
100 |
+
|
101 |
+
# def generate_caption_vitgpt(image):
|
102 |
+
# caption = vitGPT.generate_caption(image)
|
103 |
+
# return caption
|
104 |
+
|
105 |
+
|
106 |
+
|
107 |
+
# with gr.Blocks() as demo:
|
108 |
+
|
109 |
+
|
110 |
+
# gr.HTML("<h1 style='text-align: center;'>MedViT: A Vision Transformer-Driven Method for Generating Medical Reports π₯π€</h1>")
|
111 |
+
# gr.HTML("<p style='text-align: center;'>You can generate captions by uploading an X-Ray and selecting a model of your choice below</p>")
|
112 |
+
|
113 |
+
# with gr.Row():
|
114 |
+
# sample_images = [
|
115 |
+
# "CXR191_IM-0591-1001.png",
|
116 |
+
# "CXR192_IM-0598-1001.png",
|
117 |
+
# "CXR193_IM-0601-1001.png",
|
118 |
+
# "CXR194_IM-0609-1001.png",
|
119 |
+
# "CXR195_IM-0618-1001.png"
|
120 |
+
# ]
|
121 |
+
# image = gr.Image(label="Upload Chest X-ray")
|
122 |
+
# gr.Gallery(
|
123 |
+
# value = sample_images,
|
124 |
+
# label="Sample Images",
|
125 |
+
# )
|
126 |
+
# # sample_images_gallery = gr.Gallery(
|
127 |
+
# # value = sample_images,
|
128 |
+
# # label="Sample Images",
|
129 |
+
# # )
|
130 |
+
# with gr.Row():
|
131 |
+
# model_choice = gr.Radio(["CLIP-GPT2", "ViT-GPT2", "ViT-CoAttention"], label="Select Model")
|
132 |
+
# generate_button = gr.Button("Generate Caption")
|
133 |
+
# caption = gr.Textbox(label="Generated Caption")
|
134 |
+
|
135 |
+
# def predict(img, model_name):
|
136 |
+
# if model_name == "CLIP-GPT2":
|
137 |
+
# return generate_caption_clipgpt(img)
|
138 |
+
# elif model_name == "ViT-GPT2":
|
139 |
+
# return generate_caption_vitgpt(img)
|
140 |
+
# else:
|
141 |
+
# return "Caption generation for this model is not yet implemented."
|
142 |
+
|
143 |
+
# generate_button.click(predict, [image, model_choice], caption) # Trigger prediction on button click
|
144 |
+
# # sample_images_gallery.change(predict, [sample_images_gallery, model_choice], caption) # Handle sample images
|
145 |
+
|
146 |
+
|
147 |
+
# demo.launch()
|
148 |
+
|