Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,68 +1,21 @@
|
|
1 |
-
#!/usr/bin/env python
|
2 |
-
|
3 |
-
from __future__ import annotations
|
4 |
-
|
5 |
import os
|
6 |
import random
|
7 |
|
8 |
import gradio as gr
|
9 |
-
import numpy as np
|
10 |
import PIL.Image
|
11 |
-
import spaces
|
12 |
-
import torch
|
13 |
-
from diffusers import AutoencoderKL, DiffusionPipeline
|
14 |
|
15 |
DESCRIPTION = "# SDXL"
|
16 |
-
if not torch.cuda.is_available():
|
17 |
-
DESCRIPTION += "\n<p>Running on CPU 🥶 This demo does not work on CPU.</p>"
|
18 |
|
19 |
MAX_SEED = np.iinfo(np.int32).max
|
20 |
MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "1024"))
|
21 |
-
USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE") == "1"
|
22 |
-
ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD") == "1"
|
23 |
ENABLE_REFINER = os.getenv("ENABLE_REFINER", "1") == "1"
|
24 |
|
25 |
-
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
26 |
-
if torch.cuda.is_available():
|
27 |
-
vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
|
28 |
-
pipe = DiffusionPipeline.from_pretrained(
|
29 |
-
"stabilityai/stable-diffusion-xl-base-1.0",
|
30 |
-
vae=vae,
|
31 |
-
torch_dtype=torch.float16,
|
32 |
-
use_safetensors=True,
|
33 |
-
variant="fp16",
|
34 |
-
)
|
35 |
-
if ENABLE_REFINER:
|
36 |
-
refiner = DiffusionPipeline.from_pretrained(
|
37 |
-
"stabilityai/stable-diffusion-xl-refiner-1.0",
|
38 |
-
vae=vae,
|
39 |
-
torch_dtype=torch.float16,
|
40 |
-
use_safetensors=True,
|
41 |
-
variant="fp16",
|
42 |
-
)
|
43 |
-
|
44 |
-
if ENABLE_CPU_OFFLOAD:
|
45 |
-
pipe.enable_model_cpu_offload()
|
46 |
-
if ENABLE_REFINER:
|
47 |
-
refiner.enable_model_cpu_offload()
|
48 |
-
else:
|
49 |
-
pipe.to(device)
|
50 |
-
if ENABLE_REFINER:
|
51 |
-
refiner.to(device)
|
52 |
-
|
53 |
-
if USE_TORCH_COMPILE:
|
54 |
-
pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
|
55 |
-
if ENABLE_REFINER:
|
56 |
-
refiner.unet = torch.compile(refiner.unet, mode="reduce-overhead", fullgraph=True)
|
57 |
-
|
58 |
|
59 |
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
60 |
if randomize_seed:
|
61 |
seed = random.randint(0, MAX_SEED)
|
62 |
return seed
|
63 |
|
64 |
-
|
65 |
-
@spaces.GPU
|
66 |
def generate(
|
67 |
prompt: str,
|
68 |
negative_prompt: str = "",
|
@@ -79,53 +32,9 @@ def generate(
|
|
79 |
num_inference_steps_base: int = 25,
|
80 |
num_inference_steps_refiner: int = 25,
|
81 |
apply_refiner: bool = False,
|
82 |
-
)
|
83 |
-
|
84 |
-
|
85 |
-
if not use_negative_prompt:
|
86 |
-
negative_prompt = None # type: ignore
|
87 |
-
if not use_prompt_2:
|
88 |
-
prompt_2 = None # type: ignore
|
89 |
-
if not use_negative_prompt_2:
|
90 |
-
negative_prompt_2 = None # type: ignore
|
91 |
-
|
92 |
-
if not apply_refiner:
|
93 |
-
return pipe(
|
94 |
-
prompt=prompt,
|
95 |
-
negative_prompt=negative_prompt,
|
96 |
-
prompt_2=prompt_2,
|
97 |
-
negative_prompt_2=negative_prompt_2,
|
98 |
-
width=width,
|
99 |
-
height=height,
|
100 |
-
guidance_scale=guidance_scale_base,
|
101 |
-
num_inference_steps=num_inference_steps_base,
|
102 |
-
generator=generator,
|
103 |
-
output_type="pil",
|
104 |
-
).images[0]
|
105 |
-
else:
|
106 |
-
latents = pipe(
|
107 |
-
prompt=prompt,
|
108 |
-
negative_prompt=negative_prompt,
|
109 |
-
prompt_2=prompt_2,
|
110 |
-
negative_prompt_2=negative_prompt_2,
|
111 |
-
width=width,
|
112 |
-
height=height,
|
113 |
-
guidance_scale=guidance_scale_base,
|
114 |
-
num_inference_steps=num_inference_steps_base,
|
115 |
-
generator=generator,
|
116 |
-
output_type="latent",
|
117 |
-
).images
|
118 |
-
image = refiner(
|
119 |
-
prompt=prompt,
|
120 |
-
negative_prompt=negative_prompt,
|
121 |
-
prompt_2=prompt_2,
|
122 |
-
negative_prompt_2=negative_prompt_2,
|
123 |
-
guidance_scale=guidance_scale_refiner,
|
124 |
-
num_inference_steps=num_inference_steps_refiner,
|
125 |
-
image=latents,
|
126 |
-
generator=generator,
|
127 |
-
).images[0]
|
128 |
-
return image
|
129 |
|
130 |
|
131 |
examples = [
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
import random
|
3 |
|
4 |
import gradio as gr
|
|
|
5 |
import PIL.Image
|
|
|
|
|
|
|
6 |
|
7 |
DESCRIPTION = "# SDXL"
|
|
|
|
|
8 |
|
9 |
MAX_SEED = np.iinfo(np.int32).max
|
10 |
MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "1024"))
|
|
|
|
|
11 |
ENABLE_REFINER = os.getenv("ENABLE_REFINER", "1") == "1"
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
15 |
if randomize_seed:
|
16 |
seed = random.randint(0, MAX_SEED)
|
17 |
return seed
|
18 |
|
|
|
|
|
19 |
def generate(
|
20 |
prompt: str,
|
21 |
negative_prompt: str = "",
|
|
|
32 |
num_inference_steps_base: int = 25,
|
33 |
num_inference_steps_refiner: int = 25,
|
34 |
apply_refiner: bool = False,
|
35 |
+
)
|
36 |
+
print("hello")
|
37 |
+
#return image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
|
40 |
examples = [
|