iamrobotbear commited on
Commit
be487a3
·
1 Parent(s): 0e93f3e

about ready to give the fk up

Browse files
Files changed (1) hide show
  1. app.py +26 -52
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 # Add this import
 
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
- image_file = io.BytesIO(image_file.read()) # Convert temporary file to a byte stream
79
- image = Image.open(image_file)
 
 
80
  image = np.array(image)
81
  logging.info('Starting process_images_and_statements')
82
 
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
- # 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
- logging.info('Finished process_images_and_statements')
 
119
 
120
- # Save results_df to a CSV file
121
- csv_results = save_dataframe_to_csv(results_df)
122
 
123
- # Return both the DataFrame and the CSV data for the Gradio interface
124
- return results_df, csv_results
125
 
126
- # Gradio interface
127
- image_input = gr.inputs.File(label="Upload Image")
128
- output_df = gr.outputs.Dataframe(type="pandas", label="Results")
129
- output_csv = gr.outputs.File(label="Download CSV")
 
 
 
130
 
131
  iface = gr.Interface(
132
  fn=process_images_and_statements,
133
- inputs=image_input,
134
- outputs=[output_df, output_csv],
135
- title="Image Captioning and Image-Text Matching",
136
- theme='sudeepshouche/minimalist',
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()