Spaces:
Build error
Build error
iamrobotbear
commited on
Commit
·
15b96ac
1
Parent(s):
0bb8ce2
pandas.concat
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
|
@@ -36,12 +36,10 @@ def compute_textual_similarity(caption, statement):
|
|
36 |
similarity_score = cosine_similarity([caption_embedding], [statement_embedding])[0][0]
|
37 |
return similarity_score
|
38 |
|
39 |
-
# List of statements for Image-Text Matching
|
40 |
# Read statements from the external file 'statements.txt'
|
41 |
with open('statements.txt', 'r') as file:
|
42 |
statements = file.read().splitlines()
|
43 |
|
44 |
-
|
45 |
# Function to compute ITM scores for the image-statement pair
|
46 |
def compute_itm_score(image, statement):
|
47 |
logging.info('Starting compute_itm_score')
|
@@ -66,12 +64,12 @@ def save_dataframe_to_csv(df):
|
|
66 |
csv_buffer = io.StringIO()
|
67 |
df.to_csv(csv_buffer, index=False)
|
68 |
csv_string = csv_buffer.getvalue()
|
69 |
-
|
70 |
# Save the CSV string to a temporary file
|
71 |
with tempfile.NamedTemporaryFile(mode="w+", delete=False, suffix=".csv") as temp_file:
|
72 |
temp_file.write(csv_string)
|
73 |
-
temp_file_path = temp_file.name
|
74 |
-
|
75 |
# Return the file path (no need to reopen the file with "rb" mode)
|
76 |
return temp_file_path
|
77 |
|
@@ -86,38 +84,41 @@ def process_images_and_statements(image):
|
|
86 |
weight_textual_similarity = 0.5
|
87 |
weight_statement = 0.5
|
88 |
|
89 |
-
# Initialize an empty
|
90 |
-
|
91 |
|
92 |
# Loop through each predefined statement
|
93 |
for statement in statements:
|
94 |
# Compute textual similarity between caption and statement
|
95 |
-
textual_similarity_score = (compute_textual_similarity(caption, statement) * 100)
|
96 |
|
97 |
# Compute ITM score for the image-statement pair
|
98 |
-
itm_score_statement = (compute_itm_score(image, statement) * 100)
|
99 |
|
100 |
# Combine the two scores using a weighted average
|
101 |
final_score = ((weight_textual_similarity * textual_similarity_score) +
|
102 |
(weight_statement * itm_score_statement))
|
103 |
|
104 |
-
# Append the result to the
|
105 |
-
|
106 |
'Statement': statement,
|
107 |
-
'Generated Caption': caption,
|
108 |
-
'Textual Similarity Score': f"{textual_similarity_score:.2f}%",
|
109 |
-
'ITM Score': f"{itm_score_statement:.2f}%",
|
110 |
-
'Final Combined Score': f"{final_score:.2f}%"
|
111 |
-
}
|
|
|
|
|
|
|
112 |
|
113 |
logging.info('Finished process_images_and_statements')
|
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
|
122 |
image_input = gr.inputs.Image()
|
123 |
output_df = gr.outputs.Dataframe(type="pandas", label="Results")
|
@@ -126,10 +127,10 @@ output_csv = gr.outputs.File(label="Download CSV")
|
|
126 |
iface = gr.Interface(
|
127 |
fn=process_images_and_statements,
|
128 |
inputs=image_input,
|
129 |
-
outputs=[output_df, output_csv],
|
130 |
title="Image Captioning and Image-Text Matching",
|
131 |
theme='sudeepshouche/minimalist',
|
132 |
-
css=".output { flex-direction: column; } .output .outputs { width: 100%; }"
|
133 |
)
|
134 |
|
135 |
iface.launch()
|
|
|
9 |
import tensorflow_hub as hub
|
10 |
import io
|
11 |
from sklearn.metrics.pairwise import cosine_similarity
|
12 |
+
import tempfile # Add this import
|
13 |
import logging
|
14 |
|
15 |
# Configure logging
|
|
|
36 |
similarity_score = cosine_similarity([caption_embedding], [statement_embedding])[0][0]
|
37 |
return similarity_score
|
38 |
|
|
|
39 |
# Read statements from the external file 'statements.txt'
|
40 |
with open('statements.txt', 'r') as file:
|
41 |
statements = file.read().splitlines()
|
42 |
|
|
|
43 |
# Function to compute ITM scores for the image-statement pair
|
44 |
def compute_itm_score(image, statement):
|
45 |
logging.info('Starting compute_itm_score')
|
|
|
64 |
csv_buffer = io.StringIO()
|
65 |
df.to_csv(csv_buffer, index=False)
|
66 |
csv_string = csv_buffer.getvalue()
|
67 |
+
|
68 |
# Save the CSV string to a temporary file
|
69 |
with tempfile.NamedTemporaryFile(mode="w+", delete=False, suffix=".csv") as temp_file:
|
70 |
temp_file.write(csv_string)
|
71 |
+
temp_file_path = temp_file.name # Get the file path
|
72 |
+
|
73 |
# Return the file path (no need to reopen the file with "rb" mode)
|
74 |
return temp_file_path
|
75 |
|
|
|
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 between caption and statement
|
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, # Include the generated caption
|
106 |
+
'Textual Similarity Score': f"{textual_similarity_score:.2f}%", # Format as percentage with two decimal places
|
107 |
+
'ITM Score': f"{itm_score_statement:.2f}%", # Format as percentage with two decimal places
|
108 |
+
'Final Combined Score': f"{final_score:.2f}%" # Format as percentage with two decimal places
|
109 |
+
})
|
110 |
+
|
111 |
+
# Convert the results_list to a DataFrame using pandas.concat
|
112 |
+
results_df = pd.concat([pd.DataFrame([result]) for result in results_list], ignore_index=True)
|
113 |
|
114 |
logging.info('Finished process_images_and_statements')
|
115 |
|
116 |
# Save results_df to a CSV file
|
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 # <--- Return results_df and csv_results
|
121 |
+
|
122 |
# Gradio interface
|
123 |
image_input = gr.inputs.Image()
|
124 |
output_df = gr.outputs.Dataframe(type="pandas", label="Results")
|
|
|
127 |
iface = gr.Interface(
|
128 |
fn=process_images_and_statements,
|
129 |
inputs=image_input,
|
130 |
+
outputs=[output_df, output_csv], # Include both the DataFrame and CSV file outputs
|
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()
|