File size: 8,632 Bytes
6a43aa5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fd9d6b
6a43aa5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fd9d6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a43aa5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fd9d6b
6a43aa5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b5f436
7483419
 
6a43aa5
 
 
 
 
 
7483419
6a43aa5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fd9d6b
6a43aa5
 
 
2fd9d6b
6a43aa5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7483419
6a43aa5
 
 
 
 
 
 
 
 
 
 
 
7483419
6a43aa5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/env python
# coding: utf-8

# In[1]:


import validators, re
from fake_useragent import UserAgent
from bs4 import BeautifulSoup   
import streamlit as st
from transformers import pipeline
import time
import base64 
import requests
import docx2txt
from io import StringIO
from PyPDF2 import PdfFileReader
import warnings
from nltk import sent_tokenize
warnings.filterwarnings("ignore")


# In[2]:

time_str = time.strftime("%d%m%Y-%H%M%S")
#Functions

def article_text_extractor(url: str):
    
    '''Extract text from url and divide text into chunks if length of text is more than 500 words'''
    
    ua = UserAgent()

    headers = {'User-Agent':str(ua.chrome)}

    r = requests.get(url,headers=headers)
    
    soup = BeautifulSoup(r.text, "html.parser")
    title_text = soup.find_all(["h1"])
    para_text = soup.find_all(["p"])
    article_text = [result.text for result in para_text]
    article_header = [result.text for result in title_text][0]
    article = " ".join(article_text)
    article = article.replace(".", ".<eos>")
    article = article.replace("!", "!<eos>")
    article = article.replace("?", "?<eos>")
    sentences = article.split("<eos>")
    
    current_chunk = 0
    chunks = []
    
    for sentence in sentences:
        if len(chunks) == current_chunk + 1:
            if len(chunks[current_chunk]) + len(sentence.split(" ")) <= 500:
                chunks[current_chunk].extend(sentence.split(" "))
            else:
                current_chunk += 1
                chunks.append(sentence.split(" "))
        else:
            print(current_chunk)
            chunks.append(sentence.split(" "))

    for chunk_id in range(len(chunks)):
        chunks[chunk_id] = " ".join(chunks[chunk_id])

    return article_header, chunks
 
def chunk_clean_text(text):

    sentences = sent_tokenize(text)
    current_chunk = 0
    chunks = []
    
    for sentence in sentences:
        if len(chunks) == current_chunk + 1:
            if len(chunks[current_chunk]) + len(sentence.split(" ")) <= 500:
                chunks[current_chunk].extend(sentence.split(" "))
            else:
                current_chunk += 1
                chunks.append(sentence.split(" "))
        else:
            print(current_chunk)
            chunks.append(sentence.split(" "))
    
    for chunk_id in range(len(chunks)):
        chunks[chunk_id] = " ".join(chunks[chunk_id])
    
    return chunks
    
def preprocess_plain_text(x):

    x = x.encode("ascii", "ignore").decode()  # unicode
    x = re.sub(r"https*\S+", " ", x)  # url
    x = re.sub(r"@\S+", " ", x)  # mentions
    x = re.sub(r"#\S+", " ", x)  # hastags
    x = re.sub(r"\s{2,}", " ", x)  # over spaces
    x = re.sub("[^.,!?A-Za-z0-9]+", " ", x)  # special charachters except .,!?

    return x

def extract_pdf(file):
    
    '''Extract text from PDF file'''
    
    pdfReader = PdfFileReader(file)
    count = pdfReader.numPages
    all_text = ""
    for i in range(count):
        page = pdfReader.getPage(i)
        all_text += page.extractText()
    

    return all_text


def extract_text_from_file(file):
    
    '''Extract text from uploaded file'''

    # read text file
    if file.type == "text/plain":
        # To convert to a string based IO:
        stringio = StringIO(file.getvalue().decode("utf-8"))

        # To read file as string:
        file_text = stringio.read()

    # read pdf file
    elif file.type == "application/pdf":
        file_text = extract_pdf(file)

    # read docx file
    elif (
        file.type
        == "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
    ):
        file_text = docx2txt.process(file)

    return file_text

def summary_downloader(raw_text):
    
	b64 = base64.b64encode(raw_text.encode()).decode()
	new_filename = "new_text_file_{}_.txt".format(time_str)
	st.markdown("#### Download Summary as a File ###")
	href = f'<a href="data:file/txt;base64,{b64}" download="{new_filename}">Click to Download!!</a>'
	st.markdown(href,unsafe_allow_html=True)

