IAMTFRMZA commited on
Commit
36a939a
Β·
verified Β·
1 Parent(s): d9f5494

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -21
app.py CHANGED
@@ -131,6 +131,8 @@ with tab2:
131
  st.session_state.image_updated = False
132
  if "pending_prompt" not in st.session_state:
133
  st.session_state.pending_prompt = None
 
 
134
 
135
  with st.sidebar:
136
  st.header("ℹ️ Contract Tools")
@@ -147,7 +149,7 @@ with tab2:
147
  if st.button("πŸ”Ž Search Keyword") and keyword:
148
  st.session_state.pending_prompt = f"Find clauses or references related to: {keyword}"
149
 
150
- section_options = [
151
  "Select a section...",
152
  "1. Formal Instrument of Contract",
153
  "2. Offer and Acceptance",
@@ -158,21 +160,19 @@ with tab2:
158
  "7. Penalties and Delays",
159
  "8. Dispute Resolution",
160
  "9. Principal Obligations"
161
- ]
162
- section = st.selectbox("πŸ“„ Jump to Section", section_options)
163
- if section != section_options[0]:
164
  st.session_state.pending_prompt = f"Summarize or list key points from section: {section}"
165
 
166
- actions = [
167
  "Select an action...",
168
  "List all contractual obligations",
169
  "Summarize payment terms",
170
  "List WHS responsibilities",
171
  "Find delay-related penalties",
172
  "Extract dispute resolution steps"
173
- ]
174
- action = st.selectbox("βš™οΈ Common Queries", actions)
175
- if action != actions[0]:
176
  st.session_state.pending_prompt = action
177
 
178
  chat_col, image_col = st.columns([2, 1])
@@ -183,6 +183,7 @@ with tab2:
183
  st.session_state.messages.append({"role": "user", "content": user_input})
184
  elif st.session_state.pending_prompt:
185
  st.session_state.messages.append({"role": "user", "content": st.session_state.pending_prompt})
 
186
  st.session_state.pending_prompt = None
187
 
188
  if st.session_state.messages and st.session_state.messages[-1]["role"] == "user":
@@ -215,16 +216,10 @@ with tab2:
215
  if m.role == "assistant":
216
  reply = m.content[0].text.value.strip()
217
 
218
- # βœ… Avoid near-duplicate assistant responses
219
- is_duplicate = any(
220
- reply in msg["content"] or msg["content"] in reply
221
- for msg in st.session_state.messages if msg["role"] == "assistant"
222
- )
223
- if not is_duplicate:
224
  st.session_state.messages.append({"role": "assistant", "content": reply})
225
 
226
- # πŸ” Extract image reference if found
227
- match = re.search(r'Document Reference:\s*(.*?),\s*Page\s*(\d+)', reply)
228
  if match:
229
  doc, page = match.group(1).strip(), int(match.group(2))
230
  folder = quote(doc)
@@ -233,16 +228,32 @@ with tab2:
233
  st.session_state.image_updated = True
234
  break
235
  else:
236
- st.error("❌ Assistant failed.")
237
  st.rerun()
238
  except Exception as e:
239
- st.error(f"❌ Error: {e}")
240
 
241
- # 🧾 Show messages in reverse (latest on top)
242
  for msg in reversed(st.session_state.messages):
243
  with st.chat_message(msg["role"]):
244
  st.markdown(msg["content"], unsafe_allow_html=True)
245
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
246
  with image_col:
247
  if show_image and st.session_state.image_url:
248
  try:
@@ -251,8 +262,7 @@ with tab2:
251
  img = Image.open(BytesIO(r.content))
252
  st.image(img, caption="πŸ“„ OCR Page Image", use_container_width=True)
253
  except Exception as e:
254
- st.error(f"πŸ–ΌοΈ Image failed: {e}")
255
-
256
 
257
  # ------------------ Technical Tab ------------------
258
  with tab3:
 
131
  st.session_state.image_updated = False
132
  if "pending_prompt" not in st.session_state:
133
  st.session_state.pending_prompt = None
134
+ if "yaml_debug" not in st.session_state:
135
+ st.session_state.yaml_debug = False
136
 
