bizvideoschool's picture
Update app.py
c3fad62
raw
history blame
No virus
2.42 kB
import streamlit as st
import openai
import requests
from PIL import Image
from io import BytesIO
# Set up the OpenAI API key from Hugging Face Spaces secrets
openai.api_key = st.secrets["OPENAI_API_KEY"]
st.title("Holiday Video Backdrop Image Generator")
# User inputs for image generation
image_theme = st.text_input("Image Theme", placeholder="Enter a theme for your holiday video backdrop (e.g., festive living room, snowy landscape)")
color_scheme = st.text_input("Color Scheme", placeholder="Preferred color scheme (e.g., red and green, white and blue)")
additional_elements = st.text_input("Additional Elements", placeholder="Any specific elements to include in the image (e.g., Christmas tree, snowflakes)")
# Dropdown for selecting image dimensions
video_format = st.selectbox("Video Format", ["16:9 (Standard HD - 1920x1080)", "16:9 (4K - 3840x2160)", "1:1 (Square - 1080x1080)", "4:5 (Portrait - 1080x1350)", "9:16 (Tall - 1080x1920)"])
# Mapping formats to DALL-E API size parameters
format_size_mapping = {
"16:9 (Standard HD - 1920x1080)": "1024x576", # Closest available size in DALL-E for 1080p
"16:9 (4K - 3840x2160)": "1792x1024", # Closest available size in DALL-E for 4K
"1:1 (Square - 1080x1080)": "1024x1024",
"4:5 (Portrait - 1080x1350)": "1024x1792", # Closest available vertical size
"9:16 (Tall - 1080x1920)": "1024x1792" # Closest available vertical size
}
if st.button('Generate Image'):
# Construct the prompt
prompt = f"A holiday-themed backdrop image depicting: '{image_theme}', color scheme: '{color_scheme}', including elements: '{additional_elements}'."
# Call the DALL-E API
try:
response = openai.Image.create(
model="dall-e-3", # Specify DALL-E 3 model
prompt=prompt,
n=1,
size=format_size_mapping[video_format], # Set image dimensions based on selected video format
quality="hd" # Set image quality to high definition
)
# Assuming the response contains a URL to the image
image_url = response['data'][0]['url']
# Fetch the image from the URL
image_response = requests.get(image_url)
image = Image.open(BytesIO(image_response.content))
# Display the image
st.image(image, caption='Generated Holiday Backdrop Image')
except Exception as e:
st.error(f"An error occurred: {e}")