deepakdethliya commited on
Commit
4fae600
·
verified ·
1 Parent(s): c2457b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -22
app.py CHANGED
@@ -12,23 +12,24 @@ ADAPTER_MODEL_ID = "vsple/LegalBuddy-Qwen-1.5B"
12
  # Load tokenizer
13
  tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL_ID, trust_remote_code=True)
14
 
15
- # Load model (Fixed loading logic for HF Spaces)
16
- print("Loading model and adapter...")
17
- device = "cuda" if torch.cuda.is_available() else "cpu"
18
- model = AutoModelForCausalLM.from_pretrained(
19
  BASE_MODEL_ID,
20
- torch_dtype=torch.float32,
 
21
  trust_remote_code=True
22
- ).to(device)
23
 
24
- model = PeftModel.from_pretrained(model, ADAPTER_MODEL_ID)
 
 
25
  model = model.eval()
26
 
27
  def predict(message, history, system_prompt, max_tokens, temperature, top_p):
28
  messages = [{"role": "system", "content": system_prompt}]
29
- for human, assistant in history:
30
- messages.append({"role": "user", "content": human})
31
- messages.append({"role": "assistant", "content": assistant})
32
 
33
  messages.append({"role": "user", "content": message})
34
 
@@ -58,7 +59,7 @@ def predict(message, history, system_prompt, max_tokens, temperature, top_p):
58
  partial_text += new_text
59
  yield partial_text
60
 
