menikev commited on
Commit
24e2dc6
·
verified ·
1 Parent(s): f567699

Update src/app.py

Browse files
Files changed (1) hide show
  1. src/app.py +14 -6
src/app.py CHANGED
@@ -7,7 +7,7 @@ from retriever import get_retriever
7
  from transformers import pipeline
8
  import os
9
 
10
- # Load local HuggingFace pipeline (avoids .post() bug)
11
  pipe = pipeline(
12
  "text-generation",
13
  model="tiiuae/falcon-7b-instruct",
@@ -20,10 +20,9 @@ llm = HuggingFacePipeline(pipeline=pipe)
20
 
21
  retriever = get_retriever()
22
 
23
- # Prompt template for consistent answers & citations
24
  template = """
25
- You are a legal assistant. Answer the question using the provided context.
26
- Always include the source reference(s) from the context in your answer.
27
  If language mode is Nigerian Pidgin, respond in Nigerian Pidgin.
28
 
29
  Question: {question}
@@ -43,13 +42,22 @@ qa_chain = RetrievalQA.from_chain_type(
43
  llm=llm,
44
  retriever=retriever,
45
  chain_type="stuff",
 
46
  chain_type_kwargs={"prompt": prompt}
47
  )
48
 
49
  def answer_question(user_input, lang_choice):
50
  if lang_choice == "pidgin":
51
  user_input = f"Respond in Nigerian Pidgin: {user_input}"
52
- return qa_chain.run(user_input)
 
 
 
 
 
 
 
 
53
 
54
  def launch_interface():
55
  iface = gr.Interface(
@@ -60,7 +68,7 @@ def launch_interface():
60
  ],
61
  outputs=gr.Textbox(label="Answer"),
62
  title="KnowYourRight Bot",
63
- description="Ask legal rights questions in English or Nigerian Pidgin with sources"
64
  )
65
  iface.launch()
66
 
 
7
  from transformers import pipeline
8
  import os
9
 
10
+ # Load local HuggingFace pipeline
11
  pipe = pipeline(
12
  "text-generation",
13
  model="tiiuae/falcon-7b-instruct",
 
20
 
21
  retriever = get_retriever()
22
 
23
+ # Prompt template
24
  template = """
25
+ You are a legal assistant. Use the provided context to answer the question.
 
26
  If language mode is Nigerian Pidgin, respond in Nigerian Pidgin.
27
 
28
  Question: {question}
 
42
  llm=llm,
43
  retriever=retriever,
44
  chain_type="stuff",
45
+ return_source_documents=True, # Needed to list references
46
  chain_type_kwargs={"prompt": prompt}
47
  )
48
 
49
  def answer_question(user_input, lang_choice):
50
  if lang_choice == "pidgin":
51
  user_input = f"Respond in Nigerian Pidgin: {user_input}"
52
+
53
+ result = qa_chain(user_input)
54
+ answer_text = result["result"]
55
+
56
+ # Collect unique source file names
57
+ sources = list({doc.metadata.get("source", "Unknown") for doc in result["source_documents"]})
58
+ sources_list = "\n".join(f"- {src}" for src in sources)
59
+
60
+ return f"{answer_text}\n\nReferences:\n{sources_list}"
61
 
62
  def launch_interface():
63
  iface = gr.Interface(
 
68
  ],
69
  outputs=gr.Textbox(label="Answer"),
70
  title="KnowYourRight Bot",
71
+ description="Ask legal rights questions in English or Nigerian Pidgin with references"
72
  )
73
  iface.launch()
74