Spaces:
Runtime error
Runtime error
image gen with options
Browse files- .gitignore +1 -0
- app.py +51 -4
- env-sample +1 -0
- requirements.txt +3 -0
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
.env
|
app.py
CHANGED
@@ -1,7 +1,54 @@
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
5 |
|
6 |
-
|
7 |
-
iface.launch()
|
|
|
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_API_KEY")
|
11 |
+
|
12 |
+
if openai_key == "<YOUR_OPENAI_API_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=["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(share=True)
|
|
env-sample
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
OPENAI_API_KEY = YOUR_API_KEY
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
gradio==4.2.0
|
2 |
+
openai==1.2.3
|
3 |
+
python-dotenv==1.0.0
|