File size: 4,366 Bytes
2b755c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python

import os
import random

import gradio as gr
import numpy as np
import torch

from model import ADAPTER_NAMES, Model

DESCRIPTION = "# T2I-Adapter-SDXL"

if not torch.cuda.is_available():
    DESCRIPTION += "\n<p>Running on CPU 🥶 This demo does not work on CPU.</p>"

MAX_SEED = np.iinfo(np.int32).max


def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
    if randomize_seed:
        seed = random.randint(0, MAX_SEED)
    return seed


model = Model(ADAPTER_NAMES[0])

with gr.Blocks(css="style.css") as demo:
    gr.Markdown(DESCRIPTION)
    gr.DuplicateButton(
        value="Duplicate Space for private use",
        elem_id="duplicate-button",
        visible=os.getenv("SHOW_DUPLICATE_BUTTON") == "1",
    )

    with gr.Row():
        with gr.Column():
            with gr.Group():
                image = gr.Image(label="Input image", type="pil", height=600)
                prompt = gr.Textbox(label="Prompt")
                adapter_name = gr.Dropdown(label="Adapter", choices=ADAPTER_NAMES, value=ADAPTER_NAMES[0])
                run_button = gr.Button("Run")
            with gr.Accordion("Advanced options", open=False):
                apply_preprocess = gr.Checkbox(label="Apply preprocess", value=True)
                negative_prompt = gr.Textbox(
                    label="Negative prompt",
                    value="anime, cartoon, graphic, text, painting, crayon, graphite, abstract, glitch, deformed, mutated, ugly, disfigured",
                )
                num_inference_steps = gr.Slider(
                    label="Number of steps",
                    minimum=1,
                    maximum=Model.MAX_NUM_INFERENCE_STEPS,
                    step=1,
                    value=30,
                )
                guidance_scale = gr.Slider(
                    label="Guidance scale",
                    minimum=0.1,
                    maximum=30.0,
                    step=0.1,
                    value=7.5,
                )
                adapter_conditioning_scale = gr.Slider(
                    label="Adapter Conditioning Scale",
                    minimum=0.5,
                    maximum=1,
                    step=0.1,
                    value=0.8,
                )
                cond_tau = gr.Slider(
                    label="Fraction of timesteps for which adapter should be applied",
                    minimum=0.1,
                    maximum=1.0,
                    step=0.1,
                    value=0.8,
                )
                seed = gr.Slider(
                    label="Seed",
                    minimum=0,
                    maximum=MAX_SEED,
                    step=1,
                    value=0,
                )
                randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
        with gr.Column():
            result = gr.Gallery(label="Result", columns=2, height=600, object_fit="scale-down", show_label=False)

    inputs = [
        image,
        prompt,
        negative_prompt,
        num_inference_steps,
        guidance_scale,
        adapter_conditioning_scale,
        cond_tau,
        seed,
        apply_preprocess,
    ]
    prompt.submit(
        fn=randomize_seed_fn,
        inputs=[seed, randomize_seed],
        outputs=seed,
        queue=False,
        api_name=False,
    ).then(
        fn=model.change_adapter,
        inputs=adapter_name,
        api_name=False,
    ).success(
        fn=model.run,
        inputs=inputs,
        outputs=result,
        api_name=False,
    )
    negative_prompt.submit(
        fn=randomize_seed_fn,
        inputs=[seed, randomize_seed],
        outputs=seed,
        queue=False,
        api_name=False,
    ).then(
        fn=model.change_adapter,
        inputs=adapter_name,
        api_name=False,
    ).success(
        fn=model.run,
        inputs=inputs,
        outputs=result,
        api_name=False,
    )
    run_button.click(
        fn=randomize_seed_fn,
        inputs=[seed, randomize_seed],
        outputs=seed,
        queue=False,
        api_name=False,
    ).then(
        fn=model.change_adapter,
        inputs=adapter_name,
        api_name=False,
    ).success(
        fn=model.run,
        inputs=inputs,
        outputs=result,
        api_name="run",
    )

if __name__ == "__main__":
    demo.queue(max_size=20).launch()