File size: 1,220 Bytes
22ed61b 9584cec 22ed61b 9584cec 22ed61b 10bddd4 22ed61b 9584cec 838239b 9584cec 22ed61b |
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 |
import streamlit as st
import openai
import os
# Fetch the API key from an environment variable
openai_api_key = os.getenv("OPENAI_API_KEY")
openai.api_key = openai_api_key
st.title("PromptingAI.io Image Generator")
# User inputs a text description for the image
prompt = st.text_input("Enter a description for the image you want to generate:")
if prompt and st.button('Generate Image'):
if not openai_api_key:
st.error("No OpenAI API key found. Please set the OPENAI_API_KEY environment variable.")
st.stop()
# Use a spinner to indicate that the image is being generated
with st.spinner('Generating image...'):
# Ensure you use the correct function based on your API documentation. Below is for demonstration.
try:
response = openai.Image.create(
model="dall-e-3", # Use the correct model identifier
prompt=prompt,
n=1,
size="1024x1024"
)
image_url = response['data'][0]['url'] # Make sure response structure is correct
st.image(image_url, caption="Generated Image")
except Exception as e:
st.error(f"Failed to generate an image: {e}")
|