| import os |
| import requests |
| import gradio as gr |
|
|
| |
| def generate_image(prompt): |
| api_key = os.getenv("API_KEY") |
| headers = { |
| "Authorization": f"Bearer {api_key}", |
| "Content-Type": "application/json", |
| } |
| data = { |
| "prompt": prompt, |
| "num_images": 1, |
| } |
| response = requests.post("https://api.openai.com/v1/images/generations", headers=headers, json=data) |
| response_json = response.json() |
| |
| |
| image_url = response_json["data"][0]["url"] |
| |
| return image_url |
|
|
| |
| with gr.Blocks() as demo: |
| with gr.Row(): |
| prompt_input = gr.Textbox(label="Введите промпт для генерации изображения") |
| submit_btn = gr.Button("Сгенерировать") |
| with gr.Row(): |
| image_output = gr.Image(label="Сгенерированное изображение") |
| |
| submit_btn.click(fn=generate_image, inputs=prompt_input, outputs=image_output) |
|
|
| demo.launch() |