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