Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,46 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from unsloth import FastLanguageModel
|
| 3 |
import torch
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
# 1. Load the
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
)
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
|
| 16 |
-
# 2.
|
| 17 |
def legal_summarizer(legal_text):
|
| 18 |
-
# This template must match what you used during training
|
| 19 |
prompt = f"Analyze the following legal text and provide a grounded summary.\n\nInput:\n{legal_text}\n\nResponse:\n"
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
# Generate the response
|
| 25 |
-
outputs = model.generate(
|
| 26 |
-
**inputs,
|
| 27 |
-
max_new_tokens = 256,
|
| 28 |
-
use_cache = True
|
| 29 |
-
)
|
| 30 |
-
|
| 31 |
-
# Decode and clean the output to show ONLY the response
|
| 32 |
-
decoded = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
|
| 33 |
|
| 34 |
-
|
| 35 |
if "Response:" in decoded:
|
| 36 |
return decoded.split("Response:")[-1].strip()
|
| 37 |
return decoded.strip()
|
| 38 |
|
| 39 |
-
# 3.
|
| 40 |
demo = gr.Interface(
|
| 41 |
fn=legal_summarizer,
|
| 42 |
-
inputs=gr.Textbox(lines=10, label="Paste Legal Text
|
| 43 |
outputs=gr.Textbox(label="LexGuard AI Summary"),
|
| 44 |
-
title="⚖️ LexGuard AI
|
| 45 |
-
description="A fine-tuned Llama-3 model optimized for clarifying complex legal documents.",
|
| 46 |
-
theme="soft"
|
| 47 |
)
|
| 48 |
|
| 49 |
-
# Launch the app
|
| 50 |
if __name__ == "__main__":
|
| 51 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import torch
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
+
from peft import PeftModel
|
| 5 |
|
| 6 |
+
# 1. Load the Base Model and your Adapters using standard Transformers
|
| 7 |
+
base_model_name = "unsloth/Llama-3.2-3B-bnb-4bit" # The base model
|
| 8 |
+
adapter_path = "." # Your uploaded adapter files
|
| 9 |
+
|
| 10 |
+
# Load tokenizer
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained(adapter_path)
|
| 12 |
+
|
| 13 |
+
# Load the base model in 4-bit (CPU compatible)
|
| 14 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 15 |
+
base_model_name,
|
| 16 |
+
torch_dtype=torch.float32,
|
| 17 |
+
device_map="cpu",
|
| 18 |
+
low_cpu_mem_usage=True
|
| 19 |
)
|
| 20 |
|
| 21 |
+
# Merge your adapters onto the base model
|
| 22 |
+
model = PeftModel.from_pretrained(model, adapter_path)
|
| 23 |
|
| 24 |
+
# 2. Reasoning Logic
|
| 25 |
def legal_summarizer(legal_text):
|
|
|
|
| 26 |
prompt = f"Analyze the following legal text and provide a grounded summary.\n\nInput:\n{legal_text}\n\nResponse:\n"
|
| 27 |
+
inputs = tokenizer(prompt, return_tensors="pt").to("cpu")
|
| 28 |
|
| 29 |
+
with torch.no_grad():
|
| 30 |
+
outputs = model.generate(**inputs, max_new_tokens=256)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 33 |
if "Response:" in decoded:
|
| 34 |
return decoded.split("Response:")[-1].strip()
|
| 35 |
return decoded.strip()
|
| 36 |
|
| 37 |
+
# 3. UI Setup
|
| 38 |
demo = gr.Interface(
|
| 39 |
fn=legal_summarizer,
|
| 40 |
+
inputs=gr.Textbox(lines=10, label="Paste Legal Text"),
|
| 41 |
outputs=gr.Textbox(label="LexGuard AI Summary"),
|
| 42 |
+
title="⚖️ LexGuard AI (CPU Edition)"
|
|
|
|
|
|
|
| 43 |
)
|
| 44 |
|
|
|
|
| 45 |
if __name__ == "__main__":
|
| 46 |
demo.launch()
|