Spaces:
Runtime error
Runtime error
Commit
·
457709c
1
Parent(s):
7535077
Update app.py
Browse files
app.py
CHANGED
@@ -1,18 +1,55 @@
|
|
1 |
import openai
|
|
|
|
|
2 |
import gradio as gr
|
3 |
|
4 |
openai.api_key = 'sk-AAXKXTDNEpUTHQoQUJMHLrErGJyHg89uy71MyuHHEIGBRZ78'
|
5 |
|
6 |
-
def
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
iface = gr.Interface(
|
11 |
-
fn=
|
12 |
inputs="text",
|
13 |
outputs="image",
|
14 |
-
title="
|
15 |
-
description="Enter a prompt to generate
|
|
|
|
|
|
|
|
|
|
|
16 |
)
|
17 |
|
18 |
iface.launch()
|
|
|
1 |
import openai
|
2 |
+
import requests
|
3 |
+
from io import BytesIO
|
4 |
import gradio as gr
|
5 |
|
6 |
openai.api_key = 'sk-AAXKXTDNEpUTHQoQUJMHLrErGJyHg89uy71MyuHHEIGBRZ78'
|
7 |
|
8 |
+
def generate_image(prompt):
|
9 |
+
# Use GPT-3 to generate a description of the image
|
10 |
+
model_engine = "text-davinci-002"
|
11 |
+
description = openai.Completion.create(
|
12 |
+
engine=model_engine,
|
13 |
+
prompt=f"Generate an image of {prompt}",
|
14 |
+
max_tokens=256,
|
15 |
+
n=1,
|
16 |
+
stop=None,
|
17 |
+
temperature=0.5
|
18 |
+
).choices[0].text.strip()
|
19 |
+
|
20 |
+
# Use DALL-E to generate an image based on the description
|
21 |
+
response = requests.post(
|
22 |
+
"https://api.openai.com/v1/images/generations",
|
23 |
+
headers={
|
24 |
+
"Content-Type": "application/json",
|
25 |
+
"Authorization": f"Bearer {openai.api_key}"
|
26 |
+
},
|
27 |
+
json={
|
28 |
+
"model": "image-alpha-001",
|
29 |
+
"prompt": description,
|
30 |
+
"num_images": 1,
|
31 |
+
"size": "1024x1024",
|
32 |
+
"response_format": "url"
|
33 |
+
}
|
34 |
+
)
|
35 |
+
|
36 |
+
# Parse the image URL from the API response and return the image
|
37 |
+
image_url = response.json()["data"][0]["url"]
|
38 |
+
response = requests.get(image_url)
|
39 |
+
image = BytesIO(response.content)
|
40 |
+
return image
|
41 |
|
42 |
iface = gr.Interface(
|
43 |
+
fn=generate_image,
|
44 |
inputs="text",
|
45 |
outputs="image",
|
46 |
+
title="Text to Image Generator",
|
47 |
+
description="Enter a prompt to generate an image",
|
48 |
+
examples=[
|
49 |
+
["a red apple on a white background"],
|
50 |
+
["a yellow sun setting over a mountain range"],
|
51 |
+
["a futuristic cityscape at night"]
|
52 |
+
]
|
53 |
)
|
54 |
|
55 |
iface.launch()
|