Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
+
import os
|
| 7 |
+
from datetime import datetime
|
| 8 |
+
|
| 9 |
+
def save_image_from_url(image_url, prompt):
|
| 10 |
+
"""Save image from URL to local file with prompt as part of filename"""
|
| 11 |
+
# Create directory if it doesn't exist
|
| 12 |
+
if not os.path.exists("generated_images"):
|
| 13 |
+
os.makedirs("generated_images")
|
| 14 |
+
|
| 15 |
+
# Download image
|
| 16 |
+
response = requests.get(image_url)
|
| 17 |
+
|
| 18 |
+
# Create filename from timestamp and shortened prompt
|
| 19 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 20 |
+
# Clean prompt for filename (remove special characters and limit length)
|
| 21 |
+
clean_prompt = "".join(c for c in prompt if c.isalnum() or c.isspace())[:30]
|
| 22 |
+
clean_prompt = clean_prompt.replace(" ", "_")
|
| 23 |
+
|
| 24 |
+
output_path = f"generated_images/{timestamp}_{clean_prompt}.png"
|
| 25 |
+
|
| 26 |
+
with open(output_path, "wb") as f:
|
| 27 |
+
f.write(response.content)
|
| 28 |
+
return output_path
|
| 29 |
+
|
| 30 |
+
def main():
|
| 31 |
+
st.title("DALL-E 2 Image Generator")
|
| 32 |
+
|
| 33 |
+
# Sidebar for API key
|
| 34 |
+
st.sidebar.header("Settings")
|
| 35 |
+
api_key = st.sidebar.text_input("Enter OpenAI API Key", type="password")
|
| 36 |
+
|
| 37 |
+
if not api_key:
|
| 38 |
+
st.warning("Please enter your OpenAI API key in the sidebar to continue.")
|
| 39 |
+
return
|
| 40 |
+
|
| 41 |
+
# Main content
|
| 42 |
+
st.write("Generate images using DALL-E 2")
|
| 43 |
+
|
| 44 |
+
# Image generation form
|
| 45 |
+
with st.form("image_generation_form"):
|
| 46 |
+
# Prompt input
|
| 47 |
+
prompt = st.text_area("Enter your prompt",
|
| 48 |
+
help="Describe the image you want to generate",
|
| 49 |
+
max_chars=1000)
|
| 50 |
+
|
| 51 |
+
# Generation options
|
| 52 |
+
col1, col2, col3 = st.columns(3)
|
| 53 |
+
|
| 54 |
+
with col1:
|
| 55 |
+
size_options = ["1024x1024", "512x512", "256x256"]
|
| 56 |
+
size = st.selectbox("Image size", size_options)
|
| 57 |
+
|
| 58 |
+
with col2:
|
| 59 |
+
quality = st.selectbox("Image quality", ["standard", "hd"])
|
| 60 |
+
|
| 61 |
+
with col3:
|
| 62 |
+
n_images = st.slider("Number of images", min_value=1, max_value=4, value=1)
|
| 63 |
+
|
| 64 |
+
# Submit button
|
| 65 |
+
submit_button = st.form_submit_button("Generate Images")
|
| 66 |
+
|
| 67 |
+
# Handle form submission
|
| 68 |
+
if submit_button:
|
| 69 |
+
if not prompt:
|
| 70 |
+
st.error("Please enter a prompt.")
|
| 71 |
+
return
|
| 72 |
+
|
| 73 |
+
try:
|
| 74 |
+
# Initialize OpenAI client
|
| 75 |
+
client = OpenAI(api_key=api_key)
|
| 76 |
+
|
| 77 |
+
with st.spinner("Generating images..."):
|
| 78 |
+
# Generate images
|
| 79 |
+
response = client.images.generate(
|
| 80 |
+
model="dall-e-2",
|
| 81 |
+
prompt=prompt,
|
| 82 |
+
size=size,
|
| 83 |
+
quality=quality,
|
| 84 |
+
n=n_images,
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
# Display generated images
|
| 88 |
+
st.subheader("Generated Images")
|
| 89 |
+
|
| 90 |
+
# Create columns based on number of images
|
| 91 |
+
cols = st.columns(n_images)
|
| 92 |
+
|
| 93 |
+
# Display each image in its own column
|
| 94 |
+
for idx, image_data in enumerate(response.data):
|
| 95 |
+
# Save image locally
|
| 96 |
+
saved_path = save_image_from_url(image_data.url, prompt)
|
| 97 |
+
|
| 98 |
+
with cols[idx]:
|
| 99 |
+
# Display image
|
| 100 |
+
st.image(saved_path, caption=f"Generated Image {idx+1}", use_container_width=True)
|
| 101 |
+
|
| 102 |
+
# Add download button
|
| 103 |
+
with open(saved_path, "rb") as file:
|
| 104 |
+
st.download_button(
|
| 105 |
+
label=f"Download Image {idx+1}",
|
| 106 |
+
data=file,
|
| 107 |
+
file_name=f"dalle2_generation_{idx+1}.png",
|
| 108 |
+
mime="image/png"
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
# Display prompt used
|
| 112 |
+
st.info(f"Prompt used: {prompt}")
|
| 113 |
+
|
| 114 |
+
except Exception as e:
|
| 115 |
+
st.error(f"An error occurred: {str(e)}")
|
| 116 |
+
if "Rate limit" in str(e):
|
| 117 |
+
st.info("You've hit the rate limit. Please wait a moment before trying again.")
|
| 118 |
+
elif "billing" in str(e).lower():
|
| 119 |
+
st.info("Please check your OpenAI account billing status.")
|
| 120 |
+
|
| 121 |
+
if __name__ == "__main__":
|
| 122 |
+
main()
|