-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.py
More file actions
396 lines (332 loc) · 14.7 KB
/
Copy pathcore.py
File metadata and controls
396 lines (332 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import os
import streamlit as st
import pandas as pd
import requests
from streamlit_gsheets import GSheetsConnection
st.set_page_config(page_title="Contract Review Interface", layout="wide")
conn = st.connection("gsheets", type=GSheetsConnection)
import json
SD_WORD_SERVER_URL = str(
os.environ.get(
"SD_WORD_SERVER_URL", "http://word-server-simple-grpc-service.dev.spotdraft.com"
)
)
class WordServerClient:
"""
A client class to interact with the wordserver server and convert docx files to text.
"""
def __init__(self, sd_word_server_url: str = SD_WORD_SERVER_URL) -> None:
self.sd_word_server_url = sd_word_server_url
def convert_file(self, file_content: bytes):
file_content = self.accept_track_changes(file_content)
paragraphs = self.segment_text(file_content)
document_text = self.preprocess_text(
"\n".join(
[
para_obj["paragraph_text"]
for para_obj in paragraphs
if para_obj["paragraph_text"]
]
)
)
return document_text
def accept_track_changes(self, file_content: bytes) -> bytes:
path = "api/numbering/accept-track-changes"
response = requests.post(
os.path.join(self.sd_word_server_url, path),
files={"file": file_content},
)
response.raise_for_status()
return response.content
def segment_text(self, file_content: bytes) -> list:
path = "api/numbering/segmenter"
response = requests.post(
os.path.join(self.sd_word_server_url, path),
files={"file": file_content},
)
response.raise_for_status()
return json.loads(response.text)
def preprocess_text(self, converted_text: str) -> str:
return converted_text
@st.cache_data
def word_server_client(file_url):
client = WordServerClient()
file_content = requests.get(file_url).content
return client.convert_file(file_content)
def initialize_guideline(guideline):
default_values = {
"id": "",
"guideline": "",
"guideline_quality": "Pending",
"guideline_improvement": "NA",
"guideline_improvement_other": "",
"status": "NOT_APPLICABLE",
"reason": "",
"reason_quality": "Pending",
"reason_improvement": "NA",
"reason_improvement_other": "",
"comment": "",
"comment_quality": "Pending",
"comment_improvement": "NA",
"comment_improvement_other": "",
"update_clause_text": "",
"update_clause_improvement": "NA",
"update_clause_improvement_other": "",
"selected_sources": [],
}
for key, default_value in default_values.items():
if key not in guideline:
guideline[key] = default_value
return guideline
def read_from_sheets(worksheet="Data"):
df = conn.read(spreadsheet=st.secrets["spreadsheet"], worksheet=worksheet, ttl=1)
return df
def update_or_append_to_sheets(values, worksheet="ReviewResults"):
worksheet = f"ReviewResults_{st.session_state.reviewer_name}"
existing_data = read_from_sheets(worksheet)
# Check if the guideline ID already exists
guideline_id = values[0] # Assuming the ID is the first element in values
if existing_data.empty:
# Initialize the data frame with the first row
existing_data = pd.DataFrame(columns=["id", "guideline", "guideline_quality", "guideline_improvement", "guideline_improvement_other", "status", "reason", "reason_quality", "reason_improvement", "reason_improvement_other", "comment", "comment_quality", "comment_improvement", "comment_improvement_other", "selected_sources", "update_clause_text", "update_clause_improvement", "update_clause_improvement_other"])
existing_row = existing_data[existing_data['id'] == guideline_id]
if len(existing_row) > 0:
# Update existing row
existing_data.loc[existing_data['id'] == guideline_id] = values
else:
# Append new row
existing_data = existing_data.append(pd.Series(values, index=existing_data.columns), ignore_index=True)
# Write updated data back to the sheet
conn.update(spreadsheet=st.secrets["spreadsheet"], data=existing_data, worksheet=worksheet)
def save_guideline(guideline):
data_to_write = [
guideline["id"], # Unique ID for the guideline
guideline["guideline"],
guideline["guideline_quality"],
guideline["guideline_improvement"],
guideline.get("guideline_improvement_other", ""),
guideline["status"],
guideline["reason"],
guideline["reason_quality"],
guideline["reason_improvement"],
guideline.get("reason_improvement_other", ""),
guideline["comment"],
guideline["comment_quality"],
guideline["comment_improvement"],
guideline.get("comment_improvement_other", ""),
",".join(guideline["selected_sources"]),
guideline["update_clause_text"],
guideline["update_clause_improvement"],
guideline.get("update_clause_improvement_other", ""),
]
update_or_append_to_sheets(data_to_write)
def display_contract(contract):
st.header("Contract")
selected_sources = st.session_state.guidelines[st.session_state.current_guideline]["selected_sources"]
def update_selected_sources(key):
# Only if the checkbox is checked, if unchecked, remove from selected_sources,
if st.session_state[key]:
st.session_state.guidelines[st.session_state.current_guideline]["selected_sources"].append(key.split("_")[-1])
else:
st.session_state.guidelines[st.session_state.current_guideline]["selected_sources"].remove(key.split("_")[-1])
# Custom CSS for scrollable container
st.markdown("""
<style>
.streamlit-expanderHeader {
font-size: 1em;
}
.stExpander {
border: none;
}
</style>
""", unsafe_allow_html=True)
# Create a scrollable container using st.expander
with st.expander("Contract Text", expanded=True):
contract_paragraphs = contract.split("\n")
for i, paragraph in enumerate(contract_paragraphs):
col1, col2 = st.columns([0.1, 0.9])
with col1:
is_selected = str(i) in selected_sources
st.checkbox(
"",
value=is_selected,
key=f"selected_source_{i}",
on_change=update_selected_sources,
args=(f'selected_source_{i}',))
with col2:
st.write(paragraph)
# Display selected sources (optional)
if 'selected_sources' in st.session_state and st.session_state.selected_sources:
st.write("Selected Sources:", ", ".join(sorted(st.session_state.selected_sources)))
def display_guidelines():
st.header("Guidelines")
# Give option to select a guideline to review
guideline = st.session_state.guidelines[st.session_state.current_guideline]
st.subheader(f"Guideline {st.session_state.current_guideline + 1} of {len(st.session_state.guidelines)}")
st.write(guideline["guideline"])
def update_selectbox(key):
st.session_state.guidelines[st.session_state.current_guideline][key] = st.session_state[f"{key}_{st.session_state.current_guideline}"]
# Guideline Quality
st.selectbox(
f"Guideline Quality: {guideline['guideline_quality']}",
("Pending", "Excellent", "Good", "Better", "Bad"),
key=f"guideline_quality_{st.session_state.current_guideline}",
on_change=update_selectbox,
args=('guideline_quality',),
index=["Pending", "Excellent", "Good", "Better", "Bad"].index(guideline['guideline_quality']),
)
if guideline['guideline_quality'] in ["Better", "Bad"]:
st.selectbox(
"What can be improved in the guideline?",
["NA", "Clarity", "Specificity", "Relevance", "Other"],
key=f"guideline_improvement_{st.session_state.current_guideline}",
on_change=update_selectbox,
args=('guideline_improvement',),
index=["NA", "Clarity", "Specificity", "Relevance", "Other"].index(guideline['guideline_improvement']),
)
if guideline["guideline_improvement"] == "Other":
guideline["guideline_improvement_other"] = st.text_input(
"Specify other guideline improvement:",
value=guideline["guideline_improvement_other"]
)
# Guideline Status
st.radio(
"Guideline Status",
["FOLLOWED", "NOT_FOLLOWED", "NOT_APPLICABLE"],
key=f"status_{st.session_state.current_guideline}",
on_change=update_selectbox,
args=('status',),
index=["FOLLOWED", "NOT_FOLLOWED", "NOT_APPLICABLE"].index(guideline['status']),
)
st.write(
"Selected Sources:",
", ".join([f"Paragraph {i}" for i in guideline['selected_sources']]),
)
guideline["reason"] = st.text_area(
"Reason", value=guideline["reason"], height=100
)
# Reason Quality
st.selectbox(
"Reason Quality",
["Pending", "Excellent", "Good", "Better", "Bad"],
key=f"reason_quality_{st.session_state.current_guideline}",
on_change=update_selectbox,
args=('reason_quality',),
index=["Pending", "Excellent", "Good", "Better", "Bad"].index(guideline['reason_quality']),
)
if guideline["reason_quality"] in ["Better", "Bad"]:
st.selectbox(
"What can be improved in the reason?",
["NA", "Clarity", "Specificity", "Relevance", "Other"],
key=f"reason_improvement_{st.session_state.current_guideline}",
on_change=update_selectbox,
args=('reason_improvement',),
index=["NA", "Clarity", "Specificity", "Relevance", "Other"].index(guideline['reason_improvement']),
)
if guideline["reason_improvement"] == "Other":
guideline["reason_improvement_other"] = st.text_input(
"Specify other reason for improvement:",
value=guideline["reason_improvement_other"]
)
guideline["comment"] = st.text_area(
"Comment", value=guideline["comment"], height=100
)
# Comment Quality
st.selectbox(
"Comment Quality",
["Pending", "Excellent", "Good", "Better", "Bad"],
key=f"comment_quality_{st.session_state.current_guideline}",
on_change=update_selectbox,
args=('comment_quality',),
index=["Pending", "Excellent", "Good", "Better", "Bad"].index(guideline['comment_quality']),
)
if guideline["comment_quality"] in ["Better", "Bad"]:
st.selectbox(
"What can be improved in the comment?",
["NA", "Clarity", "Detail", "Relevance", "Other"],
key=f"comment_improvement_{st.session_state.current_guideline}",
on_change=update_selectbox,
args=('comment_improvement',),
index=["NA", "Clarity", "Detail", "Relevance", "Other"].index(guideline['comment_improvement']),
)
if guideline["comment_improvement"] == "Other":
guideline["comment_improvement_other"] = st.text_input(
"Specify other comment improvement:",
value=guideline["comment_improvement_other"]
)
guideline["update_clause_text"] = st.text_area(
"Updated Clause Text", value=guideline["update_clause_text"], height=100
)
update_clause = st.radio("Update Clause Text", ["Correct", "Incorrect"])
if update_clause == "Incorrect":
st.selectbox(
"What can be improved in the updated clause text?",
["NA", "Changes too extensive", "Does not meet guideline", "Content inaccuracies", "Other"],
key=f"update_clause_improvement_{st.session_state.current_guideline}",
on_change=update_selectbox,
args=('update_clause_improvement',),
index=["NA", "Changes too extensive", "Does not meet guideline", "Content inaccuracies", "Other"].index(guideline['update_clause_improvement']),
)
if guideline["update_clause_improvement"] == "Other":
guideline["update_clause_improvement_other"] = st.text_input(
"Specify other update clause improvement:",
value=guideline["update_clause_improvement_other"]
)
st.session_state.guidelines[st.session_state.current_guideline] = guideline
return guideline
def main():
st.title("Contract Review Interface")
# Initialize session state
if "current_guideline" not in st.session_state:
st.session_state.current_guideline = 0
if "selected_sources" not in st.session_state:
st.session_state.selected_sources = set()
if "guidelines" not in st.session_state:
# Assume the guidelines data now includes a unique ID for each guideline
guidelines = read_from_sheets().to_dict("records")
st.session_state.guidelines = [initialize_guideline(g) for g in guidelines]
# Ask for reviewer name
name = st.text_input("Enter your name:")
# Add reviewer name to session state
st.session_state["reviewer_name"] = name
# Create two columns
# col1, col2 = st.columns(2)
# with col1:
contract_link = st.session_state.guidelines[st.session_state.current_guideline]["contract"]
# Fetch the contract from the URL link
contract = word_server_client(contract_link)
display_contract(contract)
# col2.float()
with st.sidebar:
def update_current_guideline():
st.session_state.current_guideline = st.session_state.guideline_number - 1
st.number_input(
"Guideline Number",
min_value=1,
max_value=len(st.session_state.guidelines),
value=st.session_state.current_guideline + 1,
key="guideline_number",
on_change=update_current_guideline,
)
updated_guideline = display_guidelines()
# Save button
if st.button("Save"):
save_guideline(updated_guideline)
st.rerun()
# Navigation buttons
col1, col2, col3 = st.columns([1, 1, 1])
with col1:
if st.button("Previous") and st.session_state.current_guideline > 0:
save_guideline(updated_guideline)
st.session_state.current_guideline -= 1
st.rerun()
with col3:
if st.button("Next") and st.session_state.current_guideline < len(st.session_state.guidelines) - 1:
save_guideline(updated_guideline)
st.session_state.current_guideline += 1
st.rerun()
# Progress bar
progress = (st.session_state.current_guideline + 1) / len(st.session_state.guidelines)
st.progress(progress)
if __name__ == "__main__":
main()