Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import spacy
|
| 3 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 4 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 5 |
+
|
| 6 |
+
# Load legal data - Cases
|
| 7 |
+
cases_directory = '/kaggle/input/legalai/Object_casedocs/'
|
| 8 |
+
cases_texts = []
|
| 9 |
+
|
| 10 |
+
for file_name in os.listdir(cases_directory):
|
| 11 |
+
file_path = os.path.join(cases_directory, file_name)
|
| 12 |
+
with open(file_path, 'r') as file:
|
| 13 |
+
content = file.read()
|
| 14 |
+
cases_texts.append(content)
|
| 15 |
+
|
| 16 |
+
# Load legal data - Statutes
|
| 17 |
+
statutes_directory = '/kaggle/input/legalai/Object_statutes/'
|
| 18 |
+
statutes_texts = {}
|
| 19 |
+
for file_name in os.listdir(statutes_directory):
|
| 20 |
+
file_path = os.path.join(statutes_directory, file_name)
|
| 21 |
+
with open(file_path, 'r') as file:
|
| 22 |
+
statute_content = file.read()
|
| 23 |
+
# Use the file name (statute name) as the key and content as the value in the dictionary
|
| 24 |
+
statutes_texts[file_name] = statute_content
|
| 25 |
+
|
| 26 |
+
# Preprocess and vectorize text for cases
|
| 27 |
+
nlp = spacy.load("en_core_web_sm")
|
| 28 |
+
processed_cases_texts = [" ".join([token.lemma_ for token in nlp(text) if not token.is_stop]) for text in cases_texts]
|
| 29 |
+
vectorizer_cases = TfidfVectorizer()
|
| 30 |
+
tfidf_matrix_cases = vectorizer_cases.fit_transform(processed_cases_texts)
|
| 31 |
+
|
| 32 |
+
# Preprocess and vectorize text for statutes
|
| 33 |
+
processed_statutes_texts = [" ".join([token.lemma_ for token in nlp(text) if not token.is_stop]) for text in statutes_texts.values()]
|
| 34 |
+
vectorizer_statutes = TfidfVectorizer()
|
| 35 |
+
tfidf_matrix_statutes = vectorizer_statutes.fit_transform(processed_statutes_texts)
|
| 36 |
+
# User interaction loop
|
| 37 |
+
while True:
|
| 38 |
+
user_query = input("Ask a legal-related question (type 'exit' to quit): ")
|
| 39 |
+
|
| 40 |
+
if user_query.lower() == 'exit':
|
| 41 |
+
print("Exiting the program. Goodbye!")
|
| 42 |
+
break
|
| 43 |
+
|
| 44 |
+
# Vectorize user query
|
| 45 |
+
query_vector_cases = vectorizer_cases.transform([user_query])
|
| 46 |
+
query_vector_statutes = vectorizer_statutes.transform([user_query])
|
| 47 |
+
|
| 48 |
+
# Compute cosine similarity between the query and each case
|
| 49 |
+
query_similarities_cases = cosine_similarity(query_vector_cases, tfidf_matrix_cases).flatten()
|
| 50 |
+
|
| 51 |
+
# Compute cosine similarity between the query and each statute
|
| 52 |
+
query_similarities_statutes = cosine_similarity(query_vector_statutes, tfidf_matrix_statutes).flatten()
|
| 53 |
+
|
| 54 |
+
# Retrieve the most relevant case and statute
|
| 55 |
+
top_case_idx = query_similarities_cases.argmax()
|
| 56 |
+
top_statute_idx = query_similarities_statutes.argmax()
|
| 57 |
+
|
| 58 |
+
relevant_case = cases_texts[top_case_idx]
|
| 59 |
+
relevant_statute_name = list(statutes_texts.keys())[top_statute_idx]
|
| 60 |
+
relevant_statute_content = statutes_texts[relevant_statute_name]
|
| 61 |
+
# Extract statutes
|
| 62 |
+
doc = nlp(relevant_case)
|
| 63 |
+
statutes = [ent.text for ent in doc.ents if ent.label_ == "LAW"]
|
| 64 |
+
|
| 65 |
+
# Summarize the relevant case
|
| 66 |
+
doc_sentences = list(doc.sents)
|
| 67 |
+
case_summary = "\n".join([sent.text for sent in doc_sentences]) # Include the entire case content as a summary
|
| 68 |
+
|
| 69 |
+
# Generate Statute Explanation
|
| 70 |
+
statute_explanation = f"Statute: {relevant_statute_name}\n{relevant_statute_content}"
|
| 71 |
+
|
| 72 |
+
# Generate Legal Document
|
| 73 |
+
document = f"Legal Document - User Query: {user_query}\n\n"
|
| 74 |
+
document += f"Case Summary:\n{case_summary}\n\n"
|
| 75 |
+
document += "Statute Explanation:\n"
|
| 76 |
+
document += f"{statute_explanation}\n"
|
| 77 |
+
document += "\nGuidance for the User:\n"
|
| 78 |
+
document += "To defend your friend in court, focus on presenting evidence that supports their actions were in self-defense.\n"
|
| 79 |
+
document += "Emphasize any mitigating circumstances and demonstrate their lack of intent to harm.\n"
|
| 80 |
+
document += "Consult with a qualified legal professional to build a strong defense strategy."
|
| 81 |
+
|
| 82 |
+
# Save the document to a file or display it to the user
|
| 83 |
+
with open("legal_document.txt", "w") as output_file:
|
| 84 |
+
output_file.write(document)
|
| 85 |
+
|
| 86 |
+
print("\nLegal document created. You can find the document in 'legal_document.txt'.")
|