Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
def generate_image(prompt, quality, size, style, api_key, request_url):
|
| 5 |
+
headers = {
|
| 6 |
+
"Authorization": f"Bearer {api_key}",
|
| 7 |
+
"Content-Type": "application/json",
|
| 8 |
+
}
|
| 9 |
+
data = {
|
| 10 |
+
"prompt": prompt,
|
| 11 |
+
"model": "dall-e-3",
|
| 12 |
+
"quality": quality,
|
| 13 |
+
"size": size,
|
| 14 |
+
"style": style,
|
| 15 |
+
"num_images": 1,
|
| 16 |
+
}
|
| 17 |
+
response = requests.post(request_url, headers=headers, json=data)
|
| 18 |
+
response_json = response.json()
|
| 19 |
+
|
| 20 |
+
# Get the URL of the first generated image
|
| 21 |
+
image_url = response_json["data"][0]["url"]
|
| 22 |
+
|
| 23 |
+
return image_url
|
| 24 |
+
|
| 25 |
+
# Create the interface using Gradio
|
| 26 |
+
with gr.Blocks() as demo:
|
| 27 |
+
with gr.Row():
|
| 28 |
+
gr.Markdown("# <center> OpenAI dall-e-3 API with Gradio </center>")
|
| 29 |
+
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")
|
| 30 |
+
with gr.Row():
|
| 31 |
+
api_key_input = gr.Textbox(label="API Key", type="password")
|
| 32 |
+
request_url_input = gr.Textbox(label="Request URL", value="https://api.openai.com/v1/images/generations")
|
| 33 |
+
quality_input = gr.Dropdown(label="Quality", choices=["standard", "hd"], value="standard")
|
| 34 |
+
size_input = gr.Dropdown(label="Size", choices=["1792x1024", "1024x1024", "1024x1792"], value="1024x1024")
|
| 35 |
+
style_input = gr.Dropdown(label="Style", choices=["vivid", "natural"], value="vivid")
|
| 36 |
+
with gr.Row():
|
| 37 |
+
prompt_input = gr.Textbox(label="Image Description", value="A cute cat.")
|
| 38 |
+
submit_btn = gr.Button("Generate Image", variant='primary')
|
| 39 |
+
image_output = gr.Image(label="Generated Image")
|
| 40 |
+
|
| 41 |
+
submit_btn.click(fn=generate_image, inputs=[prompt_input, quality_input, size_input, style_input, api_key_input, request_url_input], outputs=image_output)
|
| 42 |
+
|
| 43 |
+
demo.launch()
|