Promotingai commited on
Commit
22ed61b
·
verified ·
1 Parent(s): 85ed1d3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -0
app.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
10
+ # User inputs a text description for the image
11
+ prompt = st.text_input("Enter a description for the image you want to generate:")
12
+
13
+ if prompt and st.button('Generate Image'):
14
+ if not openai_api_key:
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
+