linoyts HF Staff commited on
Commit
0d7435c
·
verified ·
1 Parent(s): 2015b5b

add export to .zip option

Browse files
Files changed (1) hide show
  1. app.py +27 -3
app.py CHANGED
@@ -3,6 +3,7 @@ import uuid
3
  import numpy as np
4
  import random
5
  import tempfile
 
6
  import spaces
7
  from PIL import Image
8
  from diffusers import QwenImageLayeredPipeline
@@ -15,7 +16,7 @@ LOG_DIR = "/tmp/local"
15
  MAX_SEED = np.iinfo(np.int32).max
16
 
17
  from huggingface_hub import login
18
- login(token=os.environ.get('hf'))
19
 
20
  dtype = torch.bfloat16
21
  device = "cuda" if torch.cuda.is_available() else "cpu"
@@ -58,6 +59,19 @@ def export_gallery(images):
58
  pptx_path = imagelist_to_pptx(images)
59
  return pptx_path
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  @spaces.GPU(duration=300)
62
  def infer(input_image,
63
  seed=777,
@@ -189,8 +203,12 @@ with gr.Blocks() as demo:
189
 
190
  with gr.Column(scale=1):
191
  gallery = gr.Gallery(label="Layers", columns=4, rows=1, format="png")
192
- export_btn = gr.Button("Export as PPTX")
193
- export_file = gr.File(label="Download PPTX")
 
 
 
 
194
 
195
  gr.Examples(examples=examples,
196
  inputs=[input_image],
@@ -207,6 +225,12 @@ with gr.Blocks() as demo:
207
  outputs=export_file
208
  )
209
 
 
 
 
 
 
 
210
  run_button.click(
211
  fn=infer,
212
  inputs=[
 
3
  import numpy as np
4
  import random
5
  import tempfile
6
+ import zipfile # Added import
7
  import spaces
8
  from PIL import Image
9
  from diffusers import QwenImageLayeredPipeline
 
16
  MAX_SEED = np.iinfo(np.int32).max
17
 
18
  from huggingface_hub import login
19
+ login(token=os.environ.get('HF_TOKEN'))
20
 
21
  dtype = torch.bfloat16
22
  device = "cuda" if torch.cuda.is_available() else "cpu"
 
59
  pptx_path = imagelist_to_pptx(images)
60
  return pptx_path
61
 
62
+ def export_gallery_zip(images):
63
+ # images: list of tuples (file_path, caption)
64
+ images = [e[0] for e in images]
65
+
66
+ with tempfile.NamedTemporaryFile(suffix=".zip", delete=False) as tmp:
67
+ with zipfile.ZipFile(tmp.name, 'w', zipfile.ZIP_DEFLATED) as zipf:
68
+ for i, img_path in enumerate(images):
69
+ # Get the file extension from original file
70
+ ext = os.path.splitext(img_path)[1] or '.png'
71
+ # Add each image to the zip with a numbered filename
72
+ zipf.write(img_path, f"layer_{i+1}{ext}")
73
+ return tmp.name
74
+
75
  @spaces.GPU(duration=300)
76
  def infer(input_image,
77
  seed=777,
 
203
 
204
  with gr.Column(scale=1):
205
  gallery = gr.Gallery(label="Layers", columns=4, rows=1, format="png")
206
+ with gr.Row():
207
+ export_btn = gr.Button("Export as PPTX")
208
+ export_zip_btn = gr.Button("Export as ZIP")
209
+ with gr.Row():
210
+ export_file = gr.File(label="Download PPTX")
211
+ export_zip_file = gr.File(label="Download ZIP")
212
 
213
  gr.Examples(examples=examples,
214
  inputs=[input_image],
 
225
  outputs=export_file
226
  )
227
 
228
+ export_zip_btn.click(
229
+ fn=export_gallery_zip,
230
+ inputs=gallery,
231
+ outputs=export_zip_file
232
+ )
233
+
234
  run_button.click(
235
  fn=infer,
236
  inputs=[