Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,20 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
| 2 |
from openai import OpenAI
|
| 3 |
|
|
|
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
# Initialize OpenAI client
|
| 7 |
-
client = OpenAI(api_key=
|
| 8 |
|
| 9 |
-
# Sample
|
| 10 |
sample_prompt = (
|
| 11 |
"A highly detailed, realistic architectural floor plan for a modern tiny home. "
|
| 12 |
"The design features an open-concept layout with a multi-functional living space "
|
|
@@ -16,87 +24,12 @@ sample_prompt = (
|
|
| 16 |
"with subtle accents. Annotate key dimensions and furniture placements. Professional architectural rendering style."
|
| 17 |
)
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
if "prompt" not in st.session_state:
|
| 21 |
-
st.session_state.prompt = ""
|
| 22 |
-
|
| 23 |
-
st.title("🏠 OpenAI Image Generator: Tiny Home Floor Plan")
|
| 24 |
-
|
| 25 |
-
# Prompt input area
|
| 26 |
-
st.text_area("Enter your prompt:", key="prompt_input", value=st.session_state.prompt, height=200)
|
| 27 |
-
|
| 28 |
-
# Button to insert sample
|
| 29 |
-
if st.button("Insert Sample Prompt"):
|
| 30 |
-
st.session_state.prompt = sample_prompt
|
| 31 |
-
st.rerun()
|
| 32 |
-
|
| 33 |
-
# Sync prompt state
|
| 34 |
-
st.session_state.prompt = st.session_state.prompt_input
|
| 35 |
-
|
| 36 |
-
# Display the current prompt as read-only
|
| 37 |
-
st.text_area("Current prompt being used:", st.session_state.prompt, height=200, disabled=True)
|
| 38 |
-
|
| 39 |
-
# Generate image
|
| 40 |
-
if st.button("Generate Image"):
|
| 41 |
-
if st.session_state.prompt.strip():
|
| 42 |
-
with st.spinner("Generating image..."):
|
| 43 |
-
try:
|
| 44 |
-
response = client.images.generate(
|
| 45 |
-
model="dall-e-3",
|
| 46 |
-
prompt=st.session_state.prompt,
|
| 47 |
-
size="1024x1024",
|
| 48 |
-
quality="standard",
|
| 49 |
-
n=1
|
| 50 |
-
)
|
| 51 |
-
image_url = response.data[0].url
|
| 52 |
-
st.image(image_url, caption="Generated Image", use_column_width=True)
|
| 53 |
-
st.markdown(f"[Download Image]({image_url})", unsafe_allow_html=True)
|
| 54 |
-
except Exception as e:
|
| 55 |
-
st.error(f"Error: {e}")
|
| 56 |
-
else:
|
| 57 |
-
st.warning("Please enter a valid prompt.")
|
| 58 |
-
# === Agent 3: Material Explorer ===
|
| 59 |
-
import re
|
| 60 |
-
|
| 61 |
-
your_dalle_prompt = """
|
| 62 |
-
A highly detailed, realistic architectural floor plan for a modern tiny home.
|
| 63 |
-
The design features an open-concept layout with a multi-functional living space
|
| 64 |
-
that combines a compact living area, dining space, and efficient kitchen with smart storage.
|
| 65 |
-
A cozy loft bedroom is accessible via a sleek staircase or ladder. The minimalist bathroom
|
| 66 |
-
includes a shower. Emphasize large windows with natural light, clean lines, and neutral tones
|
| 67 |
-
with subtle accents. Annotate key dimensions and furniture placements.
|
| 68 |
-
Professional architectural rendering style.
|
| 69 |
-
"""
|
| 70 |
-
material_prompt = f"""
|
| 71 |
-
From the following blueprint prompt and layout concept:
|
| 72 |
-
|
| 73 |
-
Blueprint Prompt:
|
| 74 |
-
\"\"\"{your_dalle_prompt}\"\"\"
|
| 75 |
-
|
| 76 |
-
Return 5 key building materials implied by the layout, even if not explicitly listed.
|
| 77 |
-
|
| 78 |
-
Respond ONLY with a raw JSON list like:
|
| 79 |
-
["Concrete slab foundation", "Metal roofing", "Plywood sheathing", "Double-pane windows", "Insulation"]
|
| 80 |
-
"""
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
import streamlit as st
|
| 84 |
-
import requests
|
| 85 |
-
from bs4 import BeautifulSoup
|
| 86 |
-
import os
|
| 87 |
-
|
| 88 |
-
# Load Zyte API key from environment
|
| 89 |
-
ZYTE_API_KEY = os.getenv("ZYTE_API_KEY")
|
| 90 |
-
ZYTE_API_URL = "https://api.zyte.com/v1/extract"
|
| 91 |
-
|
| 92 |
-
# Hardcoded Home Depot URL
|
| 93 |
LAMINATE_FLOORING_URL = "https://www.homedepot.com/b/Flooring-Laminate-Flooring/N-5yc1vZare1"
|
| 94 |
|
| 95 |
-
# Function to call Zyte API and return rendered HTML
|
| 96 |
def zyte_scrape(url):
|
| 97 |
headers = {"Content-Type": "application/json"}
|
| 98 |
payload = {"url": url, "browserHtml": True}
|
| 99 |
-
|
| 100 |
response = requests.post(ZYTE_API_URL, headers=headers, json=payload, auth=(ZYTE_API_KEY, ""))
|
| 101 |
if response.status_code == 200:
|
| 102 |
return response.json()["browserHtml"]
|
|
@@ -104,7 +37,6 @@ def zyte_scrape(url):
|
|
| 104 |
st.error(f"Zyte error: {response.status_code}")
|
| 105 |
return None
|
| 106 |
|
| 107 |
-
# Function to parse the Home Depot laminate flooring page
|
| 108 |
def parse_home_depot(html):
|
| 109 |
soup = BeautifulSoup(html, 'html.parser')
|
| 110 |
product_cards = soup.find_all('div', class_='product-pod')
|
|
@@ -114,17 +46,55 @@ def parse_home_depot(html):
|
|
| 114 |
title_tag = card.find('span', class_='product-pod__title__product')
|
| 115 |
price_dollars = card.find('span', class_='price__dollars')
|
| 116 |
price_cents = card.find('span', class_='price__cents')
|
| 117 |
-
|
| 118 |
if title_tag and price_dollars:
|
| 119 |
name = title_tag.text.strip()
|
| 120 |
price = f"${price_dollars.text.strip()}.{price_cents.text.strip() if price_cents else '00'}"
|
| 121 |
results.append({"name": name, "price": price})
|
| 122 |
-
|
| 123 |
return results
|
| 124 |
|
| 125 |
-
# Streamlit
|
| 126 |
-
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
st.markdown("Browse current **laminate flooring** options from Home Depot.")
|
| 129 |
|
| 130 |
if st.button("Get Laminate Flooring Options"):
|
|
@@ -139,6 +109,3 @@ def main():
|
|
| 139 |
st.warning("No products found. Parsing may need updating.")
|
| 140 |
else:
|
| 141 |
st.error("Failed to scrape the page.")
|
| 142 |
-
|
| 143 |
-
if __name__ == "__main__":
|
| 144 |
-
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
from bs4 import BeautifulSoup
|
| 4 |
+
import os
|
| 5 |
from openai import OpenAI
|
| 6 |
|
| 7 |
+
# === SETUP ===
|
| 8 |
|
| 9 |
+
# Load API keys
|
| 10 |
+
ZYTE_API_KEY = os.getenv("ZYTE_API_KEY")
|
| 11 |
+
ZYTE_API_URL = "https://api.zyte.com/v1/extract"
|
| 12 |
+
OPENAI_API_KEY = st.secrets["OPENAI_API_KEY"]
|
| 13 |
|
| 14 |
+
# Initialize OpenAI client
|
| 15 |
+
client = OpenAI(api_key=OPENAI_API_KEY)
|
| 16 |
|
| 17 |
+
# Sample prompt for Agent 1
|
| 18 |
sample_prompt = (
|
| 19 |
"A highly detailed, realistic architectural floor plan for a modern tiny home. "
|
| 20 |
"The design features an open-concept layout with a multi-functional living space "
|
|
|
|
| 24 |
"with subtle accents. Annotate key dimensions and furniture placements. Professional architectural rendering style."
|
| 25 |
)
|
| 26 |
|
| 27 |
+
# === Agent 3 Setup ===
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
LAMINATE_FLOORING_URL = "https://www.homedepot.com/b/Flooring-Laminate-Flooring/N-5yc1vZare1"
|
| 29 |
|
|
|
|
| 30 |
def zyte_scrape(url):
|
| 31 |
headers = {"Content-Type": "application/json"}
|
| 32 |
payload = {"url": url, "browserHtml": True}
|
|
|
|
| 33 |
response = requests.post(ZYTE_API_URL, headers=headers, json=payload, auth=(ZYTE_API_KEY, ""))
|
| 34 |
if response.status_code == 200:
|
| 35 |
return response.json()["browserHtml"]
|
|
|
|
| 37 |
st.error(f"Zyte error: {response.status_code}")
|
| 38 |
return None
|
| 39 |
|
|
|
|
| 40 |
def parse_home_depot(html):
|
| 41 |
soup = BeautifulSoup(html, 'html.parser')
|
| 42 |
product_cards = soup.find_all('div', class_='product-pod')
|
|
|
|
| 46 |
title_tag = card.find('span', class_='product-pod__title__product')
|
| 47 |
price_dollars = card.find('span', class_='price__dollars')
|
| 48 |
price_cents = card.find('span', class_='price__cents')
|
|
|
|
| 49 |
if title_tag and price_dollars:
|
| 50 |
name = title_tag.text.strip()
|
| 51 |
price = f"${price_dollars.text.strip()}.{price_cents.text.strip() if price_cents else '00'}"
|
| 52 |
results.append({"name": name, "price": price})
|
|
|
|
| 53 |
return results
|
| 54 |
|
| 55 |
+
# === Streamlit App ===
|
| 56 |
+
|
| 57 |
+
st.set_page_config(page_title="AI Architecture Agents", layout="centered")
|
| 58 |
+
st.title("🏗️ AI Architecture Assistant")
|
| 59 |
+
|
| 60 |
+
agent = st.selectbox("Choose an Agent:", ["Agent 1: Floor Plan Image Generator", "Agent 3: Material Explorer"])
|
| 61 |
+
|
| 62 |
+
if agent == "Agent 1: Floor Plan Image Generator":
|
| 63 |
+
st.header("🖼️ Generate a Tiny Home Floor Plan")
|
| 64 |
+
|
| 65 |
+
if "prompt" not in st.session_state:
|
| 66 |
+
st.session_state.prompt = ""
|
| 67 |
+
|
| 68 |
+
st.text_area("Enter your prompt:", key="prompt_input", value=st.session_state.prompt, height=200)
|
| 69 |
+
|
| 70 |
+
if st.button("Insert Sample Prompt"):
|
| 71 |
+
st.session_state.prompt = sample_prompt
|
| 72 |
+
st.rerun()
|
| 73 |
+
|
| 74 |
+
st.session_state.prompt = st.session_state.prompt_input
|
| 75 |
+
st.text_area("Current prompt being used:", st.session_state.prompt, height=200, disabled=True)
|
| 76 |
+
|
| 77 |
+
if st.button("Generate Image"):
|
| 78 |
+
if st.session_state.prompt.strip():
|
| 79 |
+
with st.spinner("Generating image..."):
|
| 80 |
+
try:
|
| 81 |
+
response = client.images.generate(
|
| 82 |
+
model="dall-e-3",
|
| 83 |
+
prompt=st.session_state.prompt,
|
| 84 |
+
size="1024x1024",
|
| 85 |
+
quality="standard",
|
| 86 |
+
n=1
|
| 87 |
+
)
|
| 88 |
+
image_url = response.data[0].url
|
| 89 |
+
st.image(image_url, caption="Generated Image", use_column_width=True)
|
| 90 |
+
st.markdown(f"[Download Image]({image_url})", unsafe_allow_html=True)
|
| 91 |
+
except Exception as e:
|
| 92 |
+
st.error(f"Error: {e}")
|
| 93 |
+
else:
|
| 94 |
+
st.warning("Please enter a valid prompt.")
|
| 95 |
+
|
| 96 |
+
elif agent == "Agent 3: Material Explorer":
|
| 97 |
+
st.header("📦 Building Material Browser")
|
| 98 |
st.markdown("Browse current **laminate flooring** options from Home Depot.")
|
| 99 |
|
| 100 |
if st.button("Get Laminate Flooring Options"):
|
|
|
|
| 109 |
st.warning("No products found. Parsing may need updating.")
|
| 110 |
else:
|
| 111 |
st.error("Failed to scrape the page.")
|
|
|
|
|
|
|
|
|