Rooni commited on
Commit
d89ec1c
·
1 Parent(s): 7d4f43d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -0
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import os
4
+
5
+ API_KEY = os.getenv("API_KEY")
6
+ DALLE_API_URL = "https://api.openai.com/v1/dalle"
7
+
8
+ def call_dalle_api(prompt, size, num_images, style, aspect_ratio, temperature):
9
+ headers = {
10
+ "Authorization": f"Bearer {API_KEY}"
11
+ }
12
+ data = {
13
+ "prompt": prompt,
14
+ "size": size,
15
+ "num_images": num_images,
16
+ "style": style,
17
+ "aspect_ratio": aspect_ratio,
18
+ "temperature": temperature
19
+ # Добавьте здесь другие параметры, если они необходимы
20
+ }
21
+ response = requests.post(DALLE_API_URL, headers=headers, json=data)
22
+ if response.status_code == 200:
23
+ images = response.json()["images"]
24
+ return [gr.Image(data=image) for image in images]
25
+ else:
26
+ raise Exception("API request failed with status code: " + str(response.status_code))
27
+
28
+ def generate_image(prompt, size, num_images, style, aspect_ratio, temperature):
29
+ try:
30
+ return call_dalle_api(prompt, size, num_images, style, aspect_ratio, temperature)
31
+ except Exception as e:
32
+ return str(e)
33
+
34
+ with gr.Blocks() as demo:
35
+ with gr.Tab("Базовые настройки"):
36
+ prompt_input = gr.Textbox(label="Введите ваш запрос")
37
+
38
+ with gr.Tab("Расширенные настройки"):
39
+ with gr.Row():
40
+ with gr.Column():
41
+ size_input = gr.Slider(minimum=256, maximum=1024, step=256, label="Размер изображения", value=512)
42
+ num_images_input = gr.Slider(minimum=1, maximum=10, step=1, label="Количество изображений", value=1)
43
+ with gr.Column():
44
+ style_input = gr.Dropdown(choices=['Фотореалистичный', 'Художественный', 'Мультфильм'], label="Стиль")
45
+ aspect_ratio_input = gr.Dropdown(choices=['1:1', '16:9', '4:3'], label="Аспектное соотношение")
46
+ temperature_input = gr.Slider(minimum=0.0, maximum=1.0, step=0.01, label="Температура", value=0.5)
47
+
48
+ generate_button = gr.Button("Сгенерировать изображение")
49
+ images_output = gr.Gallery(label="Сгенерированные изображения")
50
+
51
+ generate_button.click(
52
+ generate_image,
53
+ inputs=[prompt_input, size_input, num_images_input, style_input, aspect_ratio_input, temperature_input],
54
+ outputs=images_output
55
+ )
56
+
57
+ demo.launch()