| import streamlit as st |
| import openai |
| import requests |
| from io import BytesIO |
|
|
| |
| st.title("Generate an Image with DALL-E 3") |
|
|
| |
| api_key = st.text_input("Enter your OpenAI API Key:", type="password") |
|
|
| |
| if api_key: |
| |
| openai.api_key = api_key |
|
|
| |
| prompt = st.text_area("Enter your prompt:") |
|
|
| |
| quality = st.selectbox( |
| "Select image quality:", |
| options=["standard", "hd"], |
| index=0 |
| ) |
|
|
| |
| image_url = None |
| image_data = None |
|
|
| |
| if st.button("Generate Image"): |
| if prompt: |
| st.write(f"Generating {quality}-quality image with DALL-E 3...") |
| try: |
| |
| response = openai.Image.create( |
| model="dall-e-3", |
| prompt=prompt, |
| n=1, |
| size="1024x1024", |
| quality=quality |
| ) |
| image_url = response['data'][0]['url'] |
|
|
| |
| image_response = requests.get(image_url) |
| image_data = BytesIO(image_response.content) |
|
|
| |
| 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.") |
|
|
| |
| 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.") |
|
|