Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1 +1,86 @@
|
|
| 1 |
-
import gradio as
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
# Model configuration
|
| 8 |
+
BASE_MODEL_ID = "Qwen/Qwen2.5-1.5B-Instruct"
|
| 9 |
+
ADAPTER_MODEL_ID = "vsple/LegalBuddy-Qwen-1.5B"
|
| 10 |
+
|
| 11 |
+
# Load tokenizer
|
| 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,
|
| 18 |
+
device_map="auto",
|
| 19 |
+
trust_remote_code=True
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# Load adapter
|
| 23 |
+
model = PeftModel.from_pretrained(base_model, ADAPTER_MODEL_ID)
|
| 24 |
+
model = model.eval()
|
| 25 |
+
|
| 26 |
+
def respond(
|
| 27 |
+
message,
|
| 28 |
+
history,
|
| 29 |
+
system_message="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.",
|
| 30 |
+
max_tokens=1024,
|
| 31 |
+
temperature=0.1,
|
| 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,
|
| 48 |
+
add_generation_prompt=True
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
inputs = tokenizer([prompt], return_tensors="pt").to(model.device)
|
| 52 |
+
streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
|
| 53 |
+
|
| 54 |
+
generate_kwargs = dict(
|
| 55 |
+
inputs,
|
| 56 |
+
streamer=streamer,
|
| 57 |
+
max_new_tokens=max_tokens,
|
| 58 |
+
do_sample=True,
|
| 59 |
+
top_p=top_p,
|
| 60 |
+
temperature=temperature,
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
| 64 |
+
t.start()
|
| 65 |
+
|
| 66 |
+
partial_text = ""
|
| 67 |
+
for new_text in streamer:
|
| 68 |
+
partial_text += new_text
|
| 69 |
+
yield partial_text
|
| 70 |
+
|
| 71 |
+
# Define the Gradio Interface
|
| 72 |
+
demo = gr.ChatInterface(
|
| 73 |
+
respond,
|
| 74 |
+
additional_inputs=[
|
| 75 |
+
gr.Textbox(value="You are LegalBuddy, a professional legal assistant specializing in Indian Law and Document Drafting.", label="System message"),
|
| 76 |
+
gr.Slider(minimum=1, maximum=2048, value=1024, step=1, label="Max new tokens"),
|
| 77 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.1, step=0.1, label="Temperature"),
|
| 78 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
|
| 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 |
+
theme="soft"
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
if __name__ == "__main__":
|
| 86 |
+
demo.launch()
|