Spaces:
Sleeping
Sleeping
ericwangpq
commited on
Commit
•
83f7af5
1
Parent(s):
48674cd
first commit
Browse files
main.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import sys
|
3 |
+
|
4 |
+
import gradio as gr
|
5 |
+
from openai import OpenAI
|
6 |
+
from dotenv import load_dotenv
|
7 |
+
|
8 |
+
load_dotenv()
|
9 |
+
|
10 |
+
openai_key = os.getenv("OPENAI_KEY")
|
11 |
+
|
12 |
+
if openai_key == "<YOUR_OPENAI_KEY>":
|
13 |
+
openai_key = ""
|
14 |
+
|
15 |
+
if openai_key == "":
|
16 |
+
sys.exit("Please Provide Your OpenAI API Key")
|
17 |
+
|
18 |
+
|
19 |
+
def generate_image(text, model, quality, size):
|
20 |
+
try:
|
21 |
+
client = OpenAI(api_key=openai_key)
|
22 |
+
|
23 |
+
response = client.images.generate(
|
24 |
+
prompt=text,
|
25 |
+
model=model,
|
26 |
+
quality=quality,
|
27 |
+
size=size,
|
28 |
+
n=1,
|
29 |
+
)
|
30 |
+
except Exception as error:
|
31 |
+
print(str(error))
|
32 |
+
raise gr.Error("An error occurred while generating speech. Please check your API key and come back try again.")
|
33 |
+
|
34 |
+
return response.data[0].url
|
35 |
+
|
36 |
+
|
37 |
+
with gr.Blocks() as demo:
|
38 |
+
gr.Markdown("# <center> OpenAI Image Generate API with Gradio </center>")
|
39 |
+
with gr.Row(variant="panel"):
|
40 |
+
model = gr.Dropdown(choices=["dall-e-2", "dall-e-3"], label="Model", value="dall-e-3")
|
41 |
+
quality = gr.Dropdown(choices=["standard", "hd"], label="Quality", value="standard")
|
42 |
+
size = gr.Dropdown(choices=["256x256", "512x512", "1024x1024", "1792x1024", "1024x1792"], label="Size",
|
43 |
+
value="1024x1024")
|
44 |
+
|
45 |
+
text = gr.Textbox(label="Input Text",
|
46 |
+
placeholder="Enter your text and then click on the \"Image Generate\" button, "
|
47 |
+
"or simply press the Enter key.")
|
48 |
+
btn = gr.Button("Image Generate")
|
49 |
+
output_image = gr.Image(label="Image Output")
|
50 |
+
|
51 |
+
text.submit(fn=generate_image, inputs=[text, model, quality, size], outputs=output_image, api_name="generate_image")
|
52 |
+
btn.click(fn=generate_image, inputs=[text, model, quality, size], outputs=output_image, api_name=False)
|
53 |
+
|
54 |
+
demo.launch()
|