-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
292 lines (231 loc) · 9.39 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# import gradio as gr
# import os
# from captioning import generate_caption
# from dataset import preprocess_image
# def gradio_generate_and_evaluate(image):
# """
# Function to generate caption and evaluate it for the uploaded image.
# Args:
# image (PIL.Image): The uploaded image.
# Returns:
# str: The caption and evaluation results.
# """
# # Generate caption for the uploaded image
# generated_caption = generate_caption(image)
# # As there is no filename in the image object, we can either:
# # 1. Save the image temporarily to generate a filename, or
# # 2. Directly skip filename and focus on caption generation and evaluation.
# # If you want to skip filename handling, you can remove the following lines.
# # For now, let's just generate a temporary filename using a unique identifier.
# temp_filename = "temp_image.jpg"
# image.save(temp_filename) # Save the image temporarily
# # If you have ground truth captions to compare for evaluation, you can load them
# # Assuming you have a captions file with the ground truth data (e.g., captions.txt)
# captions_file = "/Users/arpitsharma/cvision/captions.txt" # Update with actual path
# with open(captions_file, "r") as f:
# lines = f.readlines()
# # Parse captions (assuming "image_name\tcaption" format)
# captions_data = {}
# for line in lines:
# parts = line.strip().split("\t")
# if len(parts) == 2:
# image_name, caption = parts
# captions_data[image_name] = caption
# # Evaluate the caption against ground truth (simple exact match evaluation)
# ground_truth = captions_data.get(temp_filename, "")
# evaluation_result = "Match" if generated_caption == ground_truth else "No Match"
# # Return the generated caption and evaluation result
# return f"Generated Caption: {generated_caption}\nEvaluation: {evaluation_result}"
# # Gradio Interface
# title = "Image Captioning and Evaluation"
# description = "Upload an image to generate a caption and evaluate it."
# # Input components
# inputs = gr.Image(type="pil", label="Upload Image")
# # Output component
# outputs = gr.Textbox(label="Caption and Evaluation Result")
# # Interface
# interface = gr.Interface(
# fn=gradio_generate_and_evaluate,
# inputs=inputs,
# outputs=outputs,
# title=title,
# description=description,
# )
# if __name__ == "__main__":
# interface.launch(share=True)
# import gradio as gr
# from PIL import Image
# from captioning import generate_caption # Assuming generate_caption is your captioning function
# def gradio_generate_caption(image):
# """
# Function to generate a caption for the uploaded image.
# Args:
# image (PIL.Image): The uploaded image.
# Returns:
# str: The generated caption for the image.
# """
# if image is None:
# return "No image provided."
# # Ensure the image is in the correct format (if needed)
# if not isinstance(image, Image.Image):
# try:
# image = Image.open(image) # Ensure it's a PIL image
# except Exception as e:
# return f"Error loading image: {str(e)}"
# # Generate caption for the uploaded image
# generated_caption = generate_caption(image)
# return generated_caption
# # Gradio Interface
# title = "Image Captioning"
# description = "Upload an image to generate a caption."
# # Input components
# inputs = gr.Image(type="pil", label="Upload Image")
# # Output component
# outputs = gr.Textbox(label="Generated Caption")
# # Interface
# interface = gr.Interface(
# fn=gradio_generate_caption,
# inputs=inputs,
# outputs=outputs,
# title=title,
# description=description,
# )
# if __name__ == "__main__":
# interface.launch(share=True)
# import gradio as gr
# from PIL import Image
# from captioning import generate_caption # Assuming generate_caption is your captioning function
# import nltk
# from nltk.translate.bleu_score import corpus_bleu
# # Download necessary NLTK data for BLEU
# nltk.download('punkt')
# def gradio_generate_and_compare(image, user_caption):
# """
# Function to generate a caption for the uploaded image and compare it with the user's input caption.
# Args:
# image (PIL.Image): The uploaded image.
# user_caption (str): The manually input caption.
# Returns:
# dict: Generated caption and BLEU score comparing user input with the generated caption.
# """
# if image is None:
# return "No image provided."
# # Generate caption for the uploaded image
# generated_caption = generate_caption(image)
# # Tokenize the captions
# reference_tokens = user_caption.split() # Tokenize user's input caption
# hypothesis_tokens = generated_caption.split() # Tokenize the generated caption
# # Calculate BLEU Score
# bleu_score = corpus_bleu([[reference_tokens]], [hypothesis_tokens])
# # Collect evaluation results
# evaluation_results = {
# "Generated Caption": generated_caption,
# "User's Caption": user_caption,
# "BLEU Score": bleu_score
# }
# return generated_caption, user_caption, evaluation_results
# # Gradio Interface
# title = "Image Captioning and BLEU Score Evaluation"
# description = "Upload an image to generate a caption, then input your own caption and compare it with the generated one using BLEU score."
# # Input components
# inputs = [
# gr.Image(type="pil", label="Upload Image"),
# gr.Textbox(label="Your Caption", placeholder="Enter your own caption here...")
# ]
# # Output components
# outputs = [
# gr.Textbox(label="Generated Caption"),
# gr.Textbox(label="Your Caption"),
# gr.JSON(label="Caption Comparison and BLEU Score")
# ]
# # Interface
# interface = gr.Interface(
# fn=gradio_generate_and_compare,
# inputs=inputs,
# outputs=outputs,
# title=title,
# description=description,
# )
# if __name__ == "__main__":
# interface.launch(share=True,inbrowser=True)
import gradio as gr
from PIL import Image
from captioning import generate_caption # Assuming generate_caption is your captioning function
import nltk
from nltk.translate.bleu_score import corpus_bleu
# Download necessary NLTK data for BLEU
nltk.download('punkt')
# Function to load captions from captions.txt
def load_captions(file_path):
"""
Function to load captions from the captions.txt file.
Assumes the file format is image filename, followed by the caption on the same line.
"""
captions = {}
with open(file_path, 'r') as file:
for line in file:
# Skip empty lines and lines that don't contain a comma
line = line.strip()
if not line or ', ' not in line:
continue
try:
# Try to split by the first comma
image_filename, caption = line.split(', ', 1)
captions[image_filename] = caption
except ValueError:
print(f"Skipping line: {line}") # You can log it if needed
return captions
# Load captions from the file (specify your path to captions.txt here)
captions_file_path = '/Users/arpitsharma/cvision/captions.txt'
captions_dict = load_captions(captions_file_path)
def gradio_generate_and_compare(image, image_filename):
"""
Function to generate a caption for the uploaded image and compare it with the reference caption.
Args:
image (PIL.Image): The uploaded image.
image_filename (str): The filename of the uploaded image.
Returns:
dict: Generated caption and BLEU score comparing reference caption with the generated caption.
"""
if image is None:
return "No image provided."
# Generate caption for the uploaded image
generated_caption = generate_caption(image)
# Retrieve the reference caption from the captions dictionary based on the uploaded image filename
reference_caption = captions_dict.get(image_filename, "No reference caption found for this image")
# Tokenize the captions
reference_tokens = reference_caption.split() # Tokenize reference caption
hypothesis_tokens = generated_caption.split() # Tokenize the generated caption
# Calculate BLEU Score
bleu_score = corpus_bleu([[reference_tokens]], [hypothesis_tokens])
# Collect evaluation results
evaluation_results = {
"Generated Caption": generated_caption,
"Reference Caption": reference_caption,
"BLEU Score": bleu_score
}
return generated_caption, reference_caption, evaluation_results
# Gradio Interface
title = "Image Captioning and BLEU Score Evaluation"
description = "Upload an image and provide the filename to generate a caption, then compare it with the reference caption from the dataset using BLEU score."
# Input components
inputs = [
gr.Image(type="pil", label="Upload Image"), # Only ask for image
gr.Textbox(label="Image Filename", placeholder="Enter image filename (e.g., '998845445.jpg')") # For selecting the image
]
# Output components
outputs = [
gr.Textbox(label="Generated Caption"),
gr.Textbox(label="Reference Caption"),
gr.JSON(label="Caption Comparison and BLEU Score")
]
# Interface
interface = gr.Interface(
fn=gradio_generate_and_compare,
inputs=inputs,
outputs=outputs,
title=title,
description=description,
)
if __name__ == "__main__":
interface.launch(share=True, inbrowser=True)