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}")