Spaces:
Running
Running
Quick demo / hack
Browse files- .gitattributes +1 -1
- .gitignore +2 -0
- README.md +2 -2
- app.py +221 -120
- requirements.txt +6 -6
.gitattributes
CHANGED
@@ -32,4 +32,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
32 |
*.xz filter=lfs diff=lfs merge=lfs -text
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
-
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
32 |
*.xz filter=lfs diff=lfs merge=lfs -text
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
.venv
|
2 |
+
__pycache__
|
README.md
CHANGED
@@ -1,10 +1,10 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
emoji: 🖼
|
4 |
colorFrom: purple
|
5 |
colorTo: red
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 4.
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
---
|
|
|
1 |
---
|
2 |
+
title: ⚡ Stable Diffusion 3 ⚡
|
3 |
emoji: 🖼
|
4 |
colorFrom: purple
|
5 |
colorTo: red
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 4.36.1
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
---
|
app.py
CHANGED
@@ -1,146 +1,247 @@
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
-
import
|
4 |
-
|
5 |
import torch
|
|
|
|
|
|
|
6 |
|
7 |
-
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
pipe.enable_xformers_memory_efficient_attention()
|
13 |
-
pipe = pipe.to(device)
|
14 |
-
else:
|
15 |
-
pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", use_safetensors=True)
|
16 |
-
pipe = pipe.to(device)
|
17 |
|
18 |
MAX_SEED = np.iinfo(np.int32).max
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
|
|
|
|
|
|
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
if randomize_seed:
|
24 |
seed = random.randint(0, MAX_SEED)
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
generator = torch.Generator().manual_seed(seed)
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
negative_prompt = negative_prompt,
|
31 |
-
guidance_scale = guidance_scale,
|
32 |
-
num_inference_steps = num_inference_steps,
|
33 |
-
width = width,
|
34 |
-
height = height,
|
35 |
-
generator = generator
|
36 |
-
).images[0]
|
37 |
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
-
|
41 |
-
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
42 |
-
"An astronaut riding a green horse",
|
43 |
-
"A delicious ceviche cheesecake slice",
|
44 |
-
]
|
45 |
|
46 |
-
css="""
|
47 |
-
#col-container {
|
48 |
-
margin: 0 auto;
|
49 |
-
max-width: 520px;
|
50 |
-
}
|
51 |
-
"""
|
52 |
|
53 |
-
if torch.cuda.is_available():
|
54 |
-
power_device = "GPU"
|
55 |
-
else:
|
56 |
-
power_device = "CPU"
|
57 |
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
prompt = gr.Text(
|
69 |
-
label="Prompt",
|
70 |
-
show_label=False,
|
71 |
-
max_lines=1,
|
72 |
-
placeholder="Enter your prompt",
|
73 |
-
container=False,
|
74 |
-
)
|
75 |
-
|
76 |
-
run_button = gr.Button("Run", scale=0)
|
77 |
-
|
78 |
-
result = gr.Image(label="Result", show_label=False)
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
step=1,
|
94 |
-
value=0,
|
95 |
)
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
)
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
value=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
)
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
step=0.1,
|
124 |
-
value=0.0,
|
125 |
)
|
126 |
-
|
127 |
-
|
128 |
-
label="Number of inference steps",
|
129 |
minimum=1,
|
130 |
-
maximum=
|
131 |
step=1,
|
132 |
-
value=
|
133 |
)
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
145 |
|
146 |
-
|
|
|
|
1 |
+
import os
|
2 |
+
import random
|
3 |
+
import uuid
|
4 |
+
|
5 |
import gradio as gr
|
6 |
import numpy as np
|
7 |
+
from PIL import Image
|
8 |
+
import spaces
|
9 |
import torch
|
10 |
+
from diffusers import StableDiffusion3Pipeline, DPMSolverMultistepScheduler, AutoencoderKL, StableDiffusion3Img2ImgPipeline
|
11 |
+
from transformers import T5EncoderModel, BitsAndBytesConfig
|
12 |
+
from huggingface_hub import snapshot_download
|
13 |
|
14 |
+
huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
|
15 |
|
16 |
+
DESCRIPTION = """# Stable Diffusion 3"""
|
17 |
+
if not torch.cuda.is_available():
|
18 |
+
DESCRIPTION += "\n<p>Running on CPU 🥶 This demo may not work on CPU.</p>"
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
MAX_SEED = np.iinfo(np.int32).max
|
21 |
+
CACHE_EXAMPLES = False
|
22 |
+
MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "1536"))
|
23 |
+
USE_TORCH_COMPILE = False
|
24 |
+
ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
|
25 |
+
|
26 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
27 |
+
|
28 |
+
def load_pipeline():
|
29 |
+
model_id = "stabilityai/stable-diffusion-3-medium-diffusers"
|
30 |
+
|
31 |
+
pipe = StableDiffusion3Pipeline.from_pretrained(
|
32 |
+
model_id,
|
33 |
+
#device_map="balanced",
|
34 |
+
torch_dtype=torch.float16
|
35 |
+
)
|
36 |
+
return pipe
|
37 |
+
|
38 |
+
|
39 |
+
aspect_ratios = {
|
40 |
+
"21:9": (21, 9),
|
41 |
+
"2:1": (2, 1),
|
42 |
+
"16:9": (16, 9),
|
43 |
+
"5:4": (5, 4),
|
44 |
+
"4:3": (4, 3),
|
45 |
+
"3:2": (3, 2),
|
46 |
+
"1:1": (1, 1),
|
47 |
+
}
|
48 |
+
# Function to calculate resolution
|
49 |
+
def calculate_resolution(aspect_ratio, mode='landscape', total_pixels=1024*1024, divisibility=64):
|
50 |
+
if aspect_ratio not in aspect_ratios:
|
51 |
+
raise ValueError(f"Invalid aspect ratio: {aspect_ratio}")
|
52 |
+
|
53 |
+
width_multiplier, height_multiplier = aspect_ratios[aspect_ratio]
|
54 |
+
ratio = width_multiplier / height_multiplier
|
55 |
+
if mode == 'portrait':
|
56 |
+
# Swap the ratio for portrait mode
|
57 |
+
ratio = 1 / ratio
|
58 |
+
|
59 |
+
height = int((total_pixels / ratio) ** 0.5)
|
60 |
+
height -= height % divisibility
|
61 |
+
|
62 |
+
width = int(height * ratio)
|
63 |
+
width -= width % divisibility
|
64 |
|
65 |
+
while width * height > total_pixels:
|
66 |
+
height -= divisibility
|
67 |
+
width = int(height * ratio)
|
68 |
+
width -= width % divisibility
|
69 |
|
70 |
+
return width, height
|
71 |
+
|
72 |
+
|
73 |
+
def save_image(img):
|
74 |
+
unique_name = str(uuid.uuid4()) + ".png"
|
75 |
+
img.save(unique_name)
|
76 |
+
return unique_name
|
77 |
+
|
78 |
+
|
79 |
+
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
80 |
if randomize_seed:
|
81 |
seed = random.randint(0, MAX_SEED)
|
82 |
+
return seed
|
83 |
+
|
84 |
+
|
85 |
+
@spaces.GPU
|
86 |
+
def generate(
|
87 |
+
prompt:str,
|
88 |
+
negative_prompt: str = "",
|
89 |
+
use_negative_prompt: bool = False,
|
90 |
+
seed: int = 0,
|
91 |
+
aspect: str = "1:1",
|
92 |
+
mode: str = "landscape",
|
93 |
+
guidance_scale: float = 7.5,
|
94 |
+
randomize_seed: bool = False,
|
95 |
+
num_inference_steps=30,
|
96 |
+
NUM_IMAGES_PER_PROMPT=1,
|
97 |
+
use_resolution_binning: bool = True,
|
98 |
+
progress=gr.Progress(track_tqdm=True),
|
99 |
+
):
|
100 |
+
pipe = load_pipeline()
|
101 |
+
pipe.to(device)
|
102 |
+
seed = int(randomize_seed_fn(seed, randomize_seed))
|
103 |
generator = torch.Generator().manual_seed(seed)
|
104 |
|
105 |
+
if not use_negative_prompt:
|
106 |
+
negative_prompt = None # type: ignore
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
|
108 |
+
width, height = calculate_resolution(aspect, mode)
|
109 |
+
|
110 |
+
output = pipe(
|
111 |
+
prompt=prompt,
|
112 |
+
negative_prompt=negative_prompt,
|
113 |
+
width=width,
|
114 |
+
height=height,
|
115 |
+
guidance_scale=guidance_scale,
|
116 |
+
num_inference_steps=num_inference_steps,
|
117 |
+
generator=generator,
|
118 |
+
num_images_per_prompt=NUM_IMAGES_PER_PROMPT,
|
119 |
+
output_type="pil",
|
120 |
+
).images
|
121 |
|
122 |
+
return output
|
|
|
|
|
|
|
|
|
123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
|
|
|
|
|
|
|
|
|
125 |
|
126 |
+
examples = [
|
127 |
+
"Beautiful pixel art of a wizard with hovering text \"Achievement unlocked: Diffusion models can spell now\"",
|
128 |
+
"Frog sitting in a 1950s diner wearing a leather jacket and a top hat. on the table a giant burger and a small sign that says \"froggy fridays\"",
|
129 |
+
"This dreamlike digital art capture a vibrant kaleidoscopic bird in a rainforest",
|
130 |
+
"pair of shoes made of dried fruit skins, 3d render, bright colours, clean composition, beautiful artwork, logo saying \"SD3 rocks!\"",
|
131 |
+
"post-apocalyptic city wasteland, the most delicate beautiful flower with green leaves growing from dust and rubble, vibrant colours, cinematic",
|
132 |
+
"a dark-armored warrior with ornate golden details, cloaked in a flowing black cape, wielding a radiant, fiery sword, standing amidst an ominous cloudy backdrop with dramatic lighting, exuding a menacing, powerful presence.",
|
133 |
+
"A wise old wizard with a long white beard, flowing robes, and a gnarled staff, casting a spell, photorealistic style",
|
134 |
+
"Design a film poster for a noir thriller set in 1940s Los Angeles, featuring a shadowy figure under a streetlamp and a foggy, mysterious ambiance.",
|
135 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
136 |
|
137 |
+
css = '''
|
138 |
+
.gradio-container{max-width: 1000px !important}
|
139 |
+
h1{text-align:center}
|
140 |
+
'''
|
141 |
+
with gr.Blocks(css=css) as demo:
|
142 |
+
with gr.Row():
|
143 |
+
with gr.Column():
|
144 |
+
gr.HTML(
|
145 |
+
"""
|
146 |
+
<h1 style='text-align: center'>
|
147 |
+
Stable Diffusion 3
|
148 |
+
</h1>
|
149 |
+
"""
|
|
|
|
|
150 |
)
|
151 |
+
|
152 |
+
with gr.Group():
|
153 |
+
with gr.Row():
|
154 |
+
prompt = gr.Text(
|
155 |
+
label="Prompt",
|
156 |
+
show_label=False,
|
157 |
+
max_lines=1,
|
158 |
+
placeholder="Enter your prompt",
|
159 |
+
container=False,
|
160 |
+
)
|
161 |
+
run_button = gr.Button("Run", scale=0)
|
162 |
+
with gr.Row():
|
163 |
+
aspect = gr.Dropdown(label='Aspect Ratio', choices=list(aspect_ratios.keys()), value='1:1', interactive=True)
|
164 |
+
mode = gr.Dropdown(label='Mode', choices=['landscape', 'portrait'], value='landscape')
|
165 |
+
|
166 |
+
result = gr.Gallery(label="Result", elem_id="gallery", show_label=False)
|
167 |
+
with gr.Accordion("Advanced options", open=False):
|
168 |
+
with gr.Row():
|
169 |
+
use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True)
|
170 |
+
negative_prompt = gr.Text(
|
171 |
+
label="Negative prompt",
|
172 |
+
max_lines=1,
|
173 |
+
value = "deformed, distorted, disfigured, poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, mutated hands and fingers, disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation, NSFW",
|
174 |
+
visible=True,
|
175 |
+
)
|
176 |
+
seed = gr.Slider(
|
177 |
+
label="Seed",
|
178 |
+
minimum=0,
|
179 |
+
maximum=MAX_SEED,
|
180 |
+
step=1,
|
181 |
+
value=0,
|
182 |
)
|
183 |
+
|
184 |
+
steps = gr.Slider(
|
185 |
+
label="Steps",
|
186 |
+
minimum=0,
|
187 |
+
maximum=60,
|
188 |
+
step=1,
|
189 |
+
value=30,
|
|
|
|
|
190 |
)
|
191 |
+
number_image = gr.Slider(
|
192 |
+
label="Number of Images",
|
|
|
193 |
minimum=1,
|
194 |
+
maximum=2,
|
195 |
step=1,
|
196 |
+
value=1,
|
197 |
)
|
198 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
199 |
+
with gr.Row():
|
200 |
+
guidance_scale = gr.Slider(
|
201 |
+
label="Guidance Scale",
|
202 |
+
minimum=0.1,
|
203 |
+
maximum=10,
|
204 |
+
step=0.1,
|
205 |
+
value=7.0,
|
206 |
+
)
|
207 |
+
|
208 |
+
gr.Examples(
|
209 |
+
examples=examples,
|
210 |
+
inputs=prompt,
|
211 |
+
outputs=[result],
|
212 |
+
fn=generate,
|
213 |
+
cache_examples=CACHE_EXAMPLES,
|
214 |
+
)
|
215 |
+
|
216 |
+
use_negative_prompt.change(
|
217 |
+
fn=lambda x: gr.update(visible=x),
|
218 |
+
inputs=use_negative_prompt,
|
219 |
+
outputs=negative_prompt,
|
220 |
+
api_name=False,
|
221 |
+
)
|
222 |
+
|
223 |
+
gr.on(
|
224 |
+
triggers=[
|
225 |
+
prompt.submit,
|
226 |
+
negative_prompt.submit,
|
227 |
+
run_button.click,
|
228 |
+
],
|
229 |
+
fn=generate,
|
230 |
+
inputs=[
|
231 |
+
prompt,
|
232 |
+
negative_prompt,
|
233 |
+
use_negative_prompt,
|
234 |
+
seed,
|
235 |
+
aspect,
|
236 |
+
mode,
|
237 |
+
guidance_scale,
|
238 |
+
randomize_seed,
|
239 |
+
steps,
|
240 |
+
number_image,
|
241 |
+
],
|
242 |
+
outputs=[result],
|
243 |
+
api_name="run",
|
244 |
+
)
|
245 |
|
246 |
+
if __name__ == "__main__":
|
247 |
+
demo.queue().launch()
|
requirements.txt
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
|
|
1 |
+
git+https://github.com/huggingface/diffusers
|
2 |
+
git+https://github.com/huggingface/transformers
|
3 |
+
sentencepiece
|
4 |
+
peft
|
5 |
+
protobuf
|
6 |
+
bitsandbytes
|