File size: 2,419 Bytes
f19c435
d78b704
ef4b074
 
b50bf6d
1300e82
4386d14
ef4b074
4f393ad
 
 
ef4b074
 
 
 
 
 
 
 
c6d89ea
6ac74f5
c6d89ea
 
 
 
 
 
 
 
6ac74f5
bb5d34d
c6d89ea
 
 
 
 
 
589ee95
 
 
 
f92e412
589ee95
 
 
bb5d34d
589ee95
 
f19c435
 
 
 
 
 
 
 
 
589ee95
 
067535e
8a7c98d
5ab4f40
 
f19c435
 
f33c5db
f92e412
 
a2db043
 
 
 
f92e412
 
f19c435
110d719
f19c435
 
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
import gradio as gr
import spaces
from diffusers import KandinskyPriorPipeline, KandinskyPipeline
from diffusers.utils import load_image
import torch
import os
from PIL import Image

from huggingface_hub import login
login(token=os.environ.get('TOKEN'))

pipe_prior = KandinskyPriorPipeline.from_pretrained(
    "kandinsky-community/kandinsky-2-1-prior", torch_dtype=torch.float16
)
pipe_prior.to("cuda")

pipe = KandinskyPipeline.from_pretrained("kandinsky-community/kandinsky-2-1", torch_dtype=torch.float16)
pipe.to("cuda")


@spaces.GPU()
def squarify_image(img):
    if(img.height > img.width): bg_size = img.height
    else:  bg_size = img.width
    bg = Image.new(mode="RGB", size=(bg_size,bg_size), color="white")
    bg.paste(img, ( int((bg.width - bg.width)/2), 0) )

    return bg

@spaces.GPU()
def blend(img1, img2, slider, prompt, negative_prompt):
    img1.thumbnail((1024, 1024))
    img2.thumbnail((1024, 1024))
    
    img1 = squarify_image(img1)
    img2 = squarify_image(img2)
    
    # add all the conditions we want to interpolate, can be either text or image
    images_texts = [img1, img2]
    
    # specify the weights for each condition in images_texts
    weights = [1-slider, slider]
    
    prior_out = pipe_prior.interpolate(images_texts, weights)

    image = pipe(prompt=prompt, **prior_out, height=1024, width=1024, negative_prompt=negative_prompt).images[0]
    
    return image

with gr.Blocks() as demo:
    gr.Markdown("""
    # Image Blender
    by [Tony Assi](https://www.tonyassi.com/)
    """)
    
    with gr.Row():
        with gr.Column():
            img1 = gr.Image(label='Image 0', type='pil')
            img2 = gr.Image(label='Image 1',type='pil')
            slider = gr.Slider(label='Weight', maximum=1.0, value=0.5)
            with gr.Accordion("Advanced", open=False):
                prompt = gr.Textbox(label='Prompt', value='')
                negative_prompt = gr.Textbox(label='Negative Prompt', value='')
            btn = gr.Button("Blend")
        with gr.Column():
            output = gr.Image(label='Result')

    gr.Examples(
        examples=[['./cat.png', './starry_night.jpg', 0.5, '', '']],
        inputs=[img1, img2, slider, prompt, negative_prompt],
        outputs=output,
        fn=blend,
        cache_examples=True,
    )
    
    btn.click(fn=blend, inputs=[img1, img2, slider, prompt, negative_prompt], outputs=output)

demo.launch()