Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,20 +1,14 @@
|
|
1 |
import streamlit as st
|
2 |
-
import requests
|
3 |
-
from bs4 import BeautifulSoup
|
4 |
-
import os
|
5 |
from openai import OpenAI
|
6 |
|
7 |
-
# ===
|
|
|
|
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
-
|
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 OpenAI image generation
|
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,91 +18,71 @@ sample_prompt = (
|
|
24 |
"with subtle accents. Annotate key dimensions and furniture placements. Professional architectural rendering style."
|
25 |
)
|
26 |
|
27 |
-
# ===
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
def zyte_scrape(url):
|
32 |
-
headers = {"Content-Type": "application/json"}
|
33 |
-
payload = {"url": url, "browserHtml": True}
|
34 |
-
response = requests.post(ZYTE_API_URL, headers=headers, json=payload, auth=(ZYTE_API_KEY, ""))
|
35 |
-
if response.status_code == 200:
|
36 |
-
return response.json()["browserHtml"]
|
37 |
-
else:
|
38 |
-
st.error(f"Zyte error: {response.status_code}")
|
39 |
-
return None
|
40 |
-
|
41 |
-
def parse_home_depot(html):
|
42 |
-
soup = BeautifulSoup(html, 'html.parser')
|
43 |
-
product_cards = soup.find_all('div', class_='product-pod')
|
44 |
-
|
45 |
-
results = []
|
46 |
-
for card in product_cards:
|
47 |
-
title_tag = card.find('span', class_='product-pod__title__product')
|
48 |
-
price_dollars = card.find('span', class_='price__dollars')
|
49 |
-
price_cents = card.find('span', class_='price__cents')
|
50 |
-
if title_tag and price_dollars:
|
51 |
-
name = title_tag.text.strip()
|
52 |
-
price = f"${price_dollars.text.strip()}.{price_cents.text.strip() if price_cents else '00'}"
|
53 |
-
results.append({"name": name, "price": price})
|
54 |
-
return results
|
55 |
-
|
56 |
-
# === STREAMLIT UI ===
|
57 |
-
|
58 |
-
st.set_page_config(page_title="AI Architecture Agents", layout="centered")
|
59 |
-
st.title("🏗️ AI Architecture Assistant")
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
if agent == "Agent 1: Floor Plan Generator":
|
65 |
-
st.header("🖼️ Generate a Tiny Home Floor Plan")
|
66 |
-
|
67 |
-
if "prompt" not in st.session_state:
|
68 |
-
st.session_state.prompt = ""
|
69 |
-
|
70 |
-
st.text_area("Enter your prompt:", key="prompt_input", value=st.session_state.prompt, height=200)
|
71 |
|
|
|
|
|
72 |
if st.button("Insert Sample Prompt"):
|
73 |
st.session_state.prompt = sample_prompt
|
74 |
st.rerun()
|
75 |
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
|
103 |
-
if st.button("Get Laminate Flooring Options"):
|
104 |
-
with st.spinner("Scraping Home Depot via Zyte..."):
|
105 |
-
html = zyte_scrape(LAMINATE_FLOORING_URL)
|
106 |
-
if html:
|
107 |
-
data = parse_home_depot(html)
|
108 |
-
if data:
|
109 |
-
st.success("Here are some laminate flooring options:")
|
110 |
-
st.table(data)
|
111 |
-
else:
|
112 |
-
st.warning("No products found. Parsing may need updating.")
|
113 |
-
else:
|
114 |
-
st.error("Failed to scrape the page.")
|
|
|
1 |
import streamlit as st
|
|
|
|
|
|
|
2 |
from openai import OpenAI
|
3 |
|
4 |
+
# === SETUP ===
|
5 |
+
st.set_page_config(page_title="AI Architecture Assistant", layout="centered")
|
6 |
+
client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"])
|
7 |
|
8 |
+
# === PROMPT SETUP ===
|
9 |
+
if "prompt" not in st.session_state:
|
10 |
+
st.session_state.prompt = ""
|
|
|
11 |
|
|
|
|
|
|
|
|
|
12 |
sample_prompt = (
|
13 |
"A highly detailed, realistic architectural floor plan for a modern tiny home. "
|
14 |
"The design features an open-concept layout with a multi-functional living space "
|
|
|
18 |
"with subtle accents. Annotate key dimensions and furniture placements. Professional architectural rendering style."
|
19 |
)
|
20 |
|
21 |
+
# === UI START ===
|
22 |
+
st.title("🏠 AI Architecture Assistant")
|
23 |
+
st.caption("Design smarter. Visualize faster. Build better.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
+
# === 1. Prompt Input ===
|
26 |
+
st.markdown("### ✍️ Enter Your Floor Plan Prompt")
|
27 |
+
st.text_area("Prompt", key="prompt_input", value=st.session_state.prompt, height=200)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
+
col1, col2 = st.columns(2)
|
30 |
+
with col1:
|
31 |
if st.button("Insert Sample Prompt"):
|
32 |
st.session_state.prompt = sample_prompt
|
33 |
st.rerun()
|
34 |
|
35 |
+
st.session_state.prompt = st.session_state.prompt_input
|
36 |
+
|
37 |
+
st.text_area("Current Prompt Being Used", st.session_state.prompt, height=200, disabled=True)
|
38 |
+
|
39 |
+
# === 2. Generate Image ===
|
40 |
+
st.markdown("### 🖼️ Generate a Floor Plan Image")
|
41 |
+
|
42 |
+
if st.button("Generate Image"):
|
43 |
+
if st.session_state.prompt.strip():
|
44 |
+
with st.spinner("Generating image..."):
|
45 |
+
try:
|
46 |
+
response = client.images.generate(
|
47 |
+
model="dall-e-3",
|
48 |
+
prompt=st.session_state.prompt,
|
49 |
+
size="1024x1024",
|
50 |
+
quality="standard",
|
51 |
+
n=1
|
52 |
+
)
|
53 |
+
image_url = response.data[0].url
|
54 |
+
st.image(image_url, caption="Generated Floor Plan", use_column_width=True)
|
55 |
+
st.markdown(f"[Download Image]({image_url})", unsafe_allow_html=True)
|
56 |
+
except Exception as e:
|
57 |
+
st.error(f"Error: {e}")
|
58 |
+
else:
|
59 |
+
st.warning("Please enter a valid prompt.")
|
60 |
+
|
61 |
+
# === 3. Recommend Materials ===
|
62 |
+
st.markdown("### 🧱 Recommended Building Materials")
|
63 |
+
|
64 |
+
if st.button("Get Material Recommendations"):
|
65 |
+
if st.session_state.prompt.strip():
|
66 |
+
with st.spinner("Analyzing design and recommending materials..."):
|
67 |
+
def generate_material_recommendations(prompt):
|
68 |
+
system_instruction = (
|
69 |
+
"You are a smart building material recommender. Based on the architectural prompt, "
|
70 |
+
"return a list of 5 to 10 building materials implied by the layout or concept. "
|
71 |
+
"Respond in clear, plain English using bullet points. Do not return JSON."
|
72 |
+
)
|
73 |
+
|
74 |
+
response = client.chat.completions.create(
|
75 |
+
model="gpt-4",
|
76 |
+
messages=[
|
77 |
+
{"role": "system", "content": system_instruction},
|
78 |
+
{"role": "user", "content": prompt}
|
79 |
+
],
|
80 |
+
temperature=0.7
|
81 |
+
)
|
82 |
+
return response.choices[0].message.content.strip()
|
83 |
+
|
84 |
+
result = generate_material_recommendations(st.session_state.prompt)
|
85 |
+
st.markdown(result)
|
86 |
+
else:
|
87 |
+
st.warning("Please enter a prompt first.")
|
88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|