Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import openai
|
| 3 |
+
import requests
|
| 4 |
+
from io import BytesIO
|
| 5 |
+
|
| 6 |
+
# Streamlit UI for input
|
| 7 |
+
st.title("Generate an Image with DALL-E 3")
|
| 8 |
+
|
| 9 |
+
# Input for OpenAI API key
|
| 10 |
+
api_key = st.text_input("Enter your OpenAI API Key:", type="password")
|
| 11 |
+
|
| 12 |
+
# Check if API key is provided
|
| 13 |
+
if api_key:
|
| 14 |
+
# Set the API key directly in the OpenAI library
|
| 15 |
+
openai.api_key = api_key
|
| 16 |
+
|
| 17 |
+
# Prompt input field
|
| 18 |
+
prompt = st.text_area("Enter your prompt:")
|
| 19 |
+
|
| 20 |
+
# Variables to store the generated image
|
| 21 |
+
image_url = None
|
| 22 |
+
image_data = None
|
| 23 |
+
|
| 24 |
+
# Button to generate image
|
| 25 |
+
if st.button("Generate Image"):
|
| 26 |
+
if prompt:
|
| 27 |
+
st.write("Generating image...")
|
| 28 |
+
try:
|
| 29 |
+
# Call the OpenAI API to generate the image
|
| 30 |
+
response = openai.Image.create(
|
| 31 |
+
prompt=prompt,
|
| 32 |
+
n=1, # Number of images to generate
|
| 33 |
+
size="1024x1024" # Available sizes: 256x256, 512x512, or 1024x1024
|
| 34 |
+
)
|
| 35 |
+
image_url = response['data'][0]['url']
|
| 36 |
+
|
| 37 |
+
# Download the image
|
| 38 |
+
image_response = requests.get(image_url)
|
| 39 |
+
image_data = BytesIO(image_response.content)
|
| 40 |
+
|
| 41 |
+
# Display the image
|
| 42 |
+
st.image(image_url, caption="Generated Image", use_column_width=True)
|
| 43 |
+
st.write("Image URL: " + image_url)
|
| 44 |
+
except Exception as e:
|
| 45 |
+
st.error(f"An error occurred: {e}")
|
| 46 |
+
else:
|
| 47 |
+
st.warning("Please enter a prompt.")
|
| 48 |
+
|
| 49 |
+
# Button to download the image
|
| 50 |
+
if image_data:
|
| 51 |
+
st.download_button(
|
| 52 |
+
label="Download Image",
|
| 53 |
+
data=image_data,
|
| 54 |
+
file_name="generated_image.png",
|
| 55 |
+
mime="image/png"
|
| 56 |
+
)
|
| 57 |
+
else:
|
| 58 |
+
st.warning("Please enter your OpenAI API Key.")
|