|
import os |
|
import gradio as gr |
|
from openai import OpenAI |
|
from flask import Flask, request, jsonify |
|
client = OpenAI(api_key=os.getenv("API_KEY")) |
|
|
|
|
|
|
|
app = Flask(__name__) |
|
|
|
@app.route('/', methods=['POST']) |
|
def generate_image(): |
|
|
|
auth_key = request.headers.get('Authorization') |
|
client = OpenAI(api_key=auth_key) |
|
|
|
|
|
description = request.json.get('description') |
|
if not description: |
|
return jsonify({"error": "No description provided"}), 400 |
|
|
|
response = client.images.generate( |
|
model="dall-e-3", |
|
prompt=prompt, |
|
size="1024x1024", |
|
quality="hd", |
|
n=1, |
|
) |
|
image_url = response.data[0].url |
|
|
|
return jsonify({"image_url": image_url}) |
|
|
|
|
|
|
|
|
|
def generate_image(prompt, code): |
|
if code != os.getenv("code"): |
|
raise gr.Error("❗ Не верный ключ!") |
|
return None |
|
response = client.images.generate( |
|
model="dall-e-3", |
|
prompt=prompt, |
|
size="1024x1024", |
|
quality="hd", |
|
n=1, |
|
) |
|
|
|
image_url = response.data[0].url |
|
print(image_url) |
|
return image_url |
|
css = """ |
|
footer {visibility: hidden !important;} |
|
""" |
|
|
|
with gr.Blocks(css=css, theme='YTheme/Sketch') as demo: |
|
with gr.Row(): |
|
with gr.Column(): |
|
with gr.Row(): |
|
code = gr.Textbox(label="Ключ доступа", type="password") |
|
with gr.Row(): |
|
prompt_input = gr.Textbox(label="Описание изображения", lines=3) |
|
submit_btn = gr.Button("Генерация", variant='primary') |
|
with gr.Column(): |
|
image_output = gr.Image(label="Изображение") |
|
|
|
submit_btn.click(fn=generate_image, inputs=[prompt_input, code], outputs=image_output) |
|
|
|
demo.launch() |
|
|
|
if __name__ == '__main__': |
|
app.run(debug=True) |