File size: 1,282 Bytes
a72052a
f62e9ce
a72052a
 
 
 
 
 
b602d4e
a72052a
 
 
b602d4e
 
a72052a
 
 
 
 
 
 
b602d4e
a72052a
 
f62e9ce
a72052a
f62e9ce
 
a72052a
 
f62e9ce
a72052a
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import streamlit as st
import openai
from dotenv import load_dotenv

# Streamlit UI for input
st.title("Generate an Image with DALL-E 3")

# Input for OpenAI API key
api_key = st.text_input("Enter your OpenAI API Key:", type="password")

# Check if API key is provided
if api_key:
    # Set the API key directly in the OpenAI library
    openai.api_key = api_key
    load_dotenv()  # Load environment variables if needed
    
    # Prompt input field
    prompt = st.text_area("Enter your prompt:")
    
    if prompt:
        st.write("Generating image...")

        # Call the OpenAI API to generate the image
        try:
            response = openai.Image.create(
                prompt=prompt,
                n=1,  # Number of images to generate
                size="1024x1024"  # Available sizes: 256x256, 512x512, or 1024x1024
            )
            
            image_url = response['data'][0]['url']
            
            # Display the image
            st.image(image_url, caption="Generated Image", use_column_width=True)
            st.write("Image URL: " + image_url)
        except Exception as e:
            st.error(f"An error occurred: {e}")
    else:
        st.warning("Please enter a prompt.")
else:
    st.warning("Please enter your OpenAI API Key.")