Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from diffusers import StableDiffusionXLPipeline, DDIMScheduler
|
3 |
+
import torch
|
4 |
+
import sa_handler
|
5 |
+
import inversion
|
6 |
+
import numpy as np
|
7 |
+
from diffusers.utils import load_image
|
8 |
+
from PIL import Image
|
9 |
+
import io
|
10 |
+
|
11 |
+
# Model Load
|
12 |
+
scheduler = DDIMScheduler(
|
13 |
+
beta_start=0.00085, beta_end=0.012, beta_schedule="scaled_linear",
|
14 |
+
clip_sample=False, set_alpha_to_one=False)
|
15 |
+
|
16 |
+
pipeline = StableDiffusionXLPipeline.from_pretrained(
|
17 |
+
"stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16, variant="fp16",
|
18 |
+
use_safetensors=True,
|
19 |
+
scheduler=scheduler
|
20 |
+
).to("cuda")
|
21 |
+
|
22 |
+
# Function to process the image
|
23 |
+
def process_image(image, prompt, style):
|
24 |
+
src_prompt = f'Man laying in a bed, {style}.'
|
25 |
+
|
26 |
+
num_inference_steps = 50
|
27 |
+
x0 = np.array(Image.fromarray(image).resize((1024, 1024)))
|
28 |
+
zts = inversion.ddim_inversion(pipeline, x0, src_prompt, num_inference_steps, 2)
|
29 |
+
|
30 |
+
prompts = [
|
31 |
+
src_prompt,
|
32 |
+
f"{prompt}, {style}."
|
33 |
+
]
|
34 |
+
|
35 |
+
shared_score_shift = np.log(2)
|
36 |
+
shared_score_scale = 1.0
|
37 |
+
|
38 |
+
handler = sa_handler.Handler(pipeline)
|
39 |
+
sa_args = sa_handler.StyleAlignedArgs(
|
40 |
+
share_group_norm=True, share_layer_norm=True, share_attention=True,
|
41 |
+
adain_queries=True, adain_keys=True, adain_values=False,
|
42 |
+
shared_score_shift=shared_score_shift, shared_score_scale=shared_score_scale,)
|
43 |
+
handler.register(sa_args)
|
44 |
+
|
45 |
+
zT, inversion_callback = inversion.make_inversion_callback(zts, offset=5)
|
46 |
+
|
47 |
+
g_cpu = torch.Generator(device='cpu')
|
48 |
+
g_cpu.manual_seed(10)
|
49 |
+
|
50 |
+
latents = torch.randn(len(prompts), 4, 128, 128, device='cpu', generator=g_cpu,
|
51 |
+
dtype=pipeline.unet.dtype,).to('cuda:0')
|
52 |
+
latents[0] = zT
|
53 |
+
|
54 |
+
images_a = pipeline(prompts, latents=latents,
|
55 |
+
callback_on_step_end=inversion_callback,
|
56 |
+
num_inference_steps=num_inference_steps, guidance_scale=10.0).images
|
57 |
+
|
58 |
+
handler.remove()
|
59 |
+
|
60 |
+
return Image.fromarray(images_a[1])
|
61 |
+
|
62 |
+
# Gradio interface
|
63 |
+
iface = gr.Interface(
|
64 |
+
fn=process_image,
|
65 |
+
inputs=[
|
66 |
+
gr.inputs.Image(type="numpy"),
|
67 |
+
gr.inputs.Textbox(label="Enter your prompt"),
|
68 |
+
gr.inputs.Textbox(label="Enter your style", default="medieval painting")
|
69 |
+
],
|
70 |
+
outputs="image",
|
71 |
+
title="Stable Diffusion XL with Style Alignment",
|
72 |
+
description="Generate images in the style of your choice."
|
73 |
+
)
|
74 |
+
|
75 |
+
iface.launch()
|