File size: 1,843 Bytes
a2fa8e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import requests
import gradio as gr

def generate_image(prompt, quality, size, style, api_key, request_url):
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json",
    }
    data = {
        "prompt": prompt,
        "model": "dall-e-3",
        "quality": quality,
        "size": size,
        "style": style,
        "num_images": 1,
    }
    response = requests.post(request_url, headers=headers, json=data)
    response_json = response.json()
    
    # Get the URL of the first generated image
    image_url = response_json["data"][0]["url"]
    
    return image_url

# Create the interface using Gradio
with gr.Blocks() as demo:
    with gr.Row():
        gr.Markdown("# <center> OpenAI dall-e-3 API with Gradio </center>")
        gr.Markdown("This demo uses the OpenAI dall-e-3 API to generate an image from text. You get a free session key from https://dongsiqie.me/sess")
    with gr.Row():
        api_key_input = gr.Textbox(label="API Key", type="password")
        request_url_input = gr.Textbox(label="Request URL", value="https://api.openai.com/v1/images/generations")
        quality_input = gr.Dropdown(label="Quality", choices=["standard", "hd"], value="standard")
        size_input = gr.Dropdown(label="Size", choices=["1792x1024", "1024x1024", "1024x1792"], value="1024x1024")
        style_input = gr.Dropdown(label="Style", choices=["vivid", "natural"], value="vivid")
    with gr.Row():
        prompt_input = gr.Textbox(label="Image Description", value="A cute cat.")
    submit_btn = gr.Button("Generate Image", variant='primary')
    image_output = gr.Image(label="Generated Image")
    
    submit_btn.click(fn=generate_image, inputs=[prompt_input, quality_input, size_input, style_input, api_key_input, request_url_input], outputs=image_output)

demo.launch()