Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,7 +18,8 @@ def initialize_retriever():
|
|
| 18 |
processor = LegalDocProcessor(PARENT_DATA, CHILD_DATA)
|
| 19 |
docs = processor.load_and_clean()
|
| 20 |
if not docs:
|
| 21 |
-
|
|
|
|
| 22 |
ret = HybridRetriever(documents=docs, index_dir=INDEX_DIR)
|
| 23 |
ret.save_index()
|
| 24 |
return ret
|
|
@@ -28,39 +29,40 @@ retriever = initialize_retriever()
|
|
| 28 |
|
| 29 |
def respond(
|
| 30 |
message,
|
| 31 |
-
history,
|
| 32 |
system_message,
|
| 33 |
max_tokens,
|
| 34 |
temperature,
|
| 35 |
top_p,
|
| 36 |
-
hf_token: gr.OAuthToken,
|
| 37 |
):
|
| 38 |
# 1. RETRIEVAL STEP
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
|
| 50 |
-
#
|
| 51 |
augmented_system_message = (
|
| 52 |
f"{system_message}\n\n"
|
| 53 |
"You are a legal assistant specializing in Nepalese Law. "
|
| 54 |
-
"Use the
|
| 55 |
-
"Always cite the 'Source' and 'Clause/Section'
|
| 56 |
f"{context}"
|
| 57 |
)
|
| 58 |
|
| 59 |
-
|
|
|
|
|
|
|
| 60 |
|
| 61 |
messages = [{"role": "system", "content": augmented_system_message}]
|
| 62 |
|
| 63 |
-
# 4. HISTORY CONVERSION (Convert [[u, b]] to [{"role": "user", "content": u}, ...])
|
| 64 |
for user_msg, assistant_msg in history:
|
| 65 |
if user_msg:
|
| 66 |
messages.append({"role": "user", "content": user_msg})
|
|
@@ -71,21 +73,23 @@ def respond(
|
|
| 71 |
|
| 72 |
response = ""
|
| 73 |
|
| 74 |
-
#
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
# --- Gradio UI Setup ---
|
| 88 |
-
# Removed type="messages" to support Gradio 4.x
|
| 89 |
chatbot = gr.ChatInterface(
|
| 90 |
respond,
|
| 91 |
additional_inputs=[
|
|
@@ -95,20 +99,16 @@ chatbot = gr.ChatInterface(
|
|
| 95 |
),
|
| 96 |
gr.Slider(minimum=1, maximum=2048, value=1024, step=1, label="Max new tokens"),
|
| 97 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 98 |
-
gr.Slider(
|
| 99 |
-
minimum=0.1,
|
| 100 |
-
maximum=1.0,
|
| 101 |
-
value=0.95,
|
| 102 |
-
step=0.05,
|
| 103 |
-
label="Top-p (nucleus sampling)",
|
| 104 |
-
),
|
| 105 |
],
|
| 106 |
title="Nepal Law Search AI",
|
| 107 |
-
description="Ask questions about Nepalese Acts, Codes, and the Constitution.
|
|
|
|
| 108 |
examples=[
|
| 109 |
-
"What are the punishments for cybercrime?",
|
| 110 |
-
"What does the constitution say about the right to equality?",
|
| 111 |
-
"Is witchcraft accusation a crime in Nepal?"
|
|
|
|
| 112 |
]
|
| 113 |
)
|
| 114 |
|
|
@@ -117,7 +117,7 @@ with gr.Blocks() as demo:
|
|
| 117 |
gr.Markdown("### Authentication")
|
| 118 |
gr.LoginButton()
|
| 119 |
gr.Markdown("---")
|
| 120 |
-
gr.Markdown("**
|
| 121 |
chatbot.render()
|
| 122 |
|
| 123 |
if __name__ == "__main__":
|
|
|
|
| 18 |
processor = LegalDocProcessor(PARENT_DATA, CHILD_DATA)
|
| 19 |
docs = processor.load_and_clean()
|
| 20 |
if not docs:
|
| 21 |
+
# Create a dummy doc if files are missing to prevent crash
|
| 22 |
+
return None
|
| 23 |
ret = HybridRetriever(documents=docs, index_dir=INDEX_DIR)
|
| 24 |
ret.save_index()
|
| 25 |
return ret
|
|
|
|
| 29 |
|
| 30 |
def respond(
|
| 31 |
message,
|
| 32 |
+
history,
|
| 33 |
system_message,
|
| 34 |
max_tokens,
|
| 35 |
temperature,
|
| 36 |
top_p,
|
| 37 |
+
hf_token: gr.OAuthToken, # Gradio automatically injects this from the Login button
|
| 38 |
):
|
| 39 |
# 1. RETRIEVAL STEP
|
| 40 |
+
context = ""
|
| 41 |
+
if retriever:
|
| 42 |
+
search_results = retriever.hybrid_search(message, top_k=3)
|
| 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 |
+
# 2. PROMPT ENGINEERING
|
| 52 |
augmented_system_message = (
|
| 53 |
f"{system_message}\n\n"
|
| 54 |
"You are a legal assistant specializing in Nepalese Law. "
|
| 55 |
+
"Use the provided legal context to answer accurately. "
|
| 56 |
+
"Always cite the 'Source' and 'Clause/Section'.\n"
|
| 57 |
f"{context}"
|
| 58 |
)
|
| 59 |
|
| 60 |
+
# Use the OAuth token or fall back to environment variable
|
| 61 |
+
token = hf_token.token if hf_token else os.getenv("HF_TOKEN")
|
| 62 |
+
client = InferenceClient(token=token, model="meta-llama/Llama-3.1-70B-Instruct")
|
| 63 |
|
| 64 |
messages = [{"role": "system", "content": augmented_system_message}]
|
| 65 |
|
|
|
|
| 66 |
for user_msg, assistant_msg in history:
|
| 67 |
if user_msg:
|
| 68 |
messages.append({"role": "user", "content": user_msg})
|
|
|
|
| 73 |
|
| 74 |
response = ""
|
| 75 |
|
| 76 |
+
# 3. GENERATION STEP
|
| 77 |
+
try:
|
| 78 |
+
for msg in client.chat_completion(
|
| 79 |
+
messages,
|
| 80 |
+
max_tokens=max_tokens,
|
| 81 |
+
stream=True,
|
| 82 |
+
temperature=temperature,
|
| 83 |
+
top_p=top_p,
|
| 84 |
+
):
|
| 85 |
+
token_text = msg.choices[0].delta.content
|
| 86 |
+
if token_text:
|
| 87 |
+
response += token_text
|
| 88 |
+
yield response
|
| 89 |
+
except Exception as e:
|
| 90 |
+
yield f"Error calling AI: {str(e)}. Please make sure you are logged in or provide a valid HF Token."
|
| 91 |
|
| 92 |
# --- Gradio UI Setup ---
|
|
|
|
| 93 |
chatbot = gr.ChatInterface(
|
| 94 |
respond,
|
| 95 |
additional_inputs=[
|
|
|
|
| 99 |
),
|
| 100 |
gr.Slider(minimum=1, maximum=2048, value=1024, step=1, label="Max new tokens"),
|
| 101 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 102 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
],
|
| 104 |
title="Nepal Law Search AI",
|
| 105 |
+
description="Ask questions about Nepalese Acts, Codes, and the Constitution.",
|
| 106 |
+
# FIX: Examples must be a list of lists because we have additional_inputs
|
| 107 |
examples=[
|
| 108 |
+
["What are the punishments for cybercrime?"],
|
| 109 |
+
["What does the constitution say about the right to equality?"],
|
| 110 |
+
["Is witchcraft accusation a crime in Nepal?"],
|
| 111 |
+
["What is the legal age for marriage in Nepal?"]
|
| 112 |
]
|
| 113 |
)
|
| 114 |
|
|
|
|
| 117 |
gr.Markdown("### Authentication")
|
| 118 |
gr.LoginButton()
|
| 119 |
gr.Markdown("---")
|
| 120 |
+
gr.Markdown("**Status:** Database Ready ✅")
|
| 121 |
chatbot.render()
|
| 122 |
|
| 123 |
if __name__ == "__main__":
|