File size: 2,115 Bytes
a72052a f62e9ce 34feb65 a72052a b602d4e a72052a b602d4e 34feb65 a72052a 34feb65 19ef599 34feb65 19ef599 34feb65 19ef599 34feb65 85e79f9 34feb65 19ef599 34feb65 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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
import streamlit as st
import openai
import requests
from io import BytesIO
# 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
# Prompt input field
prompt = st.text_area("Enter your prompt:")
# Quality selection dropdown
quality = st.selectbox(
"Select image quality:",
options=["standard", "hd"],
index=0 # Default to "standard"
)
# Variables to store the generated image
image_url = None
image_data = None
# Button to generate image
if st.button("Generate Image"):
if prompt:
st.write(f"Generating {quality}-quality image with DALL-E 3...")
try:
# Call the OpenAI API to generate the image
response = openai.Image.create(
model="dall-e-3", # Explicitly using DALL-E 3
prompt=prompt,
n=1, # Number of images to generate
size="1024x1024", # Available sizes: 256x256, 512x512, or 1024x1024
quality=quality # Use selected quality
)
image_url = response['data'][0]['url']
# Download the image
image_response = requests.get(image_url)
image_data = BytesIO(image_response.content)
# Display the image
st.image(image_url, caption="Generated Image", use_container_width=True)
except Exception as e:
st.error(f"An error occurred: {e}")
else:
st.warning("Please enter a prompt.")
# Button to download the image
if image_data:
st.download_button(
label="Download Image",
data=image_data,
file_name=f"generated_image_{quality}.png",
mime="image/png"
)
else:
st.warning("Please enter your OpenAI API Key.")
|