Karley8 commited on
Commit
2d9ca38
ยท
verified ยท
1 Parent(s): 3f785e8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -33
app.py CHANGED
@@ -2,43 +2,46 @@ import streamlit as st
2
  import requests
3
  from bs4 import BeautifulSoup
4
  import os
 
5
 
6
  # === CONFIGURATION ===
7
 
8
- # Set page settings
9
- st.set_page_config(page_title="AI Architecture Agents", layout="centered")
10
-
11
- # Load Zyte API key from environment
12
  ZYTE_API_KEY = os.getenv("ZYTE_API_KEY")
13
  ZYTE_API_URL = "https://api.zyte.com/v1/extract"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
- # Hardcoded Home Depot URL for laminate flooring
16
  LAMINATE_FLOORING_URL = "https://www.homedepot.com/b/Flooring-Laminate-Flooring/N-5yc1vZare1"
17
 
18
- # === ZYTE SCRAPER FUNCTION ===
19
  def zyte_scrape(url):
20
  headers = {"Content-Type": "application/json"}
21
  payload = {"url": url, "browserHtml": True}
22
- try:
23
- response = requests.post(ZYTE_API_URL, headers=headers, json=payload, auth=(ZYTE_API_KEY, ""))
24
- if response.status_code == 200:
25
- return response.json()["browserHtml"]
26
- else:
27
- st.error(f"Zyte API error: {response.status_code}")
28
- return None
29
- except Exception as e:
30
- st.error(f"Zyte request failed: {e}")
31
  return None
32
 
33
- # === PARSER FOR HOME DEPOT ===
34
  def parse_home_depot(html):
35
  soup = BeautifulSoup(html, 'html.parser')
36
  product_cards = soup.find_all('div', class_='product-pod')
37
-
38
- if not product_cards:
39
- st.warning("No product cards found. Parsing may need to be updated.")
40
- return []
41
-
42
  results = []
43
  for card in product_cards:
44
  title_tag = card.find('span', class_='product-pod__title__product')
@@ -50,22 +53,55 @@ def parse_home_depot(html):
50
  results.append({"name": name, "price": price})
51
  return results
52
 
53
- # === MAIN STREAMLIT APP ===
 
 
54
  st.title("๐Ÿ—๏ธ AI Architecture Assistant")
55
 
56
- agent = st.selectbox("Choose an Agent:", ["Agent 1: Placeholder", "Agent 3: Material Explorer"])
 
 
 
 
 
 
 
57
 
58
- if agent == "Agent 1: Placeholder":
59
- st.subheader("๐Ÿ–ผ๏ธ Agent 1 (placeholder)")
60
- st.markdown("This section is for the image generator or prompt-based tools.")
61
- st.info("Agent 1 is currently under development or simplified here.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
 
63
  elif agent == "Agent 3: Material Explorer":
64
- st.subheader("๐Ÿ“ฆ Agent 3: Laminate Flooring Options")
65
- st.markdown("Scrapes **Home Depot** for current **laminate flooring** product listings.")
66
 
67
- if st.button("Scrape Home Depot"):
68
- with st.spinner("Scraping product data via Zyte..."):
69
  html = zyte_scrape(LAMINATE_FLOORING_URL)
70
  if html:
71
  data = parse_home_depot(html)
@@ -73,6 +109,6 @@ elif agent == "Agent 3: Material Explorer":
73
  st.success("Here are some laminate flooring options:")
74
  st.table(data)
75
  else:
76
- st.warning("Scraped page, but found no product data.")
77
  else:
78
- st.error("Failed to get page content from Zyte.")
 
2
  import requests
3
  from bs4 import BeautifulSoup
4
  import os
5
+ from openai import OpenAI
6
 
7
  # === CONFIGURATION ===
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 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 "
21
+ "that combines a compact living area, dining space, and efficient kitchen with smart storage. "
22
+ "A cozy loft bedroom is accessible via a sleek staircase or ladder. The minimalist bathroom "
23
+ "includes a shower. Emphasize large windows with natural light, clean lines, and neutral tones "
24
+ "with subtle accents. Annotate key dimensions and furniture placements. Professional architectural rendering style."
25
+ )
26
+
27
+ # === ZYTE SCRAPER SETUP ===
28
 
 
29
  LAMINATE_FLOORING_URL = "https://www.homedepot.com/b/Flooring-Laminate-Flooring/N-5yc1vZare1"
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')
 
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
+ agent = st.selectbox("Choose an Agent:", ["Agent 1: Floor Plan Generator", "Agent 3: Material Explorer"])
62
+
63
+ # === AGENT 1: IMAGE GENERATOR ===
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
+ st.session_state.prompt = st.session_state.prompt_input
77
+ st.text_area("Current prompt being used:", st.session_state.prompt, height=200, disabled=True)
78
+
79
+ if st.button("Generate Image"):
80
+ if st.session_state.prompt.strip():
81
+ with st.spinner("Generating image..."):
82
+ try:
83
+ response = client.images.generate(
84
+ model="dall-e-3",
85
+ prompt=st.session_state.prompt,
86
+ size="1024x1024",
87
+ quality="standard",
88
+ n=1
89
+ )
90
+ image_url = response.data[0].url
91
+ st.image(image_url, caption="Generated Image", use_column_width=True)
92
+ st.markdown(f"[Download Image]({image_url})", unsafe_allow_html=True)
93
+ except Exception as e:
94
+ st.error(f"Error: {e}")
95
+ else:
96
+ st.warning("Please enter a valid prompt.")
97
 
98
+ # === AGENT 3: MATERIAL SCRAPER ===
99
  elif agent == "Agent 3: Material Explorer":
100
+ st.header("๐Ÿ“ฆ Building Material Browser")
101
+ st.markdown("Browse current **laminate flooring** options from Home Depot.")
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)
 
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.")