IAMTFRMZA commited on
Commit
69f3d6f
Β·
verified Β·
1 Parent(s): 6972856

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -20
app.py CHANGED
@@ -55,7 +55,6 @@ tab1, tab2 = st.tabs(["πŸ“‘ Contract", "πŸ“ Technical"])
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,7 +66,6 @@ with tab1:
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,7 +110,6 @@ with tab1:
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")
@@ -184,26 +181,22 @@ with tab1:
184
  # ------------------ Technical Tab ------------------
185
  with tab2:
186
  ASSISTANT_ID = "asst_DjvuWBc7tCvMbAhY7n1em4BZ"
187
-
188
- # Initialize session state variables
189
  if "tech_messages" not in st.session_state:
190
  st.session_state.tech_messages = []
191
  if "tech_thread_id" not in st.session_state:
192
  st.session_state.tech_thread_id = None
193
  if "tech_results" not in st.session_state:
194
  st.session_state.tech_results = []
195
- if "tech_new_input" not in st.session_state:
196
- st.session_state.tech_new_input = False
197
  if "tech_lightbox" not in st.session_state:
198
  st.session_state.tech_lightbox = None
 
 
199
 
200
- # Input box
201
  tech_input = st.chat_input("Ask about plans, drawings or components")
202
  if tech_input:
203
  st.session_state.tech_messages.append({"role": "user", "content": tech_input})
204
- st.session_state.tech_new_input = True # Flag to process this new input
205
 
206
- # Run assistant only if new input was entered
207
  if st.session_state.tech_new_input:
208
  try:
209
  if st.session_state.tech_thread_id is None:
@@ -223,10 +216,7 @@ with tab2:
223
 
224
  with st.spinner("πŸ” Searching technical drawings..."):
225
  while True:
226
- run_status = client.beta.threads.runs.retrieve(
227
- thread_id=st.session_state.tech_thread_id,
228
- run_id=run.id
229
- )
230
  if run_status.status in ("completed", "failed", "cancelled"):
231
  break
232
  time.sleep(1)
@@ -242,21 +232,17 @@ with tab2:
242
  except:
243
  st.session_state.tech_results = []
244
  break
245
- else:
246
- st.error("❌ Assistant run failed.")
247
  except Exception as e:
248
  st.error(f"❌ Technical Assistant Error: {e}")
249
  finally:
250
- st.session_state.tech_new_input = False # Reset the flag
251
 
252
- # Filtering & Pagination UI
253
  with st.expander("πŸ”§ Options (Filter + Pagination)", expanded=False):
254
  disciplines = sorted(set(d.get("discipline", "") for d in st.session_state.tech_results))
255
  selected = st.selectbox("🌍 Filter by discipline", ["All"] + disciplines)
256
  page_size = 8
257
  page = st.number_input("Page", min_value=1, step=1, value=1)
258
 
259
- # Results display
260
  if st.session_state.tech_results:
261
  st.subheader("πŸ“‚ Results")
262
  results = [r for r in st.session_state.tech_results if selected == "All" or r.get("discipline") == selected]
@@ -279,4 +265,4 @@ with tab2:
279
  else:
280
  for msg in st.session_state.tech_messages:
281
  with st.chat_message(msg["role"]):
282
- st.markdown(msg["content"], unsafe_allow_html=True)
 
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
  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
  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")
 
181
  # ------------------ Technical Tab ------------------
182
  with tab2:
183
  ASSISTANT_ID = "asst_DjvuWBc7tCvMbAhY7n1em4BZ"
 
 
184
  if "tech_messages" not in st.session_state:
185
  st.session_state.tech_messages = []
186
  if "tech_thread_id" not in st.session_state:
187
  st.session_state.tech_thread_id = None
188
  if "tech_results" not in st.session_state:
189
  st.session_state.tech_results = []
 
 
190
  if "tech_lightbox" not in st.session_state:
191
  st.session_state.tech_lightbox = None
192
+ if "tech_new_input" not in st.session_state:
193
+ st.session_state.tech_new_input = False
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
+ st.session_state.tech_new_input = True
199
 
 
200
  if st.session_state.tech_new_input:
201
  try:
202
  if st.session_state.tech_thread_id is None:
 
216
 
217
  with st.spinner("πŸ” Searching technical drawings..."):
218
  while True:
219
+ run_status = client.beta.threads.runs.retrieve(thread_id=st.session_state.tech_thread_id, run_id=run.id)
 
 
 
220
  if run_status.status in ("completed", "failed", "cancelled"):
221
  break
222
  time.sleep(1)
 
232
  except:
233
  st.session_state.tech_results = []
234
  break
 
 
235
  except Exception as e:
236
  st.error(f"❌ Technical Assistant Error: {e}")
237
  finally:
238
+ st.session_state.tech_new_input = False
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))
242
  selected = st.selectbox("🌍 Filter by discipline", ["All"] + disciplines)
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)