Spaces:
Build error
Build error
Commit
·
bbf9e08
1
Parent(s):
15b96ac
add filename to csv output
Browse files
app.py
CHANGED
@@ -74,38 +74,27 @@ def save_dataframe_to_csv(df):
|
|
74 |
return temp_file_path
|
75 |
|
76 |
# Main function to perform image captioning and image-text matching
|
77 |
-
def process_images_and_statements(image):
|
78 |
logging.info('Starting process_images_and_statements')
|
79 |
|
80 |
# Generate image caption for the uploaded image using git-large-r-textcaps
|
81 |
caption = generate_caption(git_processor_large_textcaps, git_model_large_textcaps, image)
|
82 |
|
83 |
-
# Define weights for combining textual similarity score and image-statement ITM score (adjust as needed)
|
84 |
-
weight_textual_similarity = 0.5
|
85 |
-
weight_statement = 0.5
|
86 |
-
|
87 |
# Initialize an empty list to store the results
|
88 |
results_list = []
|
89 |
|
90 |
# Loop through each predefined statement
|
91 |
for statement in statements:
|
92 |
-
# Compute textual similarity
|
93 |
-
textual_similarity_score = (compute_textual_similarity(caption, statement) * 100) # Multiply by 100
|
94 |
-
|
95 |
-
# Compute ITM score for the image-statement pair
|
96 |
-
itm_score_statement = (compute_itm_score(image, statement) * 100) # Multiply by 100
|
97 |
-
|
98 |
-
# Combine the two scores using a weighted average
|
99 |
-
final_score = ((weight_textual_similarity * textual_similarity_score) +
|
100 |
-
(weight_statement * itm_score_statement))
|
101 |
|
102 |
# Append the result to the results_list
|
103 |
results_list.append({
|
|
|
104 |
'Statement': statement,
|
105 |
-
'Generated Caption': caption,
|
106 |
-
'Textual Similarity Score': f"{textual_similarity_score:.2f}%",
|
107 |
-
'ITM Score': f"{itm_score_statement:.2f}%",
|
108 |
-
'Final Combined Score': f"{final_score:.2f}%"
|
109 |
})
|
110 |
|
111 |
# Convert the results_list to a DataFrame using pandas.concat
|
@@ -117,20 +106,20 @@ def process_images_and_statements(image):
|
|
117 |
csv_results = save_dataframe_to_csv(results_df)
|
118 |
|
119 |
# Return both the DataFrame and the CSV data for the Gradio interface
|
120 |
-
return results_df, csv_results
|
121 |
|
122 |
-
# Gradio interface
|
123 |
-
image_input = gr.inputs.Image
|
124 |
output_df = gr.outputs.Dataframe(type="pandas", label="Results")
|
125 |
output_csv = gr.outputs.File(label="Download CSV")
|
126 |
|
127 |
iface = gr.Interface(
|
128 |
fn=process_images_and_statements,
|
129 |
inputs=image_input,
|
130 |
-
outputs=[output_df, output_csv],
|
131 |
title="Image Captioning and Image-Text Matching",
|
132 |
theme='sudeepshouche/minimalist',
|
133 |
css=".output { flex-direction: column; } .output .outputs { width: 100%; }" # Custom CSS
|
134 |
)
|
135 |
|
136 |
-
iface.launch()
|
|
|
74 |
return temp_file_path
|
75 |
|
76 |
# Main function to perform image captioning and image-text matching
|
77 |
+
def process_images_and_statements(image, file_name):
|
78 |
logging.info('Starting process_images_and_statements')
|
79 |
|
80 |
# Generate image caption for the uploaded image using git-large-r-textcaps
|
81 |
caption = generate_caption(git_processor_large_textcaps, git_model_large_textcaps, image)
|
82 |
|
|
|
|
|
|
|
|
|
83 |
# Initialize an empty list to store the results
|
84 |
results_list = []
|
85 |
|
86 |
# Loop through each predefined statement
|
87 |
for statement in statements:
|
88 |
+
# Compute textual similarity and ITM score, and combine the scores ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
|
90 |
# Append the result to the results_list
|
91 |
results_list.append({
|
92 |
+
'Image File Name': file_name, # Include the image file name
|
93 |
'Statement': statement,
|
94 |
+
'Generated Caption': caption,
|
95 |
+
'Textual Similarity Score': f"{textual_similarity_score:.2f}%",
|
96 |
+
'ITM Score': f"{itm_score_statement:.2f}%",
|
97 |
+
'Final Combined Score': f"{final_score:.2f}%"
|
98 |
})
|
99 |
|
100 |
# Convert the results_list to a DataFrame using pandas.concat
|
|
|
106 |
csv_results = save_dataframe_to_csv(results_df)
|
107 |
|
108 |
# Return both the DataFrame and the CSV data for the Gradio interface
|
109 |
+
return results_df, csv_results
|
110 |
|
111 |
+
# Gradio interface with File input to receive both the image and the file name
|
112 |
+
image_input = gr.inputs.File(type="image", label="Upload Image")
|
113 |
output_df = gr.outputs.Dataframe(type="pandas", label="Results")
|
114 |
output_csv = gr.outputs.File(label="Download CSV")
|
115 |
|
116 |
iface = gr.Interface(
|
117 |
fn=process_images_and_statements,
|
118 |
inputs=image_input,
|
119 |
+
outputs=[output_df, output_csv],
|
120 |
title="Image Captioning and Image-Text Matching",
|
121 |
theme='sudeepshouche/minimalist',
|
122 |
css=".output { flex-direction: column; } .output .outputs { width: 100%; }" # Custom CSS
|
123 |
)
|
124 |
|
125 |
+
iface.launch()
|