Update legal_document_analysis.py
Browse files- legal_document_analysis.py +642 -635
legal_document_analysis.py
CHANGED
@@ -1,636 +1,643 @@
|
|
1 |
-
import os
|
2 |
-
import PyPDF2
|
3 |
-
import streamlit as st
|
4 |
-
from dotenv import load_dotenv
|
5 |
-
from langchain_groq import ChatGroq
|
6 |
-
from docx import Document
|
7 |
-
import matplotlib.pyplot as plt
|
8 |
-
import io
|
9 |
-
import base64
|
10 |
-
from email.mime.multipart import MIMEMultipart
|
11 |
-
from email.mime.text import MIMEText
|
12 |
-
from email.mime.application import MIMEApplication
|
13 |
-
import smtplib
|
14 |
-
from fpdf import FPDF
|
15 |
-
import getpass
|
16 |
-
import pandas as pd
|
17 |
-
import seaborn as sns
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
os.environ
|
25 |
-
|
26 |
-
#
|
27 |
-
|
28 |
-
|
29 |
-
#
|
30 |
-
|
31 |
-
"""
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
color:
|
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 |
-
{"clause": "
|
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 |
-
{"phrase": "
|
182 |
-
{"phrase": "
|
183 |
-
{"phrase": "
|
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 |
-
ax.
|
235 |
-
ax.
|
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 |
-
plt.
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
plt.
|
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 |
-
pdf.
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
pdf.
|
355 |
-
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
-
pdf.set_font("Arial", '',
|
362 |
-
pdf.
|
363 |
-
pdf.ln(10)
|
364 |
-
|
365 |
-
#
|
366 |
-
pdf.set_font("Arial", 'B', 14)
|
367 |
-
pdf.cell(0, 10, '
|
368 |
-
pdf.set_font("Arial", '', 12)
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
-
|
374 |
-
pdf.
|
375 |
-
pdf.
|
376 |
-
|
377 |
-
|
378 |
-
pdf.
|
379 |
-
|
380 |
-
|
381 |
-
|
382 |
-
pdf.image(base64_to_image(
|
383 |
-
pdf.ln(
|
384 |
-
|
385 |
-
|
386 |
-
pdf.
|
387 |
-
pdf.
|
388 |
-
|
389 |
-
|
390 |
-
|
391 |
-
|
392 |
-
#
|
393 |
-
|
394 |
-
|
395 |
-
|
396 |
-
|
397 |
-
|
398 |
-
|
399 |
-
|
400 |
-
|
401 |
-
|
402 |
-
|
403 |
-
|
404 |
-
|
405 |
-
|
406 |
-
|
407 |
-
|
408 |
-
|
409 |
-
|
410 |
-
|
411 |
-
|
412 |
-
|
413 |
-
|
414 |
-
|
415 |
-
|
416 |
-
|
417 |
-
|
418 |
-
|
419 |
-
|
420 |
-
|
421 |
-
|
422 |
-
|
423 |
-
|
424 |
-
|
425 |
-
|
426 |
-
|
427 |
-
|
428 |
-
|
429 |
-
|
430 |
-
|
431 |
-
|
432 |
-
|
433 |
-
|
434 |
-
|
435 |
-
|
436 |
-
|
437 |
-
|
438 |
-
|
439 |
-
|
440 |
-
|
441 |
-
|
442 |
-
|
443 |
-
|
444 |
-
|
445 |
-
|
446 |
-
|
447 |
-
|
448 |
-
|
449 |
-
|
450 |
-
|
451 |
-
|
452 |
-
|
453 |
-
|
454 |
-
|
455 |
-
|
456 |
-
|
457 |
-
msg.attach(
|
458 |
-
|
459 |
-
|
460 |
-
|
461 |
-
|
462 |
-
|
463 |
-
|
464 |
-
|
465 |
-
|
466 |
-
|
467 |
-
|
468 |
-
|
469 |
-
|
470 |
-
|
471 |
-
|
472 |
-
|
473 |
-
|
474 |
-
|
475 |
-
|
476 |
-
|
477 |
-
|
478 |
-
|
479 |
-
|
480 |
-
|
481 |
-
|
482 |
-
|
483 |
-
|
484 |
-
|
485 |
-
|
486 |
-
|
487 |
-
|
488 |
-
|
489 |
-
|
490 |
-
|
491 |
-
|
492 |
-
|
493 |
-
|
494 |
-
|
495 |
-
|
496 |
-
|
497 |
-
|
498 |
-
|
499 |
-
|
500 |
-
|
501 |
-
|
502 |
-
|
503 |
-
|
504 |
-
|
505 |
-
|
506 |
-
|
507 |
-
|
508 |
-
|
509 |
-
|
510 |
-
|
511 |
-
|
512 |
-
|
513 |
-
|
514 |
-
|
515 |
-
|
516 |
-
st.
|
517 |
-
|
518 |
-
|
519 |
-
|
520 |
-
|
521 |
-
|
522 |
-
|
523 |
-
|
524 |
-
|
525 |
-
|
526 |
-
|
527 |
-
|
528 |
-
|
529 |
-
|
530 |
-
|
531 |
-
|
532 |
-
|
533 |
-
|
534 |
-
|
535 |
-
|
536 |
-
|
537 |
-
|
538 |
-
|
539 |
-
|
540 |
-
|
541 |
-
|
542 |
-
|
543 |
-
|
544 |
-
|
545 |
-
|
546 |
-
|
547 |
-
|
548 |
-
|
549 |
-
|
550 |
-
|
551 |
-
|
552 |
-
|
553 |
-
|
554 |
-
|
555 |
-
|
556 |
-
|
557 |
-
|
558 |
-
|
559 |
-
|
560 |
-
|
561 |
-
|
562 |
-
|
563 |
-
|
564 |
-
|
565 |
-
|
566 |
-
|
567 |
-
|
568 |
-
#
|
569 |
-
|
570 |
-
|
571 |
-
|
572 |
-
|
573 |
-
|
574 |
-
|
575 |
-
|
576 |
-
st.
|
577 |
-
|
578 |
-
|
579 |
-
|
580 |
-
|
581 |
-
|
582 |
-
|
583 |
-
|
584 |
-
|
585 |
-
|
586 |
-
|
587 |
-
|
588 |
-
|
589 |
-
|
590 |
-
|
591 |
-
|
592 |
-
|
593 |
-
|
594 |
-
|
595 |
-
|
596 |
-
|
597 |
-
|
598 |
-
#
|
599 |
-
st.
|
600 |
-
|
601 |
-
|
602 |
-
|
603 |
-
|
604 |
-
|
605 |
-
|
606 |
-
|
607 |
-
|
608 |
-
|
609 |
-
|
610 |
-
|
611 |
-
|
612 |
-
|
613 |
-
|
614 |
-
|
615 |
-
|
616 |
-
|
617 |
-
|
618 |
-
|
619 |
-
|
620 |
-
|
621 |
-
|
622 |
-
|
623 |
-
|
624 |
-
|
625 |
-
|
626 |
-
|
627 |
-
|
628 |
-
|
629 |
-
|
630 |
-
|
631 |
-
|
632 |
-
|
633 |
-
|
634 |
-
|
635 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
636 |
display_legal_analysis_page()
|
|
|
1 |
+
import os
|
2 |
+
import PyPDF2
|
3 |
+
import streamlit as st
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
from langchain_groq import ChatGroq
|
6 |
+
from docx import Document
|
7 |
+
import matplotlib.pyplot as plt
|
8 |
+
import io
|
9 |
+
import base64
|
10 |
+
from email.mime.multipart import MIMEMultipart
|
11 |
+
from email.mime.text import MIMEText
|
12 |
+
from email.mime.application import MIMEApplication
|
13 |
+
import smtplib
|
14 |
+
from fpdf import FPDF
|
15 |
+
import getpass
|
16 |
+
import pandas as pd
|
17 |
+
import seaborn as sns
|
18 |
+
|
19 |
+
from langchain_core.globals import set_verbose
|
20 |
+
set_verbose(False)
|
21 |
+
|
22 |
+
model = ChatGroq(
|
23 |
+
model="llama-3.1-8b-instant",
|
24 |
+
api_key=os.environ.get("GROQ_API_KEY"),
|
25 |
+
verbose=False
|
26 |
+
# Load environment variables from .env file
|
27 |
+
load_dotenv()
|
28 |
+
|
29 |
+
# Check if the GROQ_API_KEY is in the environment variables
|
30 |
+
if not os.environ.get("GROQ_API_KEY"):
|
31 |
+
os.environ["GROQ_API_KEY"] = getpass.getpass("Enter API key for Groq: ")
|
32 |
+
|
33 |
+
# Initialize the model
|
34 |
+
model = ChatGroq(model="llama-3.1-8b-instant", api_key=os.environ.get("GROQ_API_KEY"))
|
35 |
+
|
36 |
+
# Custom CSS for improved aesthetics
|
37 |
+
st.markdown(
|
38 |
+
"""
|
39 |
+
<style>
|
40 |
+
.main {
|
41 |
+
background-color: #f0f2f5;
|
42 |
+
}
|
43 |
+
.sidebar .sidebar-content {
|
44 |
+
background-color: #ffffff;
|
45 |
+
}
|
46 |
+
h1 {
|
47 |
+
color: #2C3E50;
|
48 |
+
}
|
49 |
+
h2 {
|
50 |
+
color: #2980B9;
|
51 |
+
}
|
52 |
+
.stButton button {
|
53 |
+
background-color: #2980B9;
|
54 |
+
color: white;
|
55 |
+
border: None;
|
56 |
+
border-radius: 5px;
|
57 |
+
padding: 10px;
|
58 |
+
}
|
59 |
+
</style>
|
60 |
+
""",
|
61 |
+
unsafe_allow_html=True
|
62 |
+
)
|
63 |
+
|
64 |
+
# Function to read PDF content
|
65 |
+
def read_pdf(file):
|
66 |
+
reader = PyPDF2.PdfReader(file)
|
67 |
+
text = ""
|
68 |
+
for page in reader.pages:
|
69 |
+
text += page.extract_text()
|
70 |
+
return text
|
71 |
+
|
72 |
+
# Function to extract text from DOCX files
|
73 |
+
def extract_text_from_docx(file):
|
74 |
+
doc = Document(file)
|
75 |
+
text = "\n".join([paragraph.text for paragraph in doc.paragraphs])
|
76 |
+
return text
|
77 |
+
|
78 |
+
# Function to preprocess text
|
79 |
+
def preprocess_text(text):
|
80 |
+
return " ".join(text.replace("\n", " ").replace("\r", " ").split())
|
81 |
+
|
82 |
+
# Function to chunk large text into smaller parts
|
83 |
+
def chunk_text(text, max_tokens=2000):
|
84 |
+
chunks = []
|
85 |
+
current_chunk = []
|
86 |
+
current_length = 0
|
87 |
+
|
88 |
+
for sentence in text.split(". "):
|
89 |
+
sentence_length = len(sentence.split())
|
90 |
+
if current_length + sentence_length <= max_tokens:
|
91 |
+
current_chunk.append(sentence)
|
92 |
+
current_length += sentence_length
|
93 |
+
else:
|
94 |
+
chunks.append(". ".join(current_chunk))
|
95 |
+
current_chunk = [sentence]
|
96 |
+
current_length = sentence_length
|
97 |
+
|
98 |
+
if current_chunk:
|
99 |
+
chunks.append(". ".join(current_chunk))
|
100 |
+
|
101 |
+
return chunks
|
102 |
+
|
103 |
+
# Function to generate summary for each chunk
|
104 |
+
def generate_summary(text):
|
105 |
+
prompt = f"Please summarize the following content:\n\n{text}"
|
106 |
+
try:
|
107 |
+
response = model.invoke(prompt)
|
108 |
+
if hasattr(response, 'content'):
|
109 |
+
summary = response.content
|
110 |
+
else:
|
111 |
+
summary = str(response)
|
112 |
+
return summary.strip() if summary else "No summary available."
|
113 |
+
except Exception as e:
|
114 |
+
st.error(f"Error generating summary: {str(e)}")
|
115 |
+
return None
|
116 |
+
|
117 |
+
# Function to summarize large texts
|
118 |
+
def summarize_large_text(text, chunk_limit=5000):
|
119 |
+
chunks = chunk_text(text, max_tokens=chunk_limit)
|
120 |
+
summaries = []
|
121 |
+
for chunk in chunks:
|
122 |
+
summary = generate_summary(chunk)
|
123 |
+
if summary:
|
124 |
+
summaries.append(summary)
|
125 |
+
return " ".join(summaries)
|
126 |
+
|
127 |
+
# Function to detect key clauses
|
128 |
+
def detect_key_clauses(text):
|
129 |
+
key_clauses = [
|
130 |
+
{"clause": "confidentiality", "summary": "Confidentiality clauses ensure that sensitive information remains protected."},
|
131 |
+
{"clause": "liability", "summary": "Liability clauses outline the responsibility for damages or losses incurred."},
|
132 |
+
{"clause": "termination", "summary": "Termination clauses specify the conditions under which a contract may be ended."},
|
133 |
+
{"clause": "force majeure", "summary": "Force majeure clauses excuse parties from performance obligations due to unforeseen events."},
|
134 |
+
{"clause": "governing law", "summary": "Governing law clauses specify which jurisdiction's laws will govern the contract."},
|
135 |
+
{"clause": "dispute resolution", "summary": "Dispute resolution clauses specify how conflicts between parties will be resolved."},
|
136 |
+
{"clause": "amendment", "summary": "Amendment clauses outline the process for changing the terms of the contract."},
|
137 |
+
{"clause": "warranty", "summary": "Warranty clauses provide assurances regarding the quality or condition of goods or services."},
|
138 |
+
]
|
139 |
+
|
140 |
+
detected_clauses = []
|
141 |
+
for clause in key_clauses:
|
142 |
+
if clause["clause"].lower() in text.lower():
|
143 |
+
clause_start = text.lower().find(clause["clause"].lower())
|
144 |
+
context = text[clause_start - 50: clause_start + 200]
|
145 |
+
explanation = f"The document mentions '{clause['clause']}' clause. Context: {context.strip()}..."
|
146 |
+
detected_clauses.append({
|
147 |
+
"clause": clause["clause"].capitalize(),
|
148 |
+
"summary": clause["summary"],
|
149 |
+
"explanation": explanation
|
150 |
+
})
|
151 |
+
|
152 |
+
return detected_clauses
|
153 |
+
|
154 |
+
# Function to detect hidden obligations or dependencies
|
155 |
+
def detect_hidden_obligations_or_dependencies(text, summary):
|
156 |
+
hidden_obligations = [
|
157 |
+
{"phrase": "dependent upon", "summary": "This suggests that some action is conditional upon another."},
|
158 |
+
{"phrase": "if", "summary": "This indicates that certain conditions must be met to fulfill the obligation."},
|
159 |
+
{"phrase": "may be required", "summary": "Implies that the party could be obligated to perform an action under specific conditions."},
|
160 |
+
{"phrase": "should", "summary": "Implies a recommendation or requirement, though not explicitly mandatory."},
|
161 |
+
{"phrase": "obligated to", "summary": "Indicates a clear, binding duty to perform an action."},
|
162 |
+
]
|
163 |
+
|
164 |
+
hidden_dependencies = []
|
165 |
+
|
166 |
+
for item in hidden_obligations:
|
167 |
+
if item["phrase"].lower() in text.lower() or item["phrase"].lower() in summary.lower():
|
168 |
+
phrase_start = text.lower().find(item["phrase"].lower())
|
169 |
+
context = text[phrase_start - 50: phrase_start + 200]
|
170 |
+
hidden_dependencies.append({
|
171 |
+
"phrase": item["phrase"],
|
172 |
+
"summary": item["summary"],
|
173 |
+
"context": context.strip()
|
174 |
+
})
|
175 |
+
|
176 |
+
return hidden_dependencies
|
177 |
+
|
178 |
+
# Function to detect risks in the text
|
179 |
+
def detect_risks(text, summary):
|
180 |
+
risk_phrases = [
|
181 |
+
{"phrase": "penalty", "summary": "This indicates financial or legal consequences.", "risk_level": "High"},
|
182 |
+
{"phrase": "liability", "summary": "This suggests potential financial responsibility.", "risk_level": "Medium"},
|
183 |
+
{"phrase": "default", "summary": "This can lead to serious legal consequences.", "risk_level": "High"},
|
184 |
+
{"phrase": "breach", "summary": "This may expose the party to significant penalties.", "risk_level": "High"},
|
185 |
+
{"phrase": "suspension", "summary": "This indicates risks of halting services.", "risk_level": "Medium"},
|
186 |
+
{"phrase": "should", "summary": "This implies a recommendation, which may not be mandatory.", "risk_level": "Low"},
|
187 |
+
{"phrase": "may be required", "summary": "This suggests that obligations could exist under certain conditions.", "risk_level": "Low"},
|
188 |
+
{"phrase": "indemnify", "summary": "This entails a duty to compensate for harm or loss, indicating potential financial risk.", "risk_level": "High"},
|
189 |
+
{"phrase": "termination for cause", "summary": "This indicates a risk of ending the contract due to specific failures.", "risk_level": "High"},
|
190 |
+
{"phrase": "compliance", "summary": "Non-compliance with regulations can lead to legal penalties.", "risk_level": "High"},
|
191 |
+
]
|
192 |
+
|
193 |
+
detected_risks = []
|
194 |
+
|
195 |
+
for item in risk_phrases:
|
196 |
+
if item["phrase"].lower() in text.lower() or item["phrase"].lower() in summary.lower():
|
197 |
+
phrase_start = text.lower().find(item["phrase"].lower())
|
198 |
+
context = text[phrase_start - 50: phrase_start + 200]
|
199 |
+
detected_risks.append({
|
200 |
+
"phrase": item["phrase"],
|
201 |
+
"summary": item["summary"],
|
202 |
+
"context": context.strip(),
|
203 |
+
"risk_level": item["risk_level"]
|
204 |
+
})
|
205 |
+
|
206 |
+
return detected_risks
|
207 |
+
|
208 |
+
# Function to calculate overall risk score
|
209 |
+
def calculate_overall_risk_score(detected_risks):
|
210 |
+
risk_scores = {
|
211 |
+
"High": 3,
|
212 |
+
"Medium": 2,
|
213 |
+
"Low": 1
|
214 |
+
}
|
215 |
+
total_score = sum(risk_scores.get(risk['risk_level'], 0) for risk in detected_risks)
|
216 |
+
return total_score
|
217 |
+
|
218 |
+
# Function to plot risk assessment matrix
|
219 |
+
def plot_risk_assessment_matrix(detected_risks):
|
220 |
+
likelihood = []
|
221 |
+
impact = []
|
222 |
+
|
223 |
+
for risk in detected_risks:
|
224 |
+
if risk['risk_level'] == 'High':
|
225 |
+
likelihood.append(3)
|
226 |
+
impact.append(3)
|
227 |
+
elif risk['risk_level'] == 'Medium':
|
228 |
+
likelihood.append(2)
|
229 |
+
impact.append(2)
|
230 |
+
elif risk['risk_level'] == 'Low':
|
231 |
+
likelihood.append(1)
|
232 |
+
impact.append(1)
|
233 |
+
|
234 |
+
fig, ax = plt.subplots(figsize=(6, 6))
|
235 |
+
scatter = ax.scatter(likelihood, impact, alpha=0.6)
|
236 |
+
|
237 |
+
ax.set_xticks([1, 2, 3])
|
238 |
+
ax.set_yticks([1, 2, 3])
|
239 |
+
ax.set_xticklabels(['Low', 'Medium', 'High'])
|
240 |
+
ax.set_yticklabels(['Low', 'Medium', 'High'])
|
241 |
+
ax.set_xlabel('Likelihood')
|
242 |
+
ax.set_ylabel('Impact')
|
243 |
+
ax.set_title('Risk Assessment Matrix')
|
244 |
+
|
245 |
+
for i in range(len(detected_risks)):
|
246 |
+
ax.annotate(detected_risks[i]['phrase'], (likelihood[i], impact[i]))
|
247 |
+
|
248 |
+
buf = io.BytesIO()
|
249 |
+
plt.savefig(buf, format="png", bbox_inches='tight')
|
250 |
+
buf.seek(0)
|
251 |
+
|
252 |
+
img_str = base64.b64encode(buf.read()).decode('utf-8')
|
253 |
+
buf.close()
|
254 |
+
|
255 |
+
return img_str
|
256 |
+
|
257 |
+
# Function to plot risk level distribution pie chart
|
258 |
+
def plot_risk_level_distribution(detected_risks):
|
259 |
+
risk_levels = [risk['risk_level'] for risk in detected_risks]
|
260 |
+
level_counts = {level: risk_levels.count(level) for level in set(risk_levels)}
|
261 |
+
|
262 |
+
fig, ax = plt.subplots(figsize=(4, 3))
|
263 |
+
ax.pie(level_counts.values(), labels=level_counts.keys(), autopct='%1.1f%%', startangle=90)
|
264 |
+
ax.axis('equal')
|
265 |
+
|
266 |
+
plt.title("Risk Level Distribution", fontsize=10)
|
267 |
+
|
268 |
+
buf = io.BytesIO()
|
269 |
+
plt.savefig(buf, format="png", bbox_inches='tight')
|
270 |
+
buf.seek(0)
|
271 |
+
|
272 |
+
img_str = base64.b64encode(buf.read()).decode('utf-8')
|
273 |
+
buf.close()
|
274 |
+
|
275 |
+
return img_str
|
276 |
+
|
277 |
+
# Function to plot risks by type bar chart
|
278 |
+
def plot_risks_by_type(detected_risks):
|
279 |
+
risk_phrases = [risk['phrase'] for risk in detected_risks]
|
280 |
+
phrase_counts = {phrase: risk_phrases.count(phrase) for phrase in set(risk_phrases)}
|
281 |
+
|
282 |
+
fig, ax = plt.subplots(figsize=(4, 3))
|
283 |
+
ax.bar(phrase_counts.keys(), phrase_counts.values(), color='lightcoral')
|
284 |
+
plt.xticks(rotation=45, ha='right')
|
285 |
+
ax.set_title("Risks by Type", fontsize=10)
|
286 |
+
ax.set_ylabel("Count")
|
287 |
+
|
288 |
+
buf = io.BytesIO()
|
289 |
+
plt.savefig(buf, format="png", bbox_inches='tight')
|
290 |
+
buf.seek(0)
|
291 |
+
|
292 |
+
img_str = base64.b64encode(buf.read()).decode('utf-8')
|
293 |
+
buf.close()
|
294 |
+
|
295 |
+
return img_str
|
296 |
+
|
297 |
+
# Function to plot stacked bar chart of risks by level
|
298 |
+
def plot_stacked_bar_chart(detected_risks):
|
299 |
+
risk_levels = ['High', 'Medium', 'Low']
|
300 |
+
level_counts = {level: 0 for level in risk_levels}
|
301 |
+
|
302 |
+
for risk in detected_risks:
|
303 |
+
level_counts[risk['risk_level']] += 1
|
304 |
+
|
305 |
+
fig, ax = plt.subplots(figsize=(4, 3))
|
306 |
+
ax.bar(level_counts.keys(), level_counts.values(), color=['#ff9999', '#66b3ff', '#99ff99'])
|
307 |
+
ax.set_title("Stacked Bar Chart of Risks by Level", fontsize=10)
|
308 |
+
ax.set_ylabel("Count")
|
309 |
+
|
310 |
+
buf = io.BytesIO()
|
311 |
+
plt.savefig(buf, format="png", bbox_inches='tight')
|
312 |
+
buf.seek(0)
|
313 |
+
|
314 |
+
img_str = base64.b64encode(buf.read()).decode('utf-8')
|
315 |
+
buf.close()
|
316 |
+
|
317 |
+
return img_str
|
318 |
+
|
319 |
+
# Function to plot risk heatmap
|
320 |
+
def plot_risk_heatmap(detected_risks):
|
321 |
+
risk_data = {'Risk Level': [], 'Count': []}
|
322 |
+
|
323 |
+
for risk in detected_risks:
|
324 |
+
risk_data['Risk Level'].append(risk['risk_level'])
|
325 |
+
risk_data['Count'].append(1)
|
326 |
+
|
327 |
+
df = pd.DataFrame(risk_data)
|
328 |
+
heatmap_data = df.groupby('Risk Level').count().reset_index()
|
329 |
+
|
330 |
+
fig, ax = plt.subplots(figsize=(4, 3))
|
331 |
+
sns.heatmap(heatmap_data.pivot_table(index='Risk Level', values='Count'), annot=True, cmap='YlGnBu', ax=ax)
|
332 |
+
ax.set_title("Risk Heatmap")
|
333 |
+
|
334 |
+
buf = io.BytesIO()
|
335 |
+
plt.savefig(buf, format="png", bbox_inches='tight')
|
336 |
+
buf.seek(0)
|
337 |
+
|
338 |
+
img_str = base64.b64encode(buf.read()).decode('utf-8')
|
339 |
+
buf.close()
|
340 |
+
|
341 |
+
return img_str
|
342 |
+
|
343 |
+
# Function to convert base64 to image
|
344 |
+
def base64_to_image(data):
|
345 |
+
return io.BytesIO(base64.b64decode(data))
|
346 |
+
|
347 |
+
# Function to generate PDF document with improved aesthetics
|
348 |
+
def generate_pdf_analysis(document_text, summary, detected_clauses, hidden_obligations, detected_risks, risk_assessment_matrix, risk_level_distribution, risks_by_type, stacked_bar_chart, risk_heatmap):
|
349 |
+
pdf = FPDF()
|
350 |
+
pdf.add_page()
|
351 |
+
|
352 |
+
# Set page borders
|
353 |
+
pdf.set_draw_color(0, 0, 0)
|
354 |
+
pdf.rect(5, 5, 200, 287)
|
355 |
+
|
356 |
+
# Add Arial font
|
357 |
+
pdf.add_font("Arial", "", "arial.ttf", uni=True)
|
358 |
+
pdf.set_font("Arial", size=12)
|
359 |
+
|
360 |
+
# Title
|
361 |
+
pdf.set_font("Arial", 'B', 16)
|
362 |
+
pdf.cell(0, 10, 'Legal Document Analysis Report', ln=True, align='C')
|
363 |
+
pdf.ln(10)
|
364 |
+
|
365 |
+
# Executive Summary
|
366 |
+
pdf.set_font("Arial", 'B', 14)
|
367 |
+
pdf.cell(0, 10, 'Executive Summary', ln=True)
|
368 |
+
pdf.set_font("Arial", '', 12)
|
369 |
+
pdf.multi_cell(0, 10, summary)
|
370 |
+
pdf.ln(10)
|
371 |
+
|
372 |
+
# Risks Section
|
373 |
+
pdf.set_font("Arial", 'B', 14)
|
374 |
+
pdf.cell(0, 10, 'Risk Analysis', ln=True)
|
375 |
+
pdf.set_font("Arial", '', 12)
|
376 |
+
for risk in detected_risks:
|
377 |
+
pdf.cell(0, 10, f"{risk['phrase']}: {risk['summary']} (Risk Level: {risk['risk_level']})", ln=True)
|
378 |
+
pdf.ln(10)
|
379 |
+
|
380 |
+
# Add visualizations for risks
|
381 |
+
pdf.image(base64_to_image(risk_assessment_matrix), x=10, y=pdf.get_y(), w=90)
|
382 |
+
pdf.image(base64_to_image(risk_level_distribution), x=110, y=pdf.get_y()-50, w=90) # Position next to the first image
|
383 |
+
pdf.ln(60)
|
384 |
+
|
385 |
+
pdf.image(base64_to_image(risks_by_type), x=10, y=pdf.get_y(), w=90)
|
386 |
+
pdf.image(base64_to_image(stacked_bar_chart), x=110, y=pdf.get_y()-50, w=90) # Position next to the previous image
|
387 |
+
pdf.ln(60)
|
388 |
+
|
389 |
+
pdf.image(base64_to_image(risk_heatmap), x=10, y=pdf.get_y(), w=190) # Fit image to width
|
390 |
+
pdf.ln(10)
|
391 |
+
|
392 |
+
# Footer
|
393 |
+
pdf.set_y(-15)
|
394 |
+
pdf.set_font("Arial", 'I', 8)
|
395 |
+
pdf.cell(0, 10, f'Page {pdf.page_no()}', 0, 0, 'C')
|
396 |
+
|
397 |
+
return pdf
|
398 |
+
|
399 |
+
# Function to handle chatbot interaction
|
400 |
+
def chatbot_query(user_input):
|
401 |
+
try:
|
402 |
+
response = model({"text": user_input})
|
403 |
+
if isinstance(response, dict) and 'text' in response:
|
404 |
+
return response['text']
|
405 |
+
else:
|
406 |
+
return "Error: Unexpected response format."
|
407 |
+
except Exception as e:
|
408 |
+
return f"Error: {str(e)}"
|
409 |
+
|
410 |
+
# Function to generate suggestions for improvement
|
411 |
+
def generate_suggestions(text):
|
412 |
+
suggestions = []
|
413 |
+
|
414 |
+
if "shall" in text.lower():
|
415 |
+
suggestions.append("Consider replacing 'shall' with 'must' for clarity.")
|
416 |
+
if "may" in text.lower():
|
417 |
+
suggestions.append("Clarify the conditions under which actions 'may' be taken.")
|
418 |
+
if "if" in text.lower() and "then" not in text.lower():
|
419 |
+
suggestions.append("Ensure conditional statements are clear and complete.")
|
420 |
+
if "not" in text.lower():
|
421 |
+
suggestions.append("Review negative clauses to ensure they are not overly restrictive.")
|
422 |
+
|
423 |
+
return suggestions
|
424 |
+
|
425 |
+
# Function to send feedback via email
|
426 |
+
def send_feedback(feedback_content):
|
427 |
+
sender_email = os.getenv("SENDER_EMAIL")
|
428 |
+
receiver_email = os.getenv("FEEDBACK_EMAIL")
|
429 |
+
password = os.getenv("EMAIL_PASS")
|
430 |
+
|
431 |
+
msg = MIMEMultipart()
|
432 |
+
msg['From'] = sender_email
|
433 |
+
msg['To'] = receiver_email
|
434 |
+
msg['Subject'] = "User Feedback on Legal Document Analysis"
|
435 |
+
|
436 |
+
msg.attach(MIMEText(feedback_content, 'plain'))
|
437 |
+
|
438 |
+
try:
|
439 |
+
with smtplib.SMTP('smtp.gmail.com', 587) as server:
|
440 |
+
server.starttls()
|
441 |
+
server.login(sender_email, password)
|
442 |
+
server.send_message(msg)
|
443 |
+
return True
|
444 |
+
except Exception as e:
|
445 |
+
return False
|
446 |
+
|
447 |
+
# Function to send PDF via email
|
448 |
+
def send_pdf_via_email(pdf_buffer, recipient_email):
|
449 |
+
sender_email = os.getenv("SENDER_EMAIL")
|
450 |
+
password = os.getenv("EMAIL_PASS")
|
451 |
+
|
452 |
+
msg = MIMEMultipart()
|
453 |
+
msg['From'] = sender_email
|
454 |
+
msg['To'] = recipient_email
|
455 |
+
msg['Subject'] = "Legal Document Analysis PDF"
|
456 |
+
|
457 |
+
msg.attach(MIMEText("Please find the attached analysis of your legal document.", 'plain'))
|
458 |
+
|
459 |
+
# Attach the PDF
|
460 |
+
pdf_attachment = io.BytesIO(pdf_buffer.getvalue())
|
461 |
+
pdf_attachment.seek(0)
|
462 |
+
part = MIMEApplication(pdf_attachment.read(), Name='legal_document_analysis.pdf')
|
463 |
+
part['Content-Disposition'] = 'attachment; filename="legal_document_analysis.pdf"'
|
464 |
+
msg.attach(part)
|
465 |
+
|
466 |
+
try:
|
467 |
+
with smtplib.SMTP('smtp.gmail.com', 587) as server:
|
468 |
+
server.starttls()
|
469 |
+
server.login(sender_email, password)
|
470 |
+
server.send_message(msg)
|
471 |
+
return True
|
472 |
+
except Exception as e:
|
473 |
+
return False
|
474 |
+
|
475 |
+
# Function to simulate tracking updates in the document
|
476 |
+
def track_updates(document_text):
|
477 |
+
updates = [
|
478 |
+
{"update": "Updated confidentiality clause.", "suggestion": "Consider specifying the duration of confidentiality."},
|
479 |
+
{"update": "Revised liability limits.", "suggestion": "Ensure the limits are realistic and compliant with regulations."},
|
480 |
+
{"update": "Clarified termination conditions.", "suggestion": "Check if all potential termination scenarios are covered."},
|
481 |
+
]
|
482 |
+
return updates
|
483 |
+
|
484 |
+
# Function to get suggestion from Groq API based on the update
|
485 |
+
def get_update_suggestion(update):
|
486 |
+
prompt = f"Suggest improvements or updates for this legal clause: {update}"
|
487 |
+
suggestion = generate_summary(prompt)
|
488 |
+
return suggestion if suggestion else "No suggestion available."
|
489 |
+
|
490 |
+
# Function to display feedback form
|
491 |
+
def display_feedback_form():
|
492 |
+
st.subheader("Feedback Form")
|
493 |
+
feedback = st.text_area("Please provide your feedback or suggestions:")
|
494 |
+
|
495 |
+
question1 = st.radio("How would you rate the analysis?", ("Excellent", "Good", "Fair", "Poor"))
|
496 |
+
question2 = st.radio("Would you recommend this tool to others?", ("Yes", "No"))
|
497 |
+
|
498 |
+
if st.button("Submit Feedback"):
|
499 |
+
feedback_content = f"Feedback: {feedback}\nRating: {question1}\nRecommendation: {question2}"
|
500 |
+
if send_feedback(feedback_content):
|
501 |
+
st.success("Thank you for your feedback! It has been sent.")
|
502 |
+
else:
|
503 |
+
st.error("Failed to send feedback. Please try again later.")
|
504 |
+
|
505 |
+
# Main function to display the legal analysis page
|
506 |
+
def display_legal_analysis_page():
|
507 |
+
st.title("π Legal Document Analysis with Groq API")
|
508 |
+
|
509 |
+
uploaded_file = st.file_uploader("Upload your legal document (PDF or DOCX)", type=["pdf", "docx"])
|
510 |
+
if uploaded_file:
|
511 |
+
if uploaded_file.name.endswith(".pdf"):
|
512 |
+
document_text = preprocess_text(read_pdf(uploaded_file))
|
513 |
+
elif uploaded_file.name.endswith(".docx"):
|
514 |
+
document_text = preprocess_text(extract_text_from_docx(uploaded_file))
|
515 |
+
else:
|
516 |
+
st.error("Unsupported file type!")
|
517 |
+
return
|
518 |
+
|
519 |
+
tabs = st.tabs(["π Document Text", "π Summary", "π Key Clauses", "π Hidden Obligations", "β Risk Analysis", "π‘ Suggestions & Chatbot", "π Update Tracker"])
|
520 |
+
|
521 |
+
with tabs[0]:
|
522 |
+
st.subheader("Document Text")
|
523 |
+
st.write(document_text)
|
524 |
+
|
525 |
+
with tabs[1]:
|
526 |
+
st.subheader("Summary")
|
527 |
+
summary = summarize_large_text(document_text)
|
528 |
+
st.write(summary)
|
529 |
+
|
530 |
+
with tabs[2]:
|
531 |
+
st.subheader("Key Clauses Identified")
|
532 |
+
detected_clauses = detect_key_clauses(document_text)
|
533 |
+
if detected_clauses:
|
534 |
+
for clause in detected_clauses:
|
535 |
+
with st.expander(clause['clause'], expanded=False):
|
536 |
+
st.write(f"*Summary:* {clause['summary']}")
|
537 |
+
st.write(f"*Context:* {clause['explanation']}")
|
538 |
+
|
539 |
+
else:
|
540 |
+
st.write("No key clauses detected.")
|
541 |
+
|
542 |
+
with tabs[3]:
|
543 |
+
st.subheader("Hidden Obligations and Dependencies")
|
544 |
+
hidden_obligations = detect_hidden_obligations_or_dependencies(document_text, summary)
|
545 |
+
if hidden_obligations:
|
546 |
+
for obligation in hidden_obligations:
|
547 |
+
st.write(f"{obligation['phrase']}: {obligation['summary']}")
|
548 |
+
st.write(obligation['context'])
|
549 |
+
else:
|
550 |
+
st.write("No hidden obligations detected.")
|
551 |
+
|
552 |
+
with tabs[4]:
|
553 |
+
st.subheader("Risk Analysis")
|
554 |
+
detected_risks = detect_risks(document_text, summary)
|
555 |
+
overall_risk_score = calculate_overall_risk_score(detected_risks)
|
556 |
+
|
557 |
+
st.write(f"*Overall Risk Score:* {overall_risk_score}")
|
558 |
+
|
559 |
+
if detected_risks:
|
560 |
+
for risk in detected_risks:
|
561 |
+
with st.expander(risk['phrase'], expanded=False):
|
562 |
+
st.write(f"*Summary:* {risk['summary']} (Risk Level: {risk['risk_level']})")
|
563 |
+
short_context = risk['context'].strip().split('. ')[0] + '.'
|
564 |
+
st.write(f"*Context:* {short_context}")
|
565 |
+
else:
|
566 |
+
st.write("No risks detected.")
|
567 |
+
|
568 |
+
# Generate all visualizations
|
569 |
+
risk_assessment_matrix = plot_risk_assessment_matrix(detected_risks)
|
570 |
+
risk_level_distribution = plot_risk_level_distribution(detected_risks)
|
571 |
+
risks_by_type = plot_risks_by_type(detected_risks)
|
572 |
+
stacked_bar_chart = plot_stacked_bar_chart(detected_risks)
|
573 |
+
risk_heatmap = plot_risk_heatmap(detected_risks)
|
574 |
+
|
575 |
+
# Display the charts
|
576 |
+
st.image(f"data:image/png;base64,{risk_assessment_matrix}", caption="Risk Assessment Matrix")
|
577 |
+
st.image(f"data:image/png;base64,{risk_level_distribution}", caption="Risk Level Distribution")
|
578 |
+
st.image(f"data:image/png;base64,{risks_by_type}", caption="Risks by Type")
|
579 |
+
st.image(f"data:image/png;base64,{stacked_bar_chart}", caption="Stacked Bar Chart of Risks by Level")
|
580 |
+
st.image(f"data:image/png;base64,{risk_heatmap}", caption="Risk Heatmap")
|
581 |
+
|
582 |
+
with tabs[5]:
|
583 |
+
st.subheader("Suggestions for Improvement")
|
584 |
+
suggestions = generate_suggestions(document_text)
|
585 |
+
for suggestion in suggestions:
|
586 |
+
st.write(f"- {suggestion}")
|
587 |
+
|
588 |
+
st.subheader("Chatbot for Analysis")
|
589 |
+
user_input = st.text_input("Ask the chatbot about your document:")
|
590 |
+
if st.button("Send"):
|
591 |
+
if user_input:
|
592 |
+
chatbot_response = chatbot_query(user_input)
|
593 |
+
st.write("*Chatbot Response:*")
|
594 |
+
st.write(chatbot_response)
|
595 |
+
else:
|
596 |
+
st.warning("Please enter a question.")
|
597 |
+
|
598 |
+
# Download PDF Analysis Button
|
599 |
+
st.subheader("Download Analysis as PDF")
|
600 |
+
pdf_buffer = io.BytesIO()
|
601 |
+
pdf = generate_pdf_analysis(document_text, summary, detected_clauses, hidden_obligations, detected_risks, risk_assessment_matrix, risk_level_distribution, risks_by_type, stacked_bar_chart, risk_heatmap)
|
602 |
+
pdf.output(pdf_buffer, 'F')
|
603 |
+
pdf_buffer.seek(0)
|
604 |
+
|
605 |
+
# Add download button for PDF
|
606 |
+
st.download_button(
|
607 |
+
label="Download PDF Analysis",
|
608 |
+
data=pdf_buffer,
|
609 |
+
file_name="legal_document_analysis.pdf",
|
610 |
+
mime="application/pdf"
|
611 |
+
)
|
612 |
+
|
613 |
+
# Input for recipient email
|
614 |
+
recipient_email = st.text_input("Enter your email address to receive the PDF:")
|
615 |
+
|
616 |
+
# Button to send PDF via email
|
617 |
+
if st.button("Send PDF Analysis"):
|
618 |
+
if recipient_email:
|
619 |
+
if send_pdf_via_email(pdf_buffer, recipient_email):
|
620 |
+
st.success("PDF has been sent successfully!")
|
621 |
+
else:
|
622 |
+
st.error("Failed to send PDF. Please try again.")
|
623 |
+
else:
|
624 |
+
st.warning("Please enter a valid email address.")
|
625 |
+
|
626 |
+
# Feedback Form Section
|
627 |
+
display_feedback_form()
|
628 |
+
|
629 |
+
with tabs[6]: # Update Tracker Tab
|
630 |
+
st.subheader("Document Updates")
|
631 |
+
updates = track_updates(document_text)
|
632 |
+
if st.button("Show Updates"):
|
633 |
+
if updates:
|
634 |
+
for update in updates:
|
635 |
+
with st.expander(update['update'], expanded=False):
|
636 |
+
suggestion = get_update_suggestion(update['update'])
|
637 |
+
st.write(f"*Suggestion:* {suggestion}")
|
638 |
+
else:
|
639 |
+
st.write("No updates detected.")
|
640 |
+
|
641 |
+
# Run the application
|
642 |
+
if __name__ == "__main__":
|
643 |
display_legal_analysis_page()
|