Upgrade VakilAI chat interface
Browse files
app.py
CHANGED
|
@@ -4,76 +4,62 @@ import torch
|
|
| 4 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 5 |
from peft import PeftModel
|
| 6 |
|
| 7 |
-
# Base model used during training
|
| 8 |
BASE_MODEL = "unsloth/llama-3.2-3b-bnb-4bit"
|
| 9 |
-
|
| 10 |
-
# Your VakilAI LoRA adapter
|
| 11 |
ADAPTER_MODEL = "devNaam/vakilai-llama32-3b-v1"
|
| 12 |
|
| 13 |
print("Loading tokenizer...")
|
| 14 |
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
|
| 15 |
|
| 16 |
-
print("Loading base model...")
|
| 17 |
model = AutoModelForCausalLM.from_pretrained(
|
| 18 |
BASE_MODEL,
|
| 19 |
-
device_map="
|
|
|
|
| 20 |
)
|
| 21 |
|
| 22 |
print("Loading VakilAI adapter...")
|
| 23 |
model = PeftModel.from_pretrained(model, ADAPTER_MODEL)
|
| 24 |
|
| 25 |
-
print("Model ready
|
| 26 |
|
| 27 |
|
| 28 |
-
|
| 29 |
-
def build_prompt(user_question):
|
| 30 |
return f"""
|
| 31 |
-
You are VakilAI, an AI legal assistant
|
| 32 |
|
| 33 |
-
Explain
|
| 34 |
-
If possible, mention relevant IPC sections or legal principles.
|
| 35 |
|
| 36 |
-
|
| 37 |
-
{
|
| 38 |
|
| 39 |
Answer:
|
| 40 |
"""
|
| 41 |
|
| 42 |
|
| 43 |
-
|
| 44 |
-
def vakil_ai(user_message, history):
|
| 45 |
|
| 46 |
-
prompt = build_prompt(
|
| 47 |
|
| 48 |
-
inputs = tokenizer(prompt, return_tensors="pt")
|
| 49 |
|
| 50 |
output = model.generate(
|
| 51 |
**inputs,
|
| 52 |
-
max_new_tokens=
|
| 53 |
-
temperature=0.5
|
| 54 |
-
top_p=0.9
|
| 55 |
)
|
| 56 |
|
| 57 |
response = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 58 |
|
| 59 |
-
# Extract only the answer part
|
| 60 |
if "Answer:" in response:
|
| 61 |
response = response.split("Answer:")[-1].strip()
|
| 62 |
|
| 63 |
return response
|
| 64 |
|
| 65 |
|
| 66 |
-
# Chat interface
|
| 67 |
demo = gr.ChatInterface(
|
| 68 |
fn=vakil_ai,
|
| 69 |
title="⚖️ AI Vakil – Indian Legal Assistant",
|
| 70 |
-
description="Ask questions about Indian law,
|
| 71 |
-
examples=[
|
| 72 |
-
"What is IPC Section 307?",
|
| 73 |
-
"What is the punishment for theft in India?",
|
| 74 |
-
"What is the difference between murder and culpable homicide?",
|
| 75 |
-
"What rights does a person have during arrest in India?"
|
| 76 |
-
]
|
| 77 |
)
|
| 78 |
|
| 79 |
demo.launch()
|
|
|
|
| 4 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 5 |
from peft import PeftModel
|
| 6 |
|
|
|
|
| 7 |
BASE_MODEL = "unsloth/llama-3.2-3b-bnb-4bit"
|
|
|
|
|
|
|
| 8 |
ADAPTER_MODEL = "devNaam/vakilai-llama32-3b-v1"
|
| 9 |
|
| 10 |
print("Loading tokenizer...")
|
| 11 |
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
|
| 12 |
|
| 13 |
+
print("Loading base model on CPU...")
|
| 14 |
model = AutoModelForCausalLM.from_pretrained(
|
| 15 |
BASE_MODEL,
|
| 16 |
+
device_map="cpu",
|
| 17 |
+
torch_dtype=torch.float32
|
| 18 |
)
|
| 19 |
|
| 20 |
print("Loading VakilAI adapter...")
|
| 21 |
model = PeftModel.from_pretrained(model, ADAPTER_MODEL)
|
| 22 |
|
| 23 |
+
print("Model ready")
|
| 24 |
|
| 25 |
|
| 26 |
+
def build_prompt(question):
|
|
|
|
| 27 |
return f"""
|
| 28 |
+
You are VakilAI, an AI legal assistant for Indian law.
|
| 29 |
|
| 30 |
+
Explain the answer clearly and simply.
|
|
|
|
| 31 |
|
| 32 |
+
Question:
|
| 33 |
+
{question}
|
| 34 |
|
| 35 |
Answer:
|
| 36 |
"""
|
| 37 |
|
| 38 |
|
| 39 |
+
def vakil_ai(message, history):
|
|
|
|
| 40 |
|
| 41 |
+
prompt = build_prompt(message)
|
| 42 |
|
| 43 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 44 |
|
| 45 |
output = model.generate(
|
| 46 |
**inputs,
|
| 47 |
+
max_new_tokens=200,
|
| 48 |
+
temperature=0.5
|
|
|
|
| 49 |
)
|
| 50 |
|
| 51 |
response = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 52 |
|
|
|
|
| 53 |
if "Answer:" in response:
|
| 54 |
response = response.split("Answer:")[-1].strip()
|
| 55 |
|
| 56 |
return response
|
| 57 |
|
| 58 |
|
|
|
|
| 59 |
demo = gr.ChatInterface(
|
| 60 |
fn=vakil_ai,
|
| 61 |
title="⚖️ AI Vakil – Indian Legal Assistant",
|
| 62 |
+
description="Ask questions about IPC, Indian law, and legal concepts.",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
)
|
| 64 |
|
| 65 |
demo.launch()
|