iamrobotbear commited on
Commit
080099f
·
1 Parent(s): e7aada3

think CSV is almost here

Browse files
Files changed (1) hide show
  1. app.py +13 -7
app.py CHANGED
@@ -85,12 +85,18 @@ def generate_caption(processor, model, image):
85
  return generated_caption
86
 
87
 
88
- # Function to save dataframe results to a CSV string for download
89
  def save_dataframe_to_csv(df):
90
  csv_buffer = io.StringIO()
91
  df.to_csv(csv_buffer, index=False)
92
  csv_string = csv_buffer.getvalue()
93
- return csv_string # Return the CSV contents as a string
 
 
 
 
 
 
 
94
 
95
  # Main function to perform image captioning and image-text matching
96
  def process_images_and_statements(image):
@@ -138,17 +144,17 @@ def process_images_and_statements(image):
138
  return results_df, csv_results # <--- Return results_df and csv_results
139
 
140
  # Gradio interface
141
- image_input = inputs.Image() # Use inputs instead of gr.inputs
142
- output_df = outputs.Dataframe(type="pandas", label="Results") # Use outputs instead of gr.outputs
143
- output_csv = outputs.File(label="Download CSV") # Use outputs instead of gr.outputs
144
 
145
  iface = gr.Interface(
146
  fn=process_images_and_statements,
147
  inputs=image_input,
148
- outputs=[output_df, output_csv], # Include both the DataFrame and CSV file outputs
149
  title="Image Captioning and Image-Text Matching",
150
  theme='sudeepshouche/minimalist',
151
- css=".output { flex-direction: column; } .output .outputs { width: 100%; }" # Custom CSS
152
  )
153
 
154
  iface.launch()
 
85
  return generated_caption
86
 
87
 
 
88
  def save_dataframe_to_csv(df):
89
  csv_buffer = io.StringIO()
90
  df.to_csv(csv_buffer, index=False)
91
  csv_string = csv_buffer.getvalue()
92
+
93
+ # Save the CSV string to a temporary file
94
+ with tempfile.NamedTemporaryFile(mode="w+", delete=False, suffix=".csv") as temp_file:
95
+ temp_file.write(csv_string)
96
+ temp_file_path = temp_file.name # Get the file path
97
+
98
+ # Reopen the file with "rb" mode and return the file object
99
+ return open(temp_file_path, "rb")
100
 
101
  # Main function to perform image captioning and image-text matching
102
  def process_images_and_statements(image):
 
144
  return results_df, csv_results # <--- Return results_df and csv_results
145
 
146
  # Gradio interface
147
+ image_input = gr.inputs.Image()
148
+ output_df = gr.outputs.Dataframe(type="pandas", label="Results")
149
+ output_csv = gr.outputs.File(label="Download CSV")
150
 
151
  iface = gr.Interface(
152
  fn=process_images_and_statements,
153
  inputs=image_input,
154
+ outputs=[output_df, output_csv],
155
  title="Image Captioning and Image-Text Matching",
156
  theme='sudeepshouche/minimalist',
157
+ css=".output { flex-direction: column; } .output .outputs { width: 100%; }"
158
  )
159
 
160
  iface.launch()