menikev commited on
Commit
60fc375
·
verified ·
1 Parent(s): 0c953fd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -35
app.py CHANGED
@@ -1,24 +1,20 @@
1
- #!/usr/bin/env python3
2
  import os
3
  from pathlib import Path
4
  import gradio as gr
5
 
6
  from retriever import get_retriever
7
-
8
  from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
9
  from langchain_community.llms import HuggingFacePipeline
10
  from langchain.prompts import PromptTemplate
11
- from langchain_huggingface import HuggingFaceEmbeddings
12
 
13
- PERSIST_DIR = Path(os.getenv("VECTOR_DB_DIR", "vector_db"))
 
14
  if not PERSIST_DIR.exists() or not any(PERSIST_DIR.iterdir()):
15
- print("⚠️ Vector DB not found. Running ingestion...")
16
- os.system("python src/ingest_documents.py")
17
-
18
 
19
  retriever = get_retriever()
20
 
21
-
22
  MODEL_ID = os.getenv("LLM_ID", "TinyLlama/TinyLlama-1.1B-Chat-v1.0")
23
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
24
  model = AutoModelForCausalLM.from_pretrained(MODEL_ID)
@@ -28,8 +24,8 @@ gen_pipe = pipeline(
28
  model=model,
29
  tokenizer=tokenizer,
30
  device_map="auto" if os.getenv("SPACE_ACCELERATOR") else None,
31
- max_new_tokens=120,
32
- temperature=0.2,
33
  top_p=0.9,
34
  do_sample=True,
35
  repetition_penalty=1.1,
@@ -38,16 +34,20 @@ gen_pipe = pipeline(
38
 
39
  llm = HuggingFacePipeline(pipeline=gen_pipe)
40
 
 
41
  RAG_PROMPT = PromptTemplate.from_template(
42
  "You are a helpful Nigerian Legal Assistant.\n"
43
- "Answer briefly from ONLY the provided context. If the answer is not in the context, say you don't know.\n\n"
 
 
 
44
  "Question: {question}\n\n"
45
- "Context:\n{context}\n\n"
46
  "Answer:"
47
  )
48
 
49
 
50
- def _format_history(turns, max_turns=2):
51
  if not turns:
52
  return ""
53
  turns = turns[-max_turns:]
@@ -62,11 +62,9 @@ def _retrieve(question, k=3):
62
 
63
 
64
  def _generate(question, history):
65
- hist = _format_history(history, max_turns=2)
66
- if hist:
67
- question = f"{hist}\n\nCurrent question: {question}"
68
  context, docs = _retrieve(question, k=3)
69
- prompt = RAG_PROMPT.format(question=question, context=context)
70
  out = llm(prompt)
71
  if isinstance(out, list):
72
  text = out[0].get("generated_text", "") or out[0].get("text", "")
@@ -81,6 +79,7 @@ def answer_question(user_input, lang_choice, history=[]):
81
  if not q:
82
  return history, history
83
 
 
84
  if q.lower() in ["hi", "hello", "hey"]:
85
  if lang_choice == "pidgin":
86
  ans = "Hello! I be your Nigerian Legal AI Assistant. How I fit help you? No be legal advice o."
@@ -89,24 +88,29 @@ def answer_question(user_input, lang_choice, history=[]):
89
  history.append((user_input, ans))
90
  return history, history
91
 
 
92
  if len(q) > 300:
93
  q = q[:300] + "..."
94
 
 
95
  answer, docs = _generate(q, history)
96
 
97
  if not answer or len(answer) < 5:
98
- answer = "I don't know from the available context. Please try rephrasing your question."
99
  if lang_choice == "pidgin":
100
  answer = "I no sure from the context wey I get. Abeg rephrase your question."
 
 
101
 
 
102
  if lang_choice == "pidgin":
103
  answer += "\n\n⚠️ No be legal advice o, abeg meet lawyer."
104
  else:
105
  answer += "\n\n⚠️ This is not legal advice. Please consult a qualified lawyer."
106
 
107
- sources = [d.metadata.get("source", "Unknown") for d in docs[:2]]
 
108
  if sources:
109
- answer += f"\n\nSources: {', '.join(sources)}"
110
 
111
  history.append((user_input, answer))
112
  history = history[-8:]
@@ -123,23 +127,33 @@ def _reset():
123
  return [], []
124
 
125
 
126
- with gr.Blocks() as demo:
127
- gr.Markdown("## 📜 KnowYourRight Bot — Nigerian Legal Assistant")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
 
129
- chatbot = gr.Chatbot(label="Chat with Legal AI", height=400)
130
- msg = gr.Textbox(label="Ask your question here...")
131
- lang_choice = gr.Radio(["english", "pidgin"], value="english", label="Language")
 
 
132
 
133
- with gr.Row():
134
- submit = gr.Button("Send")
135
- clear = gr.Button("Clear Chat")
136
 
137
- state = gr.State([])
138
 
139
- submit.click(answer_question, [msg, lang_choice, state], [chatbot, state])
140
- submit.click(lambda: "", None, msg)
141
- msg.submit(answer_question, [msg, lang_choice, state], [chatbot, state])
142
- msg.submit(lambda: "", None, msg)
143
- clear.click(_reset, None, [chatbot, state])
144
 
145
- demo.launch()
 
 
1
  import os
2
  from pathlib import Path
3
  import gradio as gr
4
 
5
  from retriever import get_retriever
 
6
  from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
7
  from langchain_community.llms import HuggingFacePipeline
8
  from langchain.prompts import PromptTemplate
 
9
 
10
+ # Ensure vector DB exists (from complete_ingestion.py output)
11
+ PERSIST_DIR = Path("data/processed/vector_db")
12
  if not PERSIST_DIR.exists() or not any(PERSIST_DIR.iterdir()):
13
+ raise RuntimeError("⚠️ Vector DB not found. Please run complete_ingestion.py first.")
 
 
14
 
15
  retriever = get_retriever()
16
 
17
+ # Load lightweight conversational model
18
  MODEL_ID = os.getenv("LLM_ID", "TinyLlama/TinyLlama-1.1B-Chat-v1.0")
19
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
20
  model = AutoModelForCausalLM.from_pretrained(MODEL_ID)
 
24
  model=model,
25
  tokenizer=tokenizer,
26
  device_map="auto" if os.getenv("SPACE_ACCELERATOR") else None,
27
+ max_new_tokens=180,
28
+ temperature=0.3,
29
  top_p=0.9,
30
  do_sample=True,
31
  repetition_penalty=1.1,
 
34
 
35
  llm = HuggingFacePipeline(pipeline=gen_pipe)
36
 
37
+ # Conversational + contextual legal prompt
38
  RAG_PROMPT = PromptTemplate.from_template(
39
  "You are a helpful Nigerian Legal Assistant.\n"
40
+ "Respond conversationally, summarize clearly, and explain in plain English (or Pidgin if chosen).\n"
41
+ "Always include the referenced section(s) at the end.\n"
42
+ "If the answer is not in the context, say you don't know.\n\n"
43
+ "Conversation history (for context):\n{history}\n\n"
44
  "Question: {question}\n\n"
45
+ "Context from legal documents:\n{context}\n\n"
46
  "Answer:"
47
  )
48
 
49
 
50
+ def _format_history(turns, max_turns=4):
51
  if not turns:
52
  return ""
53
  turns = turns[-max_turns:]
 
62
 
63
 
64
  def _generate(question, history):
65
+ hist = _format_history(history, max_turns=4)
 
 
66
  context, docs = _retrieve(question, k=3)
67
+ prompt = RAG_PROMPT.format(question=question, context=context, history=hist)
68
  out = llm(prompt)
69
  if isinstance(out, list):
70
  text = out[0].get("generated_text", "") or out[0].get("text", "")
 
79
  if not q:
80
  return history, history
81
 
82
+ # Greeting handling
83
  if q.lower() in ["hi", "hello", "hey"]:
84
  if lang_choice == "pidgin":
85
  ans = "Hello! I be your Nigerian Legal AI Assistant. How I fit help you? No be legal advice o."
 
88
  history.append((user_input, ans))
89
  return history, history
90
 
91
+ # Trim long queries
92
  if len(q) > 300:
93
  q = q[:300] + "..."
94
 
95
+ # Generate answer
96
  answer, docs = _generate(q, history)
97
 
98
  if not answer or len(answer) < 5:
 
99
  if lang_choice == "pidgin":
100
  answer = "I no sure from the context wey I get. Abeg rephrase your question."
101
+ else:
102
+ answer = "I don't know from the available context. Please try rephrasing your question."
103
 
104
+ # Add disclaimer
105
  if lang_choice == "pidgin":
106
  answer += "\n\n⚠️ No be legal advice o, abeg meet lawyer."
107
  else:
108
  answer += "\n\n⚠️ This is not legal advice. Please consult a qualified lawyer."
109
 
110
+ # Add sources/sections
111
+ sources = [d.metadata.get("section", d.metadata.get("source", "Unknown")) for d in docs[:2]]
112
  if sources:
113
+ answer += f"\n\nReferenced: {', '.join(sources)}"
114
 
115
  history.append((user_input, answer))
116
  history = history[-8:]
 
127
  return [], []
128
 
129
 
130
+ # Minimal full-screen Gradio UI
131
+ def build_ui():
132
+ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue"), css=".gradio-container {max-width: 100% !important}") as demo:
133
+ gr.Markdown("""
134
+ # 📜 KnowYourRight Bot — Nigerian Legal Assistant
135
+ Conversational, contextual answers from Nigerian legal documents.
136
+ """)
137
+
138
+ chatbot = gr.Chatbot(label="Chat with Legal AI", height=600, bubble_full_width=False)
139
+ msg = gr.Textbox(label="Ask your question...", placeholder="Type your legal question here", lines=2)
140
+ lang_choice = gr.Radio(["english", "pidgin"], value="english", label="Language")
141
+
142
+ with gr.Row():
143
+ submit = gr.Button("Send", variant="primary")
144
+ clear = gr.Button("Clear Chat")
145
+
146
+ state = gr.State([])
147
 
148
+ submit.click(answer_question, [msg, lang_choice, state], [chatbot, state])
149
+ submit.click(lambda: "", None, msg)
150
+ msg.submit(answer_question, [msg, lang_choice, state], [chatbot, state])
151
+ msg.submit(lambda: "", None, msg)
152
+ clear.click(_reset, None, [chatbot, state])
153
 
154
+ return demo
 
 
155
 
 
156
 
157
+ demo = build_ui()
158
+ demo.launch()
 
 
 
159