Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,154 +1,178 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
import random
|
| 4 |
|
| 5 |
-
# import spaces #[uncomment to use ZeroGPU]
|
| 6 |
-
from diffusers import DiffusionPipeline
|
| 7 |
import torch
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
pipe =
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
negative_prompt,
|
| 28 |
-
|
| 29 |
-
randomize_seed,
|
| 30 |
width,
|
| 31 |
height,
|
| 32 |
guidance_scale,
|
| 33 |
num_inference_steps,
|
| 34 |
-
|
|
|
|
| 35 |
):
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
"""
|
| 66 |
|
| 67 |
-
with gr.Blocks(css=
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
with gr.Row():
|
| 72 |
-
prompt = gr.Text(
|
| 73 |
-
label="Prompt",
|
| 74 |
-
show_label=False,
|
| 75 |
-
max_lines=1,
|
| 76 |
-
placeholder="Enter your prompt",
|
| 77 |
-
container=False,
|
| 78 |
-
)
|
| 79 |
-
|
| 80 |
-
run_button = gr.Button("Run", scale=0, variant="primary")
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
placeholder="Enter a negative prompt",
|
| 89 |
-
visible=False,
|
| 90 |
)
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
value=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
)
|
| 99 |
-
|
| 100 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 101 |
|
| 102 |
with gr.Row():
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
minimum=256,
|
| 106 |
-
maximum=MAX_IMAGE_SIZE,
|
| 107 |
-
step=32,
|
| 108 |
-
value=1024, # Replace with defaults that work for your model
|
| 109 |
-
)
|
| 110 |
-
|
| 111 |
-
height = gr.Slider(
|
| 112 |
-
label="Height",
|
| 113 |
-
minimum=256,
|
| 114 |
-
maximum=MAX_IMAGE_SIZE,
|
| 115 |
-
step=32,
|
| 116 |
-
value=1024, # Replace with defaults that work for your model
|
| 117 |
-
)
|
| 118 |
|
| 119 |
with gr.Row():
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
num_inference_steps,
|
| 149 |
-
],
|
| 150 |
-
outputs=[result, seed],
|
| 151 |
)
|
| 152 |
|
| 153 |
if __name__ == "__main__":
|
| 154 |
demo.launch()
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import io
|
| 3 |
+
import zipfile
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
|
| 6 |
import gradio as gr
|
| 7 |
+
from PIL import Image
|
|
|
|
| 8 |
|
|
|
|
|
|
|
| 9 |
import torch
|
| 10 |
+
from diffusers import AutoPipelineForText2Image, DPMSolverMultistepScheduler
|
| 11 |
+
|
| 12 |
+
# --------- Helper: load model ----------
|
| 13 |
+
@torch.inference_mode()
|
| 14 |
+
def load_pipeline(model_id: str, torch_dtype=torch.float16, device=None):
|
| 15 |
+
pipe = AutoPipelineForText2Image.from_pretrained(
|
| 16 |
+
model_id,
|
| 17 |
+
torch_dtype=torch_dtype,
|
| 18 |
+
use_safetensors=True
|
| 19 |
+
)
|
| 20 |
+
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
|
| 21 |
+
if device is None:
|
| 22 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 23 |
+
pipe = pipe.to(device)
|
| 24 |
+
# small memory tweaks
|
| 25 |
+
if device == "cuda":
|
| 26 |
+
pipe.enable_attention_slicing()
|
| 27 |
+
pipe.enable_vae_slicing()
|
| 28 |
+
return pipe, device
|
| 29 |
+
|
| 30 |
+
# Cache of loaded models so switching is fast
|
| 31 |
+
_PIPELINES = {}
|
| 32 |
+
|
| 33 |
+
def get_pipe(model_id: str):
|
| 34 |
+
if model_id not in _PIPELINES:
|
| 35 |
+
_PIPELINES[model_id], _ = load_pipeline(model_id)
|
| 36 |
+
return _PIPELINES[model_id]
|
| 37 |
+
|
| 38 |
+
# --------- Core generation ----------
|
| 39 |
+
def parse_prompts(text: str):
|
| 40 |
+
# Split by comma, strip whitespace, drop empties
|
| 41 |
+
parts = [p.strip() for p in text.split(",")]
|
| 42 |
+
return [p for p in parts if p]
|
| 43 |
+
|
| 44 |
+
def generate_images(
|
| 45 |
+
prompts_text,
|
| 46 |
negative_prompt,
|
| 47 |
+
model_id,
|
|
|
|
| 48 |
width,
|
| 49 |
height,
|
| 50 |
guidance_scale,
|
| 51 |
num_inference_steps,
|
| 52 |
+
batch_per_prompt,
|
| 53 |
+
seed
|
| 54 |
):
|
| 55 |
+
prompts = parse_prompts(prompts_text)
|
| 56 |
+
if not prompts:
|
| 57 |
+
return [], None, "Please enter at least one prompt (use commas to separate)."
|
| 58 |
+
|
| 59 |
+
pipe = get_pipe(model_id)
|
| 60 |
+
|
| 61 |
+
# Seeding
|
| 62 |
+
if seed is None or str(seed).strip() == "":
|
| 63 |
+
generator = torch.Generator(device=pipe.device).manual_seed(torch.seed())
|
| 64 |
+
else:
|
| 65 |
+
try:
|
| 66 |
+
seed_val = int(seed)
|
| 67 |
+
except:
|
| 68 |
+
seed_val = torch.seed()
|
| 69 |
+
generator = torch.Generator(device=pipe.device).manual_seed(seed_val)
|
| 70 |
+
|
| 71 |
+
all_images = []
|
| 72 |
+
names = []
|
| 73 |
+
for i, p in enumerate(prompts, start=1):
|
| 74 |
+
images = pipe(
|
| 75 |
+
prompt=p,
|
| 76 |
+
negative_prompt=negative_prompt if negative_prompt else None,
|
| 77 |
+
width=width,
|
| 78 |
+
height=height,
|
| 79 |
+
guidance_scale=guidance_scale,
|
| 80 |
+
num_inference_steps=num_inference_steps,
|
| 81 |
+
num_images_per_prompt=batch_per_prompt,
|
| 82 |
+
generator=generator
|
| 83 |
+
).images
|
| 84 |
+
|
| 85 |
+
# Collect and name
|
| 86 |
+
for j, img in enumerate(images, start=1):
|
| 87 |
+
all_images.append(img)
|
| 88 |
+
safe_prompt = "".join(c for c in p[:40] if c.isalnum() or c in "-_ ").strip().replace(" ", "_")
|
| 89 |
+
if not safe_prompt:
|
| 90 |
+
safe_prompt = f"prompt_{i}"
|
| 91 |
+
names.append(f"{safe_prompt}_{j}.png")
|
| 92 |
+
|
| 93 |
+
# Build ZIP in-memory
|
| 94 |
+
buf = io.BytesIO()
|
| 95 |
+
with zipfile.ZipFile(buf, "w", zipfile.ZIP_DEFLATED) as zf:
|
| 96 |
+
for img, name in zip(all_images, names):
|
| 97 |
+
bio = io.BytesIO()
|
| 98 |
+
img.save(bio, format="PNG")
|
| 99 |
+
bio.seek(0)
|
| 100 |
+
zf.writestr(name, bio.read())
|
| 101 |
+
buf.seek(0)
|
| 102 |
+
|
| 103 |
+
zip_name = f"images_{datetime.now().strftime('%Y%m%d_%H%M%S')}.zip"
|
| 104 |
+
return all_images, (zip_name, buf), f"Generated {len(all_images)} image(s) from {len(prompts)} prompt(s)."
|
| 105 |
+
|
| 106 |
+
# --------- UI ----------
|
| 107 |
+
CSS = """
|
| 108 |
+
.gradio-container {max-width: 1100px !important}
|
| 109 |
"""
|
| 110 |
|
| 111 |
+
with gr.Blocks(css=CSS, theme=gr.themes.Soft()) as demo:
|
| 112 |
+
gr.Markdown("# 🖼️ Multi-Prompt Text-to-Image (Hugging Face Space)")
|
| 113 |
+
gr.Markdown("Enter **comma-separated prompts** to generate multiple images at once. Choose size, batch count, and download all results as a ZIP.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
+
with gr.Row():
|
| 116 |
+
with gr.Column():
|
| 117 |
+
prompts_text = gr.Textbox(
|
| 118 |
+
label="Prompts (comma-separated)",
|
| 119 |
+
placeholder="A futuristic city at sunset, A cozy cabin in the woods, A portrait of a cyberpunk samurai",
|
| 120 |
+
lines=4
|
|
|
|
|
|
|
| 121 |
)
|
| 122 |
+
negative_prompt = gr.Textbox(
|
| 123 |
+
label="Negative prompt (optional)",
|
| 124 |
+
placeholder="blurry, low quality, distorted"
|
| 125 |
+
)
|
| 126 |
+
model_id = gr.Dropdown(
|
| 127 |
+
label="Model",
|
| 128 |
+
value="stabilityai/sdxl-turbo",
|
| 129 |
+
choices=[
|
| 130 |
+
"stabilityai/sdxl-turbo", # very fast SDXL
|
| 131 |
+
"runwayml/stable-diffusion-v1-5",
|
| 132 |
+
"stabilityai/stable-diffusion-2-1"
|
| 133 |
+
]
|
| 134 |
+
)
|
| 135 |
+
size = gr.Dropdown(
|
| 136 |
+
label="Image Size",
|
| 137 |
+
value="1024x1024",
|
| 138 |
+
choices=["512x512", "768x768", "1024x1024", "768x1024 (portrait)", "1024x768 (landscape)"]
|
| 139 |
)
|
|
|
|
|
|
|
| 140 |
|
| 141 |
with gr.Row():
|
| 142 |
+
guidance_scale = gr.Slider(0.0, 12.0, value=2.0, step=0.5, label="Guidance scale (SDXL-Turbo likes low)")
|
| 143 |
+
steps = gr.Slider(2, 50, value=8, step=1, label="Steps")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
with gr.Row():
|
| 146 |
+
batch_per_prompt = gr.Slider(1, 6, value=2, step=1, label="Images per prompt")
|
| 147 |
+
seed = gr.Textbox(label="Seed (optional, integer)")
|
| 148 |
+
|
| 149 |
+
run_btn = gr.Button("Generate", variant="primary")
|
| 150 |
+
|
| 151 |
+
with gr.Column():
|
| 152 |
+
gallery = gr.Gallery(label="Results", show_label=True, columns=3, height=520)
|
| 153 |
+
zip_file = gr.File(label="Download all images (.zip)")
|
| 154 |
+
status = gr.Markdown("")
|
| 155 |
+
|
| 156 |
+
def on_size_change(s):
|
| 157 |
+
if "x" in s and s.count("x") == 1 and "(" not in s:
|
| 158 |
+
w, h = s.split("x")
|
| 159 |
+
return int(w), int(h)
|
| 160 |
+
if s == "768x1024 (portrait)":
|
| 161 |
+
return 768, 1024
|
| 162 |
+
if s == "1024x768 (landscape)":
|
| 163 |
+
return 1024, 768
|
| 164 |
+
return 1024, 1024
|
| 165 |
+
|
| 166 |
+
width = gr.State(1024)
|
| 167 |
+
height = gr.State(1024)
|
| 168 |
+
size.change(fn=on_size_change, inputs=size, outputs=[width, height])
|
| 169 |
+
|
| 170 |
+
run_btn.click(
|
| 171 |
+
fn=generate_images,
|
| 172 |
+
inputs=[prompts_text, negative_prompt, model_id, width, height, guidance_scale, steps, batch_per_prompt, seed],
|
| 173 |
+
outputs=[gallery, zip_file, status]
|
|
|
|
|
|
|
|
|
|
| 174 |
)
|
| 175 |
|
| 176 |
if __name__ == "__main__":
|
| 177 |
demo.launch()
|
| 178 |
+
|