Promotingai commited on
Commit
9584cec
·
verified ·
1 Parent(s): 8381ff6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -15
app.py CHANGED
@@ -1,9 +1,10 @@
1
  import streamlit as st
2
- from openai 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
 
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
- response = client.Image.create(
23
- model="dall-e-2",
24
- prompt=prompt,
25
- n=1,
26
- size="1024x1024"
27
- )
28
-
29
- # Get the image URL from the response
30
- image_url = response.data[0].url
31
-
32
- # Display the generated image
33
- st.image(image_url, caption="Generated Image")
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