Upload 3 files
Browse files- README.md +25 -7
- app.py +122 -0
- requirements.txt +4 -0
README.md
CHANGED
|
@@ -1,15 +1,33 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
colorFrom: blue
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
-
python_version: '3.12'
|
| 9 |
app_file: app.py
|
| 10 |
pinned: false
|
| 11 |
license: mit
|
| 12 |
-
short_description: Hobby
|
| 13 |
---
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: SaulLM Legal Assistant
|
| 3 |
+
emoji: ⚖️
|
| 4 |
colorFrom: blue
|
| 5 |
+
colorTo: indigo
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 5.9.1
|
|
|
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: mit
|
|
|
|
| 11 |
---
|
| 12 |
|
| 13 |
+
# SaulLM-141B Legal Assistant
|
| 14 |
+
|
| 15 |
+
Private legal AI assistant powered by Saul-141B, a specialized 141 billion parameter model for legal reasoning.
|
| 16 |
+
|
| 17 |
+
## Features
|
| 18 |
+
|
| 19 |
+
- **Specialized Legal Knowledge**: Trained on 540B legal tokens from U.S. and European legal systems
|
| 20 |
+
- **Zero GPU**: Free 25 min/day of H200 compute time
|
| 21 |
+
- **Private Queries**: Your legal questions stay private
|
| 22 |
+
- **Chat Interface**: Natural conversation with legal context
|
| 23 |
+
|
| 24 |
+
## Usage
|
| 25 |
+
|
| 26 |
+
Ask about:
|
| 27 |
+
- Statutory interpretation
|
| 28 |
+
- Case law analysis
|
| 29 |
+
- Legal concepts and definitions
|
| 30 |
+
- Compliance questions
|
| 31 |
+
- Contract review concepts
|
| 32 |
+
|
| 33 |
+
**Note**: This provides informational analysis only, not legal advice. Consult qualified legal professionals for actual legal matters.
|
app.py
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import spaces
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# Load model and tokenizer
|
| 7 |
+
model_id = "Equall/SaulLM-141B-Instruct"
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 10 |
+
model_id,
|
| 11 |
+
torch_dtype=torch.bfloat16,
|
| 12 |
+
device_map="auto"
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
@spaces.GPU()
|
| 16 |
+
def generate_response(message, history, system_prompt, max_tokens, temperature):
|
| 17 |
+
"""Generate legal analysis using Saul-141B"""
|
| 18 |
+
|
| 19 |
+
# Build conversation history
|
| 20 |
+
messages = []
|
| 21 |
+
|
| 22 |
+
if system_prompt:
|
| 23 |
+
messages.append({"role": "system", "content": system_prompt})
|
| 24 |
+
|
| 25 |
+
for human, assistant in history:
|
| 26 |
+
messages.append({"role": "user", "content": human})
|
| 27 |
+
messages.append({"role": "assistant", "content": assistant})
|
| 28 |
+
|
| 29 |
+
messages.append({"role": "user", "content": message})
|
| 30 |
+
|
| 31 |
+
# Format for model
|
| 32 |
+
input_text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 33 |
+
inputs = tokenizer(input_text, return_tensors="pt").to(model.device)
|
| 34 |
+
|
| 35 |
+
# Generate
|
| 36 |
+
outputs = model.generate(
|
| 37 |
+
**inputs,
|
| 38 |
+
max_new_tokens=max_tokens,
|
| 39 |
+
temperature=temperature,
|
| 40 |
+
do_sample=temperature > 0,
|
| 41 |
+
pad_token_id=tokenizer.eos_token_id
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
response = tokenizer.decode(outputs[0][inputs['input_ids'].shape[1]:], skip_special_tokens=True)
|
| 45 |
+
return response
|
| 46 |
+
|
| 47 |
+
# Default system prompt for legal queries
|
| 48 |
+
DEFAULT_SYSTEM = """You are SaulLM-141B, a specialized legal language model. You provide accurate legal analysis based on U.S. and European legal systems.
|
| 49 |
+
|
| 50 |
+
IMPORTANT DISCLAIMERS:
|
| 51 |
+
- This is for informational purposes only, not legal advice
|
| 52 |
+
- Information may not reflect recent legal developments
|
| 53 |
+
- Users should consult qualified legal professionals for actual legal advice
|
| 54 |
+
- Do not use this for decisions that could affect legal rights"""
|
| 55 |
+
|
| 56 |
+
# Build interface
|
| 57 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 58 |
+
gr.Markdown("# SaulLM-141B Legal Assistant")
|
| 59 |
+
gr.Markdown("*Specialized AI for legal reasoning and analysis. Private queries, powered by Zero GPU (25 min/day free).*")
|
| 60 |
+
|
| 61 |
+
with gr.Row():
|
| 62 |
+
with gr.Column(scale=3):
|
| 63 |
+
chatbot = gr.Chatbot(label="Legal Analysis", height=500)
|
| 64 |
+
msg = gr.Textbox(
|
| 65 |
+
label="Your Legal Question",
|
| 66 |
+
placeholder="Ask about statutes, case law, legal concepts, or compliance...",
|
| 67 |
+
lines=3
|
| 68 |
+
)
|
| 69 |
+
with gr.Row():
|
| 70 |
+
submit = gr.Button("Submit", variant="primary")
|
| 71 |
+
clear = gr.Button("Clear Chat")
|
| 72 |
+
|
| 73 |
+
with gr.Column(scale=1):
|
| 74 |
+
system_prompt = gr.Textbox(
|
| 75 |
+
label="System Prompt",
|
| 76 |
+
value=DEFAULT_SYSTEM,
|
| 77 |
+
lines=12,
|
| 78 |
+
max_lines=12
|
| 79 |
+
)
|
| 80 |
+
max_tokens = gr.Slider(
|
| 81 |
+
label="Max Response Tokens",
|
| 82 |
+
minimum=100,
|
| 83 |
+
maximum=2000,
|
| 84 |
+
value=1000,
|
| 85 |
+
step=100
|
| 86 |
+
)
|
| 87 |
+
temperature = gr.Slider(
|
| 88 |
+
label="Temperature",
|
| 89 |
+
minimum=0.0,
|
| 90 |
+
maximum=1.0,
|
| 91 |
+
value=0.7,
|
| 92 |
+
step=0.1
|
| 93 |
+
)
|
| 94 |
+
gr.Markdown("### Usage Tips")
|
| 95 |
+
gr.Markdown("""
|
| 96 |
+
- Be specific about jurisdiction
|
| 97 |
+
- Cite relevant statutes/cases if known
|
| 98 |
+
- Zero GPU resets after 60s idle
|
| 99 |
+
- 25 min/day free compute limit
|
| 100 |
+
""")
|
| 101 |
+
|
| 102 |
+
def user_submit(message, history):
|
| 103 |
+
return "", history + [[message, None]]
|
| 104 |
+
|
| 105 |
+
def bot_respond(history, system_prompt, max_tokens, temperature):
|
| 106 |
+
message = history[-1][0]
|
| 107 |
+
history_context = history[:-1]
|
| 108 |
+
|
| 109 |
+
response = generate_response(message, history_context, system_prompt, max_tokens, temperature)
|
| 110 |
+
history[-1][1] = response
|
| 111 |
+
return history
|
| 112 |
+
|
| 113 |
+
msg.submit(user_submit, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 114 |
+
bot_respond, [chatbot, system_prompt, max_tokens, temperature], chatbot
|
| 115 |
+
)
|
| 116 |
+
submit.click(user_submit, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 117 |
+
bot_respond, [chatbot, system_prompt, max_tokens, temperature], chatbot
|
| 118 |
+
)
|
| 119 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 120 |
+
|
| 121 |
+
if __name__ == "__main__":
|
| 122 |
+
demo.queue().launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|
| 3 |
+
spaces
|
| 4 |
+
accelerate
|