Spaces:
Sleeping
Sleeping
File size: 2,078 Bytes
1303ec7 3dd595e |
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 |
import streamlit as st
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
from peft import PeftModel
# Loading LED IN Model
base_model = "nsi319/legal-led-base-16384"
led = AutoModelForSeq2SeqLM.from_pretrained(base_model)
adapter_model_in = f"Legal-LED_IN_ABS"
led_in = PeftModel.from_pretrained(led, adapter_model_in)
led_in_tokenizer = AutoTokenizer.from_pretrained(base_model)
# Generating Summary
def summarize(model, tokenizer, text):
input_tokenized = tokenizer.encode(text, return_tensors='pt', max_length=4096, truncation=True)
summary_ids = model.generate(input_tokenized, num_beams=4, length_penalty=0.1, min_length=32, max_length=256)
summary = [tokenizer.decode(g, skip_special_tokens=True, clean_up_tokenization_spaces=False) for g in summary_ids][0]
return summary
# Reading Txt File
def read_txt_file(file):
text = file.read().decode('utf-8')
return text
st.set_page_config(page_title="Legal AI Summarizer", page_icon="img.png")
title = "Legal AI Summarizer"
col1, col2 = st.columns([1,7])
with col1:
st.image("img.png")
with col2: st.title(title)
st.write("Stuck with long legal documents? Our AI summarizer can help! Just copy-paste the text or upload a .txt file, and it will give you a quick and easy summary in plain English, so you can understand the key points without all the legalese.")
if "user_text" not in st.session_state:
st.session_state.user_text = ""
upload_file = st.file_uploader("Upload a .txt file", type="txt")
if upload_file is not None:
user_text = read_txt_file(upload_file)
else:
user_text = st.text_area("Paste your legal document here:", value=st.session_state.user_text, height=300)
if st.button("Generate Summary"):
with st.spinner("Generating summary..."):
try:
summary_text = summarize(led_in, led_in_tokenizer, user_text)
st.session_state.user_text = user_text
st.write("")
st.success(summary_text)
print(summary_text)
except Exception as e:
st.error(f"An error occurred: {e}") |