Spaces:
Build error
Build error
Commit
·
be487a3
1
Parent(s):
0e93f3e
about ready to give the fk up
Browse files
app.py
CHANGED
@@ -8,8 +8,11 @@ from transformers import AutoTokenizer, AutoModelForCausalLM, AutoProcessor
|
|
8 |
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,68 +76,39 @@ 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(image_file):
|
78 |
-
|
79 |
-
|
|
|
|
|
80 |
image = np.array(image)
|
81 |
logging.info('Starting process_images_and_statements')
|
82 |
|
83 |
-
# Generate
|
84 |
-
|
85 |
-
|
86 |
-
# Define weights for combining textual similarity score and image-statement ITM score (adjust as needed)
|
87 |
-
weight_textual_similarity = 0.5
|
88 |
-
weight_statement = 0.5
|
89 |
-
|
90 |
-
# Initialize an empty list to store the results
|
91 |
-
results_list = []
|
92 |
-
|
93 |
-
# Loop through each predefined statement
|
94 |
-
for statement in statements:
|
95 |
-
# Compute textual similarity between caption and statement
|
96 |
-
textual_similarity_score = (compute_textual_similarity(caption, statement) * 100) # Multiply by 100
|
97 |
-
|
98 |
-
# Compute ITM score for the image-statement pair
|
99 |
-
itm_score_statement = (compute_itm_score(image, statement) * 100) # Multiply by 100
|
100 |
-
|
101 |
-
# Combine the two scores using a weighted average
|
102 |
-
final_score = ((weight_textual_similarity * textual_similarity_score) +
|
103 |
-
(weight_statement * itm_score_statement))
|
104 |
-
|
105 |
-
# Append the result to the results_list
|
106 |
-
results_list.append({
|
107 |
-
'File Name': file.name, # Include the file name
|
108 |
-
'Statement': statement,
|
109 |
-
'Generated Caption': caption,
|
110 |
-
'Textual Similarity Score': f"{textual_similarity_score:.2f}%",
|
111 |
-
'ITM Score': f"{itm_score_statement:.2f}%",
|
112 |
-
'Final Combined Score': f"{final_score:.2f}%"
|
113 |
-
})
|
114 |
-
|
115 |
-
# Convert the results_list to a DataFrame using pandas.concat
|
116 |
-
results_df = pd.concat([pd.DataFrame([result]) for result in results_list], ignore_index=True)
|
117 |
|
118 |
-
|
|
|
119 |
|
120 |
-
#
|
121 |
-
csv_results = save_dataframe_to_csv(results_df)
|
122 |
|
123 |
-
|
124 |
-
return results_df, csv_results
|
125 |
|
126 |
-
# Gradio interface
|
127 |
-
image_input = gr.inputs.
|
128 |
-
|
129 |
-
|
|
|
|
|
|
|
130 |
|
131 |
iface = gr.Interface(
|
132 |
fn=process_images_and_statements,
|
133 |
-
inputs=image_input,
|
134 |
-
outputs=
|
135 |
-
title="Image Captioning and
|
136 |
-
|
137 |
-
css=".output { flex-direction: column; } .output .outputs { width: 100%; }" # Custom CSS
|
138 |
)
|
139 |
|
|
|
140 |
iface.launch()
|
|
|
8 |
import tensorflow as tf
|
9 |
import tensorflow_hub as hub
|
10 |
import io
|
11 |
+
import os
|
12 |
+
import numpy as np
|
13 |
from sklearn.metrics.pairwise import cosine_similarity
|
14 |
+
import tempfile
|
15 |
+
import shutil
|
16 |
import logging
|
17 |
|
18 |
# Configure logging
|
|
|
76 |
# Return the file path (no need to reopen the file with "rb" mode)
|
77 |
return temp_file_path
|
78 |
|
|
|
79 |
def process_images_and_statements(image_file):
|
80 |
+
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
|
81 |
+
shutil.copyfileobj(image_file, temp_file)
|
82 |
+
|
83 |
+
image = Image.open(temp_file.name)
|
84 |
image = np.array(image)
|
85 |
logging.info('Starting process_images_and_statements')
|
86 |
|
87 |
+
# Generate the image caption
|
88 |
+
generated_caption = caption_image(image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
|
90 |
+
# Match the statements
|
91 |
+
matched_statements = match_statements(image, statements)
|
92 |
|
93 |
+
os.unlink(temp_file.name) # Remove the temporary file
|
|
|
94 |
|
95 |
+
return generated_caption, matched_statements
|
|
|
96 |
|
97 |
+
# Define Gradio interface
|
98 |
+
image_input = gr.inputs.Image(type="file", label="Upload Image")
|
99 |
+
text_input = gr.inputs.Textbox(lines=5, label="Enter Statements (one per line)")
|
100 |
+
outputs = [
|
101 |
+
gr.outputs.Textbox(label="Generated Caption"),
|
102 |
+
gr.outputs.Textbox(lines=5, label="Matched Statements"),
|
103 |
+
]
|
104 |
|
105 |
iface = gr.Interface(
|
106 |
fn=process_images_and_statements,
|
107 |
+
inputs=[image_input, text_input],
|
108 |
+
outputs=outputs,
|
109 |
+
title="Image Captioning and Matching",
|
110 |
+
description="Upload an image and enter statements to generate a caption for the image and match the statements.",
|
|
|
111 |
)
|
112 |
|
113 |
+
# Launch Gradio app
|
114 |
iface.launch()
|