Gladiator commited on
Commit
4df3ec6
1 Parent(s): 7b5bdb7

add t5 abstractive summarizer

Browse files
Files changed (1) hide show
  1. app.py +42 -13
app.py CHANGED
@@ -1,24 +1,53 @@
 
1
  import streamlit as st
 
 
2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
  if __name__ == "__main__":
5
- # adding modules to path
6
- import sys
7
- sys.path.append("../extractive_summarizer")
8
  st.title("Text Summarizer 📝")
9
  summarize_type = st.sidebar.selectbox("Summarization type", options=["Extractive", "Abstractive"])
10
 
11
  inp_text = st.text_input("Enter the text here")
12
 
13
- if summarize_type == "Extractive":
14
- from extractive_summarizer.model_processors import Summarizer
 
 
 
15
 
16
- # init model
17
- model = Summarizer()
18
- summarize = st.button("Summarize")
19
- if summarize:
20
- with st.expander("View input text"):
21
- st.write(inp_text)
22
- st.subheader("Summarized text")
23
  summarized_text = model(inp_text, num_sentences=5)
24
- st.info(summarized_text)
 
 
 
 
 
 
 
1
+ import torch
2
  import streamlit as st
3
+ from extractive_summarizer.model_processors import Summarizer
4
+ from transformers import T5Tokenizer, T5ForConditionalGeneration, T5Config
5
 
6
+ def abstractive_summarizer(text : str):
7
+
8
+ model = T5ForConditionalGeneration.from_pretrained('t5-large')
9
+ tokenizer = T5Tokenizer.from_pretrained('t5-large')
10
+ device = torch.device('cpu')
11
+
12
+ preprocess_text = text.strip().replace("\n", "")
13
+ t5_prepared_text = "summarize: " + preprocess_text
14
+ tokenized_text = tokenizer.encode(t5_prepared_text, return_tensors="pt").to(device)
15
+
16
+ # summmarize
17
+ summary_ids = model.generate(tokenized_text,
18
+ num_beams=4,
19
+ no_repeat_ngram_size=2,
20
+ min_length=30,
21
+ max_length=100,
22
+ early_stopping=True)
23
+ abs_summarized_text = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
24
+
25
+ return abs_summarized_text
26
 
27
  if __name__ == "__main__":
28
+
 
 
29
  st.title("Text Summarizer 📝")
30
  summarize_type = st.sidebar.selectbox("Summarization type", options=["Extractive", "Abstractive"])
31
 
32
  inp_text = st.text_input("Enter the text here")
33
 
34
+ # view summarized text (expander)
35
+ with st.expander("View input text"):
36
+ st.write(inp_text)
37
+
38
+ summarize = st.button("Summarize")
39
 
40
+ # called on toggle button [summarize]
41
+ if summarize:
42
+ if summarize_type == "Extractive":
43
+ # extractive summarizer
44
+ # init model
45
+ model = Summarizer()
 
46
  summarized_text = model(inp_text, num_sentences=5)
47
+
48
+ elif summarize_type == "Abstractive":
49
+ summarized_text = abstractive_summarizer(inp_text)
50
+
51
+ # final summarized output
52
+ st.subheader("Summarized text")
53
+ st.info(summarized_text)