File size: 4,425 Bytes
e4b3d88
0a05d1f
b2e9e1e
0a05d1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4b3d88
0a05d1f
 
 
 
 
 
e4b3d88
0a05d1f
 
 
9b68ef6
0a05d1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4b3d88
0a05d1f
 
 
 
e4b3d88
 
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
import gradio as gr
from PIL import Image, ImageColor
#my back_task code being imported
from back_task import *

# The function that does the hard work
def generate(radio,color,prompt, guidance_loss_scale):
    print(color)
    if radio == "color guidance":
        target_color = ImageColor.getcolor(color, "RGB")  # Target color as RGB
        target_color = [a / 255 for a in target_color]  # Rescale from (0, 255) to (0, 1)

    elif radio == "text guidance":
        # We embed a prompt with CLIP as our target
        text = open_clip.tokenize([prompt]).to(device)
        with torch.no_grad(), torch.cuda.amp.autocast():
            text_features = clip_model.encode_text(text)

    x = torch.randn(1, 3, 256, 256).to(device)
    for i, t in tqdm(enumerate(scheduler.timesteps)):
        model_input = scheduler.scale_model_input(x, t)
        with torch.no_grad():
            noise_pred = image_pipe.unet(model_input, t)["sample"]

        if radio == "color guidance":
            x = x.detach().requires_grad_()
            x0 = scheduler.step(noise_pred, t, x).pred_original_sample
            loss = color_loss(x0, target_color) * guidance_loss_scale
            cond_grad = -torch.autograd.grad(loss, x)[0]
            x = x.detach() + cond_grad
        elif radio == "text guidance":
                cond_grad = 0

                for cut in range(n_cuts):

                    # Set requires grad on x
                    x = x.detach().requires_grad_()

                    # Get the predicted x0:
                    x0 = scheduler.step(noise_pred, t, x).pred_original_sample

                    # Calculate loss
                    loss = clip_loss(x0, text_features) * guidance_loss_scale

                    # Get gradient (scale by n_cuts since we want the average)
                    cond_grad -= torch.autograd.grad(loss, x)[0] / n_cuts


                # Modify x based on this gradient
                alpha_bar = scheduler.alphas_cumprod[i]
                x = x.detach() + cond_grad * alpha_bar.sqrt()  # Note the additional scaling factor here!

    
        x = scheduler.step(noise_pred, t, x).prev_sample
    grid = torchvision.utils.make_grid(x, nrow=4)
    im = grid.permute(1, 2, 0).cpu().clip(-1, 1) * 0.5 + 0.5
    im = Image.fromarray(np.array(im * 255).astype(np.uint8))
    # im.save("test.jpeg")
    return im


title="""<h1 align="center">Make me a WikiArt</h1>
<p align="center">Try-out of exercise from HF Learn [Difussion Course] </p>
<p align="center">&#128517; Inference is very very slow &#128012; since I am using HF's free cpu &#128521; </p>
<p><center>
<a href="https://huggingface.co/learn/diffusion-course" target="_blank">[HF-Learn]</a>
</center></p>"""

with gr.Blocks() as demo:
    gr.HTML(title)
    with gr.Row():
        with gr.Column():
            # Create a radio button with options "no guidance", "color guidance", and "text guidance"
            radio = gr.Radio(["no guidance", "color guidance", "text guidance"], label="Choose",value="no guidance")

            # Create a textbox that only shows when 'text guidance' is selected
            text = gr.Textbox(label="This text only shows when 'text guidance' is selected.", visible=False)

            # Create a color picker (not a tuple)
            color = gr.ColorPicker(label="color", value="#000000", visible=False)

            # Create a slider that shows when any option is selected
            slider = gr.Slider(label="guidance_scale", minimum=0, maximum=30, value=3, visible=False)

            def update_visibility(radio):
                value = radio  # Get the selected value from the radio button
                if value == "color guidance":
                    return [gr.Textbox(visible=False),gr.ColorPicker(visible=True),gr.Slider(visible=True)] #make it visible
                elif value == "text guidance":
                    return [gr.Textbox(visible=True),gr.ColorPicker(visible=False),gr.Slider(visible=True)]
                else:
                    return [gr.Textbox(visible=False),gr.ColorPicker(visible=False),gr.Slider(visible=False)]

            radio.change(update_visibility, radio,[text,color,slider])
        with gr.Column():
            outputs = gr.Image(label="result")
            
    with gr.Row():
        gen_bttn=gr.Button(value="generate")
        gen_bttn.click(generate, inputs=[radio,color,text,slider], outputs=outputs)


demo.queue().launch()