61
- # Custom theme (Fixed for Gradio 4+)
62
  theme = gr.themes.Soft(
63
  primary_hue="slate",
64
  secondary_hue="blue",
@@ -71,10 +72,11 @@ theme = gr.themes.Soft(
71
  with gr.Blocks(theme=theme, title="LegalBuddy AI Draft Engine") as demo:
72
  with gr.Row():
73
  gr.Markdown("# ⚖️ LegalBuddy: The Digital Legal Chamber")
 
74
 
75
  with gr.Row():
76
  with gr.Column(scale=2):
77
- chatbot = gr.Chatbot(height=600, show_label=False)
78
  msg = gr.Textbox(
79
  placeholder="Type your legal query or draft request here...",
80
  container=False,
@@ -86,11 +88,11 @@ with gr.Blocks(theme=theme, title="LegalBuddy AI Draft Engine") as demo:
86
 
87
  with gr.Accordion("⚙️ Expert Settings", open=False):
88
  system_msg = gr.Textbox(
89
- value="You are LegalBuddy, a professional legal assistant specializing in Indian Law and Document Drafting.",
90
  label="System Protocol"
91
  )
92
  max_tok = gr.Slider(minimum=1, maximum=2048, value=1024, step=1, label="Max Output Tokens")
93
- temp = gr.Slider(minimum=0.1, maximum=1.0, value=0.1, step=0.1, label="Drafting Precision")
94
  top_p_val = gr.Slider(minimum=0.1, maximum=1.0, value=0.9, step=0.05, label="Top-p Sampling")
95
 
96
  with gr.Column(scale=3):
@@ -98,18 +100,20 @@ with gr.Blocks(theme=theme, title="LegalBuddy AI Draft Engine") as demo:
98
  draft_viewer = gr.Markdown(
99
  label="Generated Legal Document",
100
  container=True,
 
 
101
  value="*The legal draft will appear here as you interact with the AI assistant...*"
102
  )
103
 
 
 
 
104
  def bot_msg(history, system_prompt, max_tokens, temperature, top_p):
105
- user_message = history[-1][0]
106
- history[-1][1] = ""
107
  for token in predict(user_message, history[:-1], system_prompt, max_tokens, temperature, top_p):
108
- history[-1][1] += token
109
- yield history, history[-1][1]
110
-
111
- def user_msg(user_message, history):
112
- return "", history + [[user_message, None]]
113
 
114
  submit_btn.click(
115
  user_msg, [msg, chatbot], [msg, chatbot]
@@ -126,4 +130,4 @@ with gr.Blocks(theme=theme, title="LegalBuddy AI Draft Engine") as demo:
126
  clear_btn.click(lambda: ([], "*The legal draft will appear here...*"), None, [chatbot, draft_viewer])
127
 
128
  if __name__ == "__main__":
129
- demo.launch()
 
12
  # Load tokenizer
13
  tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL_ID, trust_remote_code=True)
14
 
15
+ # Load base model
16
+ print("Loading base model...")
17
+ base_model = AutoModelForCausalLM.from_pretrained(
 
18
  BASE_MODEL_ID,
19
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
20
+ device_map="auto",
21
  trust_remote_code=True
22
+ )
23
 
24
+ # Load adapter
25
+ print("Loading adapter...")
26
+ model = PeftModel.from_pretrained(base_model, ADAPTER_MODEL_ID)
27
  model = model.eval()
28
 
29
  def predict(message, history, system_prompt, max_tokens, temperature, top_p):
30
  messages = [{"role": "system", "content": system_prompt}]
31
+ for msg in history:
32
+ messages.append(msg)
 
33
 
34
  messages.append({"role": "user", "content": message})
35
 
 
59
  partial_text += new_text
60
  yield partial_text
61
 
62
+ # Custom theme
63
  theme = gr.themes.Soft(
64
  primary_hue="slate",
65
  secondary_hue="blue",
 
72
  with gr.Blocks(theme=theme, title="LegalBuddy AI Draft Engine") as demo:
73
  with gr.Row():
74
  gr.Markdown("# ⚖️ LegalBuddy: The Digital Legal Chamber")
75
+ gr.Markdown("### 🚀 Experience the future of AI-driven Legal Drafting")
76
 
77
  with gr.Row():
78
  with gr.Column(scale=2):
79
+ chatbot = gr.Chatbot(height=600, show_label=False, type="messages")
80
  msg = gr.Textbox(
81
  placeholder="Type your legal query or draft request here...",
82
  container=False,
 
88
 
89
  with gr.Accordion("⚙️ Expert Settings", open=False):
90
  system_msg = gr.Textbox(
91
+ value="You are LegalBuddy, a professional legal assistant specializing in Indian Law and Document Drafting. Provide precise, legally compliant advice and draft clauses in a structured format.",
92
  label="System Protocol"
93
  )
94
  max_tok = gr.Slider(minimum=1, maximum=2048, value=1024, step=1, label="Max Output Tokens")
95
+ temp = gr.Slider(minimum=0.1, maximum=1.0, value=0.1, step=0.1, label="Drafting Precision (Temperature)")
96
  top_p_val = gr.Slider(minimum=0.1, maximum=1.0, value=0.9, step=0.05, label="Top-p Sampling")
97
 
98
  with gr.Column(scale=3):
 
100
  draft_viewer = gr.Markdown(
101
  label="Generated Legal Document",
102
  container=True,
103
+ line_breaks=True,
104
+ header_links=True,
105
  value="*The legal draft will appear here as you interact with the AI assistant...*"
106
  )
107
 
108
+ def user_msg(user_message, history):
109
+ return "", history + [{"role": "user", "content": user_message}]
110
+
111
  def bot_msg(history, system_prompt, max_tokens, temperature, top_p):
112
+ user_message = history[-1]["content"]
113
+ history.append({"role": "assistant", "content": ""})
114
  for token in predict(user_message, history[:-1], system_prompt, max_tokens, temperature, top_p):
115
+ history[-1]["content"] += token
116
+ yield history, history[-1]["content"]
 
 
 
117
 
118
  submit_btn.click(
119
  user_msg, [msg, chatbot], [msg, chatbot]
 
130
  clear_btn.click(lambda: ([], "*The legal draft will appear here...*"), None, [chatbot, draft_viewer])
131
 
132
  if __name__ == "__main__":
133
+ demo.launch()