@st.cache(allow_output_mutation=True)
def facebook_model():
    
    summarizer = pipeline('summarization',model='facebook/bart-large-cnn')
    return summarizer
    
@st.cache(allow_output_mutation=True)
def schleifer_model():
    
    summarizer = pipeline('summarization',model='sshleifer/distilbart-cnn-12-6')
    return summarizer
    
#Streamlit App
    
st.title("Article Text and Link Extractive Summarizer 📝")

model_type = st.sidebar.selectbox(
    "Model type", options=["Facebook-Bart", "Sshleifer-DistilBart"]
)

max_len= st.sidebar.slider("Maximum length of the summarized text",min_value=100,max_value=500)
min_len= st.sidebar.slider("Minimum length of the summarized text",min_value=30)

st.markdown(
    "Model Source: [Facebook-Bart-large-CNN](https://huggingface.co/facebook/bart-large-cnn) and [Sshleifer-distilbart-cnn-12-6](https://huggingface.co/sshleifer/distilbart-cnn-12-6)"
)

st.markdown(
    """The app supports extractive summarization which aims to identify the salient information that is then extracted and grouped together to form a concise summary. 
    For documents or text that is more than 500 words long, the app will divide the text into chunks and summarize each chunk. Please note when using the sidebar slider, those values represent the min/max text length per chunk of text to be summarized. If your article to be summarized is 1000 words, it will be divided into two chunks of 500 words first then the default max length of 100 words is applied per chunk, resulting in a summarized text with 200 words maximum. 
    There are two models available to choose from:""")

st.markdown("""   
    - Facebook-Bart, trained on large [CNN and Daily Mail](https://huggingface.co/datasets/cnn_dailymail) news articles.
    - Sshleifer-Distilbart, which is a distilled (smaller) version of the large Bart model."""
)

st.markdown("""Please do note that the model will take longer to generate summaries for documents that are too long.""")

st.markdown(
    "The app only ingests the below formats for summarization task:"
)
st.markdown(
    """- Raw text entered in text box. 
- URL of an article to be summarized. 
- Documents with .txt, .pdf or .docx file formats."""
)

st.markdown("---")

url_text = st.text_input("Please Enter a url here")


st.markdown(
    "<h3 style='text-align: center; color: red;'>OR</h3>",
    unsafe_allow_html=True,
)

plain_text = st.text_input("Please Paste/Enter plain text here")

st.markdown(
    "<h3 style='text-align: center; color: red;'>OR</h3>",
    unsafe_allow_html=True,
)

upload_doc = st.file_uploader(
    "Upload a .txt, .pdf, .docx file for summarization"
)

is_url = validators.url(url_text)

if is_url:
    # complete text, chunks to summarize (list of sentences for long docs)
    article_title,chunks = article_text_extractor(url=url_text)
    
elif upload_doc:
    
    clean_text = chunk_clean_text(preprocess_plain_text(extract_text_from_file(upload_doc)))

else:
    
    clean_text = chunk_clean_text(preprocess_plain_text(plain_text))

summarize = st.button("Summarize")

# called on toggle button [summarize]
if summarize:
    if model_type == "Facebook-Bart":
        if is_url:
            text_to_summarize = chunks
        else:
            text_to_summarize = clean_text

        with st.spinner(
            text="Loading Facebook-Bart Model and Extracting summary. This might take a few seconds depending on the length of your text..."
        ):
            summarizer_model = facebook_model()
            summarized_text = summarizer_model(text_to_summarize, max_length=max_len, min_length=min_len)
            summarized_text = ' '.join([summ['summary_text'] for summ in summarized_text])
    
    elif model_type == "Sshleifer-DistilBart":
        if is_url:
            text_to_summarize = chunks
        else:
            text_to_summarize = clean_text

        with st.spinner(
            text="Loading Sshleifer-DistilBart Model and Extracting summary. This might take a few seconds depending on the length of your text..."
        ):
            summarizer_model = schleifer_model()
            summarized_text = summarizer_model(text_to_summarize, max_length=max_len, min_length=min_len)
            summarized_text = ' '.join([summ['summary_text'] for summ in summarized_text])       
    
    # final summarized output
    st.subheader("Summarized text")
    
    if is_url:
    
        # view summarized text (expander)
        st.markdown(f"Article title: {article_title}")
        
    st.write(summarized_text)
    
    summary_downloader(summarized_text)


# In[ ]: