first commit
Browse files
app.py
ADDED
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import random
|
4 |
+
import torch
|
5 |
+
|
6 |
+
from diffusers import StableDiffusionXLPipeline, AutoencoderKL
|
7 |
+
from utils import randomize_seed_fn
|
8 |
+
|
9 |
+
MAX_SEED = np.iinfo(np.int32).max
|
10 |
+
|
11 |
+
def model_load():
|
12 |
+
vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
|
13 |
+
pipe = StableDiffusionXLPipeline.from_pretrained(
|
14 |
+
"stabilityai/stable-diffusion-xl-base-1.0", vae=vae, torch_dtype=torch.float16
|
15 |
+
)
|
16 |
+
# load lora weight
|
17 |
+
pipe.load_lora_weights("jjuun/vivid_color_style")
|
18 |
+
|
19 |
+
return pipe.to('cuda')
|
20 |
+
|
21 |
+
|
22 |
+
def sdxl_process(seed, prompt, additional_prompt, negative_prompt, num_steps, guidance_scale):
|
23 |
+
pipe = model_load()
|
24 |
+
generator = torch.Generator("cuda")
|
25 |
+
generator.manual_seed(seed)
|
26 |
+
|
27 |
+
special_prompt = 'jjj, scratch art style'
|
28 |
+
prompt = f'{special_prompt}, {prompt}, with a black background'
|
29 |
+
output = pipe(prompt, additional_prompt, negative_prompt=negative_prompt, num_inference_steps=num_steps, guidance_scale=guidance_scale,
|
30 |
+
generator=generator).images[0]
|
31 |
+
|
32 |
+
return output
|
33 |
+
|
34 |
+
|
35 |
+
title = "🌈 Colorful illustration"
|
36 |
+
description_en = "🚀 How to use: please make sure to include 'a colorful' in prompt and click Run button!"
|
37 |
+
|
38 |
+
|
39 |
+
def create_demo():
|
40 |
+
|
41 |
+
with gr.Blocks() as demo:
|
42 |
+
gr.Markdown(f"<h1 style='text-align: center;'>{title}</h1>")
|
43 |
+
gr.Markdown(f"<h3 style='text-align: center'>{description_en}</h3>")
|
44 |
+
|
45 |
+
with gr.Row():
|
46 |
+
with gr.Column():
|
47 |
+
prompt = gr.Textbox(label="Prompt")
|
48 |
+
run_button = gr.Button("Run")
|
49 |
+
with gr.Accordion("Advanced options", open=False):
|
50 |
+
|
51 |
+
num_steps = gr.Slider(label="Number of steps", minimum=1, maximum=100, value=20, step=1)
|
52 |
+
guidance_scale = gr.Slider(label="Guidance scale", minimum=0.1, maximum=30.0, value=9.0, step=0.1)
|
53 |
+
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
54 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
55 |
+
a_prompt = gr.Textbox(label="Additional prompt", value="")
|
56 |
+
n_prompt = gr.Textbox(
|
57 |
+
label="Negative prompt",
|
58 |
+
value="longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality",
|
59 |
+
)
|
60 |
+
with gr.Column():
|
61 |
+
result = gr.Image(label="Output")
|
62 |
+
result_seed = gr.Textbox(label="Used seed")
|
63 |
+
|
64 |
+
gr.Examples(
|
65 |
+
|
66 |
+
examples= [["a colorful lion", "20", "9", "0", "", "longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality", "examples/lion.png"],
|
67 |
+
["a colorful messi", "20", "9", "0", "", "longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality", "examples/messi.png"]],
|
68 |
+
inputs = [prompt, num_steps, guidance_scale, seed, a_prompt, n_prompt, result]
|
69 |
+
)
|
70 |
+
|
71 |
+
inputs = [
|
72 |
+
seed,
|
73 |
+
prompt,
|
74 |
+
a_prompt,
|
75 |
+
n_prompt,
|
76 |
+
num_steps,
|
77 |
+
guidance_scale,
|
78 |
+
]
|
79 |
+
|
80 |
+
run_button.click(
|
81 |
+
fn=randomize_seed_fn,
|
82 |
+
inputs=[seed, randomize_seed],
|
83 |
+
outputs=result_seed,
|
84 |
+
queue=False,
|
85 |
+
api_name=False,
|
86 |
+
).then(
|
87 |
+
fn=sdxl_process,
|
88 |
+
inputs=inputs,
|
89 |
+
outputs=result,
|
90 |
+
api_name=False,
|
91 |
+
)
|
92 |
+
|
93 |
+
|
94 |
+
return demo
|
95 |
+
|
96 |
+
|
97 |
+
if __name__ == "__main__":
|
98 |
+
demo = create_demo()
|
99 |
+
demo.queue().launch()
|
utils.py
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import random
|
3 |
+
import torch
|
4 |
+
import os
|
5 |
+
|
6 |
+
|
7 |
+
def seed_everything(seed: int = 42):
|
8 |
+
random.seed(seed)
|
9 |
+
np.random.seed(seed)
|
10 |
+
os.environ["PYTHONHASHSEED"] = str(seed)
|
11 |
+
torch.manual_seed(seed)
|
12 |
+
torch.cuda.manual_seed(seed) # type: ignore
|
13 |
+
torch.backends.cudnn.deterministic = True # type: ignore
|
14 |
+
torch.backends.cudnn.benchmark = True # type: ignore
|
15 |
+
|
16 |
+
|
17 |
+
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
18 |
+
MAX_SEED = np.iinfo(np.int32).max
|
19 |
+
|
20 |
+
if randomize_seed:
|
21 |
+
seed = random.randint(0, MAX_SEED)
|
22 |
+
seed_everything(seed)
|
23 |
+
return seed
|