Spaces:
Running
on
Zero
Running
on
Zero
Update app.py (#7)
Browse files- Update app.py (c99afd26793bc3db68a8a487e67f11c11999cb92)
Co-authored-by: Shengming Yin <[email protected]>
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import numpy as np
|
|
| 4 |
import random
|
| 5 |
import tempfile
|
| 6 |
import spaces
|
|
|
|
| 7 |
from PIL import Image
|
| 8 |
from diffusers import QwenImageLayeredPipeline
|
| 9 |
import torch
|
|
@@ -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 = [
|
|
@@ -136,14 +166,14 @@ with gr.Blocks() as demo:
|
|
| 136 |
with gr.Column(scale=1):
|
| 137 |
input_image = gr.Image(label="Input Image", image_mode="RGBA")
|
| 138 |
|
| 139 |
-
prompt = gr.Textbox(
|
| 140 |
-
label="Prompt (Optional)",
|
| 141 |
-
placeholder="Please enter the prompt to descibe the image. (Optional)",
|
| 142 |
-
value="",
|
| 143 |
-
lines=2,
|
| 144 |
-
)
|
| 145 |
|
| 146 |
with gr.Accordion("Advanced Settings", open=False):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
neg_prompt = gr.Textbox(
|
| 148 |
label="Negative Prompt (Optional)",
|
| 149 |
placeholder="Please enter the negative prompt",
|
|
@@ -189,26 +219,21 @@ with gr.Blocks() as demo:
|
|
| 189 |
|
| 190 |
run_button = gr.Button("Decompose!", variant="primary")
|
| 191 |
|
| 192 |
-
with gr.Column(scale=
|
| 193 |
gallery = gr.Gallery(label="Layers", columns=4, rows=1, format="png")
|
| 194 |
-
|
| 195 |
-
|
|
|
|
| 196 |
|
| 197 |
gr.Examples(examples=examples,
|
| 198 |
inputs=[input_image],
|
| 199 |
-
outputs=[gallery],
|
| 200 |
fn=infer,
|
| 201 |
examples_per_page=14,
|
| 202 |
cache_examples=False,
|
| 203 |
run_on_click=True
|
| 204 |
)
|
| 205 |
|
| 206 |
-
export_btn.click(
|
| 207 |
-
fn=export_gallery,
|
| 208 |
-
inputs=gallery,
|
| 209 |
-
outputs=export_file
|
| 210 |
-
)
|
| 211 |
-
|
| 212 |
run_button.click(
|
| 213 |
fn=infer,
|
| 214 |
inputs=[
|
|
@@ -223,7 +248,7 @@ with gr.Blocks() as demo:
|
|
| 223 |
cfg_norm,
|
| 224 |
use_en_prompt,
|
| 225 |
],
|
| 226 |
-
outputs=gallery,
|
| 227 |
)
|
| 228 |
|
| 229 |
if __name__ == "__main__":
|
|
|
|
| 4 |
import random
|
| 5 |
import tempfile
|
| 6 |
import spaces
|
| 7 |
+
import zipfile
|
| 8 |
from PIL import Image
|
| 9 |
from diffusers import QwenImageLayeredPipeline
|
| 10 |
import torch
|
|
|
|
| 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 = [
|
|
|
|
| 166 |
with gr.Column(scale=1):
|
| 167 |
input_image = gr.Image(label="Input Image", image_mode="RGBA")
|
| 168 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
|
| 170 |
with gr.Accordion("Advanced Settings", open=False):
|
| 171 |
+
prompt = gr.Textbox(
|
| 172 |
+
label="Prompt (Optional)",
|
| 173 |
+
placeholder="Please enter the prompt to descibe the image. (Optional)",
|
| 174 |
+
value="",
|
| 175 |
+
lines=2,
|
| 176 |
+
)
|
| 177 |
neg_prompt = gr.Textbox(
|
| 178 |
label="Negative Prompt (Optional)",
|
| 179 |
placeholder="Please enter the negative prompt",
|
|
|
|
| 219 |
|
| 220 |
run_button = gr.Button("Decompose!", variant="primary")
|
| 221 |
|
| 222 |
+
with gr.Column(scale=2):
|
| 223 |
gallery = gr.Gallery(label="Layers", columns=4, rows=1, format="png")
|
| 224 |
+
with gr.Row():
|
| 225 |
+
export_file = gr.File(label="Download PPTX")
|
| 226 |
+
export_zip_file = gr.File(label="Download ZIP")
|
| 227 |
|
| 228 |
gr.Examples(examples=examples,
|
| 229 |
inputs=[input_image],
|
| 230 |
+
outputs=[gallery, export_file, export_zip_file],
|
| 231 |
fn=infer,
|
| 232 |
examples_per_page=14,
|
| 233 |
cache_examples=False,
|
| 234 |
run_on_click=True
|
| 235 |
)
|
| 236 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
run_button.click(
|
| 238 |
fn=infer,
|
| 239 |
inputs=[
|
|
|
|
| 248 |
cfg_norm,
|
| 249 |
use_en_prompt,
|
| 250 |
],
|
| 251 |
+
outputs=[gallery, export_file, export_zip_file],
|
| 252 |
)
|
| 253 |
|
| 254 |
if __name__ == "__main__":
|