devNaam commited on
Commit
243a44a
·
1 Parent(s): 65b3a86

Upgrade VakilAI chat interface

Browse files
Files changed (1) hide show
  1. app.py +48 -8
app.py CHANGED
@@ -4,36 +4,76 @@ import torch
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
  tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
11
 
 
12
  model = AutoModelForCausalLM.from_pretrained(
13
  BASE_MODEL,
14
  device_map="auto"
15
  )
16
 
 
17
  model = PeftModel.from_pretrained(model, ADAPTER_MODEL)
18
 
19
- def vakil_ai(prompt):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
22
 
23
  output = model.generate(
24
  **inputs,
25
- max_new_tokens=200,
26
- temperature=0.7
 
27
  )
28
 
29
- return tokenizer.decode(output[0], skip_special_tokens=True)
 
 
 
 
 
 
30
 
31
 
32
- demo = gr.Interface(
 
33
  fn=vakil_ai,
34
- inputs=gr.Textbox(lines=4),
35
- outputs="text",
36
- title="AI Vakil"
 
 
 
 
 
37
  )
38
 
39
  demo.launch()
 
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="auto"
20
  )
21
 
22
+ print("Loading VakilAI adapter...")
23
  model = PeftModel.from_pretrained(model, ADAPTER_MODEL)
24
 
25
+ print("Model ready.")
26
+
27
+
28
+ # Prompt template for legal assistant behavior
29
+ def build_prompt(user_question):
30
+ return f"""
31
+ You are VakilAI, an AI legal assistant specializing in Indian law.
32
+
33
+ Explain legal concepts clearly in simple language.
34
+ If possible, mention relevant IPC sections or legal principles.
35
+
36
+ User Question:
37
+ {user_question}
38
+
39
+ Answer:
40
+ """
41
+
42
+
43
+ # Generate response
44
+ def vakil_ai(user_message, history):
45
+
46
+ prompt = build_prompt(user_message)
47
 
48
  inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
49
 
50
  output = model.generate(
51
  **inputs,
52
+ max_new_tokens=250,
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, IPC sections, and legal concepts.",
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()