Rooni commited on
Commit
b3ee695
·
1 Parent(s): a75b67c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -79
app.py CHANGED
@@ -1,94 +1,46 @@
1
- import gradio as gr
2
  import os
3
- from PIL import Image
4
- import requests
5
- from io import BytesIO
6
 
7
- # Получаем API ключ из переменных окружения
8
- API_KEY = os.getenv("API_KEY")
 
9
 
10
- # Функция для генерации изображения с помощью DALL-E 3 API
11
- def generate_image(prompt, seed, sampling_method, cfg, width, height):
12
- headers = {
13
- "Authorization": f"Bearer {API_KEY}"
14
- }
15
- data = {
16
- "prompt": prompt,
17
- "seed": seed,
18
- "sampling_method": sampling_method,
19
- "cfg": cfg,
20
- "width": width,
21
- "height": height
22
- }
23
-
24
- response = requests.post("https://api.openai.com/v1/images/generate", headers=headers, json=data)
25
-
26
- if response.status_code == 200:
27
- image_data = response.json()
28
- # Предполагаем, что API возвращает URL изображения
29
- image_url = image_data["data"]["image_url"]
30
- image_response = requests.get(image_url)
31
- image = Image.open(BytesIO(image_response.content))
32
- return image
33
- else:
34
- # В случае ошибки возвращаем сообщение об ошибке
35
- return f"Error: {response.text}"
36
 
37
- # Функция для улучшения качества изображения
38
- def upscale_image(image, upscale_factor):
39
- headers = {
40
- "Authorization": f"Bearer {API_KEY}"
41
- }
42
- files = {
43
- "image": image
44
- }
45
- data = {
46
- "upscale_factor": upscale_factor
47
- }
48
-
49
- response = requests.post("https://api.openai.com/v1/images/upscale", headers=headers, files=files, data=data)
50
 
51
- if response.status_code == 200:
52
- image_data = response.json()
53
- # Предполагаем, что API возвращает URL улучшенного изображения
54
- image_url = image_data["data"]["image_url"]
55
- image_response = requests.get(image_url)
56
- upscaled_image = Image.open(BytesIO(image_response.content))
57
- return upscaled_image
58
- else:
59
- # В случае ошибки возвращаем сообщение об ошибке
60
- return f"Error: {response.text}"
61
-
62
- with gr.Blocks() as demo:
63
- gr.Markdown("<style>footer {display: none;}</style>") # CSS для скрытия футера
64
- with gr.Tabs():
65
- with gr.TabItem("Базовые настройки"):
66
- prompt_input = gr.Textbox(label="Prompt", lines=3, placeholder="Введите текст для генерации изображения")
67
- with gr.TabItem("Расширенные настройки"):
68
  seed_input = gr.Number(label="Seed", value=0)
69
- sampling_method_input = gr.Radio(label="Sampling Method", choices=["random", "VQ-VAE", "DDIM"], value="random")
70
- cfg_input = gr.Slider(label="CFG", minimum=0.0, maximum=1.0, value=0.7)
71
  width_input = gr.Number(label="Width", value=256)
72
  height_input = gr.Number(label="Height", value=256)
73
- with gr.TabItem("Настройки апскейлинга"):
74
- upscale_factor_input = gr.Slider(label="Увеличить в", minimum=2, maximum=6, value=2, step=1)
 
 
 
75
 
76
- generate_button = gr.Button("Генерация")
77
- generated_image_output = gr.Image(label="Сгенерированное изображение")
78
- upscale_button = gr.Button("Улучшить качество")
79
- upscaled_image_output = gr.Image(label="Изображение с улучшенным качеством")
 
80
 
81
- # Обработ��ики кнопок
82
  generate_button.click(
83
  generate_image,
84
  inputs=[prompt_input, seed_input, sampling_method_input, cfg_input, width_input, height_input],
85
- outputs=generated_image_output
86
  )
87
- upscale_button.click(
88
- upscale_image,
89
- inputs=[generated_image_output, upscale_factor_input],
90
- outputs=upscaled_image_output
91
- )
92
-
93
- # Запуск интерфейса
94
  demo.launch()
 
 
1
  import os
2
+ import gradio as gr
3
+ from gradio.external import load_interface
 
4
 
5
+ # Загрузка интерфейса DALL-E с использованием API ключа
6
+ api_key = os.getenv("API_KEY")
7
+ dalle = load_interface("openai/dalle", api_key=api_key)
8
 
9
+ # CSS для скрытия footer
10
+ css = """
11
+ footer {
12
+ display: none;
13
+ }
14
+ """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
+ with gr.Blocks(css=css) as demo:
17
+ with gr.Tab("Базовые настройки"):
18
+ with gr.Row():
19
+ prompt_input = gr.Textbox(label="Prompt", lines=3, placeholder="Введите описание изображения...")
 
 
 
 
 
 
 
 
 
20
 
21
+ with gr.Tab("Расширенные настройки"):
22
+ with gr.Row():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  seed_input = gr.Number(label="Seed", value=0)
24
+ sampling_method_input = gr.Dropdown(label="Sampling Method", choices=["random", "DDIM", "DPM++", "DPM++_ancestral", "Heuristic"], value="random")
25
+ cfg_input = gr.Slider(label="CFG", minimum=1, maximum=20, value=7)
26
  width_input = gr.Number(label="Width", value=256)
27
  height_input = gr.Number(label="Height", value=256)
28
+
29
+ with gr.Row():
30
+ generate_button = gr.Button("Генерация", variant="primary")
31
+
32
+ output_image = gr.Image(label="Сгенерированное изображение")
33
 
34
+ def generate_image(prompt, seed, sampling_method, cfg, width, height):
35
+ # Генерация изображения с помощью DALL-E API
36
+ response = dalle([prompt], num_images=1, seed=seed, sampling=sampling_method, cfg_scale=cfg, width=width, height=height)
37
+ image_url = response[0][0]["url"]
38
+ return image_url
39
 
 
40
  generate_button.click(
41
  generate_image,
42
  inputs=[prompt_input, seed_input, sampling_method_input, cfg_input, width_input, height_input],
43
+ outputs=output_image
44
  )
45
+
 
 
 
 
 
 
46
  demo.launch()