Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
21 |
+
batch_size=1,
|
22 |
+
guidance_scale=4,
|
23 |
+
h=768, w=768,
|
24 |
+
sampler='ddim_sampler',
|
25 |
+
prior_cf_scale=1,
|
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;
|
38 |
+
}
|
39 |
+
.gr-button {
|
40 |
+
color: white;
|
41 |
+
border-color: black;
|
42 |
+
background: black;
|
43 |
+
}
|
44 |
+
input[type='range'] {
|
45 |
+
accent-color: black;
|
46 |
+
}
|
47 |
+
.dark input[type='range'] {
|
48 |
+
accent-color: #dfdfdf;
|
49 |
+
}
|
50 |
+
.container {
|
51 |
+
max-width: 730px;
|
52 |
+
margin: auto;
|
53 |
+
padding-top: 1.5rem;
|
54 |
+
}
|
55 |
+
#gallery {
|
56 |
+
min-height: 22rem;
|
57 |
+
margin-bottom: 15px;
|
58 |
+
margin-left: auto;
|
59 |
+
margin-right: auto;
|
60 |
+
border-bottom-right-radius: .5rem !important;
|
61 |
+
border-bottom-left-radius: .5rem !important;
|
62 |
+
}
|
63 |
+
#gallery>div>.h-full {
|
64 |
+
min-height: 20rem;
|
65 |
+
}
|
66 |
+
.details:hover {
|
67 |
+
text-decoration: underline;
|
68 |
+
}
|
69 |
+
.gr-button {
|
70 |
+
white-space: nowrap;
|
71 |
+
}
|
72 |
+
.gr-button:focus {
|
73 |
+
border-color: rgb(147 197 253 / var(--tw-border-opacity));
|
74 |
+
outline: none;
|
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
|