Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,31 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
def respond(
|
| 6 |
message,
|
|
@@ -11,19 +36,43 @@ def respond(
|
|
| 11 |
top_p,
|
| 12 |
hf_token: gr.OAuthToken,
|
| 13 |
):
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
messages = [{"role": "system", "content":
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
messages.append({"role": "user", "content": message})
|
| 24 |
|
| 25 |
response = ""
|
| 26 |
|
|
|
|
| 27 |
for message in client.chat_completion(
|
| 28 |
messages,
|
| 29 |
max_tokens=max_tokens,
|
|
@@ -31,23 +80,21 @@ def respond(
|
|
| 31 |
temperature=temperature,
|
| 32 |
top_p=top_p,
|
| 33 |
):
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
|
| 39 |
-
|
| 40 |
-
yield response
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
chatbot = gr.ChatInterface(
|
| 47 |
respond,
|
|
|
|
| 48 |
additional_inputs=[
|
| 49 |
-
gr.Textbox(
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
| 51 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
gr.Slider(
|
| 53 |
minimum=0.1,
|
|
@@ -57,13 +104,23 @@ chatbot = gr.ChatInterface(
|
|
| 57 |
label="Top-p (nucleus sampling)",
|
| 58 |
),
|
| 59 |
],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
)
|
| 61 |
|
| 62 |
with gr.Blocks() as demo:
|
| 63 |
with gr.Sidebar():
|
|
|
|
| 64 |
gr.LoginButton()
|
|
|
|
|
|
|
| 65 |
chatbot.render()
|
| 66 |
|
| 67 |
-
|
| 68 |
if __name__ == "__main__":
|
| 69 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
+
import os
|
| 4 |
+
from src.data_processor import LegalDocProcessor
|
| 5 |
+
from src.hybrid_retriever import HybridRetriever
|
| 6 |
|
| 7 |
+
# --- Configuration & Initialization ---
|
| 8 |
+
INDEX_DIR = "index_storage"
|
| 9 |
+
PARENT_DATA = "data/parent_docs.json"
|
| 10 |
+
CHILD_DATA = "data/child_docs.json"
|
| 11 |
+
|
| 12 |
+
# Initialize the retriever (Logic from your provided files)
|
| 13 |
+
def initialize_retriever():
|
| 14 |
+
if os.path.exists(INDEX_DIR):
|
| 15 |
+
print("[*] Loading existing index...")
|
| 16 |
+
return HybridRetriever(index_dir=INDEX_DIR)
|
| 17 |
+
else:
|
| 18 |
+
print("[*] Building new index...")
|
| 19 |
+
processor = LegalDocProcessor(PARENT_DATA, CHILD_DATA)
|
| 20 |
+
docs = processor.load_and_clean()
|
| 21 |
+
if not docs:
|
| 22 |
+
raise ValueError("No documents found to index. Check your data path.")
|
| 23 |
+
ret = HybridRetriever(documents=docs, index_dir=INDEX_DIR)
|
| 24 |
+
ret.save_index()
|
| 25 |
+
return ret
|
| 26 |
+
|
| 27 |
+
# Global retriever instance
|
| 28 |
+
retriever = initialize_retriever()
|
| 29 |
|
| 30 |
def respond(
|
| 31 |
message,
|
|
|
|
| 36 |
top_p,
|
| 37 |
hf_token: gr.OAuthToken,
|
| 38 |
):
|
| 39 |
+
# 1. RETRIEVAL STEP: Use your HybridRetriever to find relevant law snippets
|
| 40 |
+
search_results = retriever.hybrid_search(message, top_k=3)
|
| 41 |
+
|
| 42 |
+
# 2. CONTEXT BUILDING: Format the search results into a string
|
| 43 |
+
context = "\n\nRELEVANT NEPALESE LAW CONTEXT:\n"
|
| 44 |
+
if not search_results:
|
| 45 |
+
context += "No specific legal clauses found for this query."
|
| 46 |
+
for res in search_results:
|
| 47 |
+
context += f"--- Source: {res['legal_document_source']} ---\n"
|
| 48 |
+
context += f"Clause/Section: {res['parent_clause_id']}\n"
|
| 49 |
+
context += f"Text: {res['parent_clause_text']}\n\n"
|
| 50 |
+
|
| 51 |
+
# 3. PROMPT ENGINEERING: Inject context into the system message or user message
|
| 52 |
+
augmented_system_message = (
|
| 53 |
+
f"{system_message}\n\n"
|
| 54 |
+
"You are a legal assistant specializing in Nepalese Law. "
|
| 55 |
+
"Use the following legal context to answer the user's question accurately. "
|
| 56 |
+
"If the context doesn't contain the answer, state that you are answering based on general knowledge but couldn't find the specific clause."
|
| 57 |
+
f"{context}"
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
client = InferenceClient(token=hf_token.token, model="meta-llama/Llama-3.1-70B-Instruct")
|
| 61 |
|
| 62 |
+
messages = [{"role": "system", "content": augmented_system_message}]
|
| 63 |
|
| 64 |
+
# Maintain history
|
| 65 |
+
for val in history:
|
| 66 |
+
if val['role'] == 'user':
|
| 67 |
+
messages.append({"role": "user", "content": val['content']})
|
| 68 |
+
else:
|
| 69 |
+
messages.append({"role": "assistant", "content": val['content']})
|
| 70 |
|
| 71 |
messages.append({"role": "user", "content": message})
|
| 72 |
|
| 73 |
response = ""
|
| 74 |
|
| 75 |
+
# 4. GENERATION STEP: Stream the response from the LLM
|
| 76 |
for message in client.chat_completion(
|
| 77 |
messages,
|
| 78 |
max_tokens=max_tokens,
|
|
|
|
| 80 |
temperature=temperature,
|
| 81 |
top_p=top_p,
|
| 82 |
):
|
| 83 |
+
token = message.choices[0].delta.content
|
| 84 |
+
if token:
|
| 85 |
+
response += token
|
| 86 |
+
yield response
|
| 87 |
|
| 88 |
+
# --- Gradio UI Setup ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
chatbot = gr.ChatInterface(
|
| 90 |
respond,
|
| 91 |
+
type="messages", # Updated for newer Gradio versions
|
| 92 |
additional_inputs=[
|
| 93 |
+
gr.Textbox(
|
| 94 |
+
value="You are a helpful Nepalese Legal Advisor. Always cite the Source and Clause ID provided in the context.",
|
| 95 |
+
label="System message"
|
| 96 |
+
),
|
| 97 |
+
gr.Slider(minimum=1, maximum=2048, value=1024, step=1, label="Max new tokens"),
|
| 98 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 99 |
gr.Slider(
|
| 100 |
minimum=0.1,
|
|
|
|
| 104 |
label="Top-p (nucleus sampling)",
|
| 105 |
),
|
| 106 |
],
|
| 107 |
+
title="Nepal Law Search AI",
|
| 108 |
+
description="Ask questions about Nepalese Acts, Codes, and the Constitution. The AI will search the official legal database before answering.",
|
| 109 |
+
examples=[
|
| 110 |
+
"What are the punishments for cybercrime?",
|
| 111 |
+
"What does the constitution say about the right to equality?",
|
| 112 |
+
"What are the rules for divorce in the Civil Code?",
|
| 113 |
+
"Is witchcraft accusation a crime in Nepal?"
|
| 114 |
+
]
|
| 115 |
)
|
| 116 |
|
| 117 |
with gr.Blocks() as demo:
|
| 118 |
with gr.Sidebar():
|
| 119 |
+
gr.Markdown("### Authentication")
|
| 120 |
gr.LoginButton()
|
| 121 |
+
gr.Markdown("---")
|
| 122 |
+
gr.Markdown("**Note:** This system uses Hybrid Search (BM25 + Vector) to find relevant Nepalese Law.")
|
| 123 |
chatbot.render()
|
| 124 |
|
|
|
|
| 125 |
if __name__ == "__main__":
|
| 126 |
+
demo.launch()
|