Update app.py
Browse files
app.py
CHANGED
@@ -1,107 +1,60 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
from transformers import AutoModelForConditionalGeneration, AutoTokenizer
|
4 |
|
5 |
# Базовые настройки
|
6 |
-
|
7 |
-
|
8 |
-
model_list = [
|
9 |
-
"DALL-E 2",
|
10 |
-
"VQGAN+CLIP",
|
11 |
-
"BigGAN",
|
12 |
-
"StyleGAN2",
|
13 |
-
"VQGAN",
|
14 |
-
"CLIP",
|
15 |
-
"VQGAN+CLIP-Vanilla",
|
16 |
-
"VQGAN+CLIP-Cutout",
|
17 |
-
"VQGAN+CLIP-RandomizedCutout",
|
18 |
-
]
|
19 |
-
model = gr.widgets.ToggleButtons(options=model_list, label="Модель")
|
20 |
|
21 |
# Расширенные настройки
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
seed = gr.inputs.Number(min=0, max=2**31 - 1, label="Случайное число")
|
28 |
|
29 |
# Улучшение качества
|
30 |
-
|
31 |
-
upscale_algorithm = gr.inputs.RadioButtons(options=["bicubic", "lanczos"], label="Алгоритм увеличения")
|
32 |
|
33 |
# Функция генерации изображения
|
34 |
-
|
35 |
def generate_image(prompt, model, negative_prompt, sampling_method, sampling_steps, cfg_scale, seed):
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
elif model == "VQGAN":
|
49 |
-
model = AutoModelForConditionalGeneration.from_pretrained("vqgan/vqgan_imagenet_f16_1024")
|
50 |
-
tokenizer = AutoTokenizer.from_pretrained("vqgan/vqgan_imagenet_f16_1024")
|
51 |
-
elif model == "CLIP":
|
52 |
-
model = AutoModelForConditionalGeneration.from_pretrained("openai/clip")
|
53 |
-
tokenizer = AutoTokenizer.from_pretrained("openai/clip")
|
54 |
-
elif model == "VQGAN+CLIP-Vanilla":
|
55 |
-
model = AutoModelForConditionalGeneration.from_pretrained("vqgan/vqgan_imagenet_f16_1024_clip_vanilla")
|
56 |
-
tokenizer = AutoTokenizer.from_pretrained("vqgan/vqgan_imagenet_f16_1024_clip_vanilla")
|
57 |
-
elif model == "VQGAN+CLIP-Cutout":
|
58 |
-
model = AutoModelForConditionalGeneration.from_pretrained("vqgan/vqgan_imagenet_f16_1024_clip_cutout")
|
59 |
-
tokenizer = AutoTokenizer.from_pretrained("vqgan/vqgan_imagenet_f16_1024_clip_cutout")
|
60 |
-
elif model == "VQGAN+CLIP-RandomizedCutout":
|
61 |
-
model = AutoModelForConditionalGeneration.from_pretrained("vqgan/vqgan_imagenet_f16_1024_clip_randomized_cutout")
|
62 |
-
tokenizer = AutoTokenizer.from_pretrained("vqgan/vqgan_imagenet_f16_1024_clip_randomized_cutout")
|
63 |
-
|
64 |
-
prompt = f"{prompt} {negative_prompt}"
|
65 |
-
|
66 |
-
image = model.generate(
|
67 |
-
text=prompt,
|
68 |
-
sampling_method=sampling_method,
|
69 |
-
sampling_steps=sampling_steps,
|
70 |
-
cfg_scale=cfg_scale,
|
71 |
-
seed=seed,
|
72 |
-
)
|
73 |
-
|
74 |
return image
|
75 |
|
76 |
# Функция улучшения качества изображения
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
image
|
81 |
-
|
82 |
-
|
|
|
|
|
83 |
return image
|
84 |
|
85 |
-
# Функция отображения изображения
|
86 |
-
|
87 |
-
def show_image(image):
|
88 |
-
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
89 |
-
cv2.imshow("Image", image)
|
90 |
-
cv2.waitKey(0)
|
91 |
-
|
92 |
# Основная функция
|
93 |
-
|
94 |
def main():
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
|
|
105 |
|
106 |
if __name__ == "__main__":
|
107 |
main()
|
|
|
1 |
import gradio as gr
|
2 |
+
import requests
|
|
|
3 |
|
4 |
# Базовые настройки
|
5 |
+
prompt = gr.inputs.Textbox(label="Prompt")
|
6 |
+
models = gr.inputs.RadioButtons(options=["Bard", "DALL-E 2", "VQGAN+CLIP", "VQGAN", "CLIP", "InceptionV3"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
# Расширенные настройки
|
9 |
+
negative_prompt = gr.inputs.Textbox(label="Negative Prompt")
|
10 |
+
sampling_method = gr.inputs.RadioButtons(options=["random", "greedy", "nucleus", "top_k", "top_p"])
|
11 |
+
sampling_steps = gr.inputs.Number(label="Sampling Steps", min=1, max=1000, default=100)
|
12 |
+
cfg_scale = gr.inputs.Number(label="CFG Scale", min=0.1, max=10.0, default=1.0)
|
13 |
+
seed = gr.inputs.Number(label="Seed", min=0, max=2**31, default=0)
|
|
|
14 |
|
15 |
# Улучшение качества
|
16 |
+
algorithm = gr.inputs.RadioButtons(options=["nearest", "bilinear", "bicubic", "lanczos", "cubic", "mitchell"])
|
|
|
17 |
|
18 |
# Функция генерации изображения
|
|
|
19 |
def generate_image(prompt, model, negative_prompt, sampling_method, sampling_steps, cfg_scale, seed):
|
20 |
+
url = "https://api.huggingface.co/models/text-to-image/v1/generate"
|
21 |
+
data = {
|
22 |
+
"prompt": prompt,
|
23 |
+
"model": model,
|
24 |
+
"negative_prompt": negative_prompt,
|
25 |
+
"sampling_method": sampling_method,
|
26 |
+
"sampling_steps": sampling_steps,
|
27 |
+
"cfg_scale": cfg_scale,
|
28 |
+
"seed": seed,
|
29 |
+
}
|
30 |
+
response = requests.post(url, json=data)
|
31 |
+
image = response.json()["image"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
return image
|
33 |
|
34 |
# Функция улучшения качества изображения
|
35 |
+
def improve_quality(image, algorithm):
|
36 |
+
url = "https://api.huggingface.co/models/text-to-image/v1/improve-quality"
|
37 |
+
data = {
|
38 |
+
"image": image,
|
39 |
+
"algorithm": algorithm,
|
40 |
+
}
|
41 |
+
response = requests.post(url, json=data)
|
42 |
+
image = response.json()["image"]
|
43 |
return image
|
44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
# Основная функция
|
|
|
46 |
def main():
|
47 |
+
gr.Interface(
|
48 |
+
generate_image,
|
49 |
+
inputs=[prompt, models, negative_prompt, sampling_method, sampling_steps, cfg_scale, seed],
|
50 |
+
outputs=gr.outputs.Image(),
|
51 |
+
title="Gradio Image Generator",
|
52 |
+
tabs=[
|
53 |
+
gr.Tab("Базовые настройки", [prompt, models]),
|
54 |
+
gr.Tab("Расширенные настройки", [negative_prompt, sampling_method, sampling_steps, cfg_scale, seed]),
|
55 |
+
gr.Tab("Улучшение качества", [algorithm]),
|
56 |
+
],
|
57 |
+
).launch()
|
58 |
|
59 |
if __name__ == "__main__":
|
60 |
main()
|