Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -31,6 +31,8 @@ if "image_url" not in st.session_state:
|
|
31 |
st.session_state.image_url = None
|
32 |
if "image_updated" not in st.session_state:
|
33 |
st.session_state.image_updated = False
|
|
|
|
|
34 |
|
35 |
# ------------------ Sidebar ------------------
|
36 |
st.sidebar.header("βοΈ Settings")
|
@@ -39,6 +41,7 @@ if st.sidebar.button("π§Ή Clear Chat"):
|
|
39 |
st.session_state.thread_id = None
|
40 |
st.session_state.image_url = None
|
41 |
st.session_state.image_updated = False
|
|
|
42 |
st.rerun()
|
43 |
|
44 |
show_image = st.sidebar.toggle("π Show Page Image", value=True)
|
@@ -64,19 +67,46 @@ with chat_col:
|
|
64 |
st.markdown("### π§ Ask a Document-Specific Question")
|
65 |
prompt = st.chat_input("Example: What is the defects liability period?")
|
66 |
|
|
|
67 |
st.markdown("<hr style='margin:0.5em 0;'>", unsafe_allow_html=True)
|
68 |
-
st.markdown("####
|
69 |
-
|
70 |
-
with
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
if prompt:
|
81 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
82 |
try:
|
@@ -91,30 +121,40 @@ with chat_col:
|
|
91 |
with st.spinner("π€ Parsing and responding with referenced content..."):
|
92 |
while True:
|
93 |
run_status = client.beta.threads.runs.retrieve(thread_id=st.session_state.thread_id, run_id=run.id)
|
94 |
-
if run_status.status
|
95 |
break
|
96 |
time.sleep(1)
|
97 |
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
|
|
|
|
|
|
113 |
|
114 |
st.rerun()
|
115 |
except Exception as e:
|
116 |
st.error(f"β Error: {e}")
|
117 |
|
|
|
118 |
for msg in st.session_state.messages:
|
119 |
with st.chat_message(msg["role"]):
|
120 |
-
st.markdown(msg["content"], unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
st.session_state.image_url = None
|
32 |
if "image_updated" not in st.session_state:
|
33 |
st.session_state.image_updated = False
|
34 |
+
if "search_history" not in st.session_state:
|
35 |
+
st.session_state.search_history = []
|
36 |
|
37 |
# ------------------ Sidebar ------------------
|
38 |
st.sidebar.header("βοΈ Settings")
|
|
|
41 |
st.session_state.thread_id = None
|
42 |
st.session_state.image_url = None
|
43 |
st.session_state.image_updated = False
|
44 |
+
st.session_state.search_history = []
|
45 |
st.rerun()
|
46 |
|
47 |
show_image = st.sidebar.toggle("π Show Page Image", value=True)
|
|
|
67 |
st.markdown("### π§ Ask a Document-Specific Question")
|
68 |
prompt = st.chat_input("Example: What is the defects liability period?")
|
69 |
|
70 |
+
# -- Smart Query Controls
|
71 |
st.markdown("<hr style='margin:0.5em 0;'>", unsafe_allow_html=True)
|
72 |
+
st.markdown("#### π Navigate or Query Document")
|
73 |
+
col1, col2 = st.columns([1.2, 1])
|
74 |
+
with col1:
|
75 |
+
keyword = st.text_input("Jump to clause keyword or term", placeholder="e.g. defects, payment, WHS")
|
76 |
+
with col2:
|
77 |
+
if st.button("π Search Keyword") and keyword:
|
78 |
+
prompt = f"Find clauses or references related to: {keyword}"
|
79 |
+
st.session_state.search_history.append(keyword)
|
80 |
+
|
81 |
+
section_options = [
|
82 |
+
"Select a section...",
|
83 |
+
"1. Formal Instrument of Contract",
|
84 |
+
"2. Offer and Acceptance",
|
85 |
+
"3. Key Personnel",
|
86 |
+
"4. Contract Pricing",
|
87 |
+
"5. Contract Specifications",
|
88 |
+
"6. WHS Policies",
|
89 |
+
"7. Penalties and Delays",
|
90 |
+
"8. Dispute Resolution",
|
91 |
+
"9. Principal Obligations"
|
92 |
+
]
|
93 |
+
selected_section = st.selectbox("π Browse Sections", section_options)
|
94 |
+
if selected_section and selected_section != section_options[0]:
|
95 |
+
prompt = f"Summarize or list key points from section: {selected_section}"
|
96 |
+
|
97 |
+
actions = [
|
98 |
+
"Select an action...",
|
99 |
+
"List all contractual obligations",
|
100 |
+
"Summarize payment terms",
|
101 |
+
"List WHS responsibilities",
|
102 |
+
"Find delay-related penalties",
|
103 |
+
"Extract dispute resolution steps"
|
104 |
+
]
|
105 |
+
selected_action = st.selectbox("βοΈ Common Actions", actions)
|
106 |
+
if selected_action and selected_action != actions[0]:
|
107 |
+
prompt = selected_action
|
108 |
+
|
109 |
+
# -- Send prompt to assistant
|
110 |
if prompt:
|
111 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
112 |
try:
|
|
|
121 |
with st.spinner("π€ Parsing and responding with referenced content..."):
|
122 |
while True:
|
123 |
run_status = client.beta.threads.runs.retrieve(thread_id=st.session_state.thread_id, run_id=run.id)
|
124 |
+
if run_status.status in ("completed", "failed", "cancelled"):
|
125 |
break
|
126 |
time.sleep(1)
|
127 |
|
128 |
+
if run_status.status != "completed":
|
129 |
+
st.error(f"β οΈ Assistant failed: {run_status.status}")
|
130 |
+
else:
|
131 |
+
messages = client.beta.threads.messages.list(thread_id=st.session_state.thread_id)
|
132 |
+
for message in reversed(messages.data):
|
133 |
+
if message.role == "assistant":
|
134 |
+
assistant_message = message.content[0].text.value
|
135 |
+
st.session_state.messages.append({"role": "assistant", "content": assistant_message})
|
136 |
+
break
|
137 |
+
|
138 |
+
match = re.search(r'Document Reference:\s+(.+?),\s+Page\s+(\d+)', assistant_message)
|
139 |
+
if match:
|
140 |
+
doc_name = match.group(1).strip()
|
141 |
+
page = int(match.group(2))
|
142 |
+
page_str = f"{page:04d}"
|
143 |
+
image_url = f"https://raw.githubusercontent.com/AndrewLORTech/c2ozschlaegerforrestdale/main/{doc_name}/{doc_name}_page_{page_str}.png"
|
144 |
+
st.session_state.image_url = image_url
|
145 |
+
st.session_state.image_updated = True
|
146 |
|
147 |
st.rerun()
|
148 |
except Exception as e:
|
149 |
st.error(f"β Error: {e}")
|
150 |
|
151 |
+
# -- Display Chat History
|
152 |
for msg in st.session_state.messages:
|
153 |
with st.chat_message(msg["role"]):
|
154 |
+
st.markdown(msg["content"], unsafe_allow_html=True)
|
155 |
+
|
156 |
+
if st.session_state.search_history:
|
157 |
+
st.markdown("---")
|
158 |
+
st.markdown("#### π Recent Searches")
|
159 |
+
for term in reversed(st.session_state.search_history[-5:]):
|
160 |
+
st.markdown(f"- {term}")
|