Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,133 +1,227 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
from transformers import
|
| 4 |
from peft import PeftModel
|
| 5 |
from threading import Thread
|
| 6 |
-
import time
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
| 18 |
base_model = AutoModelForCausalLM.from_pretrained(
|
| 19 |
-
|
|
|
|
| 20 |
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 21 |
-
low_cpu_mem_usage=True,
|
| 22 |
trust_remote_code=True
|
| 23 |
)
|
| 24 |
|
| 25 |
-
|
| 26 |
-
model =
|
| 27 |
-
model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
def predict(message, history, system_prompt, max_tokens, temperature, top_p):
|
| 30 |
messages = [{"role": "system", "content": system_prompt}]
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
| 34 |
messages.append({"role": "user", "content": message})
|
| 35 |
-
|
| 36 |
prompt = tokenizer.apply_chat_template(
|
| 37 |
messages,
|
| 38 |
tokenize=False,
|
| 39 |
add_generation_prompt=True
|
| 40 |
)
|
| 41 |
-
|
| 42 |
-
inputs = tokenizer(
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
streamer=streamer,
|
| 48 |
max_new_tokens=max_tokens,
|
| 49 |
do_sample=True,
|
| 50 |
-
top_p=top_p,
|
| 51 |
temperature=temperature,
|
|
|
|
| 52 |
)
|
| 53 |
-
|
| 54 |
-
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
| 55 |
-
t.start()
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
# Custom theme
|
| 63 |
theme = gr.themes.Soft(
|
| 64 |
primary_hue="slate",
|
| 65 |
secondary_hue="blue",
|
| 66 |
-
).set(
|
| 67 |
-
body_background_fill="*neutral_50",
|
| 68 |
-
block_background_fill="white",
|
| 69 |
-
block_border_width="1px",
|
| 70 |
)
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
with gr.Row():
|
|
|
|
|
|
|
| 78 |
with gr.Column(scale=2):
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
msg = gr.Textbox(
|
| 81 |
-
placeholder="Type your legal
|
| 82 |
-
container=False
|
| 83 |
-
scale=7
|
| 84 |
)
|
|
|
|
| 85 |
with gr.Row():
|
| 86 |
-
|
| 87 |
clear_btn = gr.Button("Clear Session")
|
| 88 |
-
|
| 89 |
with gr.Accordion("⚙️ Expert Settings", open=False):
|
| 90 |
-
|
| 91 |
-
|
| 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):
|
| 99 |
-
gr.Markdown("## 📄 Live Draft Preview")
|
| 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 |
-
|
| 109 |
-
return "", history + [{"role": "user", "content": user_message}]
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
history[-1]["content"] += token
|
| 116 |
-
yield history, history[-1]["content"]
|
| 117 |
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
).then(
|
| 121 |
-
|
|
|
|
|
|
|
| 122 |
)
|
| 123 |
-
|
| 124 |
msg.submit(
|
| 125 |
-
|
|
|
|
|
|
|
| 126 |
).then(
|
| 127 |
-
|
|
|
|
|
|
|
| 128 |
)
|
| 129 |
-
|
| 130 |
-
clear_btn.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
if __name__ == "__main__":
|
| 133 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
|
| 4 |
from peft import PeftModel
|
| 5 |
from threading import Thread
|
|
|
|
| 6 |
|
| 7 |
+
# ==================================
|
| 8 |
+
# Model Configuration
|
| 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 |
+
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, trust_remote_code=True)
|
| 16 |
+
|
| 17 |
+
print("Loading base model...")
|
| 18 |
base_model = AutoModelForCausalLM.from_pretrained(
|
| 19 |
+
BASE_MODEL,
|
| 20 |
+
device_map="auto",
|
| 21 |
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
|
|
|
| 22 |
trust_remote_code=True
|
| 23 |
)
|
| 24 |
|
| 25 |
+
print("Loading adapter...")
|
| 26 |
+
model = PeftModel.from_pretrained(base_model, ADAPTER_MODEL)
|
| 27 |
+
model.eval()
|
| 28 |
+
|
| 29 |
+
# ==================================
|
| 30 |
+
# Generation Function
|
| 31 |
+
# ==================================
|
| 32 |
+
|
| 33 |
+
def generate_stream(message, history, system_prompt, max_tokens, temperature, top_p):
|
| 34 |
|
|
|
|
| 35 |
messages = [{"role": "system", "content": system_prompt}]
|
| 36 |
+
|
| 37 |
+
for user, assistant in history:
|
| 38 |
+
messages.append({"role": "user", "content": user})
|
| 39 |
+
messages.append({"role": "assistant", "content": assistant})
|
| 40 |
+
|
| 41 |
messages.append({"role": "user", "content": message})
|
| 42 |
+
|
| 43 |
prompt = tokenizer.apply_chat_template(
|
| 44 |
messages,
|
| 45 |
tokenize=False,
|
| 46 |
add_generation_prompt=True
|
| 47 |
)
|
| 48 |
+
|
| 49 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 50 |
+
|
| 51 |
+
streamer = TextIteratorStreamer(
|
| 52 |
+
tokenizer,
|
| 53 |
+
skip_prompt=True,
|
| 54 |
+
skip_special_tokens=True
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
generation_kwargs = dict(
|
| 58 |
+
**inputs,
|
| 59 |
streamer=streamer,
|
| 60 |
max_new_tokens=max_tokens,
|
| 61 |
do_sample=True,
|
|
|
|
| 62 |
temperature=temperature,
|
| 63 |
+
top_p=top_p
|
| 64 |
)
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
| 67 |
+
thread.start()
|
| 68 |
+
|
| 69 |
+
partial = ""
|
| 70 |
+
|
| 71 |
+
for token in streamer:
|
| 72 |
+
partial += token
|
| 73 |
+
yield partial
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
# ==================================
|
| 77 |
+
# Chat Functions
|
| 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 token in generate_stream(
|
| 90 |
+
user_message,
|
| 91 |
+
history[:-1],
|
| 92 |
+
system_prompt,
|
| 93 |
+
max_tokens,
|
| 94 |
+
temperature,
|
| 95 |
+
top_p
|
| 96 |
+
):
|
| 97 |
+
history[-1][1] = token
|
| 98 |
+
yield history, token
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
def clear_chat():
|
| 102 |
+
return [], "*The legal draft will appear here...*"
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
# ==================================
|
| 106 |
+
# Theme
|
| 107 |
+
# ==================================
|
| 108 |
|
|
|
|
| 109 |
theme = gr.themes.Soft(
|
| 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.Markdown("# ⚖️ LegalBuddy AI")
|
| 130 |
+
gr.Markdown("### AI-Powered Legal Drafting Assistant")
|
| 131 |
+
|
| 132 |
with gr.Row():
|
| 133 |
+
|
| 134 |
+
# Chat Section
|
| 135 |
with gr.Column(scale=2):
|
| 136 |
+
|
| 137 |
+
chatbot = gr.Chatbot(
|
| 138 |
+
height=600,
|
| 139 |
+
bubble_full_width=False
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
msg = gr.Textbox(
|
| 143 |
+
placeholder="Type your legal drafting request...",
|
| 144 |
+
container=False
|
|
|
|
| 145 |
)
|
| 146 |
+
|
| 147 |
with gr.Row():
|
| 148 |
+
send_btn = gr.Button("Send", variant="primary")
|
| 149 |
clear_btn = gr.Button("Clear Session")
|
| 150 |
+
|
| 151 |
with gr.Accordion("⚙️ Expert Settings", open=False):
|
| 152 |
+
|
| 153 |
+
system_prompt = gr.Textbox(
|
| 154 |
+
label="System Protocol",
|
| 155 |
+
value="You are LegalBuddy, a professional legal assistant specializing in Indian law and legal document drafting."
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
max_tokens = gr.Slider(
|
| 159 |
+
minimum=1,
|
| 160 |
+
maximum=2048,
|
| 161 |
+
value=1024,
|
| 162 |
+
step=1,
|
| 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 |
+
generate_bot_message,
|
| 202 |
+
[chatbot, system_prompt, max_tokens, temperature, top_p],
|
| 203 |
+
[chatbot, draft_output]
|
| 204 |
)
|
| 205 |
+
|
| 206 |
msg.submit(
|
| 207 |
+
add_user_message,
|
| 208 |
+
[msg, chatbot],
|
| 209 |
+
[msg, chatbot]
|
| 210 |
).then(
|
| 211 |
+
generate_bot_message,
|
| 212 |
+
[chatbot, system_prompt, max_tokens, temperature, top_p],
|
| 213 |
+
[chatbot, draft_output]
|
| 214 |
)
|
| 215 |
+
|
| 216 |
+
clear_btn.click(
|
| 217 |
+
clear_chat,
|
| 218 |
+
None,
|
| 219 |
+
[chatbot, draft_output]
|
| 220 |
+
)
|
| 221 |
+
|
| 222 |
+
# ==================================
|
| 223 |
+
# Launch
|
| 224 |
+
# ==================================
|
| 225 |
|
| 226 |
if __name__ == "__main__":
|
| 227 |
+
demo.launch(theme=theme)
|