Spaces:
Build error
Build error
Commit
·
74fc255
1
Parent(s):
9035ca2
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,7 +9,7 @@ import tensorflow as tf
|
|
| 9 |
import tensorflow_hub as hub
|
| 10 |
import io
|
| 11 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 12 |
-
import tempfile
|
| 13 |
import logging
|
| 14 |
|
| 15 |
# Configure logging
|
|
@@ -73,34 +73,44 @@ def save_dataframe_to_csv(df):
|
|
| 73 |
# Return the file path (no need to reopen the file with "rb" mode)
|
| 74 |
return temp_file_path
|
| 75 |
|
| 76 |
-
# Main function to perform image captioning and image-text matching
|
| 77 |
-
def process_images_and_statements(
|
| 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 |
# Save results_df to a CSV file
|
| 106 |
csv_results = save_dataframe_to_csv(results_df)
|
|
@@ -108,9 +118,8 @@ def process_images_and_statements(image, file_name):
|
|
| 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
|
| 112 |
-
|
| 113 |
-
image_input = gr.inputs.Image(label="Upload Images", multiple=True)
|
| 114 |
output_df = gr.outputs.Dataframe(type="pandas", label="Results")
|
| 115 |
output_csv = gr.outputs.File(label="Download CSV")
|
| 116 |
|
|
@@ -123,4 +132,5 @@ iface = gr.Interface(
|
|
| 123 |
css=".output { flex-direction: column; } .output .outputs { width: 100%; }" # Custom CSS
|
| 124 |
)
|
| 125 |
|
| 126 |
-
iface.launch()
|
|
|
|
|
|
| 9 |
import tensorflow_hub as hub
|
| 10 |
import io
|
| 11 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 12 |
+
import tempfile
|
| 13 |
import logging
|
| 14 |
|
| 15 |
# Configure logging
|
|
|
|
| 73 |
# Return the file path (no need to reopen the file with "rb" mode)
|
| 74 |
return temp_file_path
|
| 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 |
+
# Loop through each uploaded file (image)
|
| 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 |
+
# Compute textual similarity between caption and statement
|
| 89 |
+
textual_similarity_score = (compute_textual_similarity(caption, statement) * 100) # Multiply by 100
|
| 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}%", # Format as percentage with two decimal places
|
| 108 |
+
'ITM Score': f"{itm_score_statement:.2f}%", # Format as percentage with two decimal places
|
| 109 |
+
'Final Combined Score': f"{final_score:.2f}%" # Format as percentage with two decimal places
|
| 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)
|
|
|
|
| 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
|
| 122 |
+
image_input = gr.inputs.File(file_count="multiple", type="file", label="Upload Images")
|
|
|
|
| 123 |
output_df = gr.outputs.Dataframe(type="pandas", label="Results")
|
| 124 |
output_csv = gr.outputs.File(label="Download CSV")
|
| 125 |
|
|
|
|
| 132 |
css=".output { flex-direction: column; } .output .outputs { width: 100%; }" # Custom CSS
|
| 133 |
)
|
| 134 |
|
| 135 |
+
iface.launch()
|
| 136 |
+
|