ZeeAI1 commited on
Commit
8201e57
·
verified ·
1 Parent(s): a5b6894

Create app.py

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