Rahatara commited on
Commit
e649e30
1 Parent(s): b9ec497

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -6
app.py CHANGED
@@ -1,8 +1,7 @@
 
1
  import gradio as gr
2
  import numpy as np
3
- import tempfile
4
- import shutil
5
- import os
6
 
7
  def sepia(input_img, num_copies):
8
  sepia_filter = np.array([
@@ -16,20 +15,29 @@ def sepia(input_img, num_copies):
16
  sepia_imgs = [(sepia_img).astype(np.uint8) for _ in range(num_copies)]
17
  return sepia_imgs
18
 
 
 
 
 
 
 
 
 
 
19
 
20
  with gr.Blocks() as demo:
21
  with gr.Row():
22
  input_img = gr.Image()
23
  num_copies = gr.Number(label="Number of Copies", value=1)
24
  gallery = gr.Gallery(label="Sepia Images")
25
-
26
 
27
  input_img.change(fn=lambda x: x, inputs=input_img, outputs=gallery)
28
- #num_copies.change(fn=lambda x: x, inputs=num_copies, outputs=gallery)
29
 
30
  generate_btn = gr.Button("Generate Sepia Images")
31
  generate_btn.click(fn=sepia, inputs=[input_img, num_copies], outputs=gallery)
32
-
33
 
34
  if __name__ == "__main__":
35
  demo.launch()
 
1
+ from pathlib import Path
2
  import gradio as gr
3
  import numpy as np
4
+ from zipfile import ZipFile
 
 
5
 
6
  def sepia(input_img, num_copies):
7
  sepia_filter = np.array([
 
15
  sepia_imgs = [(sepia_img).astype(np.uint8) for _ in range(num_copies)]
16
  return sepia_imgs
17
 
18
+ def zip_sepia_images(sepia_imgs):
19
+ temp_dir = Path(tempfile.mkdtemp()) # Use Path for easier file handling
20
+ zip_path = temp_dir / "sepia_images.zip"
21
+ with ZipFile(zip_path, 'w') as zipf:
22
+ for i, img in enumerate(sepia_imgs):
23
+ img_path = temp_dir / f"sepia_image_{i}.png"
24
+ gr.utils.save_image(str(img_path), img) # Convert Path object to string for compatibility
25
+ zipf.write(img_path, img_path.name)
26
+ return str(zip_path) # Convert Path object to string for return
27
 
28
  with gr.Blocks() as demo:
29
  with gr.Row():
30
  input_img = gr.Image()
31
  num_copies = gr.Number(label="Number of Copies", value=1)
32
  gallery = gr.Gallery(label="Sepia Images")
33
+ download_btn = gr.File(label="Download ZIP", visible=True)
34
 
35
  input_img.change(fn=lambda x: x, inputs=input_img, outputs=gallery)
36
+ num_copies.change(fn=lambda x: x, inputs=num_copies, outputs=gallery)
37
 
38
  generate_btn = gr.Button("Generate Sepia Images")
39
  generate_btn.click(fn=sepia, inputs=[input_img, num_copies], outputs=gallery)
40
+ generate_btn.click(fn=zip_sepia_images, inputs=gallery, outputs=download_btn)
41
 
42
  if __name__ == "__main__":
43
  demo.launch()