imageGen / app.py
hfariborzi's picture
Update app.py
006dc33 verified
import streamlit as st
from openai import OpenAI
# Initialize OpenAI client using Hugging Face secrets
client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"])
# Sample architectural prompt
sample_prompt = (
"A highly detailed, realistic architectural floor plan for a modern tiny home. "
"The design features an open-concept layout with a multi-functional living space "
"that combines a compact living area, dining space, and efficient kitchen with smart storage. "
"A cozy loft bedroom is accessible via a sleek staircase or ladder. The minimalist bathroom "
"includes a shower. Emphasize large windows with natural light, clean lines, and neutral tones "
"with subtle accents. Annotate key dimensions and furniture placements. Professional architectural rendering style."
)
# Session state for user prompt
if "prompt" not in st.session_state:
st.session_state.prompt = ""
st.title("🏠 OpenAI Image Generator: Tiny Home Floor Plan")
# Prompt input area
st.text_area("Enter your prompt:", key="prompt_input", value=st.session_state.prompt, height=200)
# Button to insert sample
if st.button("Insert Sample Prompt"):
st.session_state.prompt = sample_prompt
st.rerun()
# Sync prompt state
st.session_state.prompt = st.session_state.prompt_input
# Display the current prompt as read-only
st.text_area("Current prompt being used:", st.session_state.prompt, height=200, disabled=True)
# Generate image
if st.button("Generate Image"):
if st.session_state.prompt.strip():
with st.spinner("Generating image..."):
try:
response = client.images.generate(
model="dall-e-3",
prompt=st.session_state.prompt,
size="1024x1024",
quality="standard",
n=1
)
image_url = response.data[0].url
st.image(image_url, caption="Generated Image", use_column_width=True)
st.markdown(f"[Download Image]({image_url})", unsafe_allow_html=True)
except Exception as e:
st.error(f"Error: {e}")
else:
st.warning("Please enter a valid prompt.")