Create main.py
Browse files
main.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
from openai import OpenAI
|
4 |
+
client = OpenAI(api_key=os.getenv("API_KEY"))
|
5 |
+
|
6 |
+
def generate_image(prompt, code):
|
7 |
+
if code != os.getenv("code"):
|
8 |
+
raise gr.Error("❗ Не верный ключ!")
|
9 |
+
return None
|
10 |
+
response = client.images.generate(
|
11 |
+
model="dall-e-3",
|
12 |
+
prompt=prompt,
|
13 |
+
size="1024x1024",
|
14 |
+
quality="hd",
|
15 |
+
n=1,
|
16 |
+
)
|
17 |
+
|
18 |
+
image_url = response.data[0].url
|
19 |
+
print(image_url)
|
20 |
+
return image_url
|
21 |
+
css = """
|
22 |
+
footer {visibility: hidden !important;}
|
23 |
+
"""
|
24 |
+
# Создание интерфейса с помощью Gradio
|
25 |
+
with gr.Blocks(css=css, theme='YTheme/Sketch') as demo:
|
26 |
+
with gr.Row():
|
27 |
+
with gr.Column():
|
28 |
+
with gr.Row():
|
29 |
+
code = gr.Textbox(label="Ключ доступа", type="password")
|
30 |
+
with gr.Row():
|
31 |
+
prompt_input = gr.Textbox(label="Описание изображения", lines=3)
|
32 |
+
submit_btn = gr.Button("Генерация", variant='primary')
|
33 |
+
with gr.Column():
|
34 |
+
image_output = gr.Image(label="Изображение")
|
35 |
+
|
36 |
+
submit_btn.click(fn=generate_image, inputs=[prompt_input, code], outputs=image_output)
|
37 |
+
|
38 |
+
demo.launch()
|