Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -49,24 +49,16 @@ if st.sidebar.button("π§Ή Clear Chat"):
|
|
49 |
|
50 |
show_image = st.sidebar.toggle("π Show Page Image", value=True)
|
51 |
|
52 |
-
# ------------------
|
53 |
-
st.
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
st.
|
60 |
-
|
61 |
-
|
62 |
-
with st.expander("π Navigate or Query Document"):
|
63 |
-
col1, col2 = st.columns([1.2, 1])
|
64 |
-
with col1:
|
65 |
-
keyword = st.text_input("Jump to clause keyword or term", placeholder="e.g. defects, payment, WHS")
|
66 |
-
with col2:
|
67 |
-
if st.button("π Search Keyword") and keyword:
|
68 |
-
prompt = f"Find clauses or references related to: {keyword}"
|
69 |
-
st.session_state.search_history.append(keyword)
|
70 |
|
71 |
section_options = [
|
72 |
"Select a section...",
|
@@ -82,7 +74,7 @@ with st.expander("π Navigate or Query Document"):
|
|
82 |
]
|
83 |
selected_section = st.selectbox("π Browse Sections", section_options)
|
84 |
if selected_section and selected_section != section_options[0]:
|
85 |
-
|
86 |
|
87 |
actions = [
|
88 |
"Select an action...",
|
@@ -94,7 +86,18 @@ with st.expander("π Navigate or Query Document"):
|
|
94 |
]
|
95 |
selected_action = st.selectbox("βοΈ Common Actions", actions)
|
96 |
if selected_action and selected_action != actions[0]:
|
97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
|
99 |
if st.session_state.search_history:
|
100 |
st.markdown("---")
|
@@ -102,63 +105,57 @@ with st.expander("π Navigate or Query Document"):
|
|
102 |
for term in reversed(st.session_state.search_history[-5:]):
|
103 |
st.markdown(f"- {term}")
|
104 |
|
105 |
-
# ------------------
|
106 |
-
with
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
if
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
st.rerun()
|
160 |
-
|
161 |
-
except Exception as e:
|
162 |
-
st.session_state.run_in_progress = False
|
163 |
-
st.error(f"β Error: {e}")
|
164 |
-
|
|
|
49 |
|
50 |
show_image = st.sidebar.toggle("π Show Page Image", value=True)
|
51 |
|
52 |
+
# ------------------ Horizontal Layout ------------------
|
53 |
+
left, center = st.columns([1, 2])
|
54 |
+
|
55 |
+
# ------------------ Expandable Left Section ------------------
|
56 |
+
with left.expander("π Navigate or Query Document", expanded=False):
|
57 |
+
keyword = st.text_input("Jump to clause keyword or term", placeholder="e.g. defects, payment, WHS")
|
58 |
+
if st.button("π Search Keyword") and keyword:
|
59 |
+
st.session_state.search_history.append(keyword)
|
60 |
+
if not st.session_state.run_in_progress:
|
61 |
+
st.session_state.messages.append({"role": "user", "content": f"Find clauses or references related to: {keyword}"})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
section_options = [
|
64 |
"Select a section...",
|
|
|
74 |
]
|
75 |
selected_section = st.selectbox("π Browse Sections", section_options)
|
76 |
if selected_section and selected_section != section_options[0]:
|
77 |
+
st.session_state.messages.append({"role": "user", "content": f"Summarize or list key points from section: {selected_section}"})
|
78 |
|
79 |
actions = [
|
80 |
"Select an action...",
|
|
|
86 |
]
|
87 |
selected_action = st.selectbox("βοΈ Common Actions", actions)
|
88 |
if selected_action and selected_action != actions[0]:
|
89 |
+
st.session_state.messages.append({"role": "user", "content": selected_action})
|
90 |
+
|
91 |
+
if show_image and st.session_state.image_url:
|
92 |
+
st.markdown("---")
|
93 |
+
st.markdown("### π Page Preview")
|
94 |
+
try:
|
95 |
+
response = requests.get(st.session_state.image_url)
|
96 |
+
response.raise_for_status()
|
97 |
+
img = Image.open(BytesIO(response.content))
|
98 |
+
st.image(img, caption="π OCR Page Image", use_container_width=True)
|
99 |
+
except Exception as e:
|
100 |
+
st.error(f"β Failed to load image: {e}")
|
101 |
|
102 |
if st.session_state.search_history:
|
103 |
st.markdown("---")
|
|
|
105 |
for term in reversed(st.session_state.search_history[-5:]):
|
106 |
st.markdown(f"- {term}")
|
107 |
|
108 |
+
# ------------------ Chat Section ------------------
|
109 |
+
with center:
|
110 |
+
st.markdown("### π§ Ask a Document-Specific Question")
|
111 |
+
prompt = st.chat_input("Example: What is the defects liability period?")
|
112 |
+
|
113 |
+
for msg in st.session_state.messages:
|
114 |
+
with st.chat_message(msg["role"]):
|
115 |
+
st.markdown(msg["content"], unsafe_allow_html=True)
|
116 |
+
|
117 |
+
if prompt and not st.session_state.run_in_progress:
|
118 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
119 |
+
st.session_state.run_in_progress = True
|
120 |
+
|
121 |
+
try:
|
122 |
+
if st.session_state.thread_id is None:
|
123 |
+
thread = client.beta.threads.create()
|
124 |
+
st.session_state.thread_id = thread.id
|
125 |
+
|
126 |
+
client.beta.threads.messages.create(thread_id=st.session_state.thread_id, role="user", content=prompt)
|
127 |
+
|
128 |
+
run = client.beta.threads.runs.create(thread_id=st.session_state.thread_id, assistant_id=ASSISTANT_ID)
|
129 |
+
|
130 |
+
with st.spinner("π€ Parsing and responding with referenced content..."):
|
131 |
+
while True:
|
132 |
+
run_status = client.beta.threads.runs.retrieve(thread_id=st.session_state.thread_id, run_id=run.id)
|
133 |
+
if run_status.status in ("completed", "failed", "cancelled"):
|
134 |
+
break
|
135 |
+
time.sleep(1)
|
136 |
+
|
137 |
+
if run_status.status != "completed":
|
138 |
+
st.error(f"β οΈ Assistant failed: {run_status.status}")
|
139 |
+
else:
|
140 |
+
messages = client.beta.threads.messages.list(thread_id=st.session_state.thread_id)
|
141 |
+
for message in reversed(messages.data):
|
142 |
+
if message.role == "assistant":
|
143 |
+
assistant_message = message.content[0].text.value
|
144 |
+
st.session_state.messages.append({"role": "assistant", "content": assistant_message})
|
145 |
+
break
|
146 |
+
|
147 |
+
match = re.search(r'Document Reference:\s+(.+?),\s+Page\s+(\d+)', assistant_message)
|
148 |
+
if match:
|
149 |
+
doc_name = match.group(1).strip()
|
150 |
+
page = int(match.group(2))
|
151 |
+
page_str = f"{page:04d}"
|
152 |
+
image_url = f"https://raw.githubusercontent.com/AndrewLORTech/c2ozschlaegerforrestdale/main/{doc_name}/{doc_name}_page_{page_str}.png"
|
153 |
+
st.session_state.image_url = image_url
|
154 |
+
st.session_state.image_updated = True
|
155 |
+
|
156 |
+
st.session_state.run_in_progress = False
|
157 |
+
st.rerun()
|
158 |
+
|
159 |
+
except Exception as e:
|
160 |
+
st.session_state.run_in_progress = False
|
161 |
+
st.error(f"β Error: {e}")
|
|
|
|
|
|
|
|
|
|
|
|