File size: 6,486 Bytes
3d91128
d8ca99b
49825df
1b23e89
 
56b27fc
1b23e89
36f5062
74c2aa0
36f5062
1b23e89
 
 
74c2aa0
 
 
 
49825df
 
 
 
11e01c6
1b23e89
8a3216f
 
3752ce0
 
1b23e89
 
fcae3d8
1b23e89
 
 
 
 
 
 
 
 
 
 
 
11e01c6
74c2aa0
8a3216f
 
74c2aa0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11e01c6
960c701
 
c119f33
bfdcbb9
6283028
 
 
960c701
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a6578b
960c701
74c2aa0
d8ca99b
0684317
 
 
 
 
 
 
1b23e89
8e3317d
0684317
 
1b23e89
 
8e3317d
0684317
 
1b23e89
c119f33
8e3317d
0684317
1b23e89
2edf8a1
1b23e89
7dd41a0
91f11fc
3752ce0
 
0d1a2b5
91f11fc
 
2743d6e
1b23e89
f5813b3
 
a2646c8
f5813b3
 
3978bc7
c8453fc
f5813b3
923c66b
2c48c04
 
b4f9e48
2c48c04
 
3978bc7
2c48c04
 
eacbc49
2c48c04
1b23e89
74c2aa0
5a6578b
5753d99
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import spaces
import gradio as gr
from diffusers import AutoPipelineForText2Image, AutoPipelineForImage2Image, AutoPipelineForInpainting, AutoencoderKL
from diffusers.utils import load_image
import torch
from PIL import Image, ImageOps

vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)

text_pipeline = AutoPipelineForText2Image.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", vae=vae, torch_dtype=torch.float16, variant="fp16", use_safetensors=True).to("cuda")
text_pipeline.load_ip_adapter("h94/IP-Adapter", subfolder="sdxl_models", weight_name="ip-adapter_sdxl.bin")
text_pipeline.set_ip_adapter_scale(0.6)

image_pipeline = AutoPipelineForImage2Image.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", vae=vae, torch_dtype=torch.float16, variant="fp16", use_safetensors=True).to("cuda")
image_pipeline.load_ip_adapter("h94/IP-Adapter", subfolder="sdxl_models", weight_name="ip-adapter_sdxl.bin")
image_pipeline.set_ip_adapter_scale(0.6)

inpaint_pipeline = AutoPipelineForInpainting.from_pretrained("diffusers/stable-diffusion-xl-1.0-inpainting-0.1", vae=vae, torch_dtype=torch.float16, variant="fp16", use_safetensors=True).to("cuda")
inpaint_pipeline.load_ip_adapter("h94/IP-Adapter", subfolder="sdxl_models", weight_name="ip-adapter_sdxl.bin")
inpaint_pipeline.set_ip_adapter_scale(0.6)

@spaces.GPU(enable_queue=True)
def text_to_image(ip, prompt, neg_prompt, width, height, ip_scale, strength, guidance, steps):
    text_pipeline.to("cuda")
    
    ip.thumbnail((1024, 1024))
    
    text_pipeline.set_ip_adapter_scale(ip_scale)
    
    images = text_pipeline(
        prompt=prompt,
        ip_adapter_image=ip,
        negative_prompt=neg_prompt,
        width=width,
        height=height,
        strength=strength,
        guidance_scale=guidance,
        num_inference_steps=steps,
    ).images
    
    return images[0]

@spaces.GPU(enable_queue=True)
def image_to_image(ip, image, prompt, neg_prompt, width, height, ip_scale, strength, guidance, steps):
    image_pipeline.to("cuda")
    
    ip.thumbnail((1024, 1024))
    image.thumbnail((1024, 1024))
    
    image_pipeline.set_ip_adapter_scale(ip_scale)
    
    images = image_pipeline(
        prompt=prompt,
        image=image,
        ip_adapter_image=ip,
        negative_prompt=neg_prompt,
        width=width,
        height=height,
        strength=strength,
        guidance_scale=guidance,
        num_inference_steps=steps,
    ).images
    
    return images[0]

