Spaces:
Build error
Build error
Commit
·
1e2e099
1
Parent(s):
141404c
Update app.py
Browse files
app.py
CHANGED
@@ -75,47 +75,28 @@ def save_dataframe_to_csv(df):
|
|
75 |
|
76 |
# Main function to perform image captioning and image-text matching for multiple images
|
77 |
def process_images_and_statements(files):
|
78 |
-
# Initialize an empty list to store the results for all images
|
79 |
all_results_list = []
|
80 |
-
|
81 |
-
#
|
|
|
|
|
|
|
82 |
for file_name, image in files.items():
|
83 |
-
# Generate image caption for the uploaded image using git-large-r-textcaps
|
84 |
caption = generate_caption(git_processor_large_textcaps, git_model_large_textcaps, image)
|
85 |
-
|
86 |
-
# Loop through each predefined statement
|
87 |
for statement in statements:
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
# Compute ITM score for the image-statement pair
|
92 |
-
itm_score_statement = (compute_itm_score(image, statement) * 100) # Multiply by 100
|
93 |
-
|
94 |
-
# Define weights for combining textual similarity score and image-statement ITM score (adjust as needed)
|
95 |
-
weight_textual_similarity = 0.5
|
96 |
-
weight_statement = 0.5
|
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 all_results_list
|
103 |
all_results_list.append({
|
104 |
'Image File Name': file_name, # Include the image file name
|
105 |
'Statement': statement,
|
106 |
'Generated Caption': caption,
|
107 |
-
'Textual Similarity Score': f"{textual_similarity_score:.2f}%",
|
108 |
-
'ITM Score': f"{itm_score_statement:.2f}%",
|
109 |
-
'Final Combined Score': f"{final_score:.2f}%"
|
110 |
})
|
111 |
-
|
112 |
-
# Convert the all_results_list to a DataFrame using pandas.concat
|
113 |
results_df = pd.concat([pd.DataFrame([result]) for result in all_results_list], ignore_index=True)
|
114 |
-
|
115 |
-
# Save results_df to a CSV file
|
116 |
csv_results = save_dataframe_to_csv(results_df)
|
117 |
-
|
118 |
-
# Return both the DataFrame and the CSV data for the Gradio interface
|
119 |
return results_df, csv_results
|
120 |
|
121 |
# Gradio interface with File input to receive multiple images and file names
|
@@ -132,6 +113,4 @@ iface = gr.Interface(
|
|
132 |
css=".output { flex-direction: column; } .output .outputs { width: 100%; }" # Custom CSS
|
133 |
)
|
134 |
|
135 |
-
#iface.launch()
|
136 |
iface.launch(debug=True)
|
137 |
-
|
|
|
75 |
|
76 |
# Main function to perform image captioning and image-text matching for multiple images
|
77 |
def process_images_and_statements(files):
|
|
|
78 |
all_results_list = []
|
79 |
+
|
80 |
+
# If 'files' is a list, convert it to a dictionary
|
81 |
+
if isinstance(files, list):
|
82 |
+
files = {f.name: f for f in files}
|
83 |
+
|
84 |
for file_name, image in files.items():
|
|
|
85 |
caption = generate_caption(git_processor_large_textcaps, git_model_large_textcaps, image)
|
|
|
|
|
86 |
for statement in statements:
|
87 |
+
textual_similarity_score = compute_textual_similarity(caption, statement) * 100
|
88 |
+
itm_score_statement = compute_itm_score(image, statement) * 100
|
89 |
+
final_score = 0.5 * textual_similarity_score + 0.5 * itm_score_statement
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
all_results_list.append({
|
91 |
'Image File Name': file_name, # Include the image file name
|
92 |
'Statement': statement,
|
93 |
'Generated Caption': caption,
|
94 |
+
'Textual Similarity Score': f"{textual_similarity_score:.2f}%",
|
95 |
+
'ITM Score': f"{itm_score_statement:.2f}%",
|
96 |
+
'Final Combined Score': f"{final_score:.2f}%"
|
97 |
})
|
|
|
|
|
98 |
results_df = pd.concat([pd.DataFrame([result]) for result in all_results_list], ignore_index=True)
|
|
|
|
|
99 |
csv_results = save_dataframe_to_csv(results_df)
|
|
|
|
|
100 |
return results_df, csv_results
|
101 |
|
102 |
# Gradio interface with File input to receive multiple images and file names
|
|
|
113 |
css=".output { flex-direction: column; } .output .outputs { width: 100%; }" # Custom CSS
|
114 |
)
|
115 |
|
|
|
116 |
iface.launch(debug=True)
|
|