Serg4451D commited on
Commit
ba79d24
β€’
1 Parent(s): 98b5f14

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +187 -13
app.py CHANGED
@@ -1,20 +1,28 @@
1
  import os
 
2
  import gradio as gr
3
  import torch
4
  from torch import autocast
5
  from kandinsky2 import get_kandinsky2
6
- import multiprocessing as mp
7
 
8
- device = torch.device('cpu')
9
 
10
  from kandinsky2 import get_kandinsky2
11
  model = get_kandinsky2('cpu', task_type='text2img', model_version='2.1', use_flash_attention=False)
12
 
13
 
14
- def infer(args):
15
- prompt, negative = args
16
- with torch.no_grad():
17
- images = model.generate_text2img(prompt,
 
 
 
 
 
 
 
 
18
  negative_prior_prompt=negative,
19
  negative_decoder_prompt=negative,
20
  num_steps=50,
@@ -26,12 +34,6 @@ def infer(args):
26
  prior_steps="25",)
27
  return images
28
 
29
- def run_inference(prompt, negative):
30
- args = [(prompt, negative) for i in range(2)]
31
- with mp.Pool(2) as p:
32
- results = p.map(infer, args)
33
- return results[0]
34
-
35
  css = """
36
  .gradio-container {
37
  font-family: 'IBM Plex Sans', sans-serif;
@@ -75,4 +77,176 @@ css = """
75
  box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
76
  --tw-border-opacity: 1;
77
  --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
78
- --tw-ring-shadow: var(--tw-ring-inset) 0 0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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('cpu' if torch.cuda.is_available() else 'cpu')
8
 
 
9
 
10
  from kandinsky2 import get_kandinsky2
11
  model = get_kandinsky2('cpu', 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,
26
  negative_prior_prompt=negative,
27
  negative_decoder_prompt=negative,
28
  num_steps=50,
 
34
  prior_steps="25",)
35
  return images
36
 
 
 
 
 
 
 
37
  css = """
38
  .gradio-container {
39
  font-family: 'IBM Plex Sans', sans-serif;
 
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
+ SPACE_ID = os.getenv('SPACE_ID')
167
+
168
+ with block as demo:
169
+ gr.Markdown(f"""
170
+
171
+
172
+ [![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)
173
+
174
+ <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>
175
+
176
+ [Offical BlogPost](https://habr.com/ru/company/sberbank/blog/725282/)
177
+ [Offical Telegram Bot](https://t.me/kandinsky21_bot)
178
+ [Offical site](https://fusionbrain.ai/diffusion)
179
+
180
+ ## Model architecture:
181
+ Kandinsky 2.1 inherits best practicies from Dall-E 2 and Latent diffusion, while introducing some new ideas.
182
+
183
+ 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.
184
+
185
+ For diffusion mapping of latent spaces we use transformer with num_layers=20, num_heads=32 and hidden_size=2048.
186
+
187
+ Other architecture parts:
188
+
189
+ - Text encoder (XLM-Roberta-Large-Vit-L-14) - 560M
190
+ - Diffusion Image Prior β€” 1B
191
+ - CLIP image encoder (ViT-L/14) - 427M
192
+ - Latent Diffusion U-Net - 1.22B
193
+ - MoVQ encoder/decoder - 67M
194
+
195
+ Kandinsky 2.1 was trained on a large-scale image-text dataset LAION HighRes and fine-tuned on our internal datasets.
196
+
197
+ **Kandinsky 2.1** architecture overview:
198
+
199
+ ![](https://raw.githubusercontent.com/ai-forever/Kandinsky-2/main/content/einstein.png)
200
+
201
+
202
+ """
203
+ )
204
+ with gr.Group():
205
+ with gr.Box():
206
+ with gr.Row().style(mobile_collapse=False, equal_height=True):
207
+
208
+ text = gr.Textbox(
209
+ label="Enter your prompt", show_label=True, max_lines=2
210
+ ).style(
211
+ border=(True, False, True, True),
212
+ rounded=(True, False, False, True),
213
+ container=False,
214
+ )
215
+ negative = gr.Textbox(
216
+ label="Enter your negative prompt", show_label=True, max_lines=2
217
+ ).style(
218
+ border=(True, False, True, True),
219
+ rounded=(True, False, False, True),
220
+ container=False,
221
+ )
222
+ btn = gr.Button("Run").style(
223
+ margin=False,
224
+ rounded=(False, True, True, False),
225
+ )
226
+
227
+ gallery = gr.Gallery(label="Generated images", show_label=False, elem_id="generated_id").style(
228
+ grid=[2], height="auto"
229
+ )
230
+
231
+ ex = gr.Examples(examples=examples, fn=infer, inputs=[text, negative], outputs=gallery, cache_examples=True)
232
+ ex.dataset.headers = [""]
233
+
234
+ text.submit(infer, inputs=[text, negative], outputs=gallery)
235
+ btn.click(infer, inputs=[text, negative], outputs=gallery)
236
+ gr.Markdown("""
237
+
238
+
239
+ # Authors
240
+
241
+ + Arseniy Shakhmatov: [Github](https://github.com/cene555), [Blog](https://t.me/gradientdip)
242
+ + Anton Razzhigaev: [Github](https://github.com/razzant), [Blog](https://t.me/abstractDL)
243
+ + Aleksandr Nikolich: [Github](https://github.com/AlexWortega), [Blog](https://t.me/lovedeathtransformers)
244
+ + Vladimir Arkhipkin: [Github](https://github.com/oriBetelgeuse)
245
+ + Igor Pavlov: [Github](https://github.com/boomb0om)
246
+ + Andrey Kuznetsov: [Github](https://github.com/kuznetsoffandrey)
247
+ + Denis Dimitrov: [Github](https://github.com/denndimitrov)
248
+
249
+ """
250
+ )
251
+
252
+ demo.queue(max_size=15).launch()