Update legal_document_analysis.py
Browse files- legal_document_analysis.py +8 -83
legal_document_analysis.py
CHANGED
@@ -408,13 +408,14 @@ def generate_pdf_analysis(document_text, summary, detected_clauses, hidden_oblig
|
|
408 |
return pdf_buffer
|
409 |
|
410 |
# Function to handle chatbot interaction
|
411 |
-
def chatbot_query(user_input):
|
|
|
412 |
try:
|
413 |
-
response = model(
|
414 |
-
if
|
415 |
-
return response
|
416 |
else:
|
417 |
-
return
|
418 |
except Exception as e:
|
419 |
return f"Error: {str(e)}"
|
420 |
|
@@ -530,7 +531,7 @@ def display_legal_analysis_page():
|
|
530 |
st.error("Unsupported file type!")
|
531 |
return
|
532 |
|
533 |
-
tabs = st.tabs(["π Document Text", "π Summary", "π Key Clauses", "π Hidden Obligations", "β Risk Analysis", "π‘ Suggestions & Chatbot", "π Update Tracker"
|
534 |
|
535 |
with tabs[0]:
|
536 |
st.subheader("Document Text")
|
@@ -602,41 +603,12 @@ def display_legal_analysis_page():
|
|
602 |
user_input = st.text_input("Ask the chatbot about your document:")
|
603 |
if st.button("Send"):
|
604 |
if user_input:
|
605 |
-
chatbot_response = chatbot_query(user_input)
|
606 |
st.write("*Chatbot Response:*")
|
607 |
st.write(chatbot_response)
|
608 |
else:
|
609 |
st.warning("Please enter a question.")
|
610 |
|
611 |
-
# Download PDF Analysis Button
|
612 |
-
st.subheader("Download Analysis as PDF")
|
613 |
-
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)
|
614 |
-
pdf_buffer.seek(0)
|
615 |
-
|
616 |
-
# Add download button for PDF
|
617 |
-
st.download_button(
|
618 |
-
label="Download PDF Analysis",
|
619 |
-
data=pdf_buffer,
|
620 |
-
file_name="legal_document_analysis.pdf",
|
621 |
-
mime="application/pdf"
|
622 |
-
)
|
623 |
-
|
624 |
-
# Input for recipient email
|
625 |
-
recipient_email = st.text_input("Enter your email address to receive the PDF:")
|
626 |
-
|
627 |
-
# Button to send PDF via email
|
628 |
-
if st.button("Send PDF Analysis"):
|
629 |
-
if recipient_email:
|
630 |
-
if send_pdf_via_email(pdf_buffer, recipient_email):
|
631 |
-
st.success("PDF has been sent successfully!")
|
632 |
-
else:
|
633 |
-
st.error("Failed to send PDF. Please try again.")
|
634 |
-
else:
|
635 |
-
st.warning("Please enter a valid email address.")
|
636 |
-
|
637 |
-
# Feedback Form Section
|
638 |
-
display_feedback_form()
|
639 |
-
|
640 |
with tabs[6]: # Update Tracker Tab
|
641 |
st.subheader("Document Updates")
|
642 |
updates = track_updates(document_text)
|
@@ -652,53 +624,6 @@ def display_legal_analysis_page():
|
|
652 |
else:
|
653 |
st.write("No updates detected.")
|
654 |
|
655 |
-
with tabs[7]: # GDPR Updates Tab
|
656 |
-
st.subheader("GDPR Website Updates")
|
657 |
-
if st.button("Fetch Live Recitals"):
|
658 |
-
with st.spinner("Fetching updates..."):
|
659 |
-
recitals = fetch_gdpr_recitals()
|
660 |
-
if recitals:
|
661 |
-
for number, details in recitals.items():
|
662 |
-
st.markdown(f"*Recital {number}: {details['title']}*")
|
663 |
-
st.write(details['content'])
|
664 |
-
else:
|
665 |
-
st.write("No recitals found.")
|
666 |
-
|
667 |
-
# Function to fetch live recitals from the GDPR website
|
668 |
-
def fetch_gdpr_recitals():
|
669 |
-
url = "https://gdpr-info.eu/recitals/"
|
670 |
-
response = requests.get(url)
|
671 |
-
|
672 |
-
# Check if the request was successful
|
673 |
-
if response.status_code != 200:
|
674 |
-
st.error("Failed to fetch data from the GDPR website.")
|
675 |
-
return {}
|
676 |
-
|
677 |
-
soup = BeautifulSoup(response.content, 'html.parser')
|
678 |
-
|
679 |
-
recitals = {}
|
680 |
-
# Locate all recital links
|
681 |
-
articles = soup.find_all('div', class_='artikel')
|
682 |
-
|
683 |
-
# Extract each recital's link and title
|
684 |
-
for i, article in enumerate(articles):
|
685 |
-
if i >= 3: # Limit to the first 3 recitals
|
686 |
-
break
|
687 |
-
link = article.find('a')['href']
|
688 |
-
number = article.find('span', class_='nummer').text.strip('()')
|
689 |
-
title = article.find('span', class_='titel').text.strip()
|
690 |
-
|
691 |
-
# Fetch the content of each recital
|
692 |
-
rec_response = requests.get(link)
|
693 |
-
if rec_response.status_code == 200:
|
694 |
-
rec_soup = BeautifulSoup(rec_response.content, 'html.parser')
|
695 |
-
content = rec_soup.find('div', class_='entry-content').get_text(strip=True)
|
696 |
-
recitals[number] = {'title': title, 'content': content}
|
697 |
-
else:
|
698 |
-
print(f"Failed to fetch recital {number} from {link}")
|
699 |
-
|
700 |
-
return recitals
|
701 |
-
|
702 |
# Run the application
|
703 |
if __name__ == "__main__":
|
704 |
display_legal_analysis_page()
|
|
|
408 |
return pdf_buffer
|
409 |
|
410 |
# Function to handle chatbot interaction
|
411 |
+
def chatbot_query(user_input, document_text):
|
412 |
+
prompt = f"You are a legal expert. Answer the question based on the following document text:\n\n{document_text}\n\nQuestion: {user_input}"
|
413 |
try:
|
414 |
+
response = model.invoke(prompt)
|
415 |
+
if hasattr(response, 'content'):
|
416 |
+
return response.content.strip()
|
417 |
else:
|
418 |
+
return str(response).strip()
|
419 |
except Exception as e:
|
420 |
return f"Error: {str(e)}"
|
421 |
|
|
|
531 |
st.error("Unsupported file type!")
|
532 |
return
|
533 |
|
534 |
+
tabs = st.tabs(["π Document Text", "π Summary", "π Key Clauses", "π Hidden Obligations", "β Risk Analysis", "π‘ Suggestions & Chatbot", "π Update Tracker"])
|
535 |
|
536 |
with tabs[0]:
|
537 |
st.subheader("Document Text")
|
|
|
603 |
user_input = st.text_input("Ask the chatbot about your document:")
|
604 |
if st.button("Send"):
|
605 |
if user_input:
|
606 |
+
chatbot_response = chatbot_query(user_input, document_text)
|
607 |
st.write("*Chatbot Response:*")
|
608 |
st.write(chatbot_response)
|
609 |
else:
|
610 |
st.warning("Please enter a question.")
|
611 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
612 |
with tabs[6]: # Update Tracker Tab
|
613 |
st.subheader("Document Updates")
|
614 |
updates = track_updates(document_text)
|
|
|
624 |
else:
|
625 |
st.write("No updates detected.")
|
626 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
627 |
# Run the application
|
628 |
if __name__ == "__main__":
|
629 |
display_legal_analysis_page()
|