wjm55 commited on
Commit
0388107
·
1 Parent(s): 5674d41

added download feature

Browse files
Files changed (1) hide show
  1. app.py +33 -0
app.py CHANGED
@@ -183,6 +183,39 @@ with gr.Blocks(css=css) as demo:
183
  output_text = gr.Textbox(label="Output Text", elem_id="output")
184
 
185
  submit_btn.click(run_example, [input_img, model_selector], [output_text])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
186
 
187
  demo.queue(api_open=False)
188
  demo.launch(debug=True)
 
183
  output_text = gr.Textbox(label="Output Text", elem_id="output")
184
 
185
  submit_btn.click(run_example, [input_img, model_selector], [output_text])
186
+ with gr.Row():
187
+ filename = gr.Textbox(label="Save filename (without extension)", placeholder="Enter filename to save")
188
+ download_btn = gr.Button("Download Image & Text", elem_classes="submit-btn")
189
+
190
+ def create_zip(image, text, fname):
191
+ if not image or not text or not fname:
192
+ return None
193
+
194
+ # Create a temporary directory
195
+ with tempfile.TemporaryDirectory() as temp_dir:
196
+ # Save image with same extension
197
+ img_ext = image.format.lower() if hasattr(image, 'format') else 'png'
198
+ img_path = os.path.join(temp_dir, f"{fname}.{img_ext}")
199
+ image.save(img_path)
200
+
201
+ # Save text
202
+ txt_path = os.path.join(temp_dir, f"{fname}.txt")
203
+ with open(txt_path, 'w', encoding='utf-8') as f:
204
+ f.write(text)
205
+
206
+ # Create zip file
207
+ zip_path = os.path.join(temp_dir, f"{fname}.zip")
208
+ with zipfile.ZipFile(zip_path, 'w') as zipf:
209
+ zipf.write(img_path, os.path.basename(img_path))
210
+ zipf.write(txt_path, os.path.basename(txt_path))
211
+
212
+ return zip_path
213
+
214
+ download_btn.click(
215
+ create_zip,
216
+ inputs=[input_img, output_text, filename],
217
+ outputs=gr.File(label="Download")
218
+ )
219
 
220
  demo.queue(api_open=False)
221
  demo.launch(debug=True)