heaversm commited on
Commit
37e5f7e
·
1 Parent(s): 73cb8f9

add ability to zip and download all images

Browse files
Files changed (1) hide show
  1. app.py +34 -2
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import os
2
  import sys
 
3
 
4
  import gradio as gr
5
  from openai import OpenAI
@@ -44,6 +45,30 @@ try:
44
  except Exception as e:
45
  print(e)
46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  def generate_images(prompts, pw, model):
48
  # add a conditional to check for a valid password
49
  if pw != os.getenv("PW"):
@@ -93,6 +118,7 @@ def generate_images(prompts, pw, model):
93
  except Exception as error:
94
  print(str(error))
95
  raise gr.Error(f"An error occurred while generating the image for: {prompt}")
 
96
  return image_paths
97
 
98
 
@@ -109,7 +135,13 @@ with gr.Blocks() as demo:
109
  # output_image = gr.Image(label="Image Output")
110
  output_images = gr.Gallery(label="Image Outputs",columns=[3], rows=[1], object_fit="contain", height="auto",allow_preview=False)
111
 
112
- text.submit(fn=generate_images, inputs=[text,pw,model], outputs=output_images, api_name="generate_image")
113
- btn.click(fn=generate_images, inputs=[text,pw,model], outputs=output_images, api_name=False)
 
 
 
 
 
 
114
 
115
  demo.launch(share=True)
 
1
  import os
2
  import sys
3
+ import zipfile
4
 
5
  import gradio as gr
6
  from openai import OpenAI
 
45
  except Exception as e:
46
  print(e)
47
 
48
+ image_paths_global = []
49
+
50
+ def generate_images_wrapper(prompts, pw, model):
51
+ global image_paths_global
52
+ image_paths = generate_images(prompts, pw, model)
53
+ image_paths_global = image_paths # Store paths globally
54
+ return image_paths # You might want to return something else for display
55
+
56
+ def zip_images(image_paths):
57
+ zip_file_path = tempfile.NamedTemporaryFile(delete=False, suffix='.zip').name
58
+ with zipfile.ZipFile(zip_file_path, 'w') as zipf:
59
+ for path in image_paths:
60
+ zipf.write(path, arcname=os.path.basename(path))
61
+ os.remove(path) # Clean up the temp image file
62
+ return zip_file_path
63
+
64
+ def download_all_images():
65
+ global image_paths_global
66
+ if not image_paths_global:
67
+ raise gr.Error("No images to download.")
68
+ zip_path = zip_images(image_paths_global)
69
+ image_paths_global = [] # Reset the global variable
70
+ return zip_path
71
+
72
  def generate_images(prompts, pw, model):
73
  # add a conditional to check for a valid password
74
  if pw != os.getenv("PW"):
 
118
  except Exception as error:
119
  print(str(error))
120
  raise gr.Error(f"An error occurred while generating the image for: {prompt}")
121
+
122
  return image_paths
123
 
124
 
 
135
  # output_image = gr.Image(label="Image Output")
136
  output_images = gr.Gallery(label="Image Outputs",columns=[3], rows=[1], object_fit="contain", height="auto",allow_preview=False)
137
 
138
+ text.submit(fn=generate_images_wrapper, inputs=[text,pw,model], outputs=output_images, api_name="generate_image")
139
+ btn.click(fn=generate_images_wrapper, inputs=[text,pw,model], outputs=output_images, api_name=False)
140
+
141
+ download_all_btn = gr.Button("Download All")
142
+ download_link = gr.File(label="Download Zip")
143
+ download_all_btn.click(fn=download_all_images, inputs=[], outputs=download_link)
144
+
145
+
146
 
147
  demo.launch(share=True)