Spaces:
Running
on
Zero
Running
on
Zero
votepurchase
commited on
Commit
•
92c2997
1
Parent(s):
6cabe6d
Upload 5 files
Browse files- app.py +395 -3
- config.py +111 -0
- requirements.txt +10 -6
- style.css +63 -0
- utils.py +182 -0
app.py
CHANGED
@@ -1,3 +1,395 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gc
|
3 |
+
import gradio as gr
|
4 |
+
import numpy as np
|
5 |
+
import torch
|
6 |
+
import json
|
7 |
+
import spaces
|
8 |
+
import config
|
9 |
+
import utils
|
10 |
+
import logging
|
11 |
+
from PIL import Image, PngImagePlugin
|
12 |
+
from datetime import datetime
|
13 |
+
from diffusers.models import AutoencoderKL
|
14 |
+
from diffusers import StableDiffusionXLPipeline, StableDiffusionXLImg2ImgPipeline
|
15 |
+
|
16 |
+
logging.basicConfig(level=logging.INFO)
|
17 |
+
logger = logging.getLogger(__name__)
|
18 |
+
|
19 |
+
DESCRIPTION = "Animagine XL 3.1"
|
20 |
+
if not torch.cuda.is_available():
|
21 |
+
DESCRIPTION += "\n<p>Running on CPU 🥶 This demo does not work on CPU. </p>"
|
22 |
+
IS_COLAB = utils.is_google_colab() or os.getenv("IS_COLAB") == "1"
|
23 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
24 |
+
CACHE_EXAMPLES = torch.cuda.is_available() and os.getenv("CACHE_EXAMPLES") == "1"
|
25 |
+
MIN_IMAGE_SIZE = int(os.getenv("MIN_IMAGE_SIZE", "512"))
|
26 |
+
MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "2048"))
|
27 |
+
USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE") == "1"
|
28 |
+
ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD") == "1"
|
29 |
+
OUTPUT_DIR = os.getenv("OUTPUT_DIR", "./outputs")
|
30 |
+
|
31 |
+
MODEL = os.getenv(
|
32 |
+
"MODEL",
|
33 |
+
"https://huggingface.co/votepurchase/animagine-xl-3.1/blob/main/animagine-xl-3.1.safetensors",
|
34 |
+
)
|
35 |
+
|
36 |
+
torch.backends.cudnn.deterministic = True
|
37 |
+
torch.backends.cudnn.benchmark = False
|
38 |
+
|
39 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
40 |
+
|
41 |
+
|
42 |
+
def load_pipeline(model_name):
|
43 |
+
vae = AutoencoderKL.from_pretrained(
|
44 |
+
"madebyollin/sdxl-vae-fp16-fix",
|
45 |
+
torch_dtype=torch.float16,
|
46 |
+
)
|
47 |
+
pipeline = (
|
48 |
+
StableDiffusionXLPipeline.from_single_file
|
49 |
+
if MODEL.endswith(".safetensors")
|
50 |
+
else StableDiffusionXLPipeline.from_pretrained
|
51 |
+
)
|
52 |
+
|
53 |
+
pipe = pipeline(
|
54 |
+
model_name,
|
55 |
+
vae=vae,
|
56 |
+
torch_dtype=torch.float16,
|
57 |
+
custom_pipeline="lpw_stable_diffusion_xl",
|
58 |
+
use_safetensors=True,
|
59 |
+
add_watermarker=False,
|
60 |
+
use_auth_token=HF_TOKEN,
|
61 |
+
)
|
62 |
+
|
63 |
+
pipe.to(device)
|
64 |
+
return pipe
|
65 |
+
|
66 |
+
|
67 |
+
@spaces.GPU
|
68 |
+
def generate(
|
69 |
+
prompt: str,
|
70 |
+
negative_prompt: str = "",
|
71 |
+
seed: int = 0,
|
72 |
+
custom_width: int = 1024,
|
73 |
+
custom_height: int = 1024,
|
74 |
+
guidance_scale: float = 7.0,
|
75 |
+
num_inference_steps: int = 28,
|
76 |
+
sampler: str = "Euler a",
|
77 |
+
aspect_ratio_selector: str = "896 x 1152",
|
78 |
+
style_selector: str = "(None)",
|
79 |
+
quality_selector: str = "Standard v3.1",
|
80 |
+
use_upscaler: bool = False,
|
81 |
+
upscaler_strength: float = 0.55,
|
82 |
+
upscale_by: float = 1.5,
|
83 |
+
add_quality_tags: bool = True,
|
84 |
+
progress=gr.Progress(track_tqdm=True),
|
85 |
+
):
|
86 |
+
generator = utils.seed_everything(seed)
|
87 |
+
|
88 |
+
width, height = utils.aspect_ratio_handler(
|
89 |
+
aspect_ratio_selector,
|
90 |
+
custom_width,
|
91 |
+
custom_height,
|
92 |
+
)
|
93 |
+
|
94 |
+
prompt = utils.add_wildcard(prompt, wildcard_files)
|
95 |
+
|
96 |
+
prompt, negative_prompt = utils.preprocess_prompt(
|
97 |
+
quality_prompt, quality_selector, prompt, negative_prompt, add_quality_tags
|
98 |
+
)
|
99 |
+
prompt, negative_prompt = utils.preprocess_prompt(
|
100 |
+
styles, style_selector, prompt, negative_prompt
|
101 |
+
)
|
102 |
+
|
103 |
+
width, height = utils.preprocess_image_dimensions(width, height)
|
104 |
+
|
105 |
+
backup_scheduler = pipe.scheduler
|
106 |
+
pipe.scheduler = utils.get_scheduler(pipe.scheduler.config, sampler)
|
107 |
+
|
108 |
+
if use_upscaler:
|
109 |
+
upscaler_pipe = StableDiffusionXLImg2ImgPipeline(**pipe.components)
|
110 |
+
metadata = {
|
111 |
+
"prompt": prompt,
|
112 |
+
"negative_prompt": negative_prompt,
|
113 |
+
"resolution": f"{width} x {height}",
|
114 |
+
"guidance_scale": guidance_scale,
|
115 |
+
"num_inference_steps": num_inference_steps,
|
116 |
+
"seed": seed,
|
117 |
+
"sampler": sampler,
|
118 |
+
"sdxl_style": style_selector,
|
119 |
+
"add_quality_tags": add_quality_tags,
|
120 |
+
"quality_tags": quality_selector,
|
121 |
+
}
|
122 |
+
|
123 |
+
if use_upscaler:
|
124 |
+
new_width = int(width * upscale_by)
|
125 |
+
new_height = int(height * upscale_by)
|
126 |
+
metadata["use_upscaler"] = {
|
127 |
+
"upscale_method": "nearest-exact",
|
128 |
+
"upscaler_strength": upscaler_strength,
|
129 |
+
"upscale_by": upscale_by,
|
130 |
+
"new_resolution": f"{new_width} x {new_height}",
|
131 |
+
}
|
132 |
+
else:
|
133 |
+
metadata["use_upscaler"] = None
|
134 |
+
metadata["Model"] = {
|
135 |
+
"Model": DESCRIPTION,
|
136 |
+
"Model hash": "e3c47aedb0",
|
137 |
+
}
|
138 |
+
|
139 |
+
logger.info(json.dumps(metadata, indent=4))
|
140 |
+
|
141 |
+
try:
|
142 |
+
if use_upscaler:
|
143 |
+
latents = pipe(
|
144 |
+
prompt=prompt,
|
145 |
+
negative_prompt=negative_prompt,
|
146 |
+
width=width,
|
147 |
+
height=height,
|
148 |
+
guidance_scale=guidance_scale,
|
149 |
+
num_inference_steps=num_inference_steps,
|
150 |
+
generator=generator,
|
151 |
+
output_type="latent",
|
152 |
+
).images
|
153 |
+
upscaled_latents = utils.upscale(latents, "nearest-exact", upscale_by)
|
154 |
+
images = upscaler_pipe(
|
155 |
+
prompt=prompt,
|
156 |
+
negative_prompt=negative_prompt,
|
157 |
+
image=upscaled_latents,
|
158 |
+
guidance_scale=guidance_scale,
|
159 |
+
num_inference_steps=num_inference_steps,
|
160 |
+
strength=upscaler_strength,
|
161 |
+
generator=generator,
|
162 |
+
output_type="pil",
|
163 |
+
).images
|
164 |
+
else:
|
165 |
+
images = pipe(
|
166 |
+
prompt=prompt,
|
167 |
+
negative_prompt=negative_prompt,
|
168 |
+
width=width,
|
169 |
+
height=height,
|
170 |
+
guidance_scale=guidance_scale,
|
171 |
+
num_inference_steps=num_inference_steps,
|
172 |
+
generator=generator,
|
173 |
+
output_type="pil",
|
174 |
+
).images
|
175 |
+
|
176 |
+
if images:
|
177 |
+
image_paths = [
|
178 |
+
utils.save_image(image, metadata, OUTPUT_DIR, IS_COLAB)
|
179 |
+
for image in images
|
180 |
+
]
|
181 |
+
|
182 |
+
for image_path in image_paths:
|
183 |
+
logger.info(f"Image saved as {image_path} with metadata")
|
184 |
+
|
185 |
+
return image_paths, metadata
|
186 |
+
except Exception as e:
|
187 |
+
logger.exception(f"An error occurred: {e}")
|
188 |
+
raise
|
189 |
+
finally:
|
190 |
+
if use_upscaler:
|
191 |
+
del upscaler_pipe
|
192 |
+
pipe.scheduler = backup_scheduler
|
193 |
+
utils.free_memory()
|
194 |
+
|
195 |
+
|
196 |
+
if torch.cuda.is_available():
|
197 |
+
pipe = load_pipeline(MODEL)
|
198 |
+
logger.info("Loaded on Device!")
|
199 |
+
else:
|
200 |
+
pipe = None
|
201 |
+
|
202 |
+
styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in config.style_list}
|
203 |
+
quality_prompt = {
|
204 |
+
k["name"]: (k["prompt"], k["negative_prompt"]) for k in config.quality_prompt_list
|
205 |
+
}
|
206 |
+
|
207 |
+
wildcard_files = utils.load_wildcard_files("wildcard")
|
208 |
+
|
209 |
+
with gr.Blocks(css="style.css", theme="NoCrypt/miku@1.2.1") as demo:
|
210 |
+
title = gr.HTML(
|
211 |
+
f"""<h1><span>{DESCRIPTION}</span></h1>""",
|
212 |
+
elem_id="title",
|
213 |
+
)
|
214 |
+
gr.Markdown(
|
215 |
+
f"""Gradio demo for [cagliostrolab/animagine-xl-3.1](https://huggingface.co/cagliostrolab/animagine-xl-3.1)""",
|
216 |
+
elem_id="subtitle",
|
217 |
+
)
|
218 |
+
gr.DuplicateButton(
|
219 |
+
value="Duplicate Space for private use",
|
220 |
+
elem_id="duplicate-button",
|
221 |
+
visible=os.getenv("SHOW_DUPLICATE_BUTTON") == "1",
|
222 |
+
)
|
223 |
+
with gr.Row():
|
224 |
+
with gr.Column(scale=2):
|
225 |
+
with gr.Tab("Txt2img"):
|
226 |
+
with gr.Group():
|
227 |
+
prompt = gr.Text(
|
228 |
+
label="Prompt",
|
229 |
+
max_lines=5,
|
230 |
+
placeholder="Enter your prompt",
|
231 |
+
)
|
232 |
+
negative_prompt = gr.Text(
|
233 |
+
label="Negative Prompt",
|
234 |
+
max_lines=5,
|
235 |
+
placeholder="Enter a negative prompt",
|
236 |
+
)
|
237 |
+
with gr.Accordion(label="Quality Tags", open=True):
|
238 |
+
add_quality_tags = gr.Checkbox(
|
239 |
+
label="Add Quality Tags", value=True
|
240 |
+
)
|
241 |
+
quality_selector = gr.Dropdown(
|
242 |
+
label="Quality Tags Presets",
|
243 |
+
interactive=True,
|
244 |
+
choices=list(quality_prompt.keys()),
|
245 |
+
value="Standard v3.1",
|
246 |
+
)
|
247 |
+
with gr.Tab("Advanced Settings"):
|
248 |
+
with gr.Group():
|
249 |
+
style_selector = gr.Radio(
|
250 |
+
label="Style Preset",
|
251 |
+
container=True,
|
252 |
+
interactive=True,
|
253 |
+
choices=list(styles.keys()),
|
254 |
+
value="(None)",
|
255 |
+
)
|
256 |
+
with gr.Group():
|
257 |
+
aspect_ratio_selector = gr.Radio(
|
258 |
+
label="Aspect Ratio",
|
259 |
+
choices=config.aspect_ratios,
|
260 |
+
value="896 x 1152",
|
261 |
+
container=True,
|
262 |
+
)
|
263 |
+
with gr.Group(visible=False) as custom_resolution:
|
264 |
+
with gr.Row():
|
265 |
+
custom_width = gr.Slider(
|
266 |
+
label="Width",
|
267 |
+
minimum=MIN_IMAGE_SIZE,
|
268 |
+
maximum=MAX_IMAGE_SIZE,
|
269 |
+
step=8,
|
270 |
+
value=1024,
|
271 |
+
)
|
272 |
+
custom_height = gr.Slider(
|
273 |
+
label="Height",
|
274 |
+
minimum=MIN_IMAGE_SIZE,
|
275 |
+
maximum=MAX_IMAGE_SIZE,
|
276 |
+
step=8,
|
277 |
+
value=1024,
|
278 |
+
)
|
279 |
+
with gr.Group():
|
280 |
+
use_upscaler = gr.Checkbox(label="Use Upscaler", value=False)
|
281 |
+
with gr.Row() as upscaler_row:
|
282 |
+
upscaler_strength = gr.Slider(
|
283 |
+
label="Strength",
|
284 |
+
minimum=0,
|
285 |
+
maximum=1,
|
286 |
+
step=0.05,
|
287 |
+
value=0.55,
|
288 |
+
visible=False,
|
289 |
+
)
|
290 |
+
upscale_by = gr.Slider(
|
291 |
+
label="Upscale by",
|
292 |
+
minimum=1,
|
293 |
+
maximum=1.5,
|
294 |
+
step=0.1,
|
295 |
+
value=1.5,
|
296 |
+
visible=False,
|
297 |
+
)
|
298 |
+
with gr.Group():
|
299 |
+
sampler = gr.Dropdown(
|
300 |
+
label="Sampler",
|
301 |
+
choices=config.sampler_list,
|
302 |
+
interactive=True,
|
303 |
+
value="Euler a",
|
304 |
+
)
|
305 |
+
with gr.Group():
|
306 |
+
seed = gr.Slider(
|
307 |
+
label="Seed", minimum=0, maximum=utils.MAX_SEED, step=1, value=0
|
308 |
+
)
|
309 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
310 |
+
with gr.Group():
|
311 |
+
with gr.Row():
|
312 |
+
guidance_scale = gr.Slider(
|
313 |
+
label="Guidance scale",
|
314 |
+
minimum=1,
|
315 |
+
maximum=12,
|
316 |
+
step=0.1,
|
317 |
+
value=7.0,
|
318 |
+
)
|
319 |
+
num_inference_steps = gr.Slider(
|
320 |
+
label="Number of inference steps",
|
321 |
+
minimum=1,
|
322 |
+
maximum=50,
|
323 |
+
step=1,
|
324 |
+
value=28,
|
325 |
+
)
|
326 |
+
with gr.Column(scale=3):
|
327 |
+
with gr.Blocks():
|
328 |
+
run_button = gr.Button("Generate", variant="primary")
|
329 |
+
result = gr.Gallery(
|
330 |
+
label="Result",
|
331 |
+
columns=1,
|
332 |
+
height='100%',
|
333 |
+
preview=True,
|
334 |
+
show_label=False
|
335 |
+
)
|
336 |
+
with gr.Accordion(label="Generation Parameters", open=False):
|
337 |
+
gr_metadata = gr.JSON(label="metadata", show_label=False)
|
338 |
+
gr.Examples(
|
339 |
+
examples=config.examples,
|
340 |
+
inputs=prompt,
|
341 |
+
outputs=[result, gr_metadata],
|
342 |
+
fn=lambda *args, **kwargs: generate(*args, use_upscaler=True, **kwargs),
|
343 |
+
cache_examples=CACHE_EXAMPLES,
|
344 |
+
)
|
345 |
+
use_upscaler.change(
|
346 |
+
fn=lambda x: [gr.update(visible=x), gr.update(visible=x)],
|
347 |
+
inputs=use_upscaler,
|
348 |
+
outputs=[upscaler_strength, upscale_by],
|
349 |
+
queue=False,
|
350 |
+
api_name=False,
|
351 |
+
)
|
352 |
+
aspect_ratio_selector.change(
|
353 |
+
fn=lambda x: gr.update(visible=x == "Custom"),
|
354 |
+
inputs=aspect_ratio_selector,
|
355 |
+
outputs=custom_resolution,
|
356 |
+
queue=False,
|
357 |
+
api_name=False,
|
358 |
+
)
|
359 |
+
|
360 |
+
gr.on(
|
361 |
+
triggers=[
|
362 |
+
prompt.submit,
|
363 |
+
negative_prompt.submit,
|
364 |
+
run_button.click,
|
365 |
+
],
|
366 |
+
fn=utils.randomize_seed_fn,
|
367 |
+
inputs=[seed, randomize_seed],
|
368 |
+
outputs=seed,
|
369 |
+
queue=False,
|
370 |
+
api_name=False,
|
371 |
+
).then(
|
372 |
+
fn=generate,
|
373 |
+
inputs=[
|
374 |
+
prompt,
|
375 |
+
negative_prompt,
|
376 |
+
seed,
|
377 |
+
custom_width,
|
378 |
+
custom_height,
|
379 |
+
guidance_scale,
|
380 |
+
num_inference_steps,
|
381 |
+
sampler,
|
382 |
+
aspect_ratio_selector,
|
383 |
+
style_selector,
|
384 |
+
quality_selector,
|
385 |
+
use_upscaler,
|
386 |
+
upscaler_strength,
|
387 |
+
upscale_by,
|
388 |
+
add_quality_tags,
|
389 |
+
],
|
390 |
+
outputs=[result, gr_metadata],
|
391 |
+
api_name="run",
|
392 |
+
)
|
393 |
+
|
394 |
+
if __name__ == "__main__":
|
395 |
+
demo.queue(max_size=20).launch(debug=IS_COLAB, share=IS_COLAB)
|
config.py
ADDED
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
examples = [
|
2 |
+
"1girl, souryuu asuka langley, neon genesis evangelion, plugsuit, pilot suit, red bodysuit, sitting, crossing legs, black eye patch, cat hat, throne, symmetrical, looking down, from bottom, looking at viewer, outdoors",
|
3 |
+
"1boy, male focus, yuuki makoto \(persona 3\), persona 3, black jacket, white shirt, long sleeves, closed mouth, glowing eyes, gun, hair over one eye, holding gun, handgun, looking at viewer, solo, upper body",
|
4 |
+
"1girl, makima \(chainsaw man\), chainsaw man, black jacket, black necktie, black pants, braid, business suit, fingernails, formal, hand on own chin, jacket on shoulders, light smile, long sleeves, looking at viewer, looking up, medium breasts, office lady, smile, solo, suit, upper body, white shirt, outdoors",
|
5 |
+
"1boy, male focus, gojou satoru, jujutsu kaisen, black jacket, blindfold lift, blue eyes, glowing, glowing eyes, high collar, jacket, jujutsu tech uniform, solo, grin, white hair",
|
6 |
+
"1girl, cagliostro, granblue fantasy, violet eyes, standing, hand on own chin, looking at object, smile, closed mouth, table, beaker, glass tube, experiment apparatus, dark room, laboratory",
|
7 |
+
"kimi no na wa., building, cityscape, cloud, cloudy sky, gradient sky, lens flare, no humans, outdoors, power lines, scenery, shooting star, sky, sparkle, star \(sky\), starry sky, sunset, tree, utility pole",
|
8 |
+
]
|
9 |
+
|
10 |
+
quality_prompt_list = [
|
11 |
+
{
|
12 |
+
"name": "(None)",
|
13 |
+
"prompt": "{prompt}",
|
14 |
+
"negative_prompt": "nsfw, lowres",
|
15 |
+
},
|
16 |
+
{
|
17 |
+
"name": "Standard v3.0",
|
18 |
+
"prompt": "{prompt}, masterpiece, best quality",
|
19 |
+
"negative_prompt": "nsfw, lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry, artist name",
|
20 |
+
},
|
21 |
+
{
|
22 |
+
"name": "Standard v3.1",
|
23 |
+
"prompt": "{prompt}, masterpiece, best quality, very aesthetic, absurdres",
|
24 |
+
"negative_prompt": "nsfw, lowres, (bad), text, error, fewer, extra, missing, worst quality, jpeg artifacts, low quality, watermark, unfinished, displeasing, oldest, early, chromatic aberration, signature, extra digits, artistic error, username, scan, [abstract]",
|
25 |
+
},
|
26 |
+
{
|
27 |
+
"name": "Light v3.1",
|
28 |
+
"prompt": "{prompt}, (masterpiece), best quality, very aesthetic, perfect face",
|
29 |
+
"negative_prompt": "nsfw, (low quality, worst quality:1.2), very displeasing, 3d, watermark, signature, ugly, poorly drawn",
|
30 |
+
},
|
31 |
+
{
|
32 |
+
"name": "Heavy v3.1",
|
33 |
+
"prompt": "{prompt}, (masterpiece), (best quality), (ultra-detailed), very aesthetic, illustration, disheveled hair, perfect composition, moist skin, intricate details",
|
34 |
+
"negative_prompt": "nsfw, longbody, lowres, bad anatomy, bad hands, missing fingers, pubic hair, extra digit, fewer digits, cropped, worst quality, low quality, very displeasing",
|
35 |
+
},
|
36 |
+
]
|
37 |
+
|
38 |
+
sampler_list = [
|
39 |
+
"DPM++ 2M Karras",
|
40 |
+
"DPM++ SDE Karras",
|
41 |
+
"DPM++ 2M SDE Karras",
|
42 |
+
"Euler",
|
43 |
+
"Euler a",
|
44 |
+
"DDIM",
|
45 |
+
]
|
46 |
+
|
47 |
+
aspect_ratios = [
|
48 |
+
"1024 x 1024",
|
49 |
+
"1152 x 896",
|
50 |
+
"896 x 1152",
|
51 |
+
"1216 x 832",
|
52 |
+
"832 x 1216",
|
53 |
+
"1344 x 768",
|
54 |
+
"768 x 1344",
|
55 |
+
"1536 x 640",
|
56 |
+
"640 x 1536",
|
57 |
+
"Custom",
|
58 |
+
]
|
59 |
+
|
60 |
+
style_list = [
|
61 |
+
{
|
62 |
+
"name": "(None)",
|
63 |
+
"prompt": "{prompt}",
|
64 |
+
"negative_prompt": "",
|
65 |
+
},
|
66 |
+
{
|
67 |
+
"name": "Cinematic",
|
68 |
+
"prompt": "{prompt}, cinematic still, emotional, harmonious, vignette, highly detailed, high budget, bokeh, cinemascope, moody, epic, gorgeous, film grain, grainy",
|
69 |
+
"negative_prompt": "nsfw, cartoon, graphic, text, painting, crayon, graphite, abstract, glitch, deformed, mutated, ugly, disfigured",
|
70 |
+
},
|
71 |
+
{
|
72 |
+
"name": "Photographic",
|
73 |
+
"prompt": "{prompt}, cinematic photo, 35mm photograph, film, bokeh, professional, 4k, highly detailed",
|
74 |
+
"negative_prompt": "nsfw, drawing, painting, crayon, sketch, graphite, impressionist, noisy, blurry, soft, deformed, ugly",
|
75 |
+
},
|
76 |
+
{
|
77 |
+
"name": "Anime",
|
78 |
+
"prompt": "{prompt}, anime artwork, anime style, key visual, vibrant, studio anime, highly detailed",
|
79 |
+
"negative_prompt": "nsfw, photo, deformed, black and white, realism, disfigured, low contrast",
|
80 |
+
},
|
81 |
+
{
|
82 |
+
"name": "Manga",
|
83 |
+
"prompt": "{prompt}, manga style, vibrant, high-energy, detailed, iconic, Japanese comic style",
|
84 |
+
"negative_prompt": "nsfw, ugly, deformed, noisy, blurry, low contrast, realism, photorealistic, Western comic style",
|
85 |
+
},
|
86 |
+
{
|
87 |
+
"name": "Digital Art",
|
88 |
+
"prompt": "{prompt}, concept art, digital artwork, illustrative, painterly, matte painting, highly detailed",
|
89 |
+
"negative_prompt": "nsfw, photo, photorealistic, realism, ugly",
|
90 |
+
},
|
91 |
+
{
|
92 |
+
"name": "Pixel art",
|
93 |
+
"prompt": "{prompt}, pixel-art, low-res, blocky, pixel art style, 8-bit graphics",
|
94 |
+
"negative_prompt": "nsfw, sloppy, messy, blurry, noisy, highly detailed, ultra textured, photo, realistic",
|
95 |
+
},
|
96 |
+
{
|
97 |
+
"name": "Fantasy art",
|
98 |
+
"prompt": "{prompt}, ethereal fantasy concept art, magnificent, celestial, ethereal, painterly, epic, majestic, magical, fantasy art, cover art, dreamy",
|
99 |
+
"negative_prompt": "nsfw, photographic, realistic, realism, 35mm film, dslr, cropped, frame, text, deformed, glitch, noise, noisy, off-center, deformed, cross-eyed, closed eyes, bad anatomy, ugly, disfigured, sloppy, duplicate, mutated, black and white",
|
100 |
+
},
|
101 |
+
{
|
102 |
+
"name": "Neonpunk",
|
103 |
+
"prompt": "{prompt}, neonpunk style, cyberpunk, vaporwave, neon, vibes, vibrant, stunningly beautiful, crisp, detailed, sleek, ultramodern, magenta highlights, dark purple shadows, high contrast, cinematic, ultra detailed, intricate, professional",
|
104 |
+
"negative_prompt": "nsfw, painting, drawing, illustration, glitch, deformed, mutated, cross-eyed, ugly, disfigured",
|
105 |
+
},
|
106 |
+
{
|
107 |
+
"name": "3D Model",
|
108 |
+
"prompt": "{prompt}, professional 3d model, octane render, highly detailed, volumetric, dramatic lighting",
|
109 |
+
"negative_prompt": "nsfw, ugly, deformed, noisy, low poly, blurry, painting",
|
110 |
+
},
|
111 |
+
]
|
requirements.txt
CHANGED
@@ -1,6 +1,10 @@
|
|
1 |
-
accelerate
|
2 |
-
diffusers
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
|
|
|
|
|
|
|
|
|
1 |
+
accelerate==0.27.2
|
2 |
+
diffusers==0.26.3
|
3 |
+
gradio==4.20.0
|
4 |
+
invisible-watermark==0.2.0
|
5 |
+
Pillow==10.2.0
|
6 |
+
spaces==0.24.0
|
7 |
+
torch==2.0.1
|
8 |
+
transformers==4.38.1
|
9 |
+
omegaconf==2.3.0
|
10 |
+
timm==0.9.10
|
style.css
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
:root {
|
2 |
+
--title-font-size: clamp(1.5rem, 6vw, 3rem);
|
3 |
+
--subtitle-font-size: clamp(1rem, 2vw, 1.2rem);
|
4 |
+
}
|
5 |
+
|
6 |
+
h1 {
|
7 |
+
text-align: center;
|
8 |
+
font-size: var(--title-font-size);
|
9 |
+
display: block;
|
10 |
+
}
|
11 |
+
|
12 |
+
h2 {
|
13 |
+
text-align: center;
|
14 |
+
font-size: 2rem;
|
15 |
+
display: block;
|
16 |
+
}
|
17 |
+
|
18 |
+
#duplicate-button {
|
19 |
+
display: block;
|
20 |
+
margin: 1rem auto;
|
21 |
+
color: #fff;
|
22 |
+
background: #1565c0;
|
23 |
+
border-radius: 100vh;
|
24 |
+
padding: 0.5rem 1rem;
|
25 |
+
}
|
26 |
+
|
27 |
+
#component-0 {
|
28 |
+
max-width: 85%;
|
29 |
+
margin: 2rem auto;
|
30 |
+
padding: 2rem;
|
31 |
+
}
|
32 |
+
|
33 |
+
@media (max-width: 600px) {
|
34 |
+
#component-0 {
|
35 |
+
max-width: 100%;
|
36 |
+
padding: 0.5rem;
|
37 |
+
}
|
38 |
+
}
|
39 |
+
|
40 |
+
#title-container {
|
41 |
+
text-align: center;
|
42 |
+
padding: 2rem 0;
|
43 |
+
}
|
44 |
+
|
45 |
+
#title {
|
46 |
+
font-size: var(--title-font-size);
|
47 |
+
color: #333;
|
48 |
+
font-family: 'Helvetica Neue', sans-serif;
|
49 |
+
text-transform: uppercase;
|
50 |
+
background: transparent;
|
51 |
+
}
|
52 |
+
|
53 |
+
#title span {
|
54 |
+
background: linear-gradient(45deg, #4EACEF, #28b485);
|
55 |
+
background-clip: text;
|
56 |
+
color: transparent;
|
57 |
+
}
|
58 |
+
|
59 |
+
#subtitle {
|
60 |
+
text-align: center;
|
61 |
+
font-size: var(--subtitle-font-size);
|
62 |
+
margin-top: 1rem;
|
63 |
+
}
|
utils.py
ADDED
@@ -0,0 +1,182 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gc
|
2 |
+
import os
|
3 |
+
import random
|
4 |
+
import numpy as np
|
5 |
+
import json
|
6 |
+
import torch
|
7 |
+
import uuid
|
8 |
+
from PIL import Image, PngImagePlugin
|
9 |
+
from datetime import datetime
|
10 |
+
from dataclasses import dataclass
|
11 |
+
from typing import Callable, Dict, Optional, Tuple
|
12 |
+
from diffusers import (
|
13 |
+
DDIMScheduler,
|
14 |
+
DPMSolverMultistepScheduler,
|
15 |
+
DPMSolverSinglestepScheduler,
|
16 |
+
EulerAncestralDiscreteScheduler,
|
17 |
+
EulerDiscreteScheduler,
|
18 |
+
)
|
19 |
+
|
20 |
+
MAX_SEED = np.iinfo(np.int32).max
|
21 |
+
|
22 |
+
|
23 |
+
@dataclass
|
24 |
+
class StyleConfig:
|
25 |
+
prompt: str
|
26 |
+
negative_prompt: str
|
27 |
+
|
28 |
+
|
29 |
+
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
30 |
+
if randomize_seed:
|
31 |
+
seed = random.randint(0, MAX_SEED)
|
32 |
+
return seed
|
33 |
+
|
34 |
+
|
35 |
+
def seed_everything(seed: int) -> torch.Generator:
|
36 |
+
torch.manual_seed(seed)
|
37 |
+
torch.cuda.manual_seed_all(seed)
|
38 |
+
np.random.seed(seed)
|
39 |
+
generator = torch.Generator()
|
40 |
+
generator.manual_seed(seed)
|
41 |
+
return generator
|
42 |
+
|
43 |
+
|
44 |
+
def parse_aspect_ratio(aspect_ratio: str) -> Optional[Tuple[int, int]]:
|
45 |
+
if aspect_ratio == "Custom":
|
46 |
+
return None
|
47 |
+
width, height = aspect_ratio.split(" x ")
|
48 |
+
return int(width), int(height)
|
49 |
+
|
50 |
+
|
51 |
+
def aspect_ratio_handler(
|
52 |
+
aspect_ratio: str, custom_width: int, custom_height: int
|
53 |
+
) -> Tuple[int, int]:
|
54 |
+
if aspect_ratio == "Custom":
|
55 |
+
return custom_width, custom_height
|
56 |
+
else:
|
57 |
+
width, height = parse_aspect_ratio(aspect_ratio)
|
58 |
+
return width, height
|
59 |
+
|
60 |
+
|
61 |
+
def get_scheduler(scheduler_config: Dict, name: str) -> Optional[Callable]:
|
62 |
+
scheduler_factory_map = {
|
63 |
+
"DPM++ 2M Karras": lambda: DPMSolverMultistepScheduler.from_config(
|
64 |
+
scheduler_config, use_karras_sigmas=True
|
65 |
+
),
|
66 |
+
"DPM++ SDE Karras": lambda: DPMSolverSinglestepScheduler.from_config(
|
67 |
+
scheduler_config, use_karras_sigmas=True
|
68 |
+
),
|
69 |
+
"DPM++ 2M SDE Karras": lambda: DPMSolverMultistepScheduler.from_config(
|
70 |
+
scheduler_config, use_karras_sigmas=True, algorithm_type="sde-dpmsolver++"
|
71 |
+
),
|
72 |
+
"Euler": lambda: EulerDiscreteScheduler.from_config(scheduler_config),
|
73 |
+
"Euler a": lambda: EulerAncestralDiscreteScheduler.from_config(
|
74 |
+
scheduler_config
|
75 |
+
),
|
76 |
+
"DDIM": lambda: DDIMScheduler.from_config(scheduler_config),
|
77 |
+
}
|
78 |
+
return scheduler_factory_map.get(name, lambda: None)()
|
79 |
+
|
80 |
+
|
81 |
+
def free_memory() -> None:
|
82 |
+
torch.cuda.empty_cache()
|
83 |
+
gc.collect()
|
84 |
+
|
85 |
+
|
86 |
+
def preprocess_prompt(
|
87 |
+
style_dict,
|
88 |
+
style_name: str,
|
89 |
+
positive: str,
|
90 |
+
negative: str = "",
|
91 |
+
add_style: bool = True,
|
92 |
+
) -> Tuple[str, str]:
|
93 |
+
p, n = style_dict.get(style_name, style_dict["(None)"])
|
94 |
+
|
95 |
+
if add_style and positive.strip():
|
96 |
+
formatted_positive = p.format(prompt=positive)
|
97 |
+
else:
|
98 |
+
formatted_positive = positive
|
99 |
+
|
100 |
+
combined_negative = n
|
101 |
+
if negative.strip():
|
102 |
+
if combined_negative:
|
103 |
+
combined_negative += ", " + negative
|
104 |
+
else:
|
105 |
+
combined_negative = negative
|
106 |
+
|
107 |
+
return formatted_positive, combined_negative
|
108 |
+
|
109 |
+
|
110 |
+
def common_upscale(
|
111 |
+
samples: torch.Tensor,
|
112 |
+
width: int,
|
113 |
+
height: int,
|
114 |
+
upscale_method: str,
|
115 |
+
) -> torch.Tensor:
|
116 |
+
return torch.nn.functional.interpolate(
|
117 |
+
samples, size=(height, width), mode=upscale_method
|
118 |
+
)
|
119 |
+
|
120 |
+
|
121 |
+
def upscale(
|
122 |
+
samples: torch.Tensor, upscale_method: str, scale_by: float
|
123 |
+
) -> torch.Tensor:
|
124 |
+
width = round(samples.shape[3] * scale_by)
|
125 |
+
height = round(samples.shape[2] * scale_by)
|
126 |
+
return common_upscale(samples, width, height, upscale_method)
|
127 |
+
|
128 |
+
|
129 |
+
def load_wildcard_files(wildcard_dir: str) -> Dict[str, str]:
|
130 |
+
wildcard_files = {}
|
131 |
+
for file in os.listdir(wildcard_dir):
|
132 |
+
if file.endswith(".txt"):
|
133 |
+
key = f"__{file.split('.')[0]}__" # Create a key like __character__
|
134 |
+
wildcard_files[key] = os.path.join(wildcard_dir, file)
|
135 |
+
return wildcard_files
|
136 |
+
|
137 |
+
|
138 |
+
def get_random_line_from_file(file_path: str) -> str:
|
139 |
+
with open(file_path, "r") as file:
|
140 |
+
lines = file.readlines()
|
141 |
+
if not lines:
|
142 |
+
return ""
|
143 |
+
return random.choice(lines).strip()
|
144 |
+
|
145 |
+
|
146 |
+
def add_wildcard(prompt: str, wildcard_files: Dict[str, str]) -> str:
|
147 |
+
for key, file_path in wildcard_files.items():
|
148 |
+
if key in prompt:
|
149 |
+
wildcard_line = get_random_line_from_file(file_path)
|
150 |
+
prompt = prompt.replace(key, wildcard_line)
|
151 |
+
return prompt
|
152 |
+
|
153 |
+
|
154 |
+
def preprocess_image_dimensions(width, height):
|
155 |
+
if width % 8 != 0:
|
156 |
+
width = width - (width % 8)
|
157 |
+
if height % 8 != 0:
|
158 |
+
height = height - (height % 8)
|
159 |
+
return width, height
|
160 |
+
|
161 |
+
|
162 |
+
def save_image(image, metadata, output_dir, is_colab):
|
163 |
+
if is_colab:
|
164 |
+
current_time = datetime.now().strftime("%Y%m%d_%H%M%S")
|
165 |
+
filename = f"image_{current_time}.png"
|
166 |
+
else:
|
167 |
+
filename = str(uuid.uuid4()) + ".png"
|
168 |
+
os.makedirs(output_dir, exist_ok=True)
|
169 |
+
filepath = os.path.join(output_dir, filename)
|
170 |
+
metadata_str = json.dumps(metadata)
|
171 |
+
info = PngImagePlugin.PngInfo()
|
172 |
+
info.add_text("metadata", metadata_str)
|
173 |
+
image.save(filepath, "PNG", pnginfo=info)
|
174 |
+
return filepath
|
175 |
+
|
176 |
+
|
177 |
+
def is_google_colab():
|
178 |
+
try:
|
179 |
+
import google.colab
|
180 |
+
return True
|
181 |
+
except:
|
182 |
+
return False
|