| import gradio as gr |
| import openai |
| import os |
|
|
| openai.api_key = os.environ.get("OPENAI_API_KEY") |
|
|
| |
| def open_ai_txt2img(prompt): |
| try: |
| response = openai.Image.create( |
| prompt=prompt, |
| n=3, |
| size="1024x1024", |
| ) |
| return [img["url"] for img in response["data"]] |
| except openai.error.OpenAIError as e: |
| print(e.http_status) |
| print(e.error) |
|
|
| |
| def generator(prompt): |
| return open_ai_txt2img(prompt) |
|
|
| with gr.Blocks() as demo: |
| prompt = gr.Textbox(label="Prompt") |
| submit = gr.Button(label="Generate") |
| image1, image2, image3 = gr.Image(), gr.Image(), gr.Image() |
| submit.click(generator, inputs=[prompt], outputs=[image1, image2, image3], api_name="mmd") |
|
|
| demo.launch() |
|
|