Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,42 +1,48 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
from transformers import
|
| 4 |
from peft import PeftModel
|
| 5 |
from threading import Thread
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
# ==================================
|
| 10 |
-
|
| 11 |
-
BASE_MODEL = "Qwen/Qwen2.5-1.5B-Instruct"
|
| 12 |
-
ADAPTER_MODEL = "vsple/LegalBuddy-Qwen-1.5B"
|
| 13 |
|
| 14 |
print("Loading tokenizer...")
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
print("Loading base model...")
|
|
|
|
| 18 |
base_model = AutoModelForCausalLM.from_pretrained(
|
| 19 |
-
|
| 20 |
device_map="auto",
|
| 21 |
-
|
| 22 |
-
trust_remote_code=True
|
|
|
|
| 23 |
)
|
| 24 |
|
| 25 |
print("Loading adapter...")
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
model.eval()
|
| 28 |
|
| 29 |
-
# ==================================
|
| 30 |
-
# Generation Function
|
| 31 |
-
# ==================================
|
| 32 |
|
| 33 |
-
def
|
| 34 |
|
| 35 |
messages = [{"role": "system", "content": system_prompt}]
|
| 36 |
|
| 37 |
-
for user,
|
| 38 |
messages.append({"role": "user", "content": user})
|
| 39 |
-
messages.append({"role": "assistant", "content":
|
| 40 |
|
| 41 |
messages.append({"role": "user", "content": message})
|
| 42 |
|
|
@@ -46,7 +52,7 @@ def generate_stream(message, history, system_prompt, max_tokens, temperature, to
|
|
| 46 |
add_generation_prompt=True
|
| 47 |
)
|
| 48 |
|
| 49 |
-
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 50 |
|
| 51 |
streamer = TextIteratorStreamer(
|
| 52 |
tokenizer,
|
|
@@ -55,7 +61,7 @@ def generate_stream(message, history, system_prompt, max_tokens, temperature, to
|
|
| 55 |
)
|
| 56 |
|
| 57 |
generation_kwargs = dict(
|
| 58 |
-
|
| 59 |
streamer=streamer,
|
| 60 |
max_new_tokens=max_tokens,
|
| 61 |
do_sample=True,
|
|
@@ -68,25 +74,20 @@ def generate_stream(message, history, system_prompt, max_tokens, temperature, to
|
|
| 68 |
|
| 69 |
partial = ""
|
| 70 |
|
| 71 |
-
for
|
| 72 |
-
partial +=
|
| 73 |
yield partial
|
| 74 |
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
# ==================================
|
| 79 |
|
| 80 |
-
def add_user_message(message, history):
|
| 81 |
-
history.append([message, ""])
|
| 82 |
-
return "", history
|
| 83 |
|
| 84 |
-
|
| 85 |
-
def generate_bot_message(history, system_prompt, max_tokens, temperature, top_p):
|
| 86 |
|
| 87 |
user_message = history[-1][0]
|
| 88 |
|
| 89 |
-
for
|
| 90 |
user_message,
|
| 91 |
history[:-1],
|
| 92 |
system_prompt,
|
|
@@ -94,134 +95,72 @@ def generate_bot_message(history, system_prompt, max_tokens, temperature, top_p)
|
|
| 94 |
temperature,
|
| 95 |
top_p
|
| 96 |
):
|
| 97 |
-
history[-1][1] =
|
| 98 |
-
yield history
|
| 99 |
-
|
| 100 |
|
| 101 |
-
def clear_chat():
|
| 102 |
-
return [], "*The legal draft will appear here...*"
|
| 103 |
|
|
|
|
| 104 |
|
| 105 |
-
#
|
| 106 |
-
# Theme
|
| 107 |
-
# ==================================
|
| 108 |
|
| 109 |
-
|
| 110 |
-
primary_hue="slate",
|
| 111 |
-
secondary_hue="blue",
|
| 112 |
-
)
|
| 113 |
-
|
| 114 |
-
# ==================================
|
| 115 |
-
# UI
|
| 116 |
-
# ==================================
|
| 117 |
-
|
| 118 |
-
with gr.Blocks(
|
| 119 |
-
title="LegalBuddy AI Draft Engine",
|
| 120 |
-
css="""
|
| 121 |
-
.draft-viewer {
|
| 122 |
-
max-height: 650px;
|
| 123 |
-
overflow-y: auto;
|
| 124 |
-
padding: 20px;
|
| 125 |
-
}
|
| 126 |
-
"""
|
| 127 |
-
) as demo:
|
| 128 |
|
| 129 |
-
gr.
|
| 130 |
-
|
|
|
|
| 131 |
|
| 132 |
with gr.Row():
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
label="Max Tokens"
|
| 164 |
-
)
|
| 165 |
-
|
| 166 |
-
temperature = gr.Slider(
|
| 167 |
-
minimum=0.1,
|
| 168 |
-
maximum=1.0,
|
| 169 |
-
value=0.1,
|
| 170 |
-
step=0.1,
|
| 171 |
-
label="Temperature"
|
| 172 |
-
)
|
| 173 |
-
|
| 174 |
-
top_p = gr.Slider(
|
| 175 |
-
minimum=0.1,
|
| 176 |
-
maximum=1.0,
|
| 177 |
-
value=0.9,
|
| 178 |
-
step=0.05,
|
| 179 |
-
label="Top-p"
|
| 180 |
-
)
|
| 181 |
-
|
| 182 |
-
# Draft Viewer
|
| 183 |
-
with gr.Column(scale=3):
|
| 184 |
-
|
| 185 |
-
gr.Markdown("## 📄 Draft Preview")
|
| 186 |
-
|
| 187 |
-
draft_output = gr.Markdown(
|
| 188 |
-
value="*Generated legal document will appear here...*",
|
| 189 |
-
elem_classes="draft-viewer"
|
| 190 |
-
)
|
| 191 |
-
|
| 192 |
-
# ==================================
|
| 193 |
-
# Events
|
| 194 |
-
# ==================================
|
| 195 |
-
|
| 196 |
-
send_btn.click(
|
| 197 |
-
add_user_message,
|
| 198 |
[msg, chatbot],
|
| 199 |
-
[msg, chatbot]
|
|
|
|
| 200 |
).then(
|
| 201 |
-
|
| 202 |
[chatbot, system_prompt, max_tokens, temperature, top_p],
|
| 203 |
-
|
| 204 |
)
|
| 205 |
|
| 206 |
msg.submit(
|
| 207 |
-
|
|
|
|
| 208 |
[msg, chatbot],
|
| 209 |
-
|
| 210 |
).then(
|
| 211 |
-
|
| 212 |
[chatbot, system_prompt, max_tokens, temperature, top_p],
|
| 213 |
-
|
| 214 |
-
)
|
| 215 |
-
|
| 216 |
-
clear_btn.click(
|
| 217 |
-
clear_chat,
|
| 218 |
-
None,
|
| 219 |
-
[chatbot, draft_output]
|
| 220 |
)
|
| 221 |
|
| 222 |
-
|
| 223 |
-
# Launch
|
| 224 |
-
# ==================================
|
| 225 |
|
| 226 |
-
|
| 227 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 4 |
from peft import PeftModel
|
| 5 |
from threading import Thread
|
| 6 |
|
| 7 |
+
BASE_MODEL_ID = "Qwen/Qwen2.5-1.5B-Instruct"
|
| 8 |
+
ADAPTER_MODEL_ID = "vsple/LegalBuddy-Qwen-1.5B"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
print("Loading tokenizer...")
|
| 11 |
+
|
| 12 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 13 |
+
BASE_MODEL_ID,
|
| 14 |
+
trust_remote_code=True,
|
| 15 |
+
cache_dir="/tmp/huggingface"
|
| 16 |
+
)
|
| 17 |
|
| 18 |
print("Loading base model...")
|
| 19 |
+
|
| 20 |
base_model = AutoModelForCausalLM.from_pretrained(
|
| 21 |
+
BASE_MODEL_ID,
|
| 22 |
device_map="auto",
|
| 23 |
+
dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 24 |
+
trust_remote_code=True,
|
| 25 |
+
cache_dir="/tmp/huggingface"
|
| 26 |
)
|
| 27 |
|
| 28 |
print("Loading adapter...")
|
| 29 |
+
|
| 30 |
+
model = PeftModel.from_pretrained(
|
| 31 |
+
base_model,
|
| 32 |
+
ADAPTER_MODEL_ID,
|
| 33 |
+
cache_dir="/tmp/huggingface"
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
model.eval()
|
| 37 |
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
def predict(message, history, system_prompt, max_tokens, temperature, top_p):
|
| 40 |
|
| 41 |
messages = [{"role": "system", "content": system_prompt}]
|
| 42 |
|
| 43 |
+
for user, bot in history:
|
| 44 |
messages.append({"role": "user", "content": user})
|
| 45 |
+
messages.append({"role": "assistant", "content": bot})
|
| 46 |
|
| 47 |
messages.append({"role": "user", "content": message})
|
| 48 |
|
|
|
|
| 52 |
add_generation_prompt=True
|
| 53 |
)
|
| 54 |
|
| 55 |
+
inputs = tokenizer([prompt], return_tensors="pt").to(model.device)
|
| 56 |
|
| 57 |
streamer = TextIteratorStreamer(
|
| 58 |
tokenizer,
|
|
|
|
| 61 |
)
|
| 62 |
|
| 63 |
generation_kwargs = dict(
|
| 64 |
+
inputs,
|
| 65 |
streamer=streamer,
|
| 66 |
max_new_tokens=max_tokens,
|
| 67 |
do_sample=True,
|
|
|
|
| 74 |
|
| 75 |
partial = ""
|
| 76 |
|
| 77 |
+
for new_text in streamer:
|
| 78 |
+
partial += new_text
|
| 79 |
yield partial
|
| 80 |
|
| 81 |
|
| 82 |
+
def user(user_message, history):
|
| 83 |
+
return "", history + [[user_message, None]]
|
|
|
|
| 84 |
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
+
def bot(history, system_prompt, max_tokens, temperature, top_p):
|
|
|
|
| 87 |
|
| 88 |
user_message = history[-1][0]
|
| 89 |
|
| 90 |
+
for response in predict(
|
| 91 |
user_message,
|
| 92 |
history[:-1],
|
| 93 |
system_prompt,
|
|
|
|
| 95 |
temperature,
|
| 96 |
top_p
|
| 97 |
):
|
| 98 |
+
history[-1][1] = response
|
| 99 |
+
yield history
|
|
|
|
| 100 |
|
|
|
|
|
|
|
| 101 |
|
| 102 |
+
with gr.Blocks(title="LegalBuddy AI Draft Engine") as demo:
|
| 103 |
|
| 104 |
+
gr.Markdown("# ⚖️ LegalBuddy: The Digital Legal Chamber")
|
|
|
|
|
|
|
| 105 |
|
| 106 |
+
chatbot = gr.Chatbot(height=600)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
+
msg = gr.Textbox(
|
| 109 |
+
placeholder="Type your legal query or draft request..."
|
| 110 |
+
)
|
| 111 |
|
| 112 |
with gr.Row():
|
| 113 |
+
submit = gr.Button("Send")
|
| 114 |
+
clear = gr.Button("Clear")
|
| 115 |
+
|
| 116 |
+
with gr.Accordion("Advanced Settings", open=False):
|
| 117 |
+
|
| 118 |
+
system_prompt = gr.Textbox(
|
| 119 |
+
value="You are a professional legal assistant specializing in Indian law and legal document drafting.",
|
| 120 |
+
label="System Prompt"
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
max_tokens = gr.Slider(1, 2048, value=512, label="Max Tokens")
|
| 124 |
+
|
| 125 |
+
temperature = gr.Slider(
|
| 126 |
+
0.1,
|
| 127 |
+
1.0,
|
| 128 |
+
value=0.2,
|
| 129 |
+
step=0.1,
|
| 130 |
+
label="Temperature"
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
top_p = gr.Slider(
|
| 134 |
+
0.1,
|
| 135 |
+
1.0,
|
| 136 |
+
value=0.9,
|
| 137 |
+
step=0.05,
|
| 138 |
+
label="Top-p"
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
submit.click(
|
| 142 |
+
user,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
[msg, chatbot],
|
| 144 |
+
[msg, chatbot],
|
| 145 |
+
queue=False
|
| 146 |
).then(
|
| 147 |
+
bot,
|
| 148 |
[chatbot, system_prompt, max_tokens, temperature, top_p],
|
| 149 |
+
chatbot
|
| 150 |
)
|
| 151 |
|
| 152 |
msg.submit(
|
| 153 |
+
user,
|
| 154 |
+
[msg, chatbot],
|
| 155 |
[msg, chatbot],
|
| 156 |
+
queue=False
|
| 157 |
).then(
|
| 158 |
+
bot,
|
| 159 |
[chatbot, system_prompt, max_tokens, temperature, top_p],
|
| 160 |
+
chatbot
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
)
|
| 162 |
|
| 163 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
|
|
|
|
|
|
| 164 |
|
| 165 |
+
demo.queue()
|
| 166 |
+
demo.launch()
|