Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,10 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
| 3 |
import os
|
| 4 |
|
| 5 |
# Fetch the API key from an environment variable
|
| 6 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
|
|
|
| 7 |
|
| 8 |
st.title("DALL-E Image Generator")
|
| 9 |
|
|
@@ -15,20 +16,18 @@ if prompt and st.button('Generate Image'):
|
|
| 15 |
st.error("No OpenAI API key found. Please set the OPENAI_API_KEY environment variable.")
|
| 16 |
st.stop()
|
| 17 |
|
| 18 |
-
client = OpenAI(api_key=openai_api_key)
|
| 19 |
-
|
| 20 |
# Use a spinner to indicate that the image is being generated
|
| 21 |
with st.spinner('Generating image...'):
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import openai
|
| 3 |
import os
|
| 4 |
|
| 5 |
# Fetch the API key from an environment variable
|
| 6 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 7 |
+
openai.api_key = openai_api_key
|
| 8 |
|
| 9 |
st.title("DALL-E Image Generator")
|
| 10 |
|
|
|
|
| 16 |
st.error("No OpenAI API key found. Please set the OPENAI_API_KEY environment variable.")
|
| 17 |
st.stop()
|
| 18 |
|
|
|
|
|
|
|
| 19 |
# Use a spinner to indicate that the image is being generated
|
| 20 |
with st.spinner('Generating image...'):
|
| 21 |
+
# Ensure you use the correct function based on your API documentation. Below is for demonstration.
|
| 22 |
+
try:
|
| 23 |
+
response = openai.Image.create(
|
| 24 |
+
model="image-dalle-002", # Use the correct model identifier
|
| 25 |
+
prompt=prompt,
|
| 26 |
+
n=1,
|
| 27 |
+
size="1024x1024"
|
| 28 |
+
)
|
| 29 |
+
image_url = response['data'][0]['url'] # Make sure response structure is correct
|
| 30 |
+
st.image(image_url, caption="Generated Image")
|
| 31 |
+
except Exception as e:
|
| 32 |
+
st.error(f"Failed to generate an image: {e}")
|
| 33 |
|