IAMTFRMZA commited on
Commit
e8573c0
Β·
verified Β·
1 Parent(s): 2866bc5

Update app.py

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