Spaces:
Sleeping
Sleeping
namanviber
commited on
Commit
•
1303ec7
1
Parent(s):
cefff63
Update app.py
Browse files
app.py
CHANGED
@@ -1,51 +1,51 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
3 |
-
from peft import PeftModel
|
4 |
-
|
5 |
-
# Loading LED IN Model
|
6 |
-
base_model = "nsi319/legal-led-base-16384"
|
7 |
-
led = AutoModelForSeq2SeqLM.from_pretrained(base_model)
|
8 |
-
adapter_model_in = f"Legal-LED_IN_ABS"
|
9 |
-
led_in = PeftModel.from_pretrained(led, adapter_model_in)
|
10 |
-
led_in_tokenizer = AutoTokenizer.from_pretrained(base_model)
|
11 |
-
|
12 |
-
# Generating Summary
|
13 |
-
def summarize(model, tokenizer, text):
|
14 |
-
input_tokenized = tokenizer.encode(text, return_tensors='pt', max_length=
|
15 |
-
summary_ids = model.generate(input_tokenized, num_beams=4, length_penalty=0.1, min_length=32, max_length=
|
16 |
-
summary = [tokenizer.decode(g, skip_special_tokens=True, clean_up_tokenization_spaces=False) for g in summary_ids][0]
|
17 |
-
return summary
|
18 |
-
|
19 |
-
# Reading Txt File
|
20 |
-
def read_txt_file(file):
|
21 |
-
text = file.read().decode('utf-8')
|
22 |
-
return text
|
23 |
-
|
24 |
-
st.set_page_config(page_title="Legal AI Summarizer", page_icon="img.png")
|
25 |
-
title = "Legal AI Summarizer"
|
26 |
-
col1, col2 = st.columns([1,7])
|
27 |
-
with col1:
|
28 |
-
st.image("img.png")
|
29 |
-
with col2: st.title(title)
|
30 |
-
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.")
|
31 |
-
|
32 |
-
if "user_text" not in st.session_state:
|
33 |
-
st.session_state.user_text = ""
|
34 |
-
|
35 |
-
upload_file = st.file_uploader("Upload a .txt file", type="txt")
|
36 |
-
|
37 |
-
if upload_file is not None:
|
38 |
-
user_text = read_txt_file(upload_file)
|
39 |
-
else:
|
40 |
-
user_text = st.text_area("Paste your legal document here:", value=st.session_state.user_text, height=300)
|
41 |
-
|
42 |
-
if st.button("Generate Summary"):
|
43 |
-
with st.spinner("Generating summary..."):
|
44 |
-
try:
|
45 |
-
summary_text = summarize(led_in, led_in_tokenizer, user_text)
|
46 |
-
st.session_state.user_text = user_text
|
47 |
-
st.write("")
|
48 |
-
st.success(summary_text)
|
49 |
-
print(summary_text)
|
50 |
-
except Exception as e:
|
51 |
st.error(f"An error occurred: {e}")
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
3 |
+
from peft import PeftModel
|
4 |
+
|
5 |
+
# Loading LED IN Model
|
6 |
+
base_model = "nsi319/legal-led-base-16384"
|
7 |
+
led = AutoModelForSeq2SeqLM.from_pretrained(base_model)
|
8 |
+
adapter_model_in = f"Legal-LED_IN_ABS"
|
9 |
+
led_in = PeftModel.from_pretrained(led, adapter_model_in)
|
10 |
+
led_in_tokenizer = AutoTokenizer.from_pretrained(base_model)
|
11 |
+
|
12 |
+
# Generating Summary
|
13 |
+
def summarize(model, tokenizer, text):
|
14 |
+
input_tokenized = tokenizer.encode(text, return_tensors='pt', max_length=4096, truncation=True)
|
15 |
+
summary_ids = model.generate(input_tokenized, num_beams=4, length_penalty=0.1, min_length=32, max_length=256)
|
16 |
+
summary = [tokenizer.decode(g, skip_special_tokens=True, clean_up_tokenization_spaces=False) for g in summary_ids][0]
|
17 |
+
return summary
|
18 |
+
|
19 |
+
# Reading Txt File
|
20 |
+
def read_txt_file(file):
|
21 |
+
text = file.read().decode('utf-8')
|
22 |
+
return text
|
23 |
+
|
24 |
+
st.set_page_config(page_title="Legal AI Summarizer", page_icon="img.png")
|
25 |
+
title = "Legal AI Summarizer"
|
26 |
+
col1, col2 = st.columns([1,7])
|
27 |
+
with col1:
|
28 |
+
st.image("img.png")
|
29 |
+
with col2: st.title(title)
|
30 |
+
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.")
|
31 |
+
|
32 |
+
if "user_text" not in st.session_state:
|
33 |
+
st.session_state.user_text = ""
|
34 |
+
|
35 |
+
upload_file = st.file_uploader("Upload a .txt file", type="txt")
|
36 |
+
|
37 |
+
if upload_file is not None:
|
38 |
+
user_text = read_txt_file(upload_file)
|
39 |
+
else:
|
40 |
+
user_text = st.text_area("Paste your legal document here:", value=st.session_state.user_text, height=300)
|
41 |
+
|
42 |
+
if st.button("Generate Summary"):
|
43 |
+
with st.spinner("Generating summary..."):
|
44 |
+
try:
|
45 |
+
summary_text = summarize(led_in, led_in_tokenizer, user_text)
|
46 |
+
st.session_state.user_text = user_text
|
47 |
+
st.write("")
|
48 |
+
st.success(summary_text)
|
49 |
+
print(summary_text)
|
50 |
+
except Exception as e:
|
51 |
st.error(f"An error occurred: {e}")
|