Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import streamlit as st
|
2 |
import openai
|
3 |
-
|
|
|
4 |
|
5 |
# Streamlit UI for input
|
6 |
st.title("Generate an Image with DALL-E 3")
|
@@ -12,30 +13,48 @@ api_key = st.text_input("Enter your OpenAI API Key:", type="password")
|
|
12 |
if api_key:
|
13 |
# Set the API key directly in the OpenAI library
|
14 |
openai.api_key = api_key
|
15 |
-
|
16 |
-
|
17 |
# Prompt input field
|
18 |
prompt = st.text_area("Enter your prompt:")
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
else:
|
41 |
st.warning("Please enter your OpenAI API Key.")
|
|
|
1 |
import streamlit as st
|
2 |
import openai
|
3 |
+
import requests
|
4 |
+
from io import BytesIO
|
5 |
|
6 |
# Streamlit UI for input
|
7 |
st.title("Generate an Image with DALL-E 3")
|
|
|
13 |
if api_key:
|
14 |
# Set the API key directly in the OpenAI library
|
15 |
openai.api_key = api_key
|
16 |
+
|
|
|
17 |
# Prompt input field
|
18 |
prompt = st.text_area("Enter your prompt:")
|
19 |
+
|
20 |
+
# Variables to store the generated image
|
21 |
+
image_url = None
|
22 |
+
image_data = None
|
23 |
+
|
24 |
+
# Button to generate image
|
25 |
+
if st.button("Generate Image"):
|
26 |
+
if prompt:
|
27 |
+
st.write("Generating image with DALL-E 3...")
|
28 |
+
try:
|
29 |
+
# Call the OpenAI API to generate the image
|
30 |
+
response = openai.Image.create(
|
31 |
+
model="dall-e-3", # Explicitly using DALL-E 3
|
32 |
+
prompt=prompt,
|
33 |
+
n=1, # Number of images to generate
|
34 |
+
size="1024x1024", # Available sizes: 256x256, 512x512, or 1024x1024
|
35 |
+
quality="standard" # Specify image quality: "standard" or "hd"
|
36 |
+
)
|
37 |
+
image_url = response['data'][0]['url']
|
38 |
+
|
39 |
+
# Download the image
|
40 |
+
image_response = requests.get(image_url)
|
41 |
+
image_data = BytesIO(image_response.content)
|
42 |
+
|
43 |
+
# Display the image
|
44 |
+
st.image(image_url, caption="Generated Image", use_column_width=True)
|
45 |
+
st.write("Image URL: " + image_url)
|
46 |
+
except Exception as e:
|
47 |
+
st.error(f"An error occurred: {e}")
|
48 |
+
else:
|
49 |
+
st.warning("Please enter a prompt.")
|
50 |
+
|
51 |
+
# Button to download the image
|
52 |
+
if image_data:
|
53 |
+
st.download_button(
|
54 |
+
label="Download Image",
|
55 |
+
data=image_data,
|
56 |
+
file_name="generated_image.png",
|
57 |
+
mime="image/png"
|
58 |
+
)
|
59 |
else:
|
60 |
st.warning("Please enter your OpenAI API Key.")
|