Spaces:
Running
on
Zero
Running
on
Zero
prithivMLmods
commited on
Commit
•
36b95af
1
Parent(s):
6b1a2ee
Update app.py
Browse files
app.py
CHANGED
@@ -1,59 +1,26 @@
|
|
1 |
-
#!/usr/bin/env python
|
2 |
-
#patch 0.01 ()
|
3 |
-
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4 |
-
# of this software and associated documentation files (the "Software"), to deal
|
5 |
-
# in the Software without restriction, including without limitation the rights
|
6 |
-
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7 |
-
# copies of the Software, and to permit persons to whom the Software is
|
8 |
-
# furnished to do so, subject to the following conditions:
|
9 |
-
#
|
10 |
-
# ..
|
11 |
-
|
12 |
-
import os
|
13 |
-
import random
|
14 |
-
import uuid
|
15 |
-
from typing import Tuple
|
16 |
import gradio as gr
|
17 |
import numpy as np
|
18 |
-
|
|
|
19 |
import spaces
|
|
|
20 |
import torch
|
21 |
-
from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
|
22 |
|
23 |
-
|
|
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
return unique_name
|
30 |
-
|
31 |
-
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
32 |
-
if randomize_seed:
|
33 |
-
seed = random.randint(0, MAX_SEED)
|
34 |
-
return seed
|
35 |
-
|
36 |
-
MAX_SEED = np.iinfo(np.int32).max
|
37 |
|
38 |
-
|
39 |
-
|
40 |
|
41 |
MAX_SEED = np.iinfo(np.int32).max
|
|
|
42 |
|
43 |
-
|
44 |
-
ENABLE_CPU_OFFLOAD = 0
|
45 |
-
|
46 |
-
if torch.cuda.is_available():
|
47 |
-
pipe = StableDiffusionXLPipeline.from_pretrained(
|
48 |
-
"SG161222/RealVisXL_V5.0_Lightning",
|
49 |
-
torch_dtype=torch.float16,
|
50 |
-
use_safetensors=True,
|
51 |
-
)
|
52 |
-
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
53 |
-
pipe.load_lora_weights("prithivMLmods/Canopus-Realism-LoRA", weight_name="Canopus-Realism-LoRA.safetensors", adapter_name="rlms")
|
54 |
-
pipe.set_adapters("rlms")
|
55 |
-
pipe.to("cuda")
|
56 |
-
|
57 |
style_list = [
|
58 |
{
|
59 |
"name": "3840 x 2160",
|
@@ -77,208 +44,152 @@ style_list = [
|
|
77 |
},
|
78 |
]
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
@spaces.GPU(duration=60, enable_queue=True)
|
96 |
-
def generate(
|
97 |
-
prompt: str,
|
98 |
-
negative_prompt: str = "",
|
99 |
-
use_negative_prompt: bool = False,
|
100 |
-
seed: int = 0,
|
101 |
-
width: int = 1024,
|
102 |
-
height: int = 1024,
|
103 |
-
guidance_scale: float = 3,
|
104 |
-
randomize_seed: bool = False,
|
105 |
-
style_name: str = DEFAULT_STYLE_NAME,
|
106 |
progress=gr.Progress(track_tqdm=True),
|
107 |
):
|
108 |
-
seed = int(randomize_seed_fn(seed, randomize_seed))
|
109 |
-
|
110 |
-
positive_prompt, effective_negative_prompt = apply_style(style_name, prompt, negative_prompt)
|
111 |
|
112 |
-
if
|
113 |
-
|
|
|
114 |
|
115 |
-
|
116 |
-
|
117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
width=width,
|
119 |
height=height,
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
output_type="pil",
|
125 |
-
).images
|
126 |
-
image_paths = [save_image(img) for img in images]
|
127 |
-
print(image_paths)
|
128 |
-
return image_paths, seed
|
129 |
|
130 |
examples = [
|
131 |
-
"A
|
132 |
-
"Photography, front view, dynamic range, female model, upper-body, black T-shirt, dark khaki cargo pants, urban backdrop, dusk, dramatic sunlights, bokeh, cityscape, photorealism, natural, UHD --ar 9:16 --stylize 300"
|
133 |
]
|
134 |
|
135 |
-
css =
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
visibility: hidden
|
140 |
}
|
141 |
-
|
142 |
|
143 |
-
|
144 |
-
|
145 |
-
"
|
146 |
-
"
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
|
|
|
157 |
|
|
|
158 |
|
159 |
-
with gr.
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
max_lines=1,
|
166 |
-
placeholder="Enter your prompt",
|
167 |
-
container=False,
|
168 |
-
)
|
169 |
-
run_button = gr.Button("Run", scale=0)
|
170 |
-
result = gr.Gallery(label="Result", columns=1, preview=True, show_label=False)
|
171 |
-
|
172 |
-
with gr.Accordion("Advanced options", open=False, visible=False):
|
173 |
-
use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True)
|
174 |
-
negative_prompt = gr.Text(
|
175 |
-
label="Negative prompt",
|
176 |
-
lines=4,
|
177 |
-
max_lines=6,
|
178 |
-
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",
|
179 |
-
placeholder="Enter a negative prompt",
|
180 |
-
visible=True,
|
181 |
-
)
|
182 |
-
seed = gr.Slider(
|
183 |
-
label="Seed",
|
184 |
-
minimum=0,
|
185 |
-
maximum=MAX_SEED,
|
186 |
-
step=1,
|
187 |
-
value=0,
|
188 |
-
visible=True
|
189 |
-
)
|
190 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
191 |
-
|
192 |
-
with gr.Row(visible=True):
|
193 |
-
width = gr.Slider(
|
194 |
-
label="Width",
|
195 |
-
minimum=512,
|
196 |
-
maximum=2048,
|
197 |
-
step=64,
|
198 |
-
value=1024,
|
199 |
)
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
with gr.Row():
|
209 |
-
guidance_scale = gr.Slider(
|
210 |
-
label="Guidance Scale",
|
211 |
-
minimum=0.1,
|
212 |
-
maximum=20.0,
|
213 |
-
step=0.1,
|
214 |
-
value=3.0,
|
215 |
)
|
216 |
|
217 |
-
|
218 |
-
show_label=True,
|
219 |
-
container=True,
|
220 |
-
interactive=True,
|
221 |
-
choices=STYLE_NAMES,
|
222 |
-
value=DEFAULT_STYLE_NAME,
|
223 |
-
label="Quality Style",
|
224 |
-
)
|
225 |
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
use_negative_prompt.change(
|
235 |
-
fn=lambda x: gr.update(visible=x),
|
236 |
-
inputs=use_negative_prompt,
|
237 |
-
outputs=negative_prompt,
|
238 |
-
api_name=False,
|
239 |
-
)
|
240 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
241 |
gr.on(
|
242 |
-
triggers=[
|
243 |
-
|
244 |
-
negative_prompt.submit,
|
245 |
-
run_button.click,
|
246 |
-
],
|
247 |
-
fn=generate,
|
248 |
inputs=[
|
249 |
prompt,
|
250 |
negative_prompt,
|
251 |
-
use_negative_prompt,
|
252 |
seed,
|
|
|
253 |
width,
|
254 |
height,
|
255 |
guidance_scale,
|
256 |
-
|
257 |
style_selection,
|
258 |
],
|
259 |
outputs=[result, seed],
|
260 |
-
api_name="run",
|
261 |
)
|
262 |
-
# Adding a predefined gallery section
|
263 |
-
|
264 |
-
gr.Markdown("### Generated Images")
|
265 |
-
predefined_gallery = gr.Gallery(label="Generated Images", columns=3, show_label=False, value=load_predefined_images())
|
266 |
-
|
267 |
-
gr.Markdown("**Disclaimer/Note:**")
|
268 |
-
|
269 |
-
#gr.Markdown("🙀This space provides realistic image generation, which works better for human faces and portraits. Realistic trigger works properly, better for photorealistic trigger words, close-up shots, face diffusion, male, female characters.")
|
270 |
-
|
271 |
-
#gr.Markdown("⚠️users are accountable for the content they generate and are responsible for ensuring it meets appropriate ethical standards.")
|
272 |
-
|
273 |
-
gr.Markdown("""
|
274 |
-
<div style='text-align: justify;'>
|
275 |
-
🙀This space provides realistic image generation, which works better for human faces and portraits. Realistic trigger works properly, better for photorealistic trigger words, close-up shots, face diffusion, male, female characters.
|
276 |
-
</div>""")
|
277 |
-
|
278 |
-
gr.Markdown("""
|
279 |
-
<div style='text-align: justify;'>
|
280 |
-
⚠️Users are accountable for the content they generate and are responsible for ensuring it meets appropriate ethical standards.
|
281 |
-
</div>""")
|
282 |
|
283 |
if __name__ == "__main__":
|
284 |
-
demo.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
+
import random
|
4 |
+
|
5 |
import spaces
|
6 |
+
from diffusers import DiffusionPipeline
|
7 |
import torch
|
|
|
8 |
|
9 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
10 |
+
model_repo_id = "stabilityai/stable-diffusion-3.5-large-turbo"
|
11 |
|
12 |
+
if torch.cuda.is_available():
|
13 |
+
torch_dtype = torch.bfloat16
|
14 |
+
else:
|
15 |
+
torch_dtype = torch.float32
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
+
pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
|
18 |
+
pipe = pipe.to(device)
|
19 |
|
20 |
MAX_SEED = np.iinfo(np.int32).max
|
21 |
+
MAX_IMAGE_SIZE = 1024
|
22 |
|
23 |
+
# Define styles
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
style_list = [
|
25 |
{
|
26 |
"name": "3840 x 2160",
|
|
|
44 |
},
|
45 |
]
|
46 |
|
47 |
+
STYLE_NAMES = [style["name"] for style in style_list]
|
48 |
+
DEFAULT_STYLE_NAME = STYLE_NAMES[0]
|
49 |
+
|
50 |
+
@spaces.GPU
|
51 |
+
def infer(
|
52 |
+
prompt,
|
53 |
+
negative_prompt="",
|
54 |
+
seed=42,
|
55 |
+
randomize_seed=False,
|
56 |
+
width=1024,
|
57 |
+
height=1024,
|
58 |
+
guidance_scale=0.0,
|
59 |
+
num_inference_steps=4,
|
60 |
+
style="Style Zero",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
progress=gr.Progress(track_tqdm=True),
|
62 |
):
|
|
|
|
|
|
|
63 |
|
64 |
+
selected_style = next(s for s in style_list if s["name"] == style)
|
65 |
+
styled_prompt = selected_style["prompt"].format(prompt=prompt)
|
66 |
+
styled_negative_prompt = selected_style["negative_prompt"]
|
67 |
|
68 |
+
if randomize_seed:
|
69 |
+
seed = random.randint(0, MAX_SEED)
|
70 |
+
|
71 |
+
generator = torch.Generator().manual_seed(seed)
|
72 |
+
|
73 |
+
image = pipe(
|
74 |
+
prompt=styled_prompt,
|
75 |
+
negative_prompt=styled_negative_prompt,
|
76 |
+
guidance_scale=guidance_scale,
|
77 |
+
num_inference_steps=num_inference_steps,
|
78 |
width=width,
|
79 |
height=height,
|
80 |
+
generator=generator,
|
81 |
+
).images[0]
|
82 |
+
|
83 |
+
return image, seed
|
|
|
|
|
|
|
|
|
|
|
84 |
|
85 |
examples = [
|
86 |
+
"A capybara wearing a suit holding a sign that reads Hello World",
|
|
|
87 |
]
|
88 |
|
89 |
+
css = """
|
90 |
+
#col-container {
|
91 |
+
margin: 0 auto;
|
92 |
+
max-width: 640px;
|
|
|
93 |
}
|
94 |
+
"""
|
95 |
|
96 |
+
with gr.Blocks(css=css) as demo:
|
97 |
+
with gr.Column(elem_id="col-container"):
|
98 |
+
gr.Markdown(" # [Stable Diffusion 3.5 Large Turbo (8B)](https://huggingface.co/stabilityai/stable-diffusion-3.5-large-turbo)")
|
99 |
+
gr.Markdown("[Learn more](https://stability.ai/news/introducing-stable-diffusion-3-5) about the Stable Diffusion 3.5 series. Try on [Stability AI API](https://platform.stability.ai/docs/api-reference#tag/Generate/paths/~1v2beta~1stable-image~1generate~1sd3/post), or [download model](https://huggingface.co/stabilityai/stable-diffusion-3.5-large-turbo) to run locally with ComfyUI or diffusers.")
|
100 |
+
|
101 |
+
with gr.Row():
|
102 |
+
prompt = gr.Text(
|
103 |
+
label="Prompt",
|
104 |
+
show_label=False,
|
105 |
+
max_lines=1,
|
106 |
+
placeholder="Enter your prompt",
|
107 |
+
container=False,
|
108 |
+
)
|
109 |
|
110 |
+
run_button = gr.Button("Run", scale=0, variant="primary")
|
111 |
|
112 |
+
result = gr.Image(label="Result", show_label=False)
|
113 |
|
114 |
+
with gr.Accordion("Advanced Settings", open=False):
|
115 |
+
negative_prompt = gr.Text(
|
116 |
+
label="Negative prompt",
|
117 |
+
max_lines=1,
|
118 |
+
placeholder="Enter a negative prompt",
|
119 |
+
visible=False,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
)
|
121 |
+
|
122 |
+
seed = gr.Slider(
|
123 |
+
label="Seed",
|
124 |
+
minimum=0,
|
125 |
+
maximum=MAX_SEED,
|
126 |
+
step=1,
|
127 |
+
value=0,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
)
|
129 |
|
130 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
|
132 |
+
style_selection = gr.Radio(
|
133 |
+
show_label=True,
|
134 |
+
container=True,
|
135 |
+
interactive=True,
|
136 |
+
choices=STYLE_NAMES,
|
137 |
+
value=DEFAULT_STYLE_NAME,
|
138 |
+
label="Quality Style",
|
139 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
|
141 |
+
with gr.Row():
|
142 |
+
width = gr.Slider(
|
143 |
+
label="Width",
|
144 |
+
minimum=512,
|
145 |
+
maximum=MAX_IMAGE_SIZE,
|
146 |
+
step=32,
|
147 |
+
value=1024,
|
148 |
+
)
|
149 |
+
|
150 |
+
height = gr.Slider(
|
151 |
+
label="Height",
|
152 |
+
minimum=512,
|
153 |
+
maximum=MAX_IMAGE_SIZE,
|
154 |
+
step=32,
|
155 |
+
value=1024,
|
156 |
+
)
|
157 |
+
|
158 |
+
with gr.Row():
|
159 |
+
guidance_scale = gr.Slider(
|
160 |
+
label="Guidance scale",
|
161 |
+
minimum=0.0,
|
162 |
+
maximum=7.5,
|
163 |
+
step=0.1,
|
164 |
+
value=0.0,
|
165 |
+
)
|
166 |
+
|
167 |
+
num_inference_steps = gr.Slider(
|
168 |
+
label="Number of inference steps",
|
169 |
+
minimum=1,
|
170 |
+
maximum=50,
|
171 |
+
step=1,
|
172 |
+
value=4,
|
173 |
+
)
|
174 |
+
|
175 |
+
gr.Examples(examples=examples, inputs=[prompt], outputs=[result, seed], fn=infer, cache_examples=True, cache_mode="lazy")
|
176 |
+
|
177 |
gr.on(
|
178 |
+
triggers=[run_button.click, prompt.submit],
|
179 |
+
fn=infer,
|
|
|
|
|
|
|
|
|
180 |
inputs=[
|
181 |
prompt,
|
182 |
negative_prompt,
|
|
|
183 |
seed,
|
184 |
+
randomize_seed,
|
185 |
width,
|
186 |
height,
|
187 |
guidance_scale,
|
188 |
+
num_inference_steps,
|
189 |
style_selection,
|
190 |
],
|
191 |
outputs=[result, seed],
|
|
|
192 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
193 |
|
194 |
if __name__ == "__main__":
|
195 |
+
demo.launch()
|