Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +80 -0
- requirments.txt.txt +7 -0
app.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
from kandinsky2 import get_kandinsky2
|
4 |
+
import torch
|
5 |
+
|
6 |
+
|
7 |
+
def text2img(text,heig,widt,num,sampler,numsteps,batchsize):
|
8 |
+
torch.cuda.empty_cache()
|
9 |
+
images =[]
|
10 |
+
model = get_kandinsky2('cuda', task_type='text2img', cache_dir='/tmp/kandinsky2', model_version='2.1', use_flash_attention=False)
|
11 |
+
for i in range(num):
|
12 |
+
outputs = model.generate_text2img(
|
13 |
+
text,
|
14 |
+
num_steps=numsteps,
|
15 |
+
batch_size=batchsize,
|
16 |
+
guidance_scale=4,
|
17 |
+
h=heig, w=widt,
|
18 |
+
sampler=sampler,
|
19 |
+
prior_cf_scale=4,
|
20 |
+
prior_steps="5"
|
21 |
+
|
22 |
+
)
|
23 |
+
images.append(outputs[0])
|
24 |
+
return images
|
25 |
+
def mixing(image1,image2,sampler,num_steps,batch_size,num):
|
26 |
+
torch.cuda.empty_cache()
|
27 |
+
images =[]
|
28 |
+
model = get_kandinsky2('cuda', task_type='text2img', model_version='2.1', use_flash_attention=False)
|
29 |
+
for i in range(num):
|
30 |
+
weights = [0.5, 0.5]
|
31 |
+
output_mixing = model.mix_images(
|
32 |
+
[image1,image2],
|
33 |
+
weights,
|
34 |
+
num_steps,
|
35 |
+
batch_size,
|
36 |
+
guidance_scale=5,
|
37 |
+
h=300, w=300,
|
38 |
+
sampler=sampler,
|
39 |
+
prior_cf_scale=4,
|
40 |
+
prior_steps="5"
|
41 |
+
)
|
42 |
+
images.append(output_mixing[0])
|
43 |
+
return images
|
44 |
+
with gr.Blocks()as demo:
|
45 |
+
gr.Markdown("Генерация изображения")
|
46 |
+
with gr.Tab("Генерация изображения по текстовому запросу"):
|
47 |
+
with gr.Blocks():
|
48 |
+
with gr.Row().style(equal_height=True):
|
49 |
+
with gr.Column():
|
50 |
+
text_input=gr.Textbox()
|
51 |
+
sampler = gr.Radio(
|
52 |
+
["ddim_sampler", "p_sampler", "plms_sampler"],
|
53 |
+
value="p_sampler",
|
54 |
+
label="Sampler",)
|
55 |
+
size_h=gr.Slider(216,864,step=100,label="Высота")
|
56 |
+
size_w=gr.Slider(216,864,step=100,label="Ширина")
|
57 |
+
num_steps=gr.Slider(50,250,step=25,label="Количество шагов")
|
58 |
+
batch_size=gr.Slider(2,20,step=1,label="Размер батча")
|
59 |
+
num=gr.Slider(1,8,step=1,label="Количество")
|
60 |
+
with gr.Column():
|
61 |
+
image_output=gr.Gallery()
|
62 |
+
image_button = gr.Button("Сгенерировать")
|
63 |
+
image_button.click(text2img,inputs=[text_input,size_h,size_w,num,sampler,num_steps,batch_size],outputs=image_output)
|
64 |
+
with gr.Tab("Объеденение двух изображений"):
|
65 |
+
with gr.Row():
|
66 |
+
with gr.Column():
|
67 |
+
image_input1=gr.Image(type="pil")
|
68 |
+
image_input2=gr.Image(type="pil")
|
69 |
+
sampler = gr.Radio(
|
70 |
+
["ddim_sampler", "p_sampler", "plms_sampler"],
|
71 |
+
value="p_sampler",
|
72 |
+
label="Sampler",)
|
73 |
+
num_steps=gr.Slider(50,250,step=25,label="Количество шагов")
|
74 |
+
batch_size=gr.Slider(2,20,step=1,label="Размер батча")
|
75 |
+
num=gr.Slider(1,8,step=1,label="Количество")
|
76 |
+
with gr.Column():
|
77 |
+
image_mixing_output=gr.Gallery(type="pil")
|
78 |
+
image_mixing_output_button=gr.Button("Сгенерировать")
|
79 |
+
image_mixing_output_button.click(mixing,inputs=[image_input1,image_input2,sampler,num_steps,batch_size,num],outputs=image_mixing_output)
|
80 |
+
demo.launch(share=True)
|
requirments.txt.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
GPU ONLY!
|
2 |
+
|
3 |
+
BEFORE LAUNCH TEST.PY, INSTALL THIS PACKAGES:
|
4 |
+
pip install "git+https://github.com/ai-forever/Kandinsky-2.git"
|
5 |
+
pip install gradio
|
6 |
+
pip install numpy
|
7 |
+
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu117
|