Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -7,7 +7,7 @@ import numpy as np
|
|
7 |
from PIL import Image
|
8 |
import spaces
|
9 |
import torch
|
10 |
-
from diffusers import StableDiffusion3Pipeline, DPMSolverMultistepScheduler, AutoencoderKL
|
11 |
from huggingface_hub import snapshot_download
|
12 |
|
13 |
huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
|
@@ -18,7 +18,7 @@ model_path = snapshot_download(
|
|
18 |
repo_type="model",
|
19 |
ignore_patterns=["*.md", "*..gitattributes"],
|
20 |
local_dir="stable-diffusion-3-medium",
|
21 |
-
token=huggingface_token, #
|
22 |
)
|
23 |
|
24 |
DESCRIPTION = """# Stable Diffusion 3"""
|
@@ -34,7 +34,7 @@ ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
|
|
34 |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
35 |
|
36 |
pipe = StableDiffusion3Pipeline.from_pretrained(model_path, torch_dtype=torch.float16)
|
37 |
-
|
38 |
|
39 |
def save_image(img):
|
40 |
unique_name = str(uuid.uuid4()) + ".png"
|
@@ -50,7 +50,7 @@ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
|
50 |
|
51 |
@spaces.GPU
|
52 |
def generate(
|
53 |
-
prompt:
|
54 |
negative_prompt: str = "",
|
55 |
use_negative_prompt: bool = False,
|
56 |
seed: int = 0,
|
@@ -67,10 +67,8 @@ def generate(
|
|
67 |
seed = int(randomize_seed_fn(seed, randomize_seed))
|
68 |
generator = torch.Generator().manual_seed(seed)
|
69 |
|
70 |
-
#pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
|
71 |
-
|
72 |
if not use_negative_prompt:
|
73 |
-
negative_prompt = None
|
74 |
|
75 |
output = pipe(
|
76 |
prompt=prompt,
|
@@ -81,7 +79,46 @@ def generate(
|
|
81 |
num_inference_steps=num_inference_steps,
|
82 |
generator=generator,
|
83 |
num_images_per_prompt=NUM_IMAGES_PER_PROMPT,
|
84 |
-
output_type="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
).images
|
86 |
|
87 |
return output
|
@@ -109,7 +146,7 @@ with gr.Blocks(css=css) as demo:
|
|
109 |
Stable Diffusion 3
|
110 |
</h1>
|
111 |
"""
|
112 |
-
|
113 |
gr.HTML(
|
114 |
"""
|
115 |
<h3 style='text-align: center'>
|
@@ -117,7 +154,7 @@ with gr.Blocks(css=css) as demo:
|
|
117 |
<a href='https://twitter.com/kadirnar_ai' target='_blank'>Twitter</a> | <a href='https://github.com/kadirnar' target='_blank'>Github</a> | <a href='https://www.linkedin.com/in/kadir-nar/' target='_blank'>Linkedin</a>
|
118 |
</h3>
|
119 |
"""
|
120 |
-
|
121 |
with gr.Group():
|
122 |
with gr.Row():
|
123 |
prompt = gr.Text(
|
@@ -140,7 +177,7 @@ with gr.Blocks(css=css) as demo:
|
|
140 |
)
|
141 |
seed = gr.Slider(
|
142 |
label="Seed",
|
143 |
-
|
144 |
maximum=MAX_SEED,
|
145 |
step=1,
|
146 |
value=0,
|
@@ -148,14 +185,14 @@ with gr.Blocks(css=css) as demo:
|
|
148 |
|
149 |
steps = gr.Slider(
|
150 |
label="Steps",
|
151 |
-
|
152 |
maximum=60,
|
153 |
step=1,
|
154 |
value=25,
|
155 |
)
|
156 |
number_image = gr.Slider(
|
157 |
-
label="Number of
|
158 |
-
|
159 |
maximum=4,
|
160 |
step=1,
|
161 |
value=1,
|
@@ -164,14 +201,14 @@ with gr.Blocks(css=css) as demo:
|
|
164 |
with gr.Row(visible=True):
|
165 |
width = gr.Slider(
|
166 |
label="Width",
|
167 |
-
|
168 |
maximum=MAX_IMAGE_SIZE,
|
169 |
step=32,
|
170 |
value=1024,
|
171 |
)
|
172 |
height = gr.Slider(
|
173 |
label="Height",
|
174 |
-
|
175 |
maximum=MAX_IMAGE_SIZE,
|
176 |
step=32,
|
177 |
value=1024,
|
@@ -179,12 +216,20 @@ with gr.Blocks(css=css) as demo:
|
|
179 |
with gr.Row():
|
180 |
guidance_scale = gr.Slider(
|
181 |
label="Guidance Scale",
|
182 |
-
|
183 |
maximum=10,
|
184 |
step=0.1,
|
185 |
value=7.0,
|
186 |
)
|
187 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
188 |
gr.Examples(
|
189 |
examples=examples,
|
190 |
inputs=prompt,
|
@@ -223,5 +268,28 @@ with gr.Blocks(css=css) as demo:
|
|
223 |
api_name="run",
|
224 |
)
|
225 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
226 |
if __name__ == "__main__":
|
227 |
-
demo.queue().launch()
|
|
|
7 |
from PIL import Image
|
8 |
import spaces
|
9 |
import torch
|
10 |
+
from diffusers import StableDiffusion3Pipeline, DPMSolverMultistepScheduler, AutoencoderKL, AutoPipelineForImage2Image
|
11 |
from huggingface_hub import snapshot_download
|
12 |
|
13 |
huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
|
|
|
18 |
repo_type="model",
|
19 |
ignore_patterns=["*.md", "*..gitattributes"],
|
20 |
local_dir="stable-diffusion-3-medium",
|
21 |
+
token=huggingface_token, # type a new token-id.
|
22 |
)
|
23 |
|
24 |
DESCRIPTION = """# Stable Diffusion 3"""
|
|
|
34 |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
35 |
|
36 |
pipe = StableDiffusion3Pipeline.from_pretrained(model_path, torch_dtype=torch.float16)
|
37 |
+
img2img_pipe = AutoPipelineForImage2Image.from_pretrained(model_path, torch_dtype=torch.float16)
|
38 |
|
39 |
def save_image(img):
|
40 |
unique_name = str(uuid.uuid4()) + ".png"
|
|
|
50 |
|
51 |
@spaces.GPU
|
52 |
def generate(
|
53 |
+
prompt:str,
|
54 |
negative_prompt: str = "",
|
55 |
use_negative_prompt: bool = False,
|
56 |
seed: int = 0,
|
|
|
67 |
seed = int(randomize_seed_fn(seed, randomize_seed))
|
68 |
generator = torch.Generator().manual_seed(seed)
|
69 |
|
|
|
|
|
70 |
if not use_negative_prompt:
|
71 |
+
negative_prompt = None # type: ignore
|
72 |
|
73 |
output = pipe(
|
74 |
prompt=prompt,
|
|
|
79 |
num_inference_steps=num_inference_steps,
|
80 |
generator=generator,
|
81 |
num_images_per_prompt=NUM_IMAGES_PER_PROMPT,
|
82 |
+
output_type="battery",
|
83 |
+
).images
|
84 |
+
|
85 |
+
return output
|
86 |
+
|
87 |
+
|
88 |
+
@spaces.GPU
|
89 |
+
def img2img_generate(
|
90 |
+
prompt:str,
|
91 |
+
init_image: gr.Image,
|
92 |
+
negative_prompt: str = "",
|
93 |
+
use_negative_prompt: bool = False,
|
94 |
+
seed: int = 0,
|
95 |
+
guidance_scale: float = 7,
|
96 |
+
randomize_seed: bool = False,
|
97 |
+
num_inference_steps=30,
|
98 |
+
strength: float = 0.8,
|
99 |
+
NUM_IMAGES_PER_PROMPT=1,
|
100 |
+
use_resolution_binning: bool = True,
|
101 |
+
progress=gr.Progress(track_tqdm=True),
|
102 |
+
):
|
103 |
+
img2img_pipe.to(device)
|
104 |
+
seed = int(randomize_seed_fn(seed, randomize_seed))
|
105 |
+
generator = torch.Generator().manual_seed(seed)
|
106 |
+
|
107 |
+
if not use_negative_prompt:
|
108 |
+
negative_prompt = None # type: ignore
|
109 |
+
|
110 |
+
init_image = init_image.resize((768, 768))
|
111 |
+
|
112 |
+
output = img2img_pipe(
|
113 |
+
prompt=prompt,
|
114 |
+
image=init_image,
|
115 |
+
negative_prompt=negative_prompt,
|
116 |
+
guidance_scale=guidance_scale,
|
117 |
+
num_inference_steps=num_inference_steps,
|
118 |
+
generator=generator,
|
119 |
+
strength=strength,
|
120 |
+
num_images_per_prompt=NUM_IMAGES_PER_PROMPT,
|
121 |
+
output_type="battery",
|
122 |
).images
|
123 |
|
124 |
return output
|
|
|
146 |
Stable Diffusion 3
|
147 |
</h1>
|
148 |
"""
|
149 |
+
)
|
150 |
gr.HTML(
|
151 |
"""
|
152 |
<h3 style='text-align: center'>
|
|
|
154 |
<a href='https://twitter.com/kadirnar_ai' target='_blank'>Twitter</a> | <a href='https://github.com/kadirnar' target='_blank'>Github</a> | <a href='https://www.linkedin.com/in/kadir-nar/' target='_blank'>Linkedin</a>
|
155 |
</h3>
|
156 |
"""
|
157 |
+
)
|
158 |
with gr.Group():
|
159 |
with gr.Row():
|
160 |
prompt = gr.Text(
|
|
|
177 |
)
|
178 |
seed = gr.Slider(
|
179 |
label="Seed",
|
180 |
+
min=0,
|
181 |
maximum=MAX_SEED,
|
182 |
step=1,
|
183 |
value=0,
|
|
|
185 |
|
186 |
steps = gr.Slider(
|
187 |
label="Steps",
|
188 |
+
min=0,
|
189 |
maximum=60,
|
190 |
step=1,
|
191 |
value=25,
|
192 |
)
|
193 |
number_image = gr.Slider(
|
194 |
+
label="Number of Images",
|
195 |
+
min=1,
|
196 |
maximum=4,
|
197 |
step=1,
|
198 |
value=1,
|
|
|
201 |
with gr.Row(visible=True):
|
202 |
width = gr.Slider(
|
203 |
label="Width",
|
204 |
+
min=256,
|
205 |
maximum=MAX_IMAGE_SIZE,
|
206 |
step=32,
|
207 |
value=1024,
|
208 |
)
|
209 |
height = gr.Slider(
|
210 |
label="Height",
|
211 |
+
min=256,
|
212 |
maximum=MAX_IMAGE_SIZE,
|
213 |
step=32,
|
214 |
value=1024,
|
|
|
216 |
with gr.Row():
|
217 |
guidance_scale = gr.Slider(
|
218 |
label="Guidance Scale",
|
219 |
+
min=0.1,
|
220 |
maximum=10,
|
221 |
step=0.1,
|
222 |
value=7.0,
|
223 |
)
|
224 |
|
225 |
+
with gr.Group():
|
226 |
+
with gr.Row():
|
227 |
+
with gr.Column():
|
228 |
+
init_image = gr.Image(label="Input Image", type="pil", tool="sketch")
|
229 |
+
with gr.Column():
|
230 |
+
img2img_output = gr.Gallery(label="Output Images", show_label=False).style(grid=2)
|
231 |
+
strength = gr.Slider(label="Img2Img Strength", minimum=0.0, maximum=1.0, step=0.01, value=0.8)
|
232 |
+
|
233 |
gr.Examples(
|
234 |
examples=examples,
|
235 |
inputs=prompt,
|
|
|
268 |
api_name="run",
|
269 |
)
|
270 |
|
271 |
+
gr.on(
|
272 |
+
triggers=[
|
273 |
+
prompt.submit,
|
274 |
+
negative_prompt.submit,
|
275 |
+
run_button.click,
|
276 |
+
],
|
277 |
+
fn=img2img_generate,
|
278 |
+
inputs=[
|
279 |
+
prompt,
|
280 |
+
init_image,
|
281 |
+
negative_prompt,
|
282 |
+
use_negative_prompt,
|
283 |
+
seed,
|
284 |
+
guidance_scale,
|
285 |
+
randomize_seed,
|
286 |
+
steps,
|
287 |
+
strength,
|
288 |
+
number_image,
|
289 |
+
],
|
290 |
+
outputs=[img2img_output],
|
291 |
+
api_name="img2img_run",
|
292 |
+
)
|
293 |
+
|
294 |
if __name__ == "__main__":
|
295 |
+
demo.queue().launch()
|