File size: 7,080 Bytes
a9103c4
 
919fef8
 
a9103c4
 
 
919fef8
a9103c4
 
919fef8
a9103c4
 
919fef8
a9103c4
919fef8
a9103c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
919fef8
a9103c4
 
 
 
 
 
 
 
 
 
 
919fef8
a9103c4
 
919fef8
a9103c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
919fef8
a9103c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
919fef8
 
 
 
 
 
 
 
 
 
a9103c4
7833185
919fef8
7833185
919fef8
 
 
 
 
 
7833185
 
 
 
 
 
 
 
 
919fef8
 
 
 
 
 
7833185
 
 
919fef8
7833185
919fef8
 
 
 
 
 
7833185
 
 
 
 
a9103c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f94c315
a9103c4
 
 
 
919fef8
a9103c4
 
 
 
919fef8
 
 
 
 
 
 
 
 
 
 
7833185
919fef8
 
 
 
 
7833185
 
919fef8
7833185
a9103c4
7833185
a9103c4
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import gradio as gr
from multiprocessing import cpu_count
from utils.functions import generate, train_textual_inversion
from utils.shared import model_ids, scheduler_names, default_scheduler

default_img_size = 512

with open("html/header.html") as fp:
    header = fp.read()

with open("html/footer.html") as fp:
    footer = fp.read()

with gr.Blocks(css="html/style.css") as demo:

    pipe_state = gr.State(lambda: 1)

    gr.HTML(header)

    with gr.Row():

        with gr.Column(scale=70):

            # with gr.Row():
            prompt = gr.Textbox(
                label="Prompt", placeholder="<Shift+Enter> to generate", lines=2
            )
            neg_prompt = gr.Textbox(label="Negative Prompt", placeholder="", lines=2)

        with gr.Column(scale=30):
            model_name = gr.Dropdown(
                label="Model", choices=model_ids, value=model_ids[0]
            )
            scheduler_name = gr.Dropdown(
                label="Scheduler", choices=scheduler_names, value=default_scheduler
            )
            generate_button = gr.Button(value="Generate", elem_id="generate-button")

    with gr.Row():

        with gr.Column():

            with gr.Tab("Text to Image") as tab:
                tab.select(lambda: 1, [], pipe_state)

            with gr.Tab("Image to image") as tab:
                tab.select(lambda: 2, [], pipe_state)

                image = gr.Image(
                    label="Image to Image",
                    source="upload",
                    tool="editor",
                    type="pil",
                    elem_id="image_upload",
                ).style(height=default_img_size)
                strength = gr.Slider(
                    label="Denoising strength",
                    minimum=0,
                    maximum=1,
                    step=0.02,
                    value=0.8,
                )

            with gr.Tab("Inpainting") as tab:
                tab.select(lambda: 3, [], pipe_state)

                inpaint_image = gr.Image(
                    label="Inpainting",
                    source="upload",
                    tool="sketch",
                    type="pil",
                    elem_id="image_upload",
                ).style(height=default_img_size)
                inpaint_strength = gr.Slider(
                    label="Denoising strength",
                    minimum=0,
                    maximum=1,
                    step=0.02,
                    value=0.8,
                )
                inpaint_options = [
                    "preserve non-masked portions of image",
                    "output entire inpainted image",
                ]
                inpaint_radio = gr.Radio(
                    inpaint_options,
                    value=inpaint_options[0],
                    show_label=False,
                    interactive=True,
                )

            with gr.Tab("Textual Inversion") as tab:
                tab.select(lambda: 4, [], pipe_state)

                type_of_thing = gr.Dropdown(
                    label="What would you like to train?",
                    choices=["object", "person", "style"],
                    value="object",
                    interactive=True,
                )

                text_train_bsz = gr.Slider(
                    label="Training Batch Size",
                    minimum=1,
                    maximum=8,
                    step=1,
                    value=1,
                )

                files = gr.File(
                    label=f"""Upload the images for your concept""",
                    file_count="multiple",
                    interactive=True,
                    visible=True,
                )

                text_train_steps = gr.Number(label="How many steps", value=1000)

                text_learning_rate = gr.Number(label="Learning Rate", value=5.0e-4)

                concept_word = gr.Textbox(
                    label=f"""concept word - use a unique, made up word to avoid collisions"""
                )
                init_word = gr.Textbox(
                    label=f"""initial word - to init the concept embedding"""
                )

                textual_inversion_button = gr.Button(value="Train Textual Inversion")

                training_status = gr.Text(label="Training Status")

            with gr.Row():
                batch_size = gr.Slider(
                    label="Batch Size", value=1, minimum=1, maximum=8, step=1
                )
                seed = gr.Slider(-1, 2147483647, label="Seed", value=-1, step=1)

            with gr.Row():
                guidance = gr.Slider(
                    label="Guidance scale", value=7.5, minimum=0, maximum=20
                )
                steps = gr.Slider(
                    label="Steps", value=20, minimum=1, maximum=100, step=1
                )

            with gr.Row():
                width = gr.Slider(
                    label="Width",
                    value=default_img_size,
                    minimum=64,
                    maximum=1024,
                    step=32,
                )
                height = gr.Slider(
                    label="Height",
                    value=default_img_size,
                    minimum=64,
                    maximum=1024,
                    step=32,
                )

        with gr.Column():
            gallery = gr.Gallery(
                label="Generated images", show_label=False, elem_id="gallery"
            ).style(height=default_img_size, grid=2)

            generation_details = gr.Markdown()

            pipe_kwargs = gr.Textbox(label="Pipe kwargs", value="{\n\t\n}")

            # if torch.cuda.is_available():
            #  giga = 2**30
            #  vram_guage = gr.Slider(0, torch.cuda.memory_reserved(0)/giga, label='VRAM Allocated to Reserved (GB)', value=0, step=1)
            #  demo.load(lambda : torch.cuda.memory_allocated(0)/giga, inputs=[], outputs=vram_guage, every=0.5, show_progress=False)

    gr.HTML(footer)

    inputs = [
        model_name,
        scheduler_name,
        prompt,
        guidance,
        steps,
        batch_size,
        width,
        height,
        seed,
        image,
        strength,
        inpaint_image,
        inpaint_strength,
        inpaint_radio,
        neg_prompt,
        pipe_state,
        pipe_kwargs,
    ]
    outputs = [gallery, generation_details]

    prompt.submit(generate, inputs=inputs, outputs=outputs)
    generate_button.click(generate, inputs=inputs, outputs=outputs)

    textual_inversion_inputs = [
        model_name,
        scheduler_name,
        type_of_thing,
        files,
        concept_word,
        init_word,
        text_train_steps,
        text_train_bsz,
        text_learning_rate,
    ]

    textual_inversion_button.click(
        train_textual_inversion,
        inputs=textual_inversion_inputs,
        outputs=[training_status],
    )


# demo = gr.TabbedInterface([demo, dreambooth_tab], ["Main", "Dreambooth"])

demo.queue(concurrency_count=cpu_count())

demo.launch()