Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
import streamlit as st
|
2 |
import os
|
3 |
import time
|
4 |
import re
|
@@ -55,6 +55,7 @@ tab1, tab2 = st.tabs(["๐ Contract", "๐ Technical"])
|
|
55 |
with tab1:
|
56 |
ASSISTANT_ID = "asst_KsQRedoJUnEeStzfox1o06lO"
|
57 |
|
|
|
58 |
if "messages" not in st.session_state:
|
59 |
st.session_state.messages = []
|
60 |
if "thread_id" not in st.session_state:
|
@@ -66,6 +67,7 @@ with tab1:
|
|
66 |
if "pending_prompt" not in st.session_state:
|
67 |
st.session_state.pending_prompt = None
|
68 |
|
|
|
69 |
with st.sidebar:
|
70 |
st.header("โน๏ธ Contract Tools")
|
71 |
if st.button("๐งน Clear Chat"):
|
@@ -110,6 +112,7 @@ with tab1:
|
|
110 |
if action != actions[0]:
|
111 |
st.session_state.pending_prompt = action
|
112 |
|
|
|
113 |
chat_col, image_col = st.columns([2, 1])
|
114 |
with chat_col:
|
115 |
st.markdown("### ๐ง Ask a Document-Specific Question")
|
@@ -179,43 +182,21 @@ with tab1:
|
|
179 |
st.error(f"๐ผ๏ธ Image failed: {e}")
|
180 |
|
181 |
# ------------------ Technical Tab ------------------
|
182 |
-
|
183 |
with tab2:
|
184 |
-
with st.sidebar:
|
185 |
-
st.header("๐ ๏ธ Technical Tools")
|
186 |
-
if st.button("๐งน Clear Technical Chat"):
|
187 |
-
st.session_state.tech_messages = []
|
188 |
-
st.session_state.tech_thread_id = None
|
189 |
-
st.session_state.tech_results = []
|
190 |
-
st.session_state.tech_lightbox = None
|
191 |
-
st.session_state.tech_new_input = False
|
192 |
-
st.rerun()
|
193 |
-
|
194 |
ASSISTANT_ID = "asst_DjvuWBc7tCvMbAhY7n1em4BZ"
|
195 |
if "tech_messages" not in st.session_state:
|
196 |
st.session_state.tech_messages = []
|
197 |
if "tech_thread_id" not in st.session_state:
|
198 |
st.session_state.tech_thread_id = None
|
199 |
if "tech_results" not in st.session_state:
|
200 |
-
st.session_state.tech_results = []
|
201 |
-
if "tech_last_input" not in st.session_state:
|
202 |
-
st.session_state.tech_last_input = None
|
203 |
st.session_state.tech_results = []
|
204 |
-
|
205 |
-
st.session_state.tech_lightbox = None
|
206 |
-
if "tech_new_input" not in st.session_state:
|
207 |
-
st.session_state.tech_new_input = False
|
208 |
|
209 |
tech_input = st.chat_input("Ask about plans, drawings or components")
|
210 |
if tech_input:
|
211 |
-
st.session_state.tech_last_input = tech_input
|
212 |
st.session_state.tech_messages.append({"role": "user", "content": tech_input})
|
213 |
-
st.session_state.tech_new_input = True
|
214 |
|
215 |
-
if st.session_state.
|
216 |
-
# Always start fresh thread for each query
|
217 |
-
thread = client.beta.threads.create()
|
218 |
-
st.session_state.tech_thread_id = thread.id
|
219 |
try:
|
220 |
if st.session_state.tech_thread_id is None:
|
221 |
thread = client.beta.threads.create()
|
@@ -234,7 +215,10 @@ if "tech_last_input" not in st.session_state:
|
|
234 |
|
235 |
with st.spinner("๐ Searching technical drawings..."):
|
236 |
while True:
|
237 |
-
run_status = client.beta.threads.runs.retrieve(
|
|
|
|
|
|
|
238 |
if run_status.status in ("completed", "failed", "cancelled"):
|
239 |
break
|
240 |
time.sleep(1)
|
@@ -245,24 +229,13 @@ if "tech_last_input" not in st.session_state:
|
|
245 |
if msg.role == "assistant":
|
246 |
content = msg.content[0].text.value
|
247 |
st.session_state.tech_messages.append({"role": "assistant", "content": content})
|
248 |
-
|
249 |
-
# Always clear previous results
|
250 |
-
st.session_state.tech_results = []
|
251 |
-
|
252 |
try:
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
else:
|
257 |
-
st.warning("Assistant response was not a valid list of results.")
|
258 |
-
except Exception as parse_error:
|
259 |
-
st.warning("โ ๏ธ Failed to parse assistant response as JSON.")
|
260 |
-
st.code(content, language="markdown")
|
261 |
break
|
262 |
except Exception as e:
|
263 |
st.error(f"โ Technical Assistant Error: {e}")
|
264 |
-
finally:
|
265 |
-
st.session_state.tech_new_input = False
|
266 |
|
267 |
with st.expander("๐ง Options (Filter + Pagination)", expanded=False):
|
268 |
disciplines = sorted(set(d.get("discipline", "") for d in st.session_state.tech_results))
|
@@ -270,9 +243,6 @@ if "tech_last_input" not in st.session_state:
|
|
270 |
page_size = 8
|
271 |
page = st.number_input("Page", min_value=1, step=1, value=1)
|
272 |
|
273 |
-
if st.session_state.get("tech_last_input"):
|
274 |
-
st.markdown(f"#### ๐ง You asked: `{st.session_state.tech_last_input}`")
|
275 |
-
|
276 |
if st.session_state.tech_results:
|
277 |
st.subheader("๐ Results")
|
278 |
results = [r for r in st.session_state.tech_results if selected == "All" or r.get("discipline") == selected]
|
@@ -295,5 +265,4 @@ if "tech_last_input" not in st.session_state:
|
|
295 |
else:
|
296 |
for msg in st.session_state.tech_messages:
|
297 |
with st.chat_message(msg["role"]):
|
298 |
-
st.markdown(msg["content"], unsafe_allow_html=True)
|
299 |
-
st.info("No drawing results found for the last query. Try rephrasing or narrowing the search.")
|
|
|
1 |
+
import streamlit as st
|
2 |
import os
|
3 |
import time
|
4 |
import re
|
|
|
55 |
with tab1:
|
56 |
ASSISTANT_ID = "asst_KsQRedoJUnEeStzfox1o06lO"
|
57 |
|
58 |
+
# Correct session state initialization
|
59 |
if "messages" not in st.session_state:
|
60 |
st.session_state.messages = []
|
61 |
if "thread_id" not in st.session_state:
|
|
|
67 |
if "pending_prompt" not in st.session_state:
|
68 |
st.session_state.pending_prompt = None
|
69 |
|
70 |
+
# Sidebar tools
|
71 |
with st.sidebar:
|
72 |
st.header("โน๏ธ Contract Tools")
|
73 |
if st.button("๐งน Clear Chat"):
|
|
|
112 |
if action != actions[0]:
|
113 |
st.session_state.pending_prompt = action
|
114 |
|
115 |
+
# Chat + Image layout
|
116 |
chat_col, image_col = st.columns([2, 1])
|
117 |
with chat_col:
|
118 |
st.markdown("### ๐ง Ask a Document-Specific Question")
|
|
|
182 |
st.error(f"๐ผ๏ธ Image failed: {e}")
|
183 |
|
184 |
# ------------------ Technical Tab ------------------
|
|
|
185 |
with tab2:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
186 |
ASSISTANT_ID = "asst_DjvuWBc7tCvMbAhY7n1em4BZ"
|
187 |
if "tech_messages" not in st.session_state:
|
188 |
st.session_state.tech_messages = []
|
189 |
if "tech_thread_id" not in st.session_state:
|
190 |
st.session_state.tech_thread_id = None
|
191 |
if "tech_results" not in st.session_state:
|
|
|
|
|
|
|
192 |
st.session_state.tech_results = []
|
193 |
+
st.session_state.tech_lightbox = None
|
|
|
|
|
|
|
194 |
|
195 |
tech_input = st.chat_input("Ask about plans, drawings or components")
|
196 |
if tech_input:
|
|
|
197 |
st.session_state.tech_messages.append({"role": "user", "content": tech_input})
|
|
|
198 |
|
199 |
+
if st.session_state.tech_messages and st.session_state.tech_messages[-1]["role"] == "user":
|
|
|
|
|
|
|
200 |
try:
|
201 |
if st.session_state.tech_thread_id is None:
|
202 |
thread = client.beta.threads.create()
|
|
|
215 |
|
216 |
with st.spinner("๐ Searching technical drawings..."):
|
217 |
while True:
|
218 |
+
run_status = client.beta.threads.runs.retrieve(
|
219 |
+
thread_id=st.session_state.tech_thread_id,
|
220 |
+
run_id=run.id
|
221 |
+
)
|
222 |
if run_status.status in ("completed", "failed", "cancelled"):
|
223 |
break
|
224 |
time.sleep(1)
|
|
|
229 |
if msg.role == "assistant":
|
230 |
content = msg.content[0].text.value
|
231 |
st.session_state.tech_messages.append({"role": "assistant", "content": content})
|
|
|
|
|
|
|
|
|
232 |
try:
|
233 |
+
st.session_state.tech_results = json.loads(content.strip("json "))
|
234 |
+
except:
|
235 |
+
st.session_state.tech_results = []
|
|
|
|
|
|
|
|
|
|
|
236 |
break
|
237 |
except Exception as e:
|
238 |
st.error(f"โ Technical Assistant Error: {e}")
|
|
|
|
|
239 |
|
240 |
with st.expander("๐ง Options (Filter + Pagination)", expanded=False):
|
241 |
disciplines = sorted(set(d.get("discipline", "") for d in st.session_state.tech_results))
|
|
|
243 |
page_size = 8
|
244 |
page = st.number_input("Page", min_value=1, step=1, value=1)
|
245 |
|
|
|
|
|
|
|
246 |
if st.session_state.tech_results:
|
247 |
st.subheader("๐ Results")
|
248 |
results = [r for r in st.session_state.tech_results if selected == "All" or r.get("discipline") == selected]
|
|
|
265 |
else:
|
266 |
for msg in st.session_state.tech_messages:
|
267 |
with st.chat_message(msg["role"]):
|
268 |
+
st.markdown(msg["content"], unsafe_allow_html=True)
|
|