Spaces:
Build error
Build error
add img to img
Browse files
app.py
CHANGED
@@ -1,232 +1,363 @@
|
|
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
|
11 |
-
from huggingface_hub import snapshot_download
|
12 |
-
|
13 |
-
huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
|
14 |
-
|
15 |
-
model_path = snapshot_download(
|
16 |
-
repo_id="stabilityai/stable-diffusion-3-medium",
|
17 |
-
revision="refs/pr/26",
|
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"""
|
25 |
-
if not torch.cuda.is_available():
|
26 |
-
DESCRIPTION += "\n<p>Running on CPU 🥶 This demo may not work on CPU.</p>"
|
27 |
-
|
28 |
-
MAX_SEED = np.iinfo(np.int32).max
|
29 |
-
CACHE_EXAMPLES = False
|
30 |
-
MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "1536"))
|
31 |
-
USE_TORCH_COMPILE = False
|
32 |
-
ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
|
33 |
-
|
34 |
-
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
.
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
)
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 huggingface_hub import snapshot_download
|
12 |
+
|
13 |
+
huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
|
14 |
+
|
15 |
+
model_path = snapshot_download(
|
16 |
+
repo_id="stabilityai/stable-diffusion-3-medium",
|
17 |
+
revision="refs/pr/26",
|
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"""
|
25 |
+
if not torch.cuda.is_available():
|
26 |
+
DESCRIPTION += "\n<p>Running on CPU 🥶 This demo may not work on CPU.</p>"
|
27 |
+
|
28 |
+
MAX_SEED = np.iinfo(np.int32).max
|
29 |
+
CACHE_EXAMPLES = False
|
30 |
+
MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "1536"))
|
31 |
+
USE_TORCH_COMPILE = False
|
32 |
+
ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
|
33 |
+
|
34 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
35 |
+
|
36 |
+
def load_pipeline(pipeline_type):
|
37 |
+
if pipeline_type == "text2img":
|
38 |
+
return StableDiffusion3Pipeline.from_pretrained(model_path, torch_dtype=torch.float16)
|
39 |
+
elif pipeline_type == "img2img":
|
40 |
+
return StableDiffusion3Img2ImgPipeline.from_pretrained(model_path, torch_dtype=torch.float16)
|
41 |
+
|
42 |
+
def save_image(img):
|
43 |
+
unique_name = str(uuid.uuid4()) + ".png"
|
44 |
+
img.save(unique_name)
|
45 |
+
return unique_name
|
46 |
+
|
47 |
+
|
48 |
+
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
49 |
+
if randomize_seed:
|
50 |
+
seed = random.randint(0, MAX_SEED)
|
51 |
+
return seed
|
52 |
+
|
53 |
+
|
54 |
+
@spaces.GPU
|
55 |
+
def generate(
|
56 |
+
prompt:str,
|
57 |
+
negative_prompt: str = "",
|
58 |
+
use_negative_prompt: bool = False,
|
59 |
+
seed: int = 0,
|
60 |
+
width: int = 1024,
|
61 |
+
height: int = 1024,
|
62 |
+
guidance_scale: float = 7,
|
63 |
+
randomize_seed: bool = False,
|
64 |
+
num_inference_steps=30,
|
65 |
+
NUM_IMAGES_PER_PROMPT=1,
|
66 |
+
use_resolution_binning: bool = True,
|
67 |
+
progress=gr.Progress(track_tqdm=True),
|
68 |
+
):
|
69 |
+
pipe = load_pipeline("text2img")
|
70 |
+
pipe.to(device)
|
71 |
+
seed = int(randomize_seed_fn(seed, randomize_seed))
|
72 |
+
generator = torch.Generator().manual_seed(seed)
|
73 |
+
|
74 |
+
if not use_negative_prompt:
|
75 |
+
negative_prompt = None # type: ignore
|
76 |
+
|
77 |
+
output = pipe(
|
78 |
+
prompt=prompt,
|
79 |
+
negative_prompt=negative_prompt,
|
80 |
+
width=width,
|
81 |
+
height=height,
|
82 |
+
guidance_scale=guidance_scale,
|
83 |
+
num_inference_steps=num_inference_steps,
|
84 |
+
generator=generator,
|
85 |
+
num_images_per_prompt=NUM_IMAGES_PER_PROMPT,
|
86 |
+
output_type="battery",
|
87 |
+
).images
|
88 |
+
|
89 |
+
return output
|
90 |
+
|
91 |
+
|
92 |
+
@spaces.GPU
|
93 |
+
def img2img_generate(
|
94 |
+
prompt:str,
|
95 |
+
init_image: gr.Image,
|
96 |
+
negative_prompt: str = "",
|
97 |
+
use_negative_prompt: bool = False,
|
98 |
+
seed: int = 0,
|
99 |
+
guidance_scale: float = 7,
|
100 |
+
randomize_seed: bool = False,
|
101 |
+
num_inference_steps=30,
|
102 |
+
strength: float = 0.8,
|
103 |
+
NUM_IMAGES_PER_PROMPT=1,
|
104 |
+
use_resolution_binning: bool = True,
|
105 |
+
progress=gr.Progress(track_tqdm=True),
|
106 |
+
):
|
107 |
+
pipe = load_pipeline("img2img")
|
108 |
+
pipe.to(device)
|
109 |
+
seed = int(randomize_seed_fn(seed, randomize_seed))
|
110 |
+
generator = torch.Generator().manual_seed(seed)
|
111 |
+
|
112 |
+
if not use_negative_prompt:
|
113 |
+
negative_prompt = None # type: ignore
|
114 |
+
|
115 |
+
init_image = init_image.resize((768, 768))
|
116 |
+
|
117 |
+
output = pipe(
|
118 |
+
prompt=prompt,
|
119 |
+
image=init_image,
|
120 |
+
negative_prompt=negative_prompt,
|
121 |
+
guidance_scale=guidance_scale,
|
122 |
+
num_inference_steps=num_inference_steps,
|
123 |
+
generator=generator,
|
124 |
+
strength=strength,
|
125 |
+
num_images_per_prompt=NUM_IMAGES_PER_PROMPT,
|
126 |
+
output_type="battery",
|
127 |
+
).images
|
128 |
+
|
129 |
+
return output
|
130 |
+
|
131 |
+
|
132 |
+
|
133 |
+
examples = [
|
134 |
+
"A cardboard with text 'New York' which is large and sits on a theater stage.",
|
135 |
+
"A red sofa on top of a white building.",
|
136 |
+
"A painting of an astronaut riding a pig wearing a tutu holding a pink umbrella.",
|
137 |
+
"Studio photograph closeup of a chameleon over a black background.",
|
138 |
+
"Closeup portrait photo of beautiful goth woman, makeup.",
|
139 |
+
"A living room, bright modern Scandinavian style house, large windows.",
|
140 |
+
"Portrait photograph of an anthropomorphic tortoise seated on a New York City subway train.",
|
141 |
+
"Batman, cute modern Disney style, Pixar 3d portrait, ultra detailed, gorgeous, 3d zbrush, trending on dribbble, 8k render.",
|
142 |
+
"Cinnamon bun on the plate, watercolor painting, detailed, brush strokes, light palette, light, cozy.",
|
143 |
+
"A lion, colorful, low-poly, cyan and orange eyes, poly-hd, 3d, low-poly game art, polygon mesh, jagged, blocky, wireframe edges, centered composition.",
|
144 |
+
"Long exposure photo of Tokyo street, blurred motion, streaks of light, surreal, dreamy, ghosting effect, highly detailed.",
|
145 |
+
"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.",
|
146 |
+
"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."
|
147 |
+
|
148 |
+
]
|
149 |
+
|
150 |
+
css = '''
|
151 |
+
.gradio-container{max-width: 1000px !important}
|
152 |
+
h1{text-align:center}
|
153 |
+
'''
|
154 |
+
with gr.Blocks(css=css,theme="snehilsanyal/scikit-learn") as demo:
|
155 |
+
with gr.Row():
|
156 |
+
with gr.Column():
|
157 |
+
gr.HTML(
|
158 |
+
"""
|
159 |
+
<h1 style='text-align: center'>
|
160 |
+
Stable Diffusion 3 Medium
|
161 |
+
</h1>
|
162 |
+
"""
|
163 |
+
)
|
164 |
+
gr.HTML(
|
165 |
+
"""
|
166 |
+
|
167 |
+
"""
|
168 |
+
)
|
169 |
+
|
170 |
+
with gr.Tabs():
|
171 |
+
with gr.TabItem("Text to Image"):
|
172 |
+
with gr.Group():
|
173 |
+
with gr.Row():
|
174 |
+
prompt = gr.Text(
|
175 |
+
label="Prompt",
|
176 |
+
show_label=False,
|
177 |
+
max_lines=1,
|
178 |
+
placeholder="Enter your prompt",
|
179 |
+
container=False,
|
180 |
+
)
|
181 |
+
run_button = gr.Button("Run", scale=0)
|
182 |
+
result = gr.Gallery(label="Result", elem_id="gallery", show_label=False)
|
183 |
+
with gr.Accordion("Advanced options", open=False):
|
184 |
+
with gr.Row():
|
185 |
+
use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True)
|
186 |
+
negative_prompt = gr.Text(
|
187 |
+
label="Negative prompt",
|
188 |
+
max_lines=1,
|
189 |
+
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",
|
190 |
+
visible=True,
|
191 |
+
)
|
192 |
+
seed = gr.Slider(
|
193 |
+
label="Seed",
|
194 |
+
minimum=0,
|
195 |
+
maximum=MAX_SEED,
|
196 |
+
step=1,
|
197 |
+
value=0,
|
198 |
+
)
|
199 |
+
|
200 |
+
steps = gr.Slider(
|
201 |
+
label="Steps",
|
202 |
+
minimum=0,
|
203 |
+
maximum=60,
|
204 |
+
step=1,
|
205 |
+
value=25,
|
206 |
+
)
|
207 |
+
number_image = gr.Slider(
|
208 |
+
label="Number of Images",
|
209 |
+
minimum=1,
|
210 |
+
maximum=4,
|
211 |
+
step=1,
|
212 |
+
value=2,
|
213 |
+
)
|
214 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
215 |
+
with gr.Row(visible=True):
|
216 |
+
width = gr.Slider(
|
217 |
+
label="Width",
|
218 |
+
minimum=256,
|
219 |
+
maximum=MAX_IMAGE_SIZE,
|
220 |
+
step=32,
|
221 |
+
value=1024,
|
222 |
+
)
|
223 |
+
height = gr.Slider(
|
224 |
+
label="Height",
|
225 |
+
minimum=256,
|
226 |
+
maximum=MAX_IMAGE_SIZE,
|
227 |
+
step=32,
|
228 |
+
value=1024,
|
229 |
+
)
|
230 |
+
with gr.Row():
|
231 |
+
guidance_scale = gr.Slider(
|
232 |
+
label="Guidance Scale",
|
233 |
+
minimum=0.1,
|
234 |
+
maximum=10,
|
235 |
+
step=0.1,
|
236 |
+
value=7.0,
|
237 |
+
)
|
238 |
+
|
239 |
+
gr.Examples(
|
240 |
+
examples=examples,
|
241 |
+
inputs=prompt,
|
242 |
+
outputs=[result],
|
243 |
+
fn=generate,
|
244 |
+
cache_examples=CACHE_EXAMPLES,
|
245 |
+
)
|
246 |
+
|
247 |
+
use_negative_prompt.change(
|
248 |
+
fn=lambda x: gr.update(visible=x),
|
249 |
+
inputs=use_negative_prompt,
|
250 |
+
outputs=negative_prompt,
|
251 |
+
api_name=False,
|
252 |
+
)
|
253 |
+
|
254 |
+
gr.on(
|
255 |
+
triggers=[
|
256 |
+
prompt.submit,
|
257 |
+
negative_prompt.submit,
|
258 |
+
run_button.click,
|
259 |
+
],
|
260 |
+
fn=generate,
|
261 |
+
inputs=[
|
262 |
+
prompt,
|
263 |
+
negative_prompt,
|
264 |
+
use_negative_prompt,
|
265 |
+
seed,
|
266 |
+
width,
|
267 |
+
height,
|
268 |
+
guidance_scale,
|
269 |
+
randomize_seed,
|
270 |
+
steps,
|
271 |
+
number_image,
|
272 |
+
],
|
273 |
+
outputs=[result],
|
274 |
+
api_name="run",
|
275 |
+
)
|
276 |
+
with gr.TabItem("Image to Image"):
|
277 |
+
with gr.Group():
|
278 |
+
with gr.Row(equal_height=True):
|
279 |
+
with gr.Column(scale=1):
|
280 |
+
img2img_prompt = gr.Text(
|
281 |
+
label="Prompt",
|
282 |
+
show_label=False,
|
283 |
+
max_lines=1,
|
284 |
+
placeholder="Enter your prompt",
|
285 |
+
container=False,
|
286 |
+
)
|
287 |
+
init_image = gr.Image(label="Input Image", type="pil")
|
288 |
+
with gr.Row():
|
289 |
+
img2img_run_button = gr.Button("Generate", variant="primary")
|
290 |
+
with gr.Column(scale=1):
|
291 |
+
img2img_output = gr.Gallery(label="Result", elem_id="gallery")
|
292 |
+
with gr.Accordion("Advanced options", open=False):
|
293 |
+
with gr.Row():
|
294 |
+
img2img_use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True)
|
295 |
+
img2img_negative_prompt = gr.Text(
|
296 |
+
label="Negative prompt",
|
297 |
+
max_lines=1,
|
298 |
+
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",
|
299 |
+
visible=True,
|
300 |
+
)
|
301 |
+
img2img_seed = gr.Slider(
|
302 |
+
label="Seed",
|
303 |
+
minimum=0,
|
304 |
+
maximum=MAX_SEED,
|
305 |
+
step=1,
|
306 |
+
value=0,
|
307 |
+
)
|
308 |
+
img2img_steps = gr.Slider(
|
309 |
+
label="Steps",
|
310 |
+
minimum=0,
|
311 |
+
maximum=60,
|
312 |
+
step=1,
|
313 |
+
value=25,
|
314 |
+
)
|
315 |
+
img2img_number_image = gr.Slider(
|
316 |
+
label="Number of Images",
|
317 |
+
minimum=1,
|
318 |
+
maximum=4,
|
319 |
+
step=1,
|
320 |
+
value=2,
|
321 |
+
)
|
322 |
+
img2img_randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
323 |
+
with gr.Row():
|
324 |
+
img2img_guidance_scale = gr.Slider(
|
325 |
+
label="Guidance Scale",
|
326 |
+
minimum=0.1,
|
327 |
+
maximum=10,
|
328 |
+
step=0.1,
|
329 |
+
value=7.0,
|
330 |
+
)
|
331 |
+
strength = gr.Slider(label="Img2Img Strength", minimum=0.0, maximum=1.0, step=0.01, value=0.8)
|
332 |
+
|
333 |
+
img2img_use_negative_prompt.change(
|
334 |
+
fn=lambda x: gr.update(visible=x),
|
335 |
+
inputs=img2img_use_negative_prompt,
|
336 |
+
outputs=img2img_negative_prompt,
|
337 |
+
api_name=False,
|
338 |
+
)
|
339 |
+
|
340 |
+
gr.on(
|
341 |
+
triggers=[
|
342 |
+
img2img_prompt.submit,
|
343 |
+
img2img_negative_prompt.submit,
|
344 |
+
img2img_run_button.click,
|
345 |
+
],
|
346 |
+
fn=img2img_generate,
|
347 |
+
inputs=[
|
348 |
+
img2img_prompt,
|
349 |
+
init_image,
|
350 |
+
img2img_negative_prompt,
|
351 |
+
img2img_use_negative_prompt,
|
352 |
+
img2img_seed,
|
353 |
+
img2img_guidance_scale,
|
354 |
+
img2img_randomize_seed,
|
355 |
+
img2img_steps,
|
356 |
+
strength,
|
357 |
+
img2img_number_image,
|
358 |
+
],
|
359 |
+
outputs=[img2img_output],
|
360 |
+
api_name="img2img_run",
|
361 |
+
)
|
362 |
+
if __name__ == "__main__":
|
363 |
+
demo.queue().launch(show_api=False, debug=False)
|