Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -2,29 +2,142 @@ import gradio as gr
|
|
2 |
import numpy as np
|
3 |
import random
|
4 |
from diffusers import DiffusionPipeline
|
5 |
-
from diffusers import StableDiffusionXLPipeline, DPMSolverSinglestepScheduler
|
6 |
import torch
|
7 |
import spaces
|
8 |
|
9 |
-
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
16 |
|
17 |
-
|
18 |
-
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
inputs=[prompt, negative_prompt],
|
23 |
-
outputs="image",
|
24 |
-
title="Real-time Image Generation with Diffusion",
|
25 |
-
description="Enter a prompt to generate an image",
|
26 |
-
theme="soft"
|
27 |
-
)
|
28 |
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import numpy as np
|
3 |
import random
|
4 |
from diffusers import DiffusionPipeline
|
|
|
5 |
import torch
|
6 |
import spaces
|
7 |
|
8 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
9 |
|
10 |
+
if torch.cuda.is_available():
|
11 |
+
torch.cuda.max_memory_allocated(device=device)
|
12 |
+
pipe = DiffusionPipeline.from_pretrained("sd-community/sdxl-flash", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
|
13 |
+
pipe.enable_xformers_memory_efficient_attention()
|
14 |
+
pipe = pipe.to(device)
|
15 |
+
else:
|
16 |
+
pipe = DiffusionPipeline.from_pretrained("sd-community/sdxl-flash", use_safetensors=True)
|
17 |
+
pipe = pipe.to(device)
|
18 |
|
19 |
+
MAX_SEED = np.iinfo(np.int32).max
|
20 |
+
MAX_IMAGE_SIZE = 1024
|
21 |
|
22 |
+
@spaces.GPU(duration=20,queue=False)
|
23 |
+
def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
+
if randomize_seed:
|
26 |
+
seed = random.randint(0, MAX_SEED)
|
27 |
+
|
28 |
+
generator = torch.Generator().manual_seed(seed)
|
29 |
+
|
30 |
+
image = pipe(
|
31 |
+
prompt = prompt,
|
32 |
+
negative_prompt = negative_prompt,
|
33 |
+
guidance_scale = guidance_scale,
|
34 |
+
num_inference_steps = num_inference_steps,
|
35 |
+
width = width,
|
36 |
+
height = height,
|
37 |
+
generator = generator
|
38 |
+
).images[0]
|
39 |
+
|
40 |
+
return image
|
41 |
+
|
42 |
+
examples = [
|
43 |
+
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
44 |
+
"An astronaut riding a green horse",
|
45 |
+
"A delicious ceviche cheesecake slice",
|
46 |
+
]
|
47 |
+
|
48 |
+
css="""
|
49 |
+
#col-container {
|
50 |
+
margin: 0 auto;
|
51 |
+
max-width: 520px;
|
52 |
+
}
|
53 |
+
"""
|
54 |
+
|
55 |
+
with gr.Blocks(css=css) as demo:
|
56 |
+
|
57 |
+
with gr.Column(elem_id="col-container"):
|
58 |
+
gr.Markdown(f"""
|
59 |
+
# Text-to-Image Gradio Template
|
60 |
+
Currently running on {power_device}.
|
61 |
+
""")
|
62 |
+
|
63 |
+
with gr.Row():
|
64 |
+
|
65 |
+
prompt = gr.Text(
|
66 |
+
label="Prompt",
|
67 |
+
show_label=False,
|
68 |
+
max_lines=1,
|
69 |
+
placeholder="Enter your prompt",
|
70 |
+
container=False,
|
71 |
+
)
|
72 |
+
|
73 |
+
run_button = gr.Button("Run", scale=0)
|
74 |
+
|
75 |
+
result = gr.Image(label="Result", show_label=False)
|
76 |
+
|
77 |
+
with gr.Accordion("Advanced Settings", open=False):
|
78 |
+
|
79 |
+
negative_prompt = gr.Text(
|
80 |
+
label="Negative prompt",
|
81 |
+
max_lines=1,
|
82 |
+
placeholder="Enter a negative prompt",
|
83 |
+
value = "Ugly, malformed, noise, blur, watermark",
|
84 |
+
)
|
85 |
+
|
86 |
+
seed = gr.Slider(
|
87 |
+
label="Seed",
|
88 |
+
minimum=0,
|
89 |
+
maximum=MAX_SEED,
|
90 |
+
step=1,
|
91 |
+
value=0,
|
92 |
+
)
|
93 |
+
|
94 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
95 |
+
|
96 |
+
with gr.Row():
|
97 |
+
|
98 |
+
width = gr.Slider(
|
99 |
+
label="Width",
|
100 |
+
minimum=256,
|
101 |
+
maximum=MAX_IMAGE_SIZE,
|
102 |
+
step=32,
|
103 |
+
value=512,
|
104 |
+
)
|
105 |
+
|
106 |
+
height = gr.Slider(
|
107 |
+
label="Height",
|
108 |
+
minimum=256,
|
109 |
+
maximum=MAX_IMAGE_SIZE,
|
110 |
+
step=32,
|
111 |
+
value=512,
|
112 |
+
)
|
113 |
+
|
114 |
+
with gr.Row():
|
115 |
+
|
116 |
+
guidance_scale = gr.Slider(
|
117 |
+
label="Guidance scale",
|
118 |
+
minimum=0.0,
|
119 |
+
maximum=10.0,
|
120 |
+
step=0.1,
|
121 |
+
value=3.0,
|
122 |
+
)
|
123 |
+
|
124 |
+
num_inference_steps = gr.Slider(
|
125 |
+
label="Number of inference steps",
|
126 |
+
minimum=1,
|
127 |
+
maximum=12,
|
128 |
+
step=1,
|
129 |
+
value=5,
|
130 |
+
)
|
131 |
+
|
132 |
+
gr.Examples(
|
133 |
+
examples = examples,
|
134 |
+
inputs = [prompt]
|
135 |
+
)
|
136 |
+
|
137 |
+
run_button.click(
|
138 |
+
fn = infer,
|
139 |
+
inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
140 |
+
outputs = [result]
|
141 |
+
)
|
142 |
+
|
143 |
+
demo.queue().launch()
|