IAMTFRMZA commited on
Commit
7175104
Β·
verified Β·
1 Parent(s): 37eaf44

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -24
app.py CHANGED
@@ -270,9 +270,9 @@ with tab3:
270
  st.session_state.tech_thread_id = None
271
  if "tech_results" not in st.session_state:
272
  st.session_state.tech_results = []
273
- st.session_state.tech_lightbox = None
 
274
 
275
- # βœ… Input moved inside tab3 block
276
  tech_input = st.chat_input("Ask about plans, drawings or components")
277
  if tech_input:
278
  st.session_state.tech_messages.append({"role": "user", "content": tech_input})
@@ -308,25 +308,28 @@ with tab3:
308
  messages = client.beta.threads.messages.list(thread_id=st.session_state.tech_thread_id)
309
  for msg in reversed(messages.data):
310
  if msg.role == "assistant":
311
- content = msg.content[0].text.value
312
- st.session_state.tech_messages.append({"role": "assistant", "content": content})
313
  try:
314
- st.session_state.tech_results = json.loads(content.strip("`json "))
315
- except:
316
- st.session_state.tech_results = []
 
 
 
 
317
  break
318
  except Exception as e:
319
  st.error(f"❌ Technical Assistant Error: {e}")
320
 
321
  with st.expander("πŸ”§ Options (Filter + Pagination)", expanded=False):
322
  disciplines = sorted(set(d.get("discipline", "") for d in st.session_state.tech_results))
323
- selected = st.selectbox("🌍 Filter by discipline", ["All"] + disciplines)
324
  page_size = 8
325
  page = st.number_input("Page", min_value=1, step=1, value=1)
326
 
327
  if st.session_state.tech_results:
328
  st.subheader("πŸ“‚ Results")
329
- results = [r for r in st.session_state.tech_results if selected == "All" or r.get("discipline") == selected]
330
  paged = results[(page - 1) * page_size : page * page_size]
331
  cols = st.columns(4)
332
  for i, item in enumerate(paged):
@@ -338,22 +341,36 @@ with tab3:
338
  if not image_urls:
339
  st.warning("⚠️ No image available.")
340
  else:
341
- url = image_urls[0]
342
- st.caption(f"πŸ”— Image URL: {url}")
343
- try:
344
- st.image(url, caption=f"{item['drawing_number']} - Page 1", use_container_width=True)
345
- except Exception as e:
346
- st.error(f"❌ Could not load image: {e}")
347
-
348
- if st.button("πŸ–ΌοΈ View Drawing Details", key=f"thumb_{i}"):
349
- st.session_state.tech_lightbox = url
350
-
351
- if st.session_state.tech_lightbox:
352
- st.image(st.session_state.tech_lightbox, caption="πŸ” Enlarged Drawing Preview", use_container_width=True)
 
353
  if st.button("❌ Close Preview"):
354
  st.session_state.tech_lightbox = None
355
  st.rerun()
356
  else:
357
- for msg in st.session_state.tech_messages:
358
- with st.chat_message(msg["role"]):
359
- st.markdown(msg["content"], unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
270
  st.session_state.tech_thread_id = None
271
  if "tech_results" not in st.session_state:
272
  st.session_state.tech_results = []
273
+ if "tech_lightbox" not in st.session_state:
274
+ st.session_state.tech_lightbox = None
275
 
 
276
  tech_input = st.chat_input("Ask about plans, drawings or components")
277
  if tech_input:
278
  st.session_state.tech_messages.append({"role": "user", "content": tech_input})
 
308
  messages = client.beta.threads.messages.list(thread_id=st.session_state.tech_thread_id)
309
  for msg in reversed(messages.data):
310
  if msg.role == "assistant":
311
+ raw = msg.content[0].text.value.strip()
 
312
  try:
313
+ match = re.search(r"```json(.*?)```", raw, re.DOTALL)
314
+ if match:
315
+ json_data = json.loads(match.group(1).strip())
316
+ st.session_state.tech_results = json_data
317
+ st.session_state.tech_messages.append({"role": "assistant", "content": raw})
318
+ except Exception as e:
319
+ st.error(f"❌ Failed to parse assistant response: {e}")
320
  break
321
  except Exception as e:
322
  st.error(f"❌ Technical Assistant Error: {e}")
323
 
324
  with st.expander("πŸ”§ Options (Filter + Pagination)", expanded=False):
325
  disciplines = sorted(set(d.get("discipline", "") for d in st.session_state.tech_results))
326
+ selected_disciplines = st.multiselect("🌍 Filter by discipline", disciplines, default=disciplines)
327
  page_size = 8
328
  page = st.number_input("Page", min_value=1, step=1, value=1)
329
 
330
  if st.session_state.tech_results:
331
  st.subheader("πŸ“‚ Results")
332
+ results = [r for r in st.session_state.tech_results if r.get("discipline") in selected_disciplines]
333
  paged = results[(page - 1) * page_size : page * page_size]
334
  cols = st.columns(4)
335
  for i, item in enumerate(paged):
 
341
  if not image_urls:
342
  st.warning("⚠️ No image available.")
343
  else:
344
+ for j, url in enumerate(image_urls):
345
+ try:
346
+ st.image(url, caption=f"Page {j+1}", use_container_width=True)
347
+ except Exception as e:
348
+ st.error(f"❌ Could not load image: {e}")
349
+
350
+ if st.button("πŸ–ΌοΈ View All Pages", key=f"thumb_{i}"):
351
+ st.session_state.tech_lightbox = image_urls
352
+
353
+ if isinstance(st.session_state.tech_lightbox, list):
354
+ st.subheader("πŸ” Enlarged Drawing Preview")
355
+ for url in st.session_state.tech_lightbox:
356
+ st.image(url, use_container_width=True)
357
  if st.button("❌ Close Preview"):
358
  st.session_state.tech_lightbox = None
359
  st.rerun()
360
  else:
361
+ st.info("πŸ“­ No matching drawings found. Try a different prompt.")
362
+
363
+ with st.expander("πŸ’‘ Try a Prompt", expanded=False):
364
+ cols = st.columns(3)
365
+ with cols[0]:
366
+ if st.button("πŸ“ Show architectural plans"):
367
+ st.session_state.tech_messages.append({"role": "user", "content": "Show me all architectural plans"})
368
+ st.rerun()
369
+ with cols[1]:
370
+ if st.button("πŸͺ› Electrical switchboards?"):
371
+ st.session_state.tech_messages.append({"role": "user", "content": "Where are the electrical switchboards?"})
372
+ st.rerun()
373
+ with cols[2]:
374
+ if st.button("🌳 Tree protection"):
375
+ st.session_state.tech_messages.append({"role": "user", "content": "Show tree protection diagrams"})
376
+ st.rerun()