Update app.py
Browse files
app.py
CHANGED
@@ -1,44 +1,55 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
-
import os
|
4 |
|
|
|
5 |
BASE_URL = os.getenv("BASE_URL")
|
6 |
API_KEY = os.getenv("API_KEY")
|
7 |
|
|
|
8 |
def generate_image(prompt, negative_prompt, sampling_steps, cfg_scale, seed):
|
9 |
-
headers = {
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
'seed': seed,
|
19 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
if response.status_code == 200:
|
24 |
-
image_data = response.json()['image']
|
25 |
-
return image_data
|
26 |
-
else:
|
27 |
-
return None
|
28 |
-
|
29 |
iface = gr.Interface(
|
30 |
fn=generate_image,
|
31 |
inputs=[
|
32 |
-
gr.Textbox(
|
33 |
-
gr.Textbox(
|
34 |
],
|
35 |
-
outputs=gr.Image(
|
36 |
live=True,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
)
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
iface.add_slider('CFG Scale', 1, 20, 1)
|
42 |
-
iface.add_textbox('Seed', default='-1')
|
43 |
-
iface.add_button('Generate', None)
|
44 |
-
iface.add_download_button('Download Image')
|
|
|
1 |
+
import os
|
2 |
import gradio as gr
|
3 |
import requests
|
|
|
4 |
|
5 |
+
# Получение URL модели и API-ключа из переменных окружения
|
6 |
BASE_URL = os.getenv("BASE_URL")
|
7 |
API_KEY = os.getenv("API_KEY")
|
8 |
|
9 |
+
# Функция для отправки запроса к API модели
|
10 |
def generate_image(prompt, negative_prompt, sampling_steps, cfg_scale, seed):
|
11 |
+
headers = {"Authorization": f"Bearer {API_KEY}"}
|
12 |
+
|
13 |
+
# Параметры для запроса
|
14 |
+
params = {
|
15 |
+
"prompt": prompt,
|
16 |
+
"negative_prompt": negative_prompt,
|
17 |
+
"sampling_steps": int(sampling_steps),
|
18 |
+
"cfg_scale": int(cfg_scale),
|
19 |
+
"seed": int(seed)
|
|
|
20 |
}
|
21 |
+
|
22 |
+
# Отправка запроса к API модели
|
23 |
+
response = requests.post(BASE_URL, headers=headers, json=params)
|
24 |
+
|
25 |
+
# Получение сгенерированного изображения
|
26 |
+
generated_image = response.content
|
27 |
+
|
28 |
+
return generated_image
|
29 |
|
30 |
+
# Определение интерфейса Gradio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
iface = gr.Interface(
|
32 |
fn=generate_image,
|
33 |
inputs=[
|
34 |
+
gr.Textbox(prompt="Prompt", default=""),
|
35 |
+
gr.Textbox(prompt="Negative Prompt", default=""),
|
36 |
],
|
37 |
+
outputs=gr.Image(type="pil"),
|
38 |
live=True,
|
39 |
+
layout="vertical",
|
40 |
+
title="Hugging Face Image Generator",
|
41 |
+
description="Generate images using Hugging Face Inference API.",
|
42 |
+
theme="compact",
|
43 |
+
tabs=[
|
44 |
+
"Prompt Input",
|
45 |
+
[
|
46 |
+
gr.Slider("Sampling Steps", minimum=1, maximum=30, step=1, default=15),
|
47 |
+
gr.Slider("CFG Scale", minimum=1, maximum=20, step=1, default=10),
|
48 |
+
gr.Textbox("Seed", default="-1"),
|
49 |
+
]
|
50 |
+
],
|
51 |
+
interpretation="default"
|
52 |
)
|
53 |
|
54 |
+
# Запуск интерфейса Gradio
|
55 |
+
iface.launch()
|
|
|
|
|
|
|
|