Spaces:
Runtime error
Runtime error
small qol change - add export to .zip option + make it automatic (#3)
Browse files- add export to .zip option (0d7435c6f042cf30aa9205d5dee9be347a743572)
- Update app.py (4255813cd1cd050a7aef1951d8fe17c398635c99)
- make exports automatic (4b81adda4f15186c9b054e9af37e63785997a63f)
- Update app.py (c8e8d7cf878beee394ede97752e47a29c36d33d6)
Co-authored-by: Linoy Tsaban <[email protected]>
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
|
|
@@ -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,
|
|
@@ -104,9 +118,25 @@ def infer(input_image,
|
|
| 104 |
output_images = output.images[0]
|
| 105 |
|
| 106 |
output = []
|
|
|
|
| 107 |
for i, image in enumerate(output_images):
|
| 108 |
output.append(image)
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
ensure_dirname(LOG_DIR)
|
| 112 |
examples = [
|
|
@@ -187,10 +217,11 @@ with gr.Blocks() as demo:
|
|
| 187 |
|
| 188 |
run_button = gr.Button("Decompose!", variant="primary")
|
| 189 |
|
| 190 |
-
with gr.Column(scale=
|
| 191 |
gallery = gr.Gallery(label="Layers", columns=4, rows=1, format="png")
|
| 192 |
-
|
| 193 |
-
|
|
|
|
| 194 |
|
| 195 |
gr.Examples(examples=examples,
|
| 196 |
inputs=[input_image],
|
|
@@ -201,12 +232,6 @@ with gr.Blocks() as demo:
|
|
| 201 |
run_on_click=True
|
| 202 |
)
|
| 203 |
|
| 204 |
-
export_btn.click(
|
| 205 |
-
fn=export_gallery,
|
| 206 |
-
inputs=gallery,
|
| 207 |
-
outputs=export_file
|
| 208 |
-
)
|
| 209 |
-
|
| 210 |
run_button.click(
|
| 211 |
fn=infer,
|
| 212 |
inputs=[
|
|
@@ -221,7 +246,7 @@ with gr.Blocks() as demo:
|
|
| 221 |
cfg_norm,
|
| 222 |
use_en_prompt,
|
| 223 |
],
|
| 224 |
-
outputs=gallery,
|
| 225 |
)
|
| 226 |
|
| 227 |
if __name__ == "__main__":
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import random
|
| 5 |
import tempfile
|
| 6 |
+
import zipfile
|
| 7 |
import spaces
|
| 8 |
from PIL import Image
|
| 9 |
from diffusers import QwenImageLayeredPipeline
|
|
|
|
| 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,
|
|
|
|
| 118 |
output_images = output.images[0]
|
| 119 |
|
| 120 |
output = []
|
| 121 |
+
temp_files = []
|
| 122 |
for i, image in enumerate(output_images):
|
| 123 |
output.append(image)
|
| 124 |
+
# Save to temp file for export
|
| 125 |
+
tmp = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
|
| 126 |
+
image.save(tmp.name)
|
| 127 |
+
temp_files.append(tmp.name)
|
| 128 |
+
|
| 129 |
+
# Generate PPTX
|
| 130 |
+
pptx_path = imagelist_to_pptx(temp_files)
|
| 131 |
+
|
| 132 |
+
# Generate ZIP
|
| 133 |
+
with tempfile.NamedTemporaryFile(suffix=".zip", delete=False) as tmp:
|
| 134 |
+
with zipfile.ZipFile(tmp.name, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
| 135 |
+
for i, img_path in enumerate(temp_files):
|
| 136 |
+
zipf.write(img_path, f"layer_{i+1}.png")
|
| 137 |
+
zip_path = tmp.name
|
| 138 |
+
|
| 139 |
+
return output, pptx_path, zip_path
|
| 140 |
|
| 141 |
ensure_dirname(LOG_DIR)
|
| 142 |
examples = [
|
|
|
|
| 217 |
|
| 218 |
run_button = gr.Button("Decompose!", variant="primary")
|
| 219 |
|
| 220 |
+
with gr.Column(scale=2):
|
| 221 |
gallery = gr.Gallery(label="Layers", columns=4, rows=1, format="png")
|
| 222 |
+
with gr.Row():
|
| 223 |
+
export_file = gr.File(label="Download PPTX")
|
| 224 |
+
export_zip_file = gr.File(label="Download ZIP")
|
| 225 |
|
| 226 |
gr.Examples(examples=examples,
|
| 227 |
inputs=[input_image],
|
|
|
|
| 232 |
run_on_click=True
|
| 233 |
)
|
| 234 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
run_button.click(
|
| 236 |
fn=infer,
|
| 237 |
inputs=[
|
|
|
|
| 246 |
cfg_norm,
|
| 247 |
use_en_prompt,
|
| 248 |
],
|
| 249 |
+
outputs=[gallery, export_file, export_zip_file],
|
| 250 |
)
|
| 251 |
|
| 252 |
if __name__ == "__main__":
|