IAMTFRMZA commited on
Commit
8910d8d
Β·
verified Β·
1 Parent(s): e8573c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -81
app.py CHANGED
@@ -254,7 +254,7 @@ with tab2:
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,89 +265,88 @@ with tab3:
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)
 
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)