Aranwer commited on
Commit
0e1688f
·
verified ·
1 Parent(s): cf46505

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -30
app.py CHANGED
@@ -5,9 +5,15 @@ import numpy as np
5
  import ast
6
  import gradio as gr
7
  import faiss
 
8
  from sentence_transformers import SentenceTransformer
9
  from transformers import pipeline
10
 
 
 
 
 
 
11
  zip_path = "lexglue-legal-nlp-benchmark-dataset.zip"
12
  extract_dir = "lexglue_data"
13
 
@@ -36,52 +42,39 @@ generator = pipeline("text-generation", model="gpt2")
36
 
37
  history = []
38
 
39
- def simplify_legal_text(text):
40
- prompt = f"Simplify the following legal text into plain English:\n\n{text}"
41
- simplified_text = generator(prompt, max_new_tokens=100, do_sample=False)[0]['generated_text']
42
- return simplified_text.strip()
43
-
44
- sample_questions = [
45
- "Can you explain the constitutional rights of a citizen in simple terms?",
46
- "What does a breach of contract mean?",
47
- "How do courts determine if someone is guilty of a crime?",
48
- "What is the difference between civil and criminal law?",
49
- "Can you explain what 'reasonable doubt' is in a criminal trial?"
50
- ]
51
-
52
- def legal_assistant_query(query, _):
53
  query_embedding = embedder.encode([query])
54
  D, I = index.search(np.array(query_embedding), k=5)
 
55
  retrieved_docs = [corpus[i] for i in I[0]]
56
  context_combined = "\n\n".join(retrieved_docs[:3])
57
- max_length = 1024
58
- context_combined = context_combined[:max_length]
59
 
60
  prompt = f"Given the following legal references, answer the question:\n\n{context_combined}\n\nQuestion: {query}\nAnswer:"
61
  result = generator(prompt, max_new_tokens=200, do_sample=True)[0]['generated_text']
62
  answer = result.split("Answer:")[-1].strip()
63
- simplified_answer = simplify_legal_text(answer)
64
 
65
- history.append({"question": query, "answer": simplified_answer})
66
  if len(history) > 5:
67
  history.pop(0)
68
 
69
- history_text = "\n\n".join([f"Q: {entry['question']}\nA: {entry['answer']}" for entry in history])
70
- return simplified_answer, history_text, "\n".join(sample_questions)
 
 
 
 
 
 
 
 
71
 
72
  iface = gr.Interface(
73
  fn=legal_assistant_query,
74
- inputs=[
75
- gr.Textbox(lines=2, placeholder="Ask a legal question..."),
76
- gr.Button("Submit")
77
- ],
78
- outputs=[
79
- gr.Textbox(label="Legal Response"),
80
- gr.Textbox(label="Session History", lines=10),
81
- gr.Textbox(label="Sample Questions", lines=6)
82
- ],
83
  title="🧑‍⚖️ Legal Assistant Chatbot",
84
- description="Ask any legal question and get context-based case references using the LexGLUE dataset. The assistant will simplify legal responses into plain English and show your last 5 questions."
85
  )
86
 
87
  iface.launch()
 
5
  import ast
6
  import gradio as gr
7
  import faiss
8
+
9
  from sentence_transformers import SentenceTransformer
10
  from transformers import pipeline
11
 
12
+ """
13
+ Legal Assistant Chatbot using LexGLUE dataset and GPT-2
14
+ Includes session memory for last 5 Q&A and sample questions for user guidance.
15
+ """
16
+
17
  zip_path = "lexglue-legal-nlp-benchmark-dataset.zip"
18
  extract_dir = "lexglue_data"
19
 
 
42
 
43
  history = []
44
 
45
+ def legal_assistant_query(query):
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  query_embedding = embedder.encode([query])
47
  D, I = index.search(np.array(query_embedding), k=5)
48
+
49
  retrieved_docs = [corpus[i] for i in I[0]]
50
  context_combined = "\n\n".join(retrieved_docs[:3])
51
+ context_combined = context_combined[:1024]
 
52
 
53
  prompt = f"Given the following legal references, answer the question:\n\n{context_combined}\n\nQuestion: {query}\nAnswer:"
54
  result = generator(prompt, max_new_tokens=200, do_sample=True)[0]['generated_text']
55
  answer = result.split("Answer:")[-1].strip()
 
56
 
57
+ history.append((query, answer))
58
  if len(history) > 5:
59
  history.pop(0)
60
 
61
+ formatted_history = "\n\n".join([f"Q: {q}\nA: {a}" for q, a in history])
62
+ return f"{answer}\n\n---\nRecent Q&A:\n{formatted_history}"
63
+
64
+ sample_questions = [
65
+ "What rights does a person have under the Fourth Amendment?",
66
+ "Explain due process in simple terms.",
67
+ "What is double jeopardy?",
68
+ "Can the police search your car without a warrant?",
69
+ "What is considered a fair trial?"
70
+ ]
71
 
72
  iface = gr.Interface(
73
  fn=legal_assistant_query,
74
+ inputs=gr.Textbox(lines=2, placeholder="Ask a legal question...", label="Your Question"),
75
+ outputs=gr.Textbox(label="Legal Response with History"),
 
 
 
 
 
 
 
76
  title="🧑‍⚖️ Legal Assistant Chatbot",
77
+ description="Ask any legal question and get context-based case.\n\n💡 Sample Questions:\n- " + "\n- ".join(sample_questions)
78
  )
79
 
80
  iface.launch()