137
  with st.sidebar:
138
  st.header("ℹ️ Contract Tools")
 
149
  if st.button("πŸ”Ž Search Keyword") and keyword:
150
  st.session_state.pending_prompt = f"Find clauses or references related to: {keyword}"
151
 
152
+ section = st.selectbox("πŸ“„ Jump to Section", [
153
  "Select a section...",
154
  "1. Formal Instrument of Contract",
155
  "2. Offer and Acceptance",
 
160
  "7. Penalties and Delays",
161
  "8. Dispute Resolution",
162
  "9. Principal Obligations"
163
+ ])
164
+ if section and section != "Select a section...":
 
165
  st.session_state.pending_prompt = f"Summarize or list key points from section: {section}"
166
 
167
+ action = st.selectbox("βš™οΈ Common Queries", [
168
  "Select an action...",
169
  "List all contractual obligations",
170
  "Summarize payment terms",
171
  "List WHS responsibilities",
172
  "Find delay-related penalties",
173
  "Extract dispute resolution steps"
174
+ ])
175
+ if action and action != "Select an action...":
 
176
  st.session_state.pending_prompt = action
177
 
178
  chat_col, image_col = st.columns([2, 1])
 
183
  st.session_state.messages.append({"role": "user", "content": user_input})
184
  elif st.session_state.pending_prompt:
185
  st.session_state.messages.append({"role": "user", "content": st.session_state.pending_prompt})
186
+ st.markdown(f"🧠 Triggered Query Mode: `{st.session_state.pending_prompt}`")
187
  st.session_state.pending_prompt = None
188
 
189
  if st.session_state.messages and st.session_state.messages[-1]["role"] == "user":
 
216
  if m.role == "assistant":
217
  reply = m.content[0].text.value.strip()
218
 
219
+ if not any(reply in msg["content"] for msg in st.session_state.messages if msg["role"] == "assistant"):
 
 
 
 
 
220
  st.session_state.messages.append({"role": "assistant", "content": reply})
221
 
222
+ match = re.search(r'πŸ“„\s*(.*?)\.txt\s*πŸ”’.*?Page\s*(\d+)', reply)
 
223
  if match:
224
  doc, page = match.group(1).strip(), int(match.group(2))
225
  folder = quote(doc)
 
228
  st.session_state.image_updated = True
229
  break
230
  else:
231
+ st.error("❌ Assistant failed to respond.")
232
  st.rerun()
233
  except Exception as e:
234
+ st.error(f"❌ Assistant Error: {e}")
235
 
 
236
  for msg in reversed(st.session_state.messages):
237
  with st.chat_message(msg["role"]):
238
  st.markdown(msg["content"], unsafe_allow_html=True)
239
 
240
+ if st.session_state.messages and st.session_state.messages[-1]["role"] == "assistant":
241
+ st.markdown("#### πŸ’‘ Follow-Up Suggestions")
242
+ if st.button("πŸ“Œ Delay-related clauses"):
243
+ st.session_state.pending_prompt = "Find clauses or references related to: delay"
244
+ st.rerun()
245
+ if st.button("πŸ“Œ List WHS responsibilities"):
246
+ st.session_state.pending_prompt = "List WHS responsibilities"
247
+ st.rerun()
248
+
249
+ if st.checkbox("πŸ› οΈ Show raw YAML structure"):
250
+ st.session_state.yaml_debug = True
251
+
252
+ if st.session_state.yaml_debug and st.session_state.messages:
253
+ for msg in st.session_state.messages:
254
+ if msg["role"] == "assistant":
255
+ st.code(msg["content"], language="yaml")
256
+
257
  with image_col:
258
  if show_image and st.session_state.image_url:
259
  try:
 
262
  img = Image.open(BytesIO(r.content))
263
  st.image(img, caption="πŸ“„ OCR Page Image", use_container_width=True)
264
  except Exception as e:
265
+ st.error(f"πŸ–ΌοΈ Failed to load image: {e}")
 
266
 
267
  # ------------------ Technical Tab ------------------
268
  with tab3: