Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import torch
|
|
| 3 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 4 |
from peft import PeftModel
|
| 5 |
from threading import Thread
|
|
|
|
| 6 |
|
| 7 |
# Model configuration
|
| 8 |
BASE_MODEL_ID = "Qwen/Qwen2.5-1.5B-Instruct"
|
|
@@ -12,6 +13,7 @@ ADAPTER_MODEL_ID = "vsple/LegalBuddy-Qwen-1.5B"
|
|
| 12 |
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL_ID, trust_remote_code=True)
|
| 13 |
|
| 14 |
# Load base model
|
|
|
|
| 15 |
base_model = AutoModelForCausalLM.from_pretrained(
|
| 16 |
BASE_MODEL_ID,
|
| 17 |
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
|
@@ -20,28 +22,18 @@ base_model = AutoModelForCausalLM.from_pretrained(
|
|
| 20 |
)
|
| 21 |
|
| 22 |
# Load adapter
|
|
|
|
| 23 |
model = PeftModel.from_pretrained(base_model, ADAPTER_MODEL_ID)
|
| 24 |
model = model.eval()
|
| 25 |
|
| 26 |
-
def
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
top_p=0.9,
|
| 33 |
-
):
|
| 34 |
-
messages = [{"role": "system", "content": system_message}]
|
| 35 |
-
|
| 36 |
-
for val in history:
|
| 37 |
-
if val[0]:
|
| 38 |
-
messages.append({"role": "user", "content": val[0]})
|
| 39 |
-
if val[1]:
|
| 40 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 41 |
-
|
| 42 |
messages.append({"role": "user", "content": message})
|
| 43 |
-
|
| 44 |
-
# Apply chat template for Qwen
|
| 45 |
prompt = tokenizer.apply_chat_template(
|
| 46 |
messages,
|
| 47 |
tokenize=False,
|
|
@@ -68,18 +60,72 @@ def respond(
|
|
| 68 |
partial_text += new_text
|
| 69 |
yield partial_text
|
| 70 |
|
| 71 |
-
#
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
],
|
| 80 |
-
title="⚖️ LegalBuddy AI Draft Demo",
|
| 81 |
-
description="Live demo of LegalBuddy-Qwen-1.5B (Fine-tuned). Type your legal queries or drafting requests below."
|
| 82 |
)
|
| 83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
if __name__ == "__main__":
|
| 85 |
demo.launch()
|
|
|
|
| 3 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 4 |
from peft import PeftModel
|
| 5 |
from threading import Thread
|
| 6 |
+
import time
|
| 7 |
|
| 8 |
# Model configuration
|
| 9 |
BASE_MODEL_ID = "Qwen/Qwen2.5-1.5B-Instruct"
|
|
|
|
| 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,
|
|
|
|
| 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 human, assistant in history:
|
| 32 |
+
messages.append({"role": "user", "content": human})
|
| 33 |
+
messages.append({"role": "assistant", "content": assistant})
|
| 34 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
messages.append({"role": "user", "content": message})
|
| 36 |
+
|
|
|
|
| 37 |
prompt = tokenizer.apply_chat_template(
|
| 38 |
messages,
|
| 39 |
tokenize=False,
|
|
|
|
| 60 |
partial_text += new_text
|
| 61 |
yield partial_text
|
| 62 |
|
| 63 |
+
# Custom theme
|
| 64 |
+
theme = gr.themes.Soft(
|
| 65 |
+
primary_hub_palette=gr.themes.colors.slate,
|
| 66 |
+
secondary_hub_palette=gr.themes.colors.blue,
|
| 67 |
+
).set(
|
| 68 |
+
body_background_fill="*neutral_50",
|
| 69 |
+
block_background_fill="white",
|
| 70 |
+
block_border_width="1px",
|
|
|
|
|
|
|
|
|
|
| 71 |
)
|
| 72 |
|
| 73 |
+
with gr.Blocks(theme=theme, title="LegalBuddy AI Draft Engine") as demo:
|
| 74 |
+
with gr.Row():
|
| 75 |
+
gr.Markdown("# ⚖️ LegalBuddy: The Digital Legal Chamber")
|
| 76 |
+
|
| 77 |
+
with gr.Row():
|
| 78 |
+
with gr.Column(scale=2):
|
| 79 |
+
chatbot = gr.Chatbot(height=600, show_label=False)
|
| 80 |
+
msg = gr.Textbox(
|
| 81 |
+
placeholder="Type your legal query or draft request here...",
|
| 82 |
+
container=False,
|
| 83 |
+
scale=7
|
| 84 |
+
)
|
| 85 |
+
with gr.Row():
|
| 86 |
+
submit_btn = gr.Button("Send Request", variant="primary")
|
| 87 |
+
clear_btn = gr.Button("Clear Session")
|
| 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.",
|
| 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")
|
| 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):
|
| 99 |
+
gr.Markdown("## 📄 Live Draft Preview")
|
| 100 |
+
draft_viewer = gr.Markdown(
|
| 101 |
+
label="Generated Legal Document",
|
| 102 |
+
container=True,
|
| 103 |
+
value="*The legal draft will appear here as you interact with the AI assistant...*"
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
def bot_msg(history, system_prompt, max_tokens, temperature, top_p):
|
| 107 |
+
user_message = history[-1][0]
|
| 108 |
+
history[-1][1] = ""
|
| 109 |
+
for token in predict(user_message, history[:-1], system_prompt, max_tokens, temperature, top_p):
|
| 110 |
+
history[-1][1] += token
|
| 111 |
+
yield history, history[-1][1]
|
| 112 |
+
|
| 113 |
+
def user_msg(user_message, history):
|
| 114 |
+
return "", history + [[user_message, None]]
|
| 115 |
+
|
| 116 |
+
submit_btn.click(
|
| 117 |
+
user_msg, [msg, chatbot], [msg, chatbot]
|
| 118 |
+
).then(
|
| 119 |
+
bot_msg, [chatbot, system_msg, max_tok, temp, top_p_val], [chatbot, draft_viewer]
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
msg.submit(
|
| 123 |
+
user_msg, [msg, chatbot], [msg, chatbot]
|
| 124 |
+
).then(
|
| 125 |
+
bot_msg, [chatbot, system_msg, max_tok, temp, top_p_val], [chatbot, draft_viewer]
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
clear_btn.click(lambda: ([], "*The legal draft will appear here...*"), None, [chatbot, draft_viewer])
|
| 129 |
+
|
| 130 |
if __name__ == "__main__":
|
| 131 |
demo.launch()
|