MansoorSarookh commited on
Commit
b1ed526
·
verified ·
1 Parent(s): c1262d4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Import Libraries
2
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
3
+ from sentence_transformers import SentenceTransformer
4
+ from datasets import load_dataset
5
+ import faiss
6
+ import numpy as np
7
+ import streamlit as st
8
+
9
+ # Load the Legal Guidance Dataset
10
+ dataset = load_dataset("lex_glue", "ecthr_a")
11
+ texts = dataset['train']['text'][:100] # Limiting to 100 samples for faster processing
12
+
13
+ # Initialize Models for Retrieval and Summarization
14
+ sbert_model = SentenceTransformer("all-mpnet-base-v2") # Model for encoding legal guidance texts
15
+ t5_tokenizer = AutoTokenizer.from_pretrained("t5-small") # Tokenizer for summarization
16
+ t5_model = AutoModelForSeq2SeqLM.from_pretrained("t5-small") # Summarization model
17
+
18
+ # Encode Documents with SBERT and Build FAISS Index
19
+ case_embeddings = sbert_model.encode(texts, convert_to_tensor=True, show_progress_bar=True)
20
+ index = faiss.IndexFlatL2(case_embeddings.shape[1])
21
+ index.add(np.array(case_embeddings.cpu()))
22
+
23
+ # Function to Retrieve Similar Cases
24
+ def retrieve_cases(query, top_k=3):
25
+ query_embedding = sbert_model.encode(query, convert_to_tensor=True)
26
+ _, indices = index.search(np.array([query_embedding.cpu()]), top_k)
27
+ return [(texts[i], i) for i in indices[0]]
28
+
29
+ # Function to Summarize the Retrieved Text
30
+ def summarize_text(text):
31
+ inputs = t5_tokenizer("summarize: " + text, return_tensors="pt", max_length=512, truncation=True)
32
+ outputs = t5_model.generate(inputs["input_ids"], max_length=150, min_length=40, length_penalty=2.0, num_beams=4, early_stopping=True)
33
+ return t5_tokenizer.decode(outputs[0], skip_special_tokens=True)
34
+
35
+ # Streamlit UI for LawyerGuide App
36
+ def main():
37
+ st.title("LawyerGuide App: Legal Guidance for False Accusations")
38
+ query = st.text_input("Describe your situation or legal concern:")
39
+ top_k = st.slider("Number of similar cases to retrieve:", 1, 5, 3)
40
+
41
+ if st.button("Get Guidance"):
42
+ results = retrieve_cases(query, top_k=top_k)
43
+ for i, (case_text, index) in enumerate(results):
44
+ st.subheader(f"Guidance {i+1}")
45
+ st.write("Relevant Text:", case_text)
46
+ summary = summarize_text(case_text)
47
+ st.write("Summary of Legal Guidance:", summary)
48
+
49
+ if __name__ == "__main__":
50
+ main()