Update legal_document_analysis.py
Browse files- legal_document_analysis.py +19 -13
legal_document_analysis.py
CHANGED
|
@@ -15,6 +15,7 @@ from fpdf import FPDF
|
|
| 15 |
import getpass
|
| 16 |
import pandas as pd
|
| 17 |
import seaborn as sns
|
|
|
|
| 18 |
|
| 19 |
# Load environment variables from .env file
|
| 20 |
load_dotenv()
|
|
@@ -333,9 +334,12 @@ def plot_risk_heatmap(detected_risks):
|
|
| 333 |
|
| 334 |
return img_str
|
| 335 |
|
| 336 |
-
# Function to
|
| 337 |
-
def
|
| 338 |
-
|
|
|
|
|
|
|
|
|
|
| 339 |
|
| 340 |
# Function to generate PDF document with improved aesthetics
|
| 341 |
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):
|
|
@@ -371,15 +375,15 @@ def generate_pdf_analysis(document_text, summary, detected_clauses, hidden_oblig
|
|
| 371 |
pdf.ln(10)
|
| 372 |
|
| 373 |
# Add visualizations for risks
|
| 374 |
-
pdf.image(
|
| 375 |
-
pdf.image(
|
| 376 |
pdf.ln(60)
|
| 377 |
|
| 378 |
-
pdf.image(
|
| 379 |
-
pdf.image(
|
| 380 |
pdf.ln(60)
|
| 381 |
|
| 382 |
-
pdf.image(
|
| 383 |
pdf.ln(10)
|
| 384 |
|
| 385 |
# Footer
|
|
@@ -387,7 +391,12 @@ def generate_pdf_analysis(document_text, summary, detected_clauses, hidden_oblig
|
|
| 387 |
pdf.set_font("Arial", 'I', 8)
|
| 388 |
pdf.cell(0, 10, f'Page {pdf.page_no()}', 0, 0, 'C')
|
| 389 |
|
| 390 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 391 |
|
| 392 |
# Function to handle chatbot interaction
|
| 393 |
def chatbot_query(user_input):
|
|
@@ -590,10 +599,7 @@ def display_legal_analysis_page():
|
|
| 590 |
|
| 591 |
# Download PDF Analysis Button
|
| 592 |
st.subheader("Download Analysis as PDF")
|
| 593 |
-
pdf_buffer =
|
| 594 |
-
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)
|
| 595 |
-
pdf.output(pdf_buffer, 'F')
|
| 596 |
-
pdf_buffer.seek(0)
|
| 597 |
|
| 598 |
# Add download button for PDF
|
| 599 |
st.download_button(
|
|
|
|
| 15 |
import getpass
|
| 16 |
import pandas as pd
|
| 17 |
import seaborn as sns
|
| 18 |
+
import tempfile
|
| 19 |
|
| 20 |
# Load environment variables from .env file
|
| 21 |
load_dotenv()
|
|
|
|
| 334 |
|
| 335 |
return img_str
|
| 336 |
|
| 337 |
+
# Function to save base64 images to temporary files
|
| 338 |
+
def save_base64_image(base64_data):
|
| 339 |
+
img_data = base64.b64decode(base64_data)
|
| 340 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_file:
|
| 341 |
+
temp_file.write(img_data)
|
| 342 |
+
return temp_file.name
|
| 343 |
|
| 344 |
# Function to generate PDF document with improved aesthetics
|
| 345 |
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):
|
|
|
|
| 375 |
pdf.ln(10)
|
| 376 |
|
| 377 |
# Add visualizations for risks
|
| 378 |
+
pdf.image(save_base64_image(risk_assessment_matrix), x=10, y=pdf.get_y(), w=90)
|
| 379 |
+
pdf.image(save_base64_image(risk_level_distribution), x=110, y=pdf.get_y()-50, w=90) # Position next to the first image
|
| 380 |
pdf.ln(60)
|
| 381 |
|
| 382 |
+
pdf.image(save_base64_image(risks_by_type), x=10, y=pdf.get_y(), w=90)
|
| 383 |
+
pdf.image(save_base64_image(stacked_bar_chart), x=110, y=pdf.get_y()-50, w=90) # Position next to the previous image
|
| 384 |
pdf.ln(60)
|
| 385 |
|
| 386 |
+
pdf.image(save_base64_image(risk_heatmap), x=10, y=pdf.get_y(), w=190) # Fit image to width
|
| 387 |
pdf.ln(10)
|
| 388 |
|
| 389 |
# Footer
|
|
|
|
| 391 |
pdf.set_font("Arial", 'I', 8)
|
| 392 |
pdf.cell(0, 10, f'Page {pdf.page_no()}', 0, 0, 'C')
|
| 393 |
|
| 394 |
+
# Save PDF to a BytesIO object
|
| 395 |
+
pdf_buffer = io.BytesIO()
|
| 396 |
+
pdf.output(pdf_buffer, 'F') # Save to BytesIO
|
| 397 |
+
pdf_buffer.seek(0)
|
| 398 |
+
|
| 399 |
+
return pdf_buffer
|
| 400 |
|
| 401 |
# Function to handle chatbot interaction
|
| 402 |
def chatbot_query(user_input):
|
|
|
|
| 599 |
|
| 600 |
# Download PDF Analysis Button
|
| 601 |
st.subheader("Download Analysis as PDF")
|
| 602 |
+
pdf_buffer = 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)
|
|
|
|
|
|
|
|
|
|
| 603 |
|
| 604 |
# Add download button for PDF
|
| 605 |
st.download_button(
|