File size: 2,614 Bytes
0a476ff 19e70cb cc814c9 0a476ff 19e70cb cc814c9 0a476ff 19e70cb 0a476ff 19e70cb 0a476ff 19e70cb 0a476ff cc814c9 93894a3 cc814c9 0a476ff cc814c9 93894a3 cc814c9 93894a3 cc814c9 0a476ff cc814c9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | import streamlit as st
import torch
from transformers import BartTokenizer, BartForConditionalGeneration
from peft import PeftModel
import textstat
@st.cache_resource
def load_model():
base = BartForConditionalGeneration.from_pretrained(
"facebook/bart-large-cnn",
torch_dtype=torch.float32,
device_map=None
)
model = PeftModel.from_pretrained(base, "./checkpoint")
tokenizer = BartTokenizer.from_pretrained("facebook/bart-large-cnn")
model.to("cpu")
model.eval()
return tokenizer, model
def simplify(text, tokenizer, model):
prompt = f"simplify: {text}"
inputs = tokenizer(prompt, return_tensors="pt", max_length=512, truncation=True)
with torch.inference_mode():
outputs = model.generate(**inputs, max_new_tokens=256, num_beams=4, early_stopping=True)
return tokenizer.decode(outputs[0], skip_special_tokens=True)
st.set_page_config(page_title="Legaleaze", layout="wide")
st.title("Legaleaze: Legal Text Simplifier")
st.caption("BART-Large + LoRA | 121k steps on asylum cases")
try:
tokenizer, model = load_model()
col1, col2 = st.columns(2)
with col1:
st.subheader("Complex Legal Text")
text = st.text_area("", height=300, placeholder="Paste legal text here...", key="input")
btn = st.button("Simplify", type="primary", use_container_width=True)
with col2:
st.subheader("Simplified Output")
if btn and text.strip():
with st.spinner("Simplifying (30s on CPU)..."):
result = simplify(text, tokenizer, model)
st.session_state['result'] = result
st.session_state['original_text'] = text
if 'result' in st.session_state:
# Editable output
simplified = st.text_area("", value=st.session_state['result'], height=300, key="output")
# Copy button
if st.button("📋 Copy to Clipboard", use_container_width=True):
st.write("Copy the text above manually (browser limitation)")
st.divider()
m1, m2, m3 = st.columns(3)
orig = textstat.flesch_kincaid_grade(st.session_state['original_text'])
simp = textstat.flesch_kincaid_grade(simplified)
m1.metric("Original FKGL", f"{orig:.1f}")
m2.metric("Simplified FKGL", f"{simp:.1f}")
m3.metric("Improvement", f"{((orig-simp)/orig*100):.0f}%")
else:
st.info("Simplified text appears here")
except Exception as e:
st.error(f"Error: {e}") |