Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,33 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
|
|
|
| 4 |
from threading import Thread
|
| 5 |
|
| 6 |
# Model configuration
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
-
# Load tokenizer
|
| 10 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
| 13 |
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 14 |
device_map="auto",
|
| 15 |
trust_remote_code=True
|
| 16 |
)
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
def respond(
|
| 19 |
message,
|
| 20 |
history,
|
| 21 |
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.",
|
| 22 |
-
max_tokens=
|
| 23 |
temperature=0.1,
|
| 24 |
top_p=0.9,
|
| 25 |
):
|
|
@@ -65,12 +73,12 @@ demo = gr.ChatInterface(
|
|
| 65 |
respond,
|
| 66 |
additional_inputs=[
|
| 67 |
gr.Textbox(value="You are LegalBuddy, a professional legal assistant specializing in Indian Law and Document Drafting.", label="System message"),
|
| 68 |
-
gr.Slider(minimum=1, maximum=
|
| 69 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.1, step=0.1, label="Temperature"),
|
| 70 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
|
| 71 |
],
|
| 72 |
-
title="LegalBuddy AI Draft Demo",
|
| 73 |
-
description="Live demo of LegalBuddy-Qwen-1.5B. Type your legal queries or drafting requests below.",
|
| 74 |
theme="soft"
|
| 75 |
)
|
| 76 |
|
|
|
|
| 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/Qwen1.5-1.5B"
|
| 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 |
):
|
|
|
|
| 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 |
|