Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -1,38 +1,24 @@
|
|
1 |
-
import gradio as gr
|
2 |
from diffusers import StableDiffusionXLPipeline, DDIMScheduler
|
3 |
import torch
|
|
|
|
|
4 |
import numpy as np
|
5 |
from PIL import Image
|
6 |
-
import io
|
7 |
-
import sys
|
8 |
-
import os
|
9 |
import sa_handler
|
10 |
-
import inversion
|
11 |
-
|
12 |
-
# Model Load
|
13 |
-
scheduler = DDIMScheduler(
|
14 |
-
beta_start=0.00085, beta_end=0.012, beta_schedule="scaled_linear",
|
15 |
-
clip_sample=False, set_alpha_to_one=False)
|
16 |
-
|
17 |
-
pipeline = StableDiffusionXLPipeline.from_pretrained(
|
18 |
-
"stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16, variant="fp16",
|
19 |
-
use_safetensors=True,
|
20 |
-
scheduler=scheduler
|
21 |
-
).to("cuda")
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
29 |
zts = inversion.ddim_inversion(pipeline, x0, src_prompt, num_inference_steps, 2)
|
|
|
30 |
|
31 |
-
|
32 |
-
src_prompt,
|
33 |
-
f"{prompt}, {style}."
|
34 |
-
]
|
35 |
-
|
36 |
handler = sa_handler.Handler(pipeline)
|
37 |
sa_args = sa_handler.StyleAlignedArgs(
|
38 |
share_group_norm=True, share_layer_norm=True, share_attention=True,
|
@@ -40,38 +26,43 @@ def process_image(image, prompt, style, src_description, inference_steps, shared
|
|
40 |
shared_score_shift=shared_score_shift, shared_score_scale=shared_score_scale,)
|
41 |
handler.register(sa_args)
|
42 |
|
43 |
-
|
|
|
44 |
|
|
|
45 |
g_cpu = torch.Generator(device='cpu')
|
46 |
-
|
47 |
-
|
48 |
-
latents = torch.randn(len(prompts), 4,
|
49 |
-
dtype=pipeline.unet.dtype,).to('cuda:0')
|
50 |
latents[0] = zT
|
51 |
|
52 |
-
images_a = pipeline(prompts, latents=latents,
|
53 |
-
callback_on_step_end=inversion_callback,
|
54 |
-
num_inference_steps=num_inference_steps, guidance_scale=guidance_scale).images
|
55 |
|
56 |
handler.remove()
|
|
|
|
|
|
|
57 |
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
gr.
|
65 |
-
|
66 |
-
gr.Textbox(label="
|
67 |
-
gr.
|
68 |
-
gr.
|
69 |
-
|
70 |
-
gr.
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
)
|
|
|
|
|
|
|
76 |
|
77 |
-
|
|
|
|
|
1 |
from diffusers import StableDiffusionXLPipeline, DDIMScheduler
|
2 |
import torch
|
3 |
+
import gradio as gr
|
4 |
+
import inversion
|
5 |
import numpy as np
|
6 |
from PIL import Image
|
|
|
|
|
|
|
7 |
import sa_handler
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
10 |
+
scheduler = DDIMScheduler(beta_start=0.00085, beta_end=0.012, beta_schedule="scaled_linear", clip_sample=False, set_alpha_to_one=False)
|
11 |
+
pipeline = StableDiffusionXLPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16, variant="fp16", use_safetensors=True, scheduler=scheduler).to(device)
|
12 |
|
13 |
+
def run(image, src_style, src_prompt, prompts, shared_score_shift, shared_score_scale, guidance_scale, num_inference_steps, large, seed):
|
14 |
+
prompts = prompts.splitlines()
|
15 |
+
dim, d = (1024, 128) if large else (512, 64)
|
16 |
+
image = image.resize((dim, dim))
|
17 |
+
x0 = np.array(image)
|
18 |
zts = inversion.ddim_inversion(pipeline, x0, src_prompt, num_inference_steps, 2)
|
19 |
+
prompts.insert(0, src_prompt)
|
20 |
|
21 |
+
shared_score_shift = np.log(shared_score_shift)
|
|
|
|
|
|
|
|
|
22 |
handler = sa_handler.Handler(pipeline)
|
23 |
sa_args = sa_handler.StyleAlignedArgs(
|
24 |
share_group_norm=True, share_layer_norm=True, share_attention=True,
|
|
|
26 |
shared_score_shift=shared_score_shift, shared_score_scale=shared_score_scale,)
|
27 |
handler.register(sa_args)
|
28 |
|
29 |
+
for i in range(1, len(prompts)):
|
30 |
+
prompts[i] = f'{prompts[i]}, {src_style}.'
|
31 |
|
32 |
+
zT, inversion_callback = inversion.make_inversion_callback(zts, offset=5)
|
33 |
g_cpu = torch.Generator(device='cpu')
|
34 |
+
if seed > 0:
|
35 |
+
g_cpu.manual_seed(seed)
|
36 |
+
latents = torch.randn(len(prompts), 4, d, d, device='cpu', generator=g_cpu, dtype=pipeline.unet.dtype,).to(device)
|
|
|
37 |
latents[0] = zT
|
38 |
|
39 |
+
images_a = pipeline(prompts, latents=latents, callback_on_step_end=inversion_callback, num_inference_steps=num_inference_steps, guidance_scale=guidance_scale).images
|
|
|
|
|
40 |
|
41 |
handler.remove()
|
42 |
+
torch.cuda.empty_cache()
|
43 |
+
images_pil = [Image.fromarray((img * 255).astype(np.uint8)) for img in images_a]
|
44 |
+
return images_pil
|
45 |
|
46 |
+
with gr.Blocks() as demo:
|
47 |
+
with gr.Markdown("""
|
48 |
+
# Welcome to Tonic's Stable Style Align
|
49 |
+
Add a reference picture , describe the style and add prompts to generate images in that style. It's the most interesting with your own art !
|
50 |
+
""")
|
51 |
+
with gr.Row():
|
52 |
+
gr.Image(label="Reference image", type="pil")
|
53 |
+
with gr.Row():
|
54 |
+
gr.Textbox(label="Describe the reference style")
|
55 |
+
gr.Textbox(label="Describe the reference image")
|
56 |
+
gr.Textbox(label="Prompts to generate images (separate with new lines)", lines=5)
|
57 |
+
with gr.Accordion(label="Advanced Settings"):
|
58 |
+
with gr.Row():
|
59 |
+
gr.Number(value=1.1, label="shared_score_shift", min=1.0, max=2.0)
|
60 |
+
gr.Number(value=1.0, label="shared_score_scale", min=0.0, max=1.0)
|
61 |
+
gr.Number(value=10.0, label="guidance_scale", min=5.0, max=20.0)
|
62 |
+
gr.Number(value=50, label="num_inference_steps", min=1, max=100, precision=0)
|
63 |
+
gr.Checkbox(False, label="Large (1024x1024)")
|
64 |
+
gr.Number(value=0, label="seed (0 for random)", min=0, max=10000, precision=0)
|
65 |
+
with gr.Row():
|
66 |
+
gr.Gallery()
|
67 |
|
68 |
+
demo.launch()
|