@spaces.GPU(enable_queue=True)
def inpaint(ip, image_editor, prompt, neg_prompt, width, height, ip_scale, strength, guidance, steps):
    inpaint_pipeline.to("cuda")
    print(image_editor)
    image = image_editor['background'].convert('RGB')
    mask = Image.new("RGBA", image_editor["layers"][0].size, "WHITE") 
    mask.paste(image_editor["layers"][0], (0, 0), image_editor["layers"][0])
    mask = ImageOps.invert(mask.convert('L'))
    
    ip.thumbnail((1024, 1024))
    image.thumbnail((1024, 1024))
    mask.thumbnail((1024, 1024))
    
    inpaint_pipeline.set_ip_adapter_scale(ip_scale)
    
    images = inpaint_pipeline(
        prompt=prompt,
        image=image,
        mask_image=mask,
        ip_adapter_image=ip,
        negative_prompt=neg_prompt,
        width=width,
        height=height,
        strength=strength,
        guidance_scale=guidance,
        num_inference_steps=steps,
    ).images
    
    return images[0]


with gr.Blocks() as demo:
    gr.Markdown("""
    # IP-Adapter Playground

    by [Tony Assi](https://www.tonyassi.com/)
    """)
    with gr.Row():
        with gr.Tab("Text-to-Image"):
            text_ip = gr.Image(label='IP-Adapter Image', type='pil')
            text_prompt = gr.Textbox(label='Prompt')
            text_button = gr.Button("Generate")
        with gr.Tab("Image-to-Image"):
            image_ip = gr.Image(label='IP-Adapter Image', type='pil')
            image_image = gr.Image(label='Image', type='pil')
            image_prompt = gr.Textbox(label='Prompt')
            image_button = gr.Button("Generate")
        with gr.Tab("Inpainting"):
            inpaint_ip = gr.Image(label='IP-Adapter Image', type='pil')
            inpaint_editor = gr.ImageMask(type='pil')
            inpaint_prompt = gr.Textbox(label='Prompt')
            inpaint_button = gr.Button("Generate")
            
        output_image = gr.Image(label='Result')
        
    with gr.Accordion("Advanced Settings", open=False):
        neg_prompt = gr.Textbox(label='Negative Prompt', value='ugly, deformed, nsfw')
        width_slider = gr.Slider(256, 1024, value=1024, step=8, label="Width")
        height_slider = gr.Slider(256, 1024, value=1024, step=8, label="Height")
        ip_scale_slider = gr.Slider(0.0, 3.0, value=0.8, label="IP-Adapter Scale")
        strength_slider = gr.Slider(0.0, 1.0, value=0.7, label="Strength")
        guidance_slider = gr.Slider(1.0, 15.0, value=7.5, label="Guidance")
        steps_slider = gr.Slider(50, 100, value=75, step=1, label="Steps")

    gr.Examples(
        [["./images/img1.jpg", "Paris Hilton", "ugly, deformed, nsfw",  1024, 1024, 0.8, 0.7, 7.5, 75]],
        [text_ip, text_prompt, neg_prompt, width_slider, height_slider, ip_scale_slider, strength_slider, guidance_slider, steps_slider],
        output_image,
        text_to_image,
        cache_examples='lazy',
        label='Text-to-Image Example'
    )

    gr.Examples(
        [["./images/img1.jpg", "./images/tony.jpg", "photo", "ugly, deformed, nsfw",  1024, 1024, 0.8, 0.7, 7.5, 75]],
        [image_ip, image_image, image_prompt, neg_prompt, width_slider, height_slider, ip_scale_slider, strength_slider, guidance_slider, steps_slider], 
        output_image,
        image_to_image,
        cache_examples='lazy',
        label='Image-to-Image Example'
    )
    

    text_button.click(text_to_image, inputs=[text_ip, text_prompt, neg_prompt, width_slider, height_slider, ip_scale_slider, strength_slider, guidance_slider, steps_slider], outputs=output_image)
    image_button.click(image_to_image, inputs=[image_ip, image_image, image_prompt, neg_prompt, width_slider, height_slider, ip_scale_slider, strength_slider, guidance_slider, steps_slider], outputs=output_image)
    inpaint_button.click(inpaint, inputs=[inpaint_ip, inpaint_editor, inpaint_prompt, neg_prompt, width_slider, height_slider, ip_scale_slider, strength_slider, guidance_slider, steps_slider], outputs=output_image)

demo.launch()