Streamlit app
Browse files
app.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import torch
|
3 |
+
from transformers import(
|
4 |
+
T5TokenizerFast as T5Tokenizer)
|
5 |
+
import warnings
|
6 |
+
warnings.filterwarnings("ignore")
|
7 |
+
|
8 |
+
MODEL_NAME = "t5-small"
|
9 |
+
tokenizer = T5Tokenizer.from_pretrained(MODEL_NAME)
|
10 |
+
device = torch.device('cpu')
|
11 |
+
model = torch.load('models.pth', map_location=device)
|
12 |
+
|
13 |
+
|
14 |
+
def summarize(text):
|
15 |
+
text_encoding = tokenizer(
|
16 |
+
text,
|
17 |
+
max_length=512,
|
18 |
+
padding="max_length",
|
19 |
+
truncation=True,
|
20 |
+
return_attention_mask=True,
|
21 |
+
add_special_tokens=True,
|
22 |
+
return_tensors="pt")
|
23 |
+
|
24 |
+
generated_ids = model.generate(
|
25 |
+
input_ids=text_encoding["input_ids"],
|
26 |
+
attention_mask=text_encoding["attention_mask"],
|
27 |
+
max_length=150,
|
28 |
+
num_beams=2,
|
29 |
+
repetition_penalty=2.5,
|
30 |
+
length_penalty=1.0,
|
31 |
+
early_stopping=True)
|
32 |
+
|
33 |
+
preds = [
|
34 |
+
tokenizer.decode(gen_id, skip_special_tokens = True, clean_up_tokenization_spaces=True)
|
35 |
+
for gen_id in generated_ids
|
36 |
+
]
|
37 |
+
|
38 |
+
return "".join(preds)
|
39 |
+
|
40 |
+
|
41 |
+
def main():
|
42 |
+
"""Text Summarizer app with streamlit"""
|
43 |
+
st.title("T5 text summarizer with streamlit")
|
44 |
+
st.subheader("Summarize your 512 words here!")
|
45 |
+
message = st.text_area("Enter your text", "Type Here")
|
46 |
+
if st.button("Summarize text"):
|
47 |
+
summary_results = summarize(message)
|
48 |
+
|
49 |
+
|
50 |
+
if __name__ == '__main__':
|
51 |
+
main()
|