Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -3,7 +3,6 @@
|
|
3 |
import os
|
4 |
import random
|
5 |
import uuid
|
6 |
-
import json
|
7 |
|
8 |
import gradio as gr
|
9 |
import numpy as np
|
@@ -13,20 +12,32 @@ import torch
|
|
13 |
from diffusers import StableDiffusion3Pipeline, DPMSolverMultistepScheduler, AutoencoderKL
|
14 |
from huggingface_hub import snapshot_download
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
29 |
|
|
|
30 |
style_list = [
|
31 |
{
|
32 |
"name": "3840 x 2160",
|
@@ -127,48 +138,10 @@ def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str
|
|
127 |
negative = ""
|
128 |
return p.replace("{prompt}", positive), n + negative
|
129 |
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
MAX_SEED = np.iinfo(np.int32).max
|
135 |
-
CACHE_EXAMPLES = torch.cuda.is_available() and os.getenv("CACHE_EXAMPLES", "0") == "1"
|
136 |
-
MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "2048"))
|
137 |
-
USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE", "0") == "1"
|
138 |
-
ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
|
139 |
-
|
140 |
-
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
141 |
-
|
142 |
-
huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
|
143 |
-
|
144 |
-
# Download the model to a local directory
|
145 |
-
model_path = snapshot_download(
|
146 |
-
repo_id="stabilityai/stable-diffusion-3-medium",
|
147 |
-
revision="refs/pr/26",
|
148 |
-
repo_type="model",
|
149 |
-
ignore_patterns=["*.md", "*.gitattributes"],
|
150 |
-
local_dir="stable-diffusion-3-medium",
|
151 |
-
token=huggingface_token,
|
152 |
-
)
|
153 |
-
|
154 |
-
if torch.cuda.is_available():
|
155 |
-
pipe = StableDiffusion3Pipeline.from_pretrained(
|
156 |
-
model_path,
|
157 |
-
torch_dtype=torch.float16,
|
158 |
-
).to(device)
|
159 |
-
|
160 |
-
if ENABLE_CPU_OFFLOAD:
|
161 |
-
pipe.enable_model_cpu_offload()
|
162 |
-
else:
|
163 |
-
pipe.to(device)
|
164 |
-
print("Loaded on Device!")
|
165 |
-
|
166 |
-
if USE_TORCH_COMPILE:
|
167 |
-
pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
|
168 |
-
print("Model Compiled!")
|
169 |
-
|
170 |
-
def save_image(img, path):
|
171 |
-
img.save(path)
|
172 |
|
173 |
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
174 |
if randomize_seed:
|
@@ -182,82 +155,77 @@ def generate(
|
|
182 |
use_negative_prompt: bool = False,
|
183 |
style: str = DEFAULT_STYLE_NAME,
|
184 |
collage_style: str = DEFAULT_COLLAGE_STYLE_NAME,
|
185 |
-
grid_size: str = "2x2",
|
186 |
seed: int = 0,
|
187 |
width: int = 1024,
|
188 |
height: int = 1024,
|
189 |
-
guidance_scale: float =
|
190 |
randomize_seed: bool = False,
|
|
|
|
|
191 |
use_resolution_binning: bool = True,
|
192 |
progress=gr.Progress(track_tqdm=True),
|
193 |
):
|
194 |
-
|
195 |
-
|
|
|
196 |
|
197 |
if collage_style != "No Style":
|
198 |
prompt, negative_prompt = apply_style(collage_style, prompt, negative_prompt)
|
199 |
else:
|
200 |
prompt, negative_prompt = apply_style(style, prompt, negative_prompt)
|
201 |
|
202 |
-
seed = int(randomize_seed_fn(seed, randomize_seed))
|
203 |
-
generator = torch.Generator().manual_seed(seed)
|
204 |
-
|
205 |
if not use_negative_prompt:
|
206 |
-
negative_prompt =
|
207 |
-
negative_prompt += default_negative
|
208 |
-
|
209 |
-
grid_sizes = {
|
210 |
-
"2x1": (2, 1),
|
211 |
-
"1x2": (1, 2),
|
212 |
-
"2x2": (2, 2),
|
213 |
-
"2x3": (2, 3),
|
214 |
-
"3x2": (3, 2),
|
215 |
-
"1x1": (1, 1)
|
216 |
-
}
|
217 |
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
"
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
}
|
232 |
-
|
233 |
-
torch.cuda.empty_cache() # Clear GPU memory
|
234 |
-
images = pipe(**options).images
|
235 |
-
|
236 |
-
grid_img = Image.new('RGB', (width * grid_size_x, height * grid_size_y))
|
237 |
-
|
238 |
-
for i, img in enumerate(images[:num_images]):
|
239 |
-
grid_img.paste(img, (i % grid_size_x * width, i // grid_size_x * height))
|
240 |
-
|
241 |
-
unique_name = str(uuid.uuid4()) + ".png"
|
242 |
-
save_image(grid_img, unique_name)
|
243 |
-
return [unique_name], seed
|
244 |
|
245 |
examples = [
|
246 |
-
"
|
247 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
248 |
]
|
249 |
|
250 |
css = '''
|
251 |
-
.gradio-container{max-width:
|
252 |
h1{text-align:center}
|
253 |
'''
|
254 |
-
with gr.Blocks(css=css
|
255 |
-
gr.
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
261 |
with gr.Group():
|
262 |
with gr.Row():
|
263 |
prompt = gr.Text(
|
@@ -267,103 +235,77 @@ with gr.Blocks(css=css, theme="xiaobaiyuan/theme_brief") as demo:
|
|
267 |
placeholder="Enter your prompt",
|
268 |
container=False,
|
269 |
)
|
270 |
-
run_button = gr.Button("Run")
|
271 |
-
result = gr.Gallery(label="
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
grid_size_selection = gr.Dropdown(
|
284 |
-
choices=["2x1", "1x2", "2x2", "2x3", "3x2", "1x1"],
|
285 |
-
value="2x2",
|
286 |
-
label="Grid Size"
|
287 |
-
)
|
288 |
-
with gr.Row(visible=True):
|
289 |
-
style_selection = gr.Radio(
|
290 |
-
show_label=True,
|
291 |
-
container=True,
|
292 |
-
interactive=True,
|
293 |
choices=STYLE_NAMES,
|
294 |
value=DEFAULT_STYLE_NAME,
|
295 |
-
label="Style",
|
296 |
)
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
label="Negative prompt",
|
302 |
-
max_lines=1,
|
303 |
-
placeholder="Enter a negative prompt",
|
304 |
-
value="(deformed, distorted, disfigured:1.3), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers:1.4), disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation",
|
305 |
-
visible=True,
|
306 |
)
|
307 |
-
with gr.Row():
|
308 |
-
num_inference_steps = gr.Slider(
|
309 |
-
label="Steps",
|
310 |
-
minimum=10,
|
311 |
-
maximum=30,
|
312 |
-
step=1,
|
313 |
-
value=15,
|
314 |
-
)
|
315 |
-
with gr.Row():
|
316 |
-
num_images_per_prompt = gr.Slider(
|
317 |
-
label="Images",
|
318 |
-
minimum=1,
|
319 |
-
maximum=5,
|
320 |
-
step=1,
|
321 |
-
value=2,
|
322 |
-
)
|
323 |
seed = gr.Slider(
|
324 |
label="Seed",
|
325 |
minimum=0,
|
326 |
maximum=MAX_SEED,
|
327 |
step=1,
|
328 |
value=0,
|
329 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
330 |
)
|
331 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
332 |
-
|
333 |
-
|
334 |
with gr.Row(visible=True):
|
335 |
width = gr.Slider(
|
336 |
label="Width",
|
337 |
-
minimum=
|
338 |
-
maximum=
|
339 |
-
step=
|
340 |
value=1024,
|
341 |
)
|
342 |
height = gr.Slider(
|
343 |
label="Height",
|
344 |
-
minimum=
|
345 |
-
maximum=
|
346 |
-
step=
|
347 |
value=1024,
|
348 |
)
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
with gr.Row():
|
353 |
guidance_scale = gr.Slider(
|
354 |
label="Guidance Scale",
|
355 |
minimum=0.1,
|
356 |
-
maximum=
|
357 |
step=0.1,
|
358 |
-
value=
|
359 |
)
|
360 |
-
|
361 |
-
|
362 |
|
363 |
gr.Examples(
|
364 |
examples=examples,
|
365 |
inputs=prompt,
|
366 |
-
outputs=[result
|
367 |
fn=generate,
|
368 |
cache_examples=CACHE_EXAMPLES,
|
369 |
)
|
@@ -388,36 +330,17 @@ with gr.Blocks(css=css, theme="xiaobaiyuan/theme_brief") as demo:
|
|
388 |
use_negative_prompt,
|
389 |
style_selection,
|
390 |
collage_style_selection,
|
391 |
-
grid_size_selection,
|
392 |
seed,
|
393 |
width,
|
394 |
height,
|
395 |
guidance_scale,
|
396 |
randomize_seed,
|
|
|
|
|
397 |
],
|
398 |
-
outputs=[result
|
399 |
api_name="run",
|
400 |
)
|
401 |
|
402 |
if __name__ == "__main__":
|
403 |
-
demo.queue(
|
404 |
-
|
405 |
-
# Updated inference function
|
406 |
-
@spaces.GPU
|
407 |
-
def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, progress=gr.Progress(track_tqdm=True)):
|
408 |
-
if randomize_seed:
|
409 |
-
seed = random.randint(0, MAX_SEED)
|
410 |
-
|
411 |
-
generator = torch.Generator().manual_seed(seed)
|
412 |
-
|
413 |
-
image = pipe(
|
414 |
-
prompt = prompt,
|
415 |
-
negative_prompt = negative_prompt,
|
416 |
-
guidance_scale = guidance_scale,
|
417 |
-
num_inference_steps = num_inference_steps,
|
418 |
-
width = width,
|
419 |
-
height = height,
|
420 |
-
generator = generator
|
421 |
-
).images[0]
|
422 |
-
|
423 |
-
return image, seed
|
|
|
3 |
import os
|
4 |
import random
|
5 |
import uuid
|
|
|
6 |
|
7 |
import gradio as gr
|
8 |
import numpy as np
|
|
|
12 |
from diffusers import StableDiffusion3Pipeline, DPMSolverMultistepScheduler, AutoencoderKL
|
13 |
from huggingface_hub import snapshot_download
|
14 |
|
15 |
+
huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
|
16 |
+
|
17 |
+
model_path = snapshot_download(
|
18 |
+
repo_id="stabilityai/stable-diffusion-3-medium",
|
19 |
+
revision="refs/pr/26",
|
20 |
+
repo_type="model",
|
21 |
+
ignore_patterns=["*.md", "*.gitattributes"],
|
22 |
+
local_dir="stable-diffusion-3-medium",
|
23 |
+
token=huggingface_token, # yeni bir token-id yazın.
|
24 |
+
)
|
25 |
+
|
26 |
+
DESCRIPTION = """# Stable Diffusion 3"""
|
27 |
+
if not torch.cuda.is_available():
|
28 |
+
DESCRIPTION += "\n<p>Running on CPU 🥶 This demo may not work on CPU.</p>"
|
29 |
|
30 |
+
MAX_SEED = np.iinfo(np.int32).max
|
31 |
+
CACHE_EXAMPLES = False
|
32 |
+
MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "1536"))
|
33 |
+
USE_TORCH_COMPILE = False
|
34 |
+
ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
|
35 |
+
|
36 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
37 |
+
|
38 |
+
pipe = StableDiffusion3Pipeline.from_pretrained(model_path, torch_dtype=torch.float16)
|
39 |
|
40 |
+
# Define styles and collage templates
|
41 |
style_list = [
|
42 |
{
|
43 |
"name": "3840 x 2160",
|
|
|
138 |
negative = ""
|
139 |
return p.replace("{prompt}", positive), n + negative
|
140 |
|
141 |
+
def save_image(img):
|
142 |
+
unique_name = str(uuid.uuid4()) + ".png"
|
143 |
+
img.save(unique_name)
|
144 |
+
return unique_name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
145 |
|
146 |
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
147 |
if randomize_seed:
|
|
|
155 |
use_negative_prompt: bool = False,
|
156 |
style: str = DEFAULT_STYLE_NAME,
|
157 |
collage_style: str = DEFAULT_COLLAGE_STYLE_NAME,
|
|
|
158 |
seed: int = 0,
|
159 |
width: int = 1024,
|
160 |
height: int = 1024,
|
161 |
+
guidance_scale: float = 7,
|
162 |
randomize_seed: bool = False,
|
163 |
+
num_inference_steps=30,
|
164 |
+
NUM_IMAGES_PER_PROMPT=1,
|
165 |
use_resolution_binning: bool = True,
|
166 |
progress=gr.Progress(track_tqdm=True),
|
167 |
):
|
168 |
+
pipe.to(device)
|
169 |
+
seed = int(randomize_seed_fn(seed, randomize_seed))
|
170 |
+
generator = torch.Generator().manual_seed(seed)
|
171 |
|
172 |
if collage_style != "No Style":
|
173 |
prompt, negative_prompt = apply_style(collage_style, prompt, negative_prompt)
|
174 |
else:
|
175 |
prompt, negative_prompt = apply_style(style, prompt, negative_prompt)
|
176 |
|
|
|
|
|
|
|
177 |
if not use_negative_prompt:
|
178 |
+
negative_prompt = None # type: ignore
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
179 |
|
180 |
+
output = pipe(
|
181 |
+
prompt=prompt,
|
182 |
+
negative_prompt=negative_prompt,
|
183 |
+
width=width,
|
184 |
+
height=height,
|
185 |
+
guidance_scale=guidance_scale,
|
186 |
+
num_inference_steps=num_inference_steps,
|
187 |
+
generator=generator,
|
188 |
+
num_images_per_prompt=NUM_IMAGES_PER_PROMPT,
|
189 |
+
output_type="pil",
|
190 |
+
).images
|
191 |
+
|
192 |
+
return output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
193 |
|
194 |
examples = [
|
195 |
+
"A red sofa on top of a white building.",
|
196 |
+
"A cardboard which is large and sits on a theater stage.",
|
197 |
+
"A painting of an astronaut riding a pig wearing a tutu holding a pink umbrella.",
|
198 |
+
"Studio photograph closeup of a chameleon over a black background.",
|
199 |
+
"Closeup portrait photo of beautiful goth woman, makeup.",
|
200 |
+
"A living room, bright modern Scandinavian style house, large windows.",
|
201 |
+
"Portrait photograph of an anthropomorphic tortoise seated on a New York City subway train.",
|
202 |
+
"Batman, cute modern Disney style, Pixar 3d portrait, ultra detailed, gorgeous, 3d zbrush, trending on dribbble, 8k render.",
|
203 |
+
"Cinnamon bun on the plate, watercolor painting, detailed, brush strokes, light palette, light, cozy.",
|
204 |
+
"A lion, colorful, low-poly, cyan and orange eyes, poly-hd, 3d, low-poly game art, polygon mesh, jagged, blocky, wireframe edges, centered composition.",
|
205 |
+
"Long exposure photo of Tokyo street, blurred motion, streaks of light, surreal, dreamy, ghosting effect, highly detailed.",
|
206 |
+
"A glamorous digital magazine photoshoot, a fashionable model wearing avant-garde clothing, set in a futuristic cyberpunk roof-top environment, with a neon-lit city background, intricate high fashion details, backlit by vibrant city glow, Vogue fashion photography.",
|
207 |
+
"Masterpiece, best quality, girl, collarbone, wavy hair, looking at viewer, blurry foreground, upper body, necklace, contemporary, plain pants, intricate, print, pattern, ponytail, freckles, red hair, dappled sunlight, smile, happy."
|
208 |
]
|
209 |
|
210 |
css = '''
|
211 |
+
.gradio-container{max-width: 1000px !important}
|
212 |
h1{text-align:center}
|
213 |
'''
|
214 |
+
with gr.Blocks(css=css) as demo:
|
215 |
+
with gr.Row():
|
216 |
+
with gr.Column():
|
217 |
+
gr.HTML(
|
218 |
+
"""
|
219 |
+
<h1 style='text-align: center'>
|
220 |
+
Stable Diffusion 3 Medium
|
221 |
+
</h1>
|
222 |
+
"""
|
223 |
+
)
|
224 |
+
gr.HTML(
|
225 |
+
"""
|
226 |
+
|
227 |
+
"""
|
228 |
+
)
|
229 |
with gr.Group():
|
230 |
with gr.Row():
|
231 |
prompt = gr.Text(
|
|
|
235 |
placeholder="Enter your prompt",
|
236 |
container=False,
|
237 |
)
|
238 |
+
run_button = gr.Button("Run", scale=0)
|
239 |
+
result = gr.Gallery(label="Result", elem_id="gallery", show_label=False)
|
240 |
+
with gr.Accordion("Advanced options", open=False):
|
241 |
+
with gr.Row():
|
242 |
+
use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True)
|
243 |
+
negative_prompt = gr.Text(
|
244 |
+
label="Negative prompt",
|
245 |
+
max_lines=1,
|
246 |
+
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",
|
247 |
+
visible=True,
|
248 |
+
)
|
249 |
+
style_selection = gr.Dropdown(
|
250 |
+
label="Style",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
251 |
choices=STYLE_NAMES,
|
252 |
value=DEFAULT_STYLE_NAME,
|
|
|
253 |
)
|
254 |
+
collage_style_selection = gr.Dropdown(
|
255 |
+
label="Collage Template",
|
256 |
+
choices=COLLAGE_STYLE_NAMES,
|
257 |
+
value=DEFAULT_COLLAGE_STYLE_NAME,
|
|
|
|
|
|
|
|
|
|
|
258 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
259 |
seed = gr.Slider(
|
260 |
label="Seed",
|
261 |
minimum=0,
|
262 |
maximum=MAX_SEED,
|
263 |
step=1,
|
264 |
value=0,
|
265 |
+
)
|
266 |
+
steps = gr.Slider(
|
267 |
+
label="Steps",
|
268 |
+
minimum=0,
|
269 |
+
maximum=60,
|
270 |
+
step=1,
|
271 |
+
value=30,
|
272 |
+
)
|
273 |
+
number_image = gr.Slider(
|
274 |
+
label="Number of Image",
|
275 |
+
minimum=1,
|
276 |
+
maximum=4,
|
277 |
+
step=1,
|
278 |
+
value=2,
|
279 |
)
|
280 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
|
|
|
|
281 |
with gr.Row(visible=True):
|
282 |
width = gr.Slider(
|
283 |
label="Width",
|
284 |
+
minimum=256,
|
285 |
+
maximum=MAX_IMAGE_SIZE,
|
286 |
+
step=32,
|
287 |
value=1024,
|
288 |
)
|
289 |
height = gr.Slider(
|
290 |
label="Height",
|
291 |
+
minimum=256,
|
292 |
+
maximum=MAX_IMAGE_SIZE,
|
293 |
+
step=32,
|
294 |
value=1024,
|
295 |
)
|
|
|
|
|
|
|
296 |
with gr.Row():
|
297 |
guidance_scale = gr.Slider(
|
298 |
label="Guidance Scale",
|
299 |
minimum=0.1,
|
300 |
+
maximum=10,
|
301 |
step=0.1,
|
302 |
+
value=7.0,
|
303 |
)
|
|
|
|
|
304 |
|
305 |
gr.Examples(
|
306 |
examples=examples,
|
307 |
inputs=prompt,
|
308 |
+
outputs=[result],
|
309 |
fn=generate,
|
310 |
cache_examples=CACHE_EXAMPLES,
|
311 |
)
|
|
|
330 |
use_negative_prompt,
|
331 |
style_selection,
|
332 |
collage_style_selection,
|
|
|
333 |
seed,
|
334 |
width,
|
335 |
height,
|
336 |
guidance_scale,
|
337 |
randomize_seed,
|
338 |
+
steps,
|
339 |
+
number_image,
|
340 |
],
|
341 |
+
outputs=[result],
|
342 |
api_name="run",
|
343 |
)
|
344 |
|
345 |
if __name__ == "__main__":
|
346 |
+
demo.queue().launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|