Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -254,7 +254,7 @@ with tab2:
|
|
254 |
st.error(f"πΌοΈ Image failed: {e}")
|
255 |
|
256 |
|
257 |
-
|
258 |
with tab3:
|
259 |
ASSISTANT_ID = "asst_DjvuWBc7tCvMbAhY7n1em4BZ"
|
260 |
if "tech_messages" not in st.session_state:
|
@@ -265,89 +265,88 @@ with tab3:
|
|
265 |
st.session_state.tech_results = []
|
266 |
st.session_state.tech_lightbox = None
|
267 |
|
|
|
|
|
|
|
|
|
268 |
|
|
|
|
|
|
|
|
|
|
|
269 |
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
try:
|
276 |
-
if st.session_state.tech_thread_id is None:
|
277 |
-
thread = client.beta.threads.create()
|
278 |
-
st.session_state.tech_thread_id = thread.id
|
279 |
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
)
|
285 |
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
|
|
|
|
|
|
|
|
|
|
290 |
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
307 |
try:
|
308 |
-
st.
|
309 |
-
except:
|
310 |
-
st.
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
paged = results[(page - 1) * page_size : page * page_size]
|
325 |
-
cols = st.columns(4)
|
326 |
-
for i, item in enumerate(paged):
|
327 |
-
with cols[i % 4]:
|
328 |
-
st.markdown(f"**π {item['drawing_number']} ({item['discipline']})**")
|
329 |
-
st.caption(item.get("summary", ""))
|
330 |
-
|
331 |
-
image_urls = item.get("images", [])
|
332 |
-
if not image_urls:
|
333 |
-
st.warning("β οΈ No image available.")
|
334 |
-
else:
|
335 |
-
url = image_urls[0]
|
336 |
-
st.caption(f"π Image URL: {url}")
|
337 |
-
try:
|
338 |
-
st.image(url, caption=f"{item['drawing_number']} - Page 1", use_container_width=True)
|
339 |
-
except Exception as e:
|
340 |
-
st.error(f"β Could not load image: {e}")
|
341 |
-
|
342 |
-
if st.button("πΌοΈ View Drawing Details", key=f"thumb_{i}"):
|
343 |
-
st.session_state.tech_lightbox = url
|
344 |
-
|
345 |
-
if st.session_state.tech_lightbox:
|
346 |
-
st.image(st.session_state.tech_lightbox, caption="π Enlarged Drawing Preview", use_container_width=True)
|
347 |
-
if st.button("β Close Preview"):
|
348 |
-
st.session_state.tech_lightbox = None
|
349 |
-
st.rerun()
|
350 |
-
else:
|
351 |
-
for msg in st.session_state.tech_messages:
|
352 |
-
with st.chat_message(msg["role"]):
|
353 |
-
st.markdown(msg["content"], unsafe_allow_html=True)
|
|
|
254 |
st.error(f"πΌοΈ Image failed: {e}")
|
255 |
|
256 |
|
257 |
+
# ------------------ Technical Tab ------------------
|
258 |
with tab3:
|
259 |
ASSISTANT_ID = "asst_DjvuWBc7tCvMbAhY7n1em4BZ"
|
260 |
if "tech_messages" not in st.session_state:
|
|
|
265 |
st.session_state.tech_results = []
|
266 |
st.session_state.tech_lightbox = None
|
267 |
|
268 |
+
# β
Input moved inside tab3 block
|
269 |
+
tech_input = st.chat_input("Ask about plans, drawings or components")
|
270 |
+
if tech_input:
|
271 |
+
st.session_state.tech_messages.append({"role": "user", "content": tech_input})
|
272 |
|
273 |
+
if st.session_state.tech_messages and st.session_state.tech_messages[-1]["role"] == "user":
|
274 |
+
try:
|
275 |
+
if st.session_state.tech_thread_id is None:
|
276 |
+
thread = client.beta.threads.create()
|
277 |
+
st.session_state.tech_thread_id = thread.id
|
278 |
|
279 |
+
client.beta.threads.messages.create(
|
280 |
+
thread_id=st.session_state.tech_thread_id,
|
281 |
+
role="user",
|
282 |
+
content=st.session_state.tech_messages[-1]["content"]
|
283 |
+
)
|
|
|
|
|
|
|
|
|
284 |
|
285 |
+
run = client.beta.threads.runs.create(
|
286 |
+
thread_id=st.session_state.tech_thread_id,
|
287 |
+
assistant_id=ASSISTANT_ID
|
288 |
+
)
|
|
|
289 |
|
290 |
+
with st.spinner("π Searching technical drawings..."):
|
291 |
+
while True:
|
292 |
+
run_status = client.beta.threads.runs.retrieve(
|
293 |
+
thread_id=st.session_state.tech_thread_id,
|
294 |
+
run_id=run.id
|
295 |
+
)
|
296 |
+
if run_status.status in ("completed", "failed", "cancelled"):
|
297 |
+
break
|
298 |
+
time.sleep(1)
|
299 |
|
300 |
+
if run_status.status == "completed":
|
301 |
+
messages = client.beta.threads.messages.list(thread_id=st.session_state.tech_thread_id)
|
302 |
+
for msg in reversed(messages.data):
|
303 |
+
if msg.role == "assistant":
|
304 |
+
content = msg.content[0].text.value
|
305 |
+
st.session_state.tech_messages.append({"role": "assistant", "content": content})
|
306 |
+
try:
|
307 |
+
st.session_state.tech_results = json.loads(content.strip("`json "))
|
308 |
+
except:
|
309 |
+
st.session_state.tech_results = []
|
310 |
+
break
|
311 |
+
except Exception as e:
|
312 |
+
st.error(f"β Technical Assistant Error: {e}")
|
313 |
+
|
314 |
+
with st.expander("π§ Options (Filter + Pagination)", expanded=False):
|
315 |
+
disciplines = sorted(set(d.get("discipline", "") for d in st.session_state.tech_results))
|
316 |
+
selected = st.selectbox("π Filter by discipline", ["All"] + disciplines)
|
317 |
+
page_size = 8
|
318 |
+
page = st.number_input("Page", min_value=1, step=1, value=1)
|
319 |
+
|
320 |
+
if st.session_state.tech_results:
|
321 |
+
st.subheader("π Results")
|
322 |
+
results = [r for r in st.session_state.tech_results if selected == "All" or r.get("discipline") == selected]
|
323 |
+
paged = results[(page - 1) * page_size : page * page_size]
|
324 |
+
cols = st.columns(4)
|
325 |
+
for i, item in enumerate(paged):
|
326 |
+
with cols[i % 4]:
|
327 |
+
st.markdown(f"**π {item['drawing_number']} ({item['discipline']})**")
|
328 |
+
st.caption(item.get("summary", ""))
|
329 |
+
|
330 |
+
image_urls = item.get("images", [])
|
331 |
+
if not image_urls:
|
332 |
+
st.warning("β οΈ No image available.")
|
333 |
+
else:
|
334 |
+
url = image_urls[0]
|
335 |
+
st.caption(f"π Image URL: {url}")
|
336 |
try:
|
337 |
+
st.image(url, caption=f"{item['drawing_number']} - Page 1", use_container_width=True)
|
338 |
+
except Exception as e:
|
339 |
+
st.error(f"β Could not load image: {e}")
|
340 |
+
|
341 |
+
if st.button("πΌοΈ View Drawing Details", key=f"thumb_{i}"):
|
342 |
+
st.session_state.tech_lightbox = url
|
343 |
+
|
344 |
+
if st.session_state.tech_lightbox:
|
345 |
+
st.image(st.session_state.tech_lightbox, caption="π Enlarged Drawing Preview", use_container_width=True)
|
346 |
+
if st.button("β Close Preview"):
|
347 |
+
st.session_state.tech_lightbox = None
|
348 |
+
st.rerun()
|
349 |
+
else:
|
350 |
+
for msg in st.session_state.tech_messages:
|
351 |
+
with st.chat_message(msg["role"]):
|
352 |
+
st.markdown(msg["content"], unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|