Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -140,28 +140,23 @@ with tab1:
|
|
140 |
# ------------------ Tab 2: Contract Queries ------------------
|
141 |
with tab2:
|
142 |
ASSISTANT_ID = "asst_Qcl6k4bYxK2UiOfBfqsC6LZv"
|
143 |
-
if "messages" not in st.session_state:
|
144 |
-
st.session_state.messages = []
|
145 |
-
if "thread_id" not in st.session_state:
|
146 |
-
st.session_state.thread_id = None
|
147 |
-
if "image_url" not in st.session_state:
|
148 |
-
st.session_state.image_url = None
|
149 |
-
if "image_updated" not in st.session_state:
|
150 |
-
st.session_state.image_updated = False
|
151 |
-
if "pending_prompt" not in st.session_state:
|
152 |
-
st.session_state.pending_prompt = None
|
153 |
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
with st.sidebar:
|
155 |
st.header("ℹ️ Contract Tools")
|
|
|
156 |
if st.button("🧹 Clear Chat"):
|
157 |
-
|
158 |
-
|
159 |
-
st.session_state.image_url = None
|
160 |
-
st.session_state.image_updated = False
|
161 |
-
st.session_state.pending_prompt = None
|
162 |
st.rerun()
|
163 |
|
164 |
show_image = st.toggle("📑 Show Page Image", value=True)
|
|
|
165 |
keyword = st.text_input("Search by Keyword", placeholder="e.g. defects, WHS, delay")
|
166 |
if st.button("🔎 Search Keyword") and keyword:
|
167 |
st.session_state.pending_prompt = f"Find clauses or references related to: {keyword}"
|
@@ -195,6 +190,7 @@ with tab2:
|
|
195 |
st.rerun()
|
196 |
|
197 |
chat_col, image_col = st.columns([2, 1])
|
|
|
198 |
with chat_col:
|
199 |
st.markdown("### 🧠 Ask a Document-Specific Question")
|
200 |
user_input = st.chat_input("Example: What is the defects liability period?")
|
@@ -206,7 +202,7 @@ with tab2:
|
|
206 |
|
207 |
if st.session_state.messages and st.session_state.messages[-1]["role"] == "user":
|
208 |
try:
|
209 |
-
if st.session_state.thread_id
|
210 |
thread = client.beta.threads.create()
|
211 |
st.session_state.thread_id = thread.id
|
212 |
|
@@ -223,7 +219,10 @@ with tab2:
|
|
223 |
|
224 |
with st.spinner("🤖 Thinking..."):
|
225 |
while True:
|
226 |
-
status = client.beta.threads.runs.retrieve(
|
|
|
|
|
|
|
227 |
if status.status in ("completed", "failed", "cancelled"):
|
228 |
break
|
229 |
time.sleep(1)
|
@@ -232,16 +231,26 @@ with tab2:
|
|
232 |
messages = client.beta.threads.messages.list(thread_id=st.session_state.thread_id)
|
233 |
for m in reversed(messages.data):
|
234 |
if m.role == "assistant":
|
235 |
-
|
236 |
|
237 |
-
#
|
238 |
-
|
239 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
240 |
|
241 |
-
|
242 |
-
|
|
|
243 |
|
244 |
-
|
|
|
245 |
if match:
|
246 |
doc, page = match.group(1).strip(), int(match.group(2))
|
247 |
folder = quote(doc)
|
@@ -259,14 +268,12 @@ with tab2:
|
|
259 |
with st.chat_message(msg["role"]):
|
260 |
st.markdown(msg["content"], unsafe_allow_html=True)
|
261 |
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
questions = [line.strip(" -•") for line in block.splitlines() if line.strip().startswith(("-", "•"))]
|
269 |
-
|
270 |
if questions:
|
271 |
st.markdown("#### 💡 Follow-Up Suggestions")
|
272 |
for q in questions:
|
@@ -284,6 +291,7 @@ with tab2:
|
|
284 |
except Exception as e:
|
285 |
st.error(f"🖼️ Failed to load image: {e}")
|
286 |
|
|
|
287 |
# ------------------ Technical Tab ------------------
|
288 |
with tab3:
|
289 |
ASSISTANT_ID = "asst_DjvuWBc7tCvMbAhY7n1em4BZ"
|
|
|
140 |
# ------------------ Tab 2: Contract Queries ------------------
|
141 |
with tab2:
|
142 |
ASSISTANT_ID = "asst_Qcl6k4bYxK2UiOfBfqsC6LZv"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
143 |
|
144 |
+
# State initialization
|
145 |
+
for key in ["messages", "thread_id", "image_url", "image_updated", "pending_prompt"]:
|
146 |
+
if key not in st.session_state:
|
147 |
+
st.session_state[key] = [] if key == "messages" else None if "id" in key or key.endswith("_url") else False
|
148 |
+
|
149 |
+
# Sidebar
|
150 |
with st.sidebar:
|
151 |
st.header("ℹ️ Contract Tools")
|
152 |
+
|
153 |
if st.button("🧹 Clear Chat"):
|
154 |
+
for key in ["messages", "thread_id", "image_url", "image_updated", "pending_prompt"]:
|
155 |
+
st.session_state[key] = [] if key == "messages" else None if "id" in key or key.endswith("_url") else False
|
|
|
|
|
|
|
156 |
st.rerun()
|
157 |
|
158 |
show_image = st.toggle("📑 Show Page Image", value=True)
|
159 |
+
|
160 |
keyword = st.text_input("Search by Keyword", placeholder="e.g. defects, WHS, delay")
|
161 |
if st.button("🔎 Search Keyword") and keyword:
|
162 |
st.session_state.pending_prompt = f"Find clauses or references related to: {keyword}"
|
|
|
190 |
st.rerun()
|
191 |
|
192 |
chat_col, image_col = st.columns([2, 1])
|
193 |
+
|
194 |
with chat_col:
|
195 |
st.markdown("### 🧠 Ask a Document-Specific Question")
|
196 |
user_input = st.chat_input("Example: What is the defects liability period?")
|
|
|
202 |
|
203 |
if st.session_state.messages and st.session_state.messages[-1]["role"] == "user":
|
204 |
try:
|
205 |
+
if not st.session_state.thread_id:
|
206 |
thread = client.beta.threads.create()
|
207 |
st.session_state.thread_id = thread.id
|
208 |
|
|
|
219 |
|
220 |
with st.spinner("🤖 Thinking..."):
|
221 |
while True:
|
222 |
+
status = client.beta.threads.runs.retrieve(
|
223 |
+
thread_id=st.session_state.thread_id,
|
224 |
+
run_id=run.id
|
225 |
+
)
|
226 |
if status.status in ("completed", "failed", "cancelled"):
|
227 |
break
|
228 |
time.sleep(1)
|
|
|
231 |
messages = client.beta.threads.messages.list(thread_id=st.session_state.thread_id)
|
232 |
for m in reversed(messages.data):
|
233 |
if m.role == "assistant":
|
234 |
+
full_reply = m.content[0].text.value.strip()
|
235 |
|
236 |
+
# ✂️ Strip "Some Possible Questions"
|
237 |
+
match = re.search(r"Some Possible Questions:\s*(.*?)(?=\n{2,}|Other References:|\Z)", full_reply, re.DOTALL)
|
238 |
+
question_block = None
|
239 |
+
if match:
|
240 |
+
question_block = match.group(1).strip()
|
241 |
+
full_reply = full_reply.replace(f"Some Possible Questions:\n{question_block}", "").strip()
|
242 |
+
|
243 |
+
# 🧼 Remove all source tags like [8:0†source], [source], [8:1:source]
|
244 |
+
cleaned_reply = re.sub(r"\[\d+:\d+†[^\]]+\]", "", full_reply)
|
245 |
+
cleaned_reply = re.sub(r"\[\d+:\d+:source\]", "", cleaned_reply)
|
246 |
+
cleaned_reply = re.sub(r"\[source\]", "", cleaned_reply)
|
247 |
|
248 |
+
# Save reply and extract image if unique
|
249 |
+
if not any(cleaned_reply in msg["content"] for msg in st.session_state.messages if msg["role"] == "assistant"):
|
250 |
+
st.session_state.messages.append({"role": "assistant", "content": cleaned_reply})
|
251 |
|
252 |
+
# 🔍 Extract image reference
|
253 |
+
match = re.search(r'📄\s*(.*?)\.txt\s*🔢.*?Page\s*(\d+)', cleaned_reply)
|
254 |
if match:
|
255 |
doc, page = match.group(1).strip(), int(match.group(2))
|
256 |
folder = quote(doc)
|
|
|
268 |
with st.chat_message(msg["role"]):
|
269 |
st.markdown(msg["content"], unsafe_allow_html=True)
|
270 |
|
271 |
+
# ✅ Follow-up questions
|
272 |
+
if 'question_block' in locals() and question_block:
|
273 |
+
questions = [
|
274 |
+
line.strip(" -•") for line in question_block.splitlines()
|
275 |
+
if line.strip().startswith(("-", "•")) and line.strip().endswith("?")
|
276 |
+
]
|
|
|
|
|
277 |
if questions:
|
278 |
st.markdown("#### 💡 Follow-Up Suggestions")
|
279 |
for q in questions:
|
|
|
291 |
except Exception as e:
|
292 |
st.error(f"🖼️ Failed to load image: {e}")
|
293 |
|
294 |
+
|
295 |
# ------------------ Technical Tab ------------------
|
296 |
with tab3:
|
297 |
ASSISTANT_ID = "asst_DjvuWBc7tCvMbAhY7n1em4BZ"
|