Tonic commited on
Commit
488aaf5
β€’
1 Parent(s): dcec7a4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -53
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
- # Function to process the image
24
- def process_image(image, prompt, style, src_description, inference_steps, shared_score_shift, shared_score_scale, guidance_scale):
25
- src_prompt = f'{src_description}, {style}.'
26
 
27
- num_inference_steps = inference_steps
28
- x0 = np.array(Image.fromarray(image).resize((1024, 1024)))
 
 
 
29
  zts = inversion.ddim_inversion(pipeline, x0, src_prompt, num_inference_steps, 2)
 
30
 
31
- prompts = [
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
- zT, inversion_callback = inversion.make_inversion_callback(zts, offset=5)
 
44
 
 
45
  g_cpu = torch.Generator(device='cpu')
46
- g_cpu.manual_seed(10)
47
-
48
- latents = torch.randn(len(prompts), 4, 128, 128, device='cpu', generator=g_cpu,
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
- return Image.fromarray(images_a[1])
59
-
60
- iface = gr.Interface(
61
- fn=process_image,
62
- inputs=[
63
- gr.Image(type="numpy"),
64
- gr.Textbox(label="Enter your prompt"),
65
- gr.Textbox(label="Enter your style", value="medieval painting"),
66
- gr.Textbox(label="Enter source description", value="Man laying in a bed"),
67
- gr.Slider(minimum=5, maximum=50, step=1, value=50, label="Number of Inference Steps"),
68
- gr.Slider(minimum=1, maximum=2, step=0.01, value=1.5, label="Shared Score Shift"),
69
- gr.Slider(minimum=0, maximum=1, step=0.01, value=0.5, label="Shared Score Scale"),
70
- gr.Slider(minimum=5, maximum=120, step=1, value=10, label="Guidance Scale")
71
- ],
72
- outputs="image",
73
- title="Stable Diffusion XL with Style Alignment",
74
- description="Generate images in the style of your choice."
75
- )
 
 
 
76
 
77
- iface.launch()
 
 
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()