Aranwer commited on
Commit
2a8a0e5
·
verified ·
1 Parent(s): 95bed81

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -16
app.py CHANGED
@@ -5,40 +5,44 @@ import faiss
5
  import numpy as np
6
  from transformers import pipeline
7
 
8
- # Load dataset
9
  dataset = load_dataset("lex_glue", "scotus")
10
- corpus = [doc['text'] for doc in dataset['train'].select(range(200))] # just 200 to keep it light
11
 
12
- # Embedding model
13
  embedder = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
14
  corpus_embeddings = embedder.encode(corpus, convert_to_numpy=True)
15
 
16
- # Build FAISS index
17
  dimension = corpus_embeddings.shape[1]
18
  index = faiss.IndexFlatL2(dimension)
19
  index.add(corpus_embeddings)
20
 
21
- # Text generation model
22
  gen_pipeline = pipeline("text2text-generation", model="facebook/bart-large-cnn")
23
 
24
- # RAG-like query function
25
  def rag_query(user_question):
26
  question_embedding = embedder.encode([user_question])
27
  _, indices = index.search(np.array(question_embedding), k=3)
28
  context = " ".join([corpus[i] for i in indices[0]])
29
-
30
  prompt = f"Question: {user_question}\nContext: {context}\nAnswer:"
31
  result = gen_pipeline(prompt, max_length=250, do_sample=False)[0]['generated_text']
32
  return result
33
 
34
- # Gradio UI
35
- def chatbot_interface(query):
36
- return rag_query(query)
37
-
38
- iface = gr.Interface(fn=chatbot_interface,
39
- inputs="text",
40
- outputs="text",
41
- title="🧑‍⚖️ Legal Assistant Chatbot",
42
- description="Ask legal questions based on case data (LexGLUE - SCOTUS subset)")
 
 
 
 
 
 
 
 
 
 
43
 
44
  iface.launch()
 
5
  import numpy as np
6
  from transformers import pipeline
7
 
 
8
  dataset = load_dataset("lex_glue", "scotus")
9
+ corpus = [doc['text'] for doc in dataset['train'].select(range(200))]
10
 
 
11
  embedder = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
12
  corpus_embeddings = embedder.encode(corpus, convert_to_numpy=True)
13
 
 
14
  dimension = corpus_embeddings.shape[1]
15
  index = faiss.IndexFlatL2(dimension)
16
  index.add(corpus_embeddings)
17
 
 
18
  gen_pipeline = pipeline("text2text-generation", model="facebook/bart-large-cnn")
19
 
 
20
  def rag_query(user_question):
21
  question_embedding = embedder.encode([user_question])
22
  _, indices = index.search(np.array(question_embedding), k=3)
23
  context = " ".join([corpus[i] for i in indices[0]])
 
24
  prompt = f"Question: {user_question}\nContext: {context}\nAnswer:"
25
  result = gen_pipeline(prompt, max_length=250, do_sample=False)[0]['generated_text']
26
  return result
27
 
28
+ def chatbot_interface(query, history):
29
+ response = rag_query(query)
30
+ history.append((query, response))
31
+ chat_history = "\n\n".join([f"👤 You: {q}\n🧑‍⚖️ Bot: {a}" for q, a in history])
32
+ return chat_history, history
33
+
34
+ iface = gr.Interface(
35
+ fn=chatbot_interface,
36
+ inputs=[
37
+ gr.Textbox(lines=2, placeholder="Enter your legal question here...", label="Your Question"),
38
+ gr.State([]) # Keeps history
39
+ ],
40
+ outputs=[
41
+ gr.Textbox(label="Chat History", lines=20, interactive=False),
42
+ gr.State()
43
+ ],
44
+ title="🧑‍⚖️ Legal Assistant Chatbot",
45
+ description="Ask legal questions based on case data (LexGLUE - SCOTUS subset). The bot will retrieve relevant context and answer your question."
46
+ )
47
 
48
  iface.launch()