Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,16 +2,16 @@ import spaces
|
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
from PIL import Image
|
| 5 |
-
from diffusers import DiffusionPipeline
|
| 6 |
import random
|
| 7 |
import uuid
|
| 8 |
-
from typing import
|
| 9 |
import numpy as np
|
| 10 |
import time
|
| 11 |
import zipfile
|
| 12 |
|
| 13 |
# Description for the app
|
| 14 |
-
DESCRIPTION = """## Qwen Image
|
| 15 |
|
| 16 |
# Helper functions
|
| 17 |
def save_image(img):
|
|
@@ -27,11 +27,10 @@ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
|
| 27 |
MAX_SEED = np.iinfo(np.int32).max
|
| 28 |
MAX_IMAGE_SIZE = 2048
|
| 29 |
|
| 30 |
-
# Load Qwen/Qwen-Image pipeline
|
| 31 |
dtype = torch.bfloat16
|
| 32 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 33 |
-
|
| 34 |
-
pipe_qwen = DiffusionPipeline.from_pretrained("Qwen/Qwen-Image", torch_dtype=dtype, vae=taef1).to(device)
|
| 35 |
|
| 36 |
# Aspect ratios
|
| 37 |
aspect_ratios = {
|
|
@@ -90,6 +89,37 @@ def generate_qwen(
|
|
| 90 |
|
| 91 |
return image_paths, seed, f"{duration:.2f}", zip_path
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
# Examples
|
| 94 |
examples = [
|
| 95 |
"An attractive young woman with blue eyes lying face down on the bed, light white and light amber, timeless beauty, sunrays shine upon it",
|
|
@@ -134,6 +164,7 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
| 134 |
use_negative_prompt = gr.Checkbox(
|
| 135 |
label="Use negative prompt",
|
| 136 |
value=False,
|
|
|
|
| 137 |
)
|
| 138 |
negative_prompt = gr.Text(
|
| 139 |
label="Negative prompt",
|
|
@@ -213,10 +244,11 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
| 213 |
# Run button and prompt submit
|
| 214 |
gr.on(
|
| 215 |
triggers=[prompt.submit, run_button.click],
|
| 216 |
-
fn=
|
| 217 |
inputs=[
|
| 218 |
prompt,
|
| 219 |
negative_prompt,
|
|
|
|
| 220 |
seed,
|
| 221 |
width,
|
| 222 |
height,
|
|
@@ -235,7 +267,7 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
| 235 |
examples=examples,
|
| 236 |
inputs=prompt,
|
| 237 |
outputs=[result, seed_display, generation_time, zip_file],
|
| 238 |
-
fn=
|
| 239 |
cache_examples=False,
|
| 240 |
)
|
| 241 |
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
from PIL import Image
|
| 5 |
+
from diffusers import DiffusionPipeline
|
| 6 |
import random
|
| 7 |
import uuid
|
| 8 |
+
from typing import Union, List, Optional
|
| 9 |
import numpy as np
|
| 10 |
import time
|
| 11 |
import zipfile
|
| 12 |
|
| 13 |
# Description for the app
|
| 14 |
+
DESCRIPTION = """## Qwen Image Generator"""
|
| 15 |
|
| 16 |
# Helper functions
|
| 17 |
def save_image(img):
|
|
|
|
| 27 |
MAX_SEED = np.iinfo(np.int32).max
|
| 28 |
MAX_IMAGE_SIZE = 2048
|
| 29 |
|
| 30 |
+
# Load Qwen/Qwen-Image pipeline
|
| 31 |
dtype = torch.bfloat16
|
| 32 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 33 |
+
pipe_qwen = DiffusionPipeline.from_pretrained("Qwen/Qwen-Image", torch_dtype=dtype).to(device)
|
|
|
|
| 34 |
|
| 35 |
# Aspect ratios
|
| 36 |
aspect_ratios = {
|
|
|
|
| 89 |
|
| 90 |
return image_paths, seed, f"{duration:.2f}", zip_path
|
| 91 |
|
| 92 |
+
# Wrapper function to handle UI logic
|
| 93 |
+
@spaces.GPU
|
| 94 |
+
def generate(
|
| 95 |
+
prompt: str,
|
| 96 |
+
negative_prompt: str,
|
| 97 |
+
use_negative_prompt: bool,
|
| 98 |
+
seed: int,
|
| 99 |
+
width: int,
|
| 100 |
+
height: int,
|
| 101 |
+
guidance_scale: float,
|
| 102 |
+
randomize_seed: bool,
|
| 103 |
+
num_inference_steps: int,
|
| 104 |
+
num_images: int,
|
| 105 |
+
zip_images: bool,
|
| 106 |
+
progress=gr.Progress(track_tqdm=True),
|
| 107 |
+
):
|
| 108 |
+
final_negative_prompt = negative_prompt if use_negative_prompt else ""
|
| 109 |
+
return generate_qwen(
|
| 110 |
+
prompt=prompt,
|
| 111 |
+
negative_prompt=final_negative_prompt,
|
| 112 |
+
seed=seed,
|
| 113 |
+
width=width,
|
| 114 |
+
height=height,
|
| 115 |
+
guidance_scale=guidance_scale,
|
| 116 |
+
randomize_seed=randomize_seed,
|
| 117 |
+
num_inference_steps=num_inference_steps,
|
| 118 |
+
num_images=num_images,
|
| 119 |
+
zip_images=zip_images,
|
| 120 |
+
progress=progress,
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
# Examples
|
| 124 |
examples = [
|
| 125 |
"An attractive young woman with blue eyes lying face down on the bed, light white and light amber, timeless beauty, sunrays shine upon it",
|
|
|
|
| 164 |
use_negative_prompt = gr.Checkbox(
|
| 165 |
label="Use negative prompt",
|
| 166 |
value=False,
|
| 167 |
+
visible=True
|
| 168 |
)
|
| 169 |
negative_prompt = gr.Text(
|
| 170 |
label="Negative prompt",
|
|
|
|
| 244 |
# Run button and prompt submit
|
| 245 |
gr.on(
|
| 246 |
triggers=[prompt.submit, run_button.click],
|
| 247 |
+
fn=generate,
|
| 248 |
inputs=[
|
| 249 |
prompt,
|
| 250 |
negative_prompt,
|
| 251 |
+
use_negative_prompt,
|
| 252 |
seed,
|
| 253 |
width,
|
| 254 |
height,
|
|
|
|
| 267 |
examples=examples,
|
| 268 |
inputs=prompt,
|
| 269 |
outputs=[result, seed_display, generation_time, zip_file],
|
| 270 |
+
fn=generate,
|
| 271 |
cache_examples=False,
|
| 272 |
)
|
| 273 |
|