lachine commited on
Commit
5450eb4
β€’
1 Parent(s): 165626a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +227 -24
app.py CHANGED
@@ -1,29 +1,232 @@
 
 
1
  import gradio as gr
2
  import torch
3
  from torch import autocast
4
  from kandinsky2 import get_kandinsky2
5
-
6
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
7
- model = get_kandinsky2(device.type, task_type='text2img', cache_dir='/tmp/kandinsky2', model_version='2.1', use_flash_attention=False)
8
-
9
- def generate_text(prompt, quality="High (Default)"):
10
- length_dict = {"Low": 50, "High (Default)": 100, "Ultra": 150}
11
- length = length_dict[quality]
12
- return model.generate_text2img('''red cat, 4k photo''', num_steps=length,
13
- batch_size=1, guidance_scale=4,
14
- h=768, w=768
15
- ,sampler='p_sampler', prior_cf_scale=4,
16
- prior_steps="5",)[0]
17
-
18
- iface = gr.Interface(
19
- fn=generate_text,
20
- inputs=["textbox", gr.inputs.Dropdown(["Low", "Medium (Default)", "High"], label="Quality")],
21
- outputs=gr.outputs.Image(label="Generated image:")
22
- )
23
-
24
- if device.type == 'cpu':
25
- model.load_state_dict(torch.load('path/to/model.pth', map_location=device))
26
- else:
27
- model.load_state_dict(torch.load('path/to/model.pth'))
28
-
29
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
  import gradio as gr
4
  import torch
5
  from torch import autocast
6
  from kandinsky2 import get_kandinsky2
 
7
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
8
+
9
+
10
+ from kandinsky2 import get_kandinsky2
11
+ model = get_kandinsky2('cuda', task_type='text2img', model_version='2.1', use_flash_attention=False)
12
+
13
+
14
+ """
15
+ num_steps=50,
16
+ batch_size=4,
17
+ guidance_scale=7,
18
+ h=768,
19
+ w=768,
20
+ sampler='ddim_sampler',
21
+ prior_cf_scale=1,
22
+ prior_steps='25',
23
+ """
24
+ def infer(prompt, negative='low quality, bad quality'):
25
+ images = model.generate_text2img(prompt+', 4k photo, unreal engine, high quality',
26
+ negative_prior_prompt=negative,
27
+ negative_decoder_prompt=negative,
28
+ num_steps=50,
29
+ batch_size=1,
30
+ guidance_scale=4,
31
+ h=768, w=768,
32
+ sampler='p_sampler',
33
+ prior_cf_scale=1,
34
+ prior_steps="5",)
35
+ return images
36
+
37
+ css = """
38
+ .gradio-container {
39
+ font-family: 'IBM Plex Sans', sans-serif;
40
+ }
41
+ .gr-button {
42
+ color: white;
43
+ border-color: black;
44
+ background: black;
45
+ }
46
+ input[type='range'] {
47
+ accent-color: black;
48
+ }
49
+ .dark input[type='range'] {
50
+ accent-color: #dfdfdf;
51
+ }
52
+ .container {
53
+ max-width: 730px;
54
+ margin: auto;
55
+ padding-top: 1.5rem;
56
+ }
57
+ #gallery {
58
+ min-height: 22rem;
59
+ margin-bottom: 15px;
60
+ margin-left: auto;
61
+ margin-right: auto;
62
+ border-bottom-right-radius: .5rem !important;
63
+ border-bottom-left-radius: .5rem !important;
64
+ }
65
+ #gallery>div>.h-full {
66
+ min-height: 20rem;
67
+ }
68
+ .details:hover {
69
+ text-decoration: underline;
70
+ }
71
+ .gr-button {
72
+ white-space: nowrap;
73
+ }
74
+ .gr-button:focus {
75
+ border-color: rgb(147 197 253 / var(--tw-border-opacity));
76
+ outline: none;
77
+ box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
78
+ --tw-border-opacity: 1;
79
+ --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
80
+ --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
81
+ --tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
82
+ --tw-ring-opacity: .5;
83
+ }
84
+ #advanced-btn {
85
+ font-size: .7rem !important;
86
+ line-height: 19px;
87
+ margin-top: 12px;
88
+ margin-bottom: 12px;
89
+ padding: 2px 8px;
90
+ border-radius: 14px !important;
91
+ }
92
+ #advanced-options {
93
+ display: none;
94
+ margin-bottom: 20px;
95
+ }
96
+ .footer {
97
+ margin-bottom: 45px;
98
+ margin-top: 35px;
99
+ text-align: center;
100
+ border-bottom: 1px solid #e5e5e5;
101
+ }
102
+ .footer>p {
103
+ font-size: .8rem;
104
+ display: inline-block;
105
+ padding: 0 10px;
106
+ transform: translateY(10px);
107
+ background: white;
108
+ }
109
+ .dark .footer {
110
+ border-color: #303030;
111
+ }
112
+ .dark .footer>p {
113
+ background: #0b0f19;
114
+ }
115
+ .acknowledgments h4{
116
+ margin: 1.25em 0 .25em 0;
117
+ font-weight: bold;
118
+ font-size: 115%;
119
+ }
120
+ #container-advanced-btns{
121
+ display: flex;
122
+ flex-wrap: wrap;
123
+ justify-content: space-between;
124
+ align-items: center;
125
+ }
126
+ .animate-spin {
127
+ animation: spin 1s linear infinite;
128
+ }
129
+ @keyframes spin {
130
+ from {
131
+ transform: rotate(0deg);
132
+ }
133
+ to {
134
+ transform: rotate(360deg);
135
+ }
136
+ }
137
+ #share-btn-container {
138
+ display: flex; padding-left: 0.5rem !important; padding-right: 0.5rem !important; background-color: #000000; justify-content: center; align-items: center; border-radius: 9999px !important; width: 13rem;
139
+ }
140
+ #share-btn {
141
+ all: initial; color: #ffffff;font-weight: 600; cursor:pointer; font-family: 'IBM Plex Sans', sans-serif; margin-left: 0.5rem !important; padding-top: 0.25rem !important; padding-bottom: 0.25rem !important;
142
+ }
143
+ #share-btn * {
144
+ all: unset;
145
+ }
146
+ .gr-form{
147
+ flex: 1 1 50%; border-top-right-radius: 0; border-bottom-right-radius: 0;
148
+ }
149
+ #prompt-container{
150
+ gap: 0;
151
+ }
152
+ #generated_id{
153
+ min-height: 700px
154
+ }
155
+ """
156
+ block = gr.Blocks(css=css)
157
+
158
+ examples = [
159
+
160
+ [
161
+ 'Thinking man in anime style'
162
+ ],
163
+
164
+ ]
165
+
166
+ with block as demo:
167
+ gr.Markdown(f"""
168
+ [![Framework: PyTorch](https://img.shields.io/badge/Framework-PyTorch-orange.svg)](https://pytorch.org/) [![Huggingface space](https://img.shields.io/badge/πŸ€—-Huggingface-yello.svg)](https://huggingface.co/sberbank-ai/Kandinsky_2.0)
169
+ <p>For faster inference without waiting in queue, you may duplicate the space and upgrade to GPU in settings. <a href="https://huggingface.co/spaces/{SPACE_ID}?duplicate=true"><img style="display: inline; margin-top: 0em; margin-bottom: 0em" src="https://bit.ly/3gLdBN6" alt="Duplicate Space" /></a></p>
170
+ [Offical BlogPost](https://habr.com/ru/company/sberbank/blog/725282/)
171
+ [Offical Telegram Bot](https://t.me/kandinsky21_bot)
172
+ [Offical site](https://fusionbrain.ai/diffusion)
173
+ ## Model architecture:
174
+ Kandinsky 2.1 inherits best practicies from Dall-E 2 and Latent diffusion, while introducing some new ideas.
175
+ As text and image encoder it uses CLIP model and diffusion image prior (mapping) between latent spaces of CLIP modalities. This approach increases the visual performance of the model and unveils new horizons in blending images and text-guided image manipulation.
176
+ For diffusion mapping of latent spaces we use transformer with num_layers=20, num_heads=32 and hidden_size=2048.
177
+ Other architecture parts:
178
+ - Text encoder (XLM-Roberta-Large-Vit-L-14) - 560M
179
+ - Diffusion Image Prior β€” 1B
180
+ - CLIP image encoder (ViT-L/14) - 427M
181
+ - Latent Diffusion U-Net - 1.22B
182
+ - MoVQ encoder/decoder - 67M
183
+ Kandinsky 2.1 was trained on a large-scale image-text dataset LAION HighRes and fine-tuned on our internal datasets.
184
+ **Kandinsky 2.1** architecture overview:
185
+ ![](https://raw.githubusercontent.com/ai-forever/Kandinsky-2/main/content/einstein.png)
186
+ """
187
+ )
188
+ with gr.Group():
189
+ with gr.Box():
190
+ with gr.Row().style(mobile_collapse=False, equal_height=True):
191
+
192
+ text = gr.Textbox(
193
+ label="Enter your prompt", show_label=True, max_lines=2
194
+ ).style(
195
+ border=(True, False, True, True),
196
+ rounded=(True, False, False, True),
197
+ container=False,
198
+ )
199
+ negative = gr.Textbox(
200
+ label="Enter your negative prompt", show_label=True, max_lines=2
201
+ ).style(
202
+ border=(True, False, True, True),
203
+ rounded=(True, False, False, True),
204
+ container=False,
205
+ )
206
+ btn = gr.Button("Run").style(
207
+ margin=False,
208
+ rounded=(False, True, True, False),
209
+ )
210
+
211
+ gallery = gr.Gallery(label="Generated images", show_label=False, elem_id="generated_id").style(
212
+ grid=[2], height="auto"
213
+ )
214
+
215
+ ex = gr.Examples(examples=examples, fn=infer, inputs=[text, negative], outputs=gallery, cache_examples=True)
216
+ ex.dataset.headers = [""]
217
+
218
+ text.submit(infer, inputs=[text, negative], outputs=gallery)
219
+ btn.click(infer, inputs=[text, negative], outputs=gallery)
220
+ gr.Markdown("""
221
+ # Authors
222
+ + Arseniy Shakhmatov: [Github](https://github.com/cene555), [Blog](https://t.me/gradientdip)
223
+ + Anton Razzhigaev: [Github](https://github.com/razzant), [Blog](https://t.me/abstractDL)
224
+ + Aleksandr Nikolich: [Github](https://github.com/AlexWortega), [Blog](https://t.me/lovedeathtransformers)
225
+ + Vladimir Arkhipkin: [Github](https://github.com/oriBetelgeuse)
226
+ + Igor Pavlov: [Github](https://github.com/boomb0om)
227
+ + Andrey Kuznetsov: [Github](https://github.com/kuznetsoffandrey)
228
+ + Denis Dimitrov: [Github](https://github.com/denndimitrov)
229
+ """
230
+ )
231
+
232
+ demo.queue(max_size=15).launch()