Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
|
| 4 |
+
# Initialize OpenAI client with secret key from Hugging Face space settings
|
| 5 |
+
client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"])
|
| 6 |
+
|
| 7 |
+
# Sample prompt
|
| 8 |
+
sample_prompt = (
|
| 9 |
+
"A highly detailed, realistic architectural floor plan for a modern tiny home. "
|
| 10 |
+
"The design features an open-concept layout with a multi-functional living space "
|
| 11 |
+
"that combines a compact living area, dining space, and efficient kitchen with smart storage. "
|
| 12 |
+
"A cozy loft bedroom is accessible via a sleek staircase or ladder. The minimalist bathroom "
|
| 13 |
+
"includes a shower. Emphasize large windows with natural light, clean lines, and neutral tones "
|
| 14 |
+
"with subtle accents. Annotate key dimensions and furniture placements. Professional architectural rendering style."
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
# UI
|
| 18 |
+
st.title("🧠 OpenAI Image Generator")
|
| 19 |
+
st.markdown("Generate images using DALL·E 3 based on your prompt.")
|
| 20 |
+
|
| 21 |
+
# Input with optional sample prompt
|
| 22 |
+
user_prompt = st.text_area("Enter your prompt:", height=200)
|
| 23 |
+
|
| 24 |
+
if st.button("Insert Sample Prompt"):
|
| 25 |
+
st.session_state['prompt'] = sample_prompt
|
| 26 |
+
st.experimental_rerun()
|
| 27 |
+
|
| 28 |
+
# Maintain prompt in session state
|
| 29 |
+
user_prompt = st.session_state.get('prompt', user_prompt)
|
| 30 |
+
|
| 31 |
+
# Display updated prompt
|
| 32 |
+
st.text_area("Current prompt:", user_prompt, height=200, disabled=True)
|
| 33 |
+
|
| 34 |
+
if st.button("Generate Image") and user_prompt.strip():
|
| 35 |
+
with st.spinner("Generating image..."):
|
| 36 |
+
try:
|
| 37 |
+
response = client.images.generate(
|
| 38 |
+
model="dall-e-3",
|
| 39 |
+
prompt=user_prompt,
|
| 40 |
+
size="1024x1024",
|
| 41 |
+
quality="standard",
|
| 42 |
+
n=1
|
| 43 |
+
)
|
| 44 |
+
image_url = response.data[0].url
|
| 45 |
+
st.image(image_url, caption="Generated Image", use_column_width=True)
|
| 46 |
+
st.markdown(f"[Download Image]({image_url})", unsafe_allow_html=True)
|
| 47 |
+
except Exception as e:
|
| 48 |
+
st.error(f"Error: {e}")
|
| 49 |
+
else:
|
| 50 |
+
st.info("Enter a prompt or insert a sample to get started.")
|