Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,7 +5,7 @@ import json
|
|
5 |
import re
|
6 |
from openai import OpenAI
|
7 |
|
8 |
-
#
|
9 |
st.set_page_config(page_title="Forrestdale Drawing Viewer", layout="wide")
|
10 |
st.title("π Forrestdale Technical Drawing Assistant")
|
11 |
|
@@ -25,17 +25,15 @@ if "tech_thread_id" not in st.session_state:
|
|
25 |
if "tech_messages" not in st.session_state:
|
26 |
st.session_state.tech_messages = []
|
27 |
|
28 |
-
# Prompt input
|
29 |
prompt = st.chat_input("Ask about plans, drawings or components (e.g. Show me all electrical plans)")
|
30 |
if prompt:
|
31 |
st.session_state.tech_messages.append({"role": "user", "content": prompt})
|
32 |
|
33 |
-
# Display assistant messages
|
34 |
for msg in st.session_state.tech_messages:
|
35 |
with st.chat_message(msg["role"]):
|
36 |
st.markdown(msg["content"])
|
37 |
|
38 |
-
#
|
39 |
if st.session_state.tech_messages and st.session_state.tech_messages[-1]["role"] == "user":
|
40 |
with st.spinner("β³ Fetching results from assistant..."):
|
41 |
try:
|
@@ -68,33 +66,28 @@ if st.session_state.tech_messages and st.session_state.tech_messages[-1]["role"]
|
|
68 |
reply_content = message.content[0].text.value.strip()
|
69 |
st.session_state.tech_messages.append({"role": "assistant", "content": reply_content})
|
70 |
|
71 |
-
# Try to parse response as JSON block (```json ... ```)
|
72 |
try:
|
73 |
match = re.search(r"```json\s*(.*?)```", reply_content, re.DOTALL)
|
74 |
json_str = match.group(1) if match else reply_content
|
75 |
results = json.loads(json_str)
|
76 |
|
77 |
if isinstance(results, list):
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
st.image(single_image, caption=drawing_number)
|
95 |
-
elif images:
|
96 |
-
for i, img in enumerate(images, start=1):
|
97 |
-
st.image(img, caption=f"{drawing_number} β Page {i}")
|
98 |
except Exception as parse_error:
|
99 |
st.warning("π‘ Could not parse assistant response as JSON.")
|
100 |
break
|
|
|
5 |
import re
|
6 |
from openai import OpenAI
|
7 |
|
8 |
+
# Basic config
|
9 |
st.set_page_config(page_title="Forrestdale Drawing Viewer", layout="wide")
|
10 |
st.title("π Forrestdale Technical Drawing Assistant")
|
11 |
|
|
|
25 |
if "tech_messages" not in st.session_state:
|
26 |
st.session_state.tech_messages = []
|
27 |
|
|
|
28 |
prompt = st.chat_input("Ask about plans, drawings or components (e.g. Show me all electrical plans)")
|
29 |
if prompt:
|
30 |
st.session_state.tech_messages.append({"role": "user", "content": prompt})
|
31 |
|
|
|
32 |
for msg in st.session_state.tech_messages:
|
33 |
with st.chat_message(msg["role"]):
|
34 |
st.markdown(msg["content"])
|
35 |
|
36 |
+
# Fetch assistant reply
|
37 |
if st.session_state.tech_messages and st.session_state.tech_messages[-1]["role"] == "user":
|
38 |
with st.spinner("β³ Fetching results from assistant..."):
|
39 |
try:
|
|
|
66 |
reply_content = message.content[0].text.value.strip()
|
67 |
st.session_state.tech_messages.append({"role": "assistant", "content": reply_content})
|
68 |
|
|
|
69 |
try:
|
70 |
match = re.search(r"```json\s*(.*?)```", reply_content, re.DOTALL)
|
71 |
json_str = match.group(1) if match else reply_content
|
72 |
results = json.loads(json_str)
|
73 |
|
74 |
if isinstance(results, list):
|
75 |
+
cols = st.columns(4)
|
76 |
+
for idx, item in enumerate(results):
|
77 |
+
with cols[idx % 4]:
|
78 |
+
with st.container(border=True):
|
79 |
+
st.markdown(f"### π {item.get('drawing_number')} ({item.get('discipline')})", help=item.get("summary"))
|
80 |
+
st.caption(item.get("summary"))
|
81 |
+
|
82 |
+
with st.expander("π View Drawing Details"):
|
83 |
+
if item.get("question"):
|
84 |
+
st.markdown(f"**Question Match:** {item.get('question')}")
|
85 |
+
if item.get("image"):
|
86 |
+
st.image(item.get("image"), caption=item.get("drawing_number"))
|
87 |
+
elif item.get("images"):
|
88 |
+
for i, img in enumerate(item["images"]):
|
89 |
+
if img.startswith("http"):
|
90 |
+
st.image(img, caption=f"{item['drawing_number']} β Page {i+1}")
|
|
|
|
|
|
|
|
|
91 |
except Exception as parse_error:
|
92 |
st.warning("π‘ Could not parse assistant response as JSON.")
|
93 |
break
|