File size: 17,413 Bytes
6a43aa5
 
 
 
 
b14f109
6a43aa5
 
 
b633e3e
 
 
6a43aa5
 
 
 
 
 
 
90688c1
48467ba
22d1dcc
9c8ceb7
90688c1
 
 
9c8ceb7
6a43aa5
 
 
 
 
 
2081499
 
6a43aa5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b90beb
 
 
 
 
 
 
 
 
7b6e772
 
6a43aa5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fd9d6b
 
7b6e772
 
 
914a677
7b6e772
 
2fd9d6b
 
 
 
 
 
 
 
 
 
 
 
7b6e772
2fd9d6b
 
 
 
6a43aa5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fd9d6b
6a43aa5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b633e3e
 
8c5f28b
6a43aa5
b633e3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85cad61
b633e3e
 
85cad61
b633e3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c8a1518
 
b633e3e
 
7b6e772
b633e3e
 
da04a86
c8a1518
 
 
da04a86
 
54fae71
da04a86
12da133
b633e3e
85cad61
b633e3e
 
 
008800a
b633e3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf69e9e
972218d
b633e3e
 
 
 
972218d
6a43aa5
93c8623
 
 
6a43aa5
 
972218d
6a43aa5
93c8623
 
 
6a43aa5
168a75c
12da133
 
5e7bda8
c250e24
12da133
 
6a43aa5
972218d
b633e3e
 
 
972218d
b633e3e
 
 
 
cf69e9e
 
 
100890e
cf69e9e
b633e3e
6a43aa5
 
f62861c
6a43aa5
 
168a75c
6a43aa5
 
0ca63d5
ee00680
7483419
6a43aa5
fdec697
6a43aa5
 
 
 
7483419
6a43aa5
 
 
 
168a75c
fdec697
6a43aa5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
010b72f
a789bbe
839a86d
 
 
010b72f
6a43aa5
 
 
 
 
 
 
22f7433
6a43aa5
 
 
 
 
 
 
 
 
 
82a9342
 
4f1be9b
6a43aa5
82a9342
43f270e
00822cc
82a9342
6a43aa5
43f270e
00822cc
82a9342
 
 
 
 
 
 
6a43aa5
 
 
 
 
 
b633e3e
b0f397c
6a43aa5
e1bacfa
6a43aa5
 
 
 
 
21aa9ad
12da133
21aa9ad
6a43aa5
 
b633e3e
b0f397c
6a43aa5
e1bacfa
6a43aa5
 
 
 
 
12da133
b633e3e
168a75c
 
 
12da133
 
168a75c
e1bacfa
168a75c
 
7617d05
168a75c
 
12da133
168a75c
6a43aa5
b633e3e
85cad61
b633e3e
21aa9ad
b633e3e
 
 
 
 
839a86d
 
 
3cbde1b
 
839a86d
51f3975
839a86d
 
 
1095427
3cbde1b
 
 
 
 
b633e3e
6a43aa5
b633e3e
6a43aa5
b633e3e
6a43aa5
 
f21add8
 
 
 
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
#!/usr/bin/env python
# coding: utf-8

# In[1]:
import validators, re
import torch
from fake_useragent import UserAgent
from bs4 import BeautifulSoup   
import streamlit as st
from transformers import pipeline, AutoModelForTokenClassification, AutoTokenizer
from sentence_transformers import SentenceTransformer
import en_core_web_lg
import time
import base64 
import requests
import docx2txt
from io import StringIO
from PyPDF2 import PdfFileReader
import warnings
import nltk
import itertools
import numpy as np

nltk.download('punkt')

from nltk import sent_tokenize

warnings.filterwarnings("ignore")


# In[2]:

time_str = time.strftime("%d%m%Y-%H%M%S")
HTML_WRAPPER = """<div style="overflow-x: auto; border: 1px solid #e6e9ef; border-radius: 0.25rem; padding: 1rem; 
margin-bottom: 2.5rem">{}</div> """
#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]
    
    try:
    
        article_header = [result.text for result in title_text][0]
        
    except:
    
        article_header = ''
        
    article = nlp(" ".join(article_text))
    sentences = [i.text for i in list(article.sents)]
    
    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:
            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):
    
    """Chunk text longer than 500 tokens"""
    
    article = nlp(text)
    sentences = [i.text for i in list(article.sents)]
    
    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:
            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)
	
def get_all_entities_per_sentence(text):
    doc = nlp(''.join(text))

    sentences = list(doc.sents)

    entities_all_sentences = []
    for sentence in sentences:
        entities_this_sentence = []

        # SPACY ENTITIES
        for entity in sentence.ents:
            entities_this_sentence.append(str(entity))

        # FLAIR ENTITIES (CURRENTLY NOT USED)
        # sentence_entities = Sentence(str(sentence))
        # tagger.predict(sentence_entities)
        # for entity in sentence_entities.get_spans('ner'):
        #     entities_this_sentence.append(entity.text)

        # XLM ENTITIES
        entities_xlm = [entity["word"] for entity in ner_model(str(sentence))]
        for entity in entities_xlm:
            entities_this_sentence.append(str(entity))

        entities_all_sentences.append(entities_this_sentence)

    return entities_all_sentences

def get_all_entities(text):
    all_entities_per_sentence = get_all_entities_per_sentence(text)
    return list(itertools.chain.from_iterable(all_entities_per_sentence))
    
def get_and_compare_entities(article_content,summary_output):
    
    all_entities_per_sentence = get_all_entities_per_sentence(article_content)
    entities_article = list(itertools.chain.from_iterable(all_entities_per_sentence))
   
    all_entities_per_sentence = get_all_entities_per_sentence(summary_output)
    entities_summary = list(itertools.chain.from_iterable(all_entities_per_sentence))
   
    matched_entities = []
    unmatched_entities = []
    for entity in entities_summary:
        if any(entity.lower() in substring_entity.lower() for substring_entity in entities_article):
            matched_entities.append(entity)
        elif any(
                np.inner(sentence_embedding_model.encode(entity, show_progress_bar=False),
                         sentence_embedding_model.encode(art_entity, show_progress_bar=False)) > 0.9 for
                art_entity in entities_article):
            matched_entities.append(entity)
        else:
            unmatched_entities.append(entity)

    matched_entities = list(dict.fromkeys(matched_entities))
    unmatched_entities = list(dict.fromkeys(unmatched_entities))

    matched_entities_to_remove = []
    unmatched_entities_to_remove = []

    for entity in matched_entities:
        for substring_entity in matched_entities:
            if entity != substring_entity and entity.lower() in substring_entity.lower():
                matched_entities_to_remove.append(entity)

    for entity in unmatched_entities:
        for substring_entity in unmatched_entities:
            if entity != substring_entity and entity.lower() in substring_entity.lower():
                unmatched_entities_to_remove.append(entity)

    matched_entities_to_remove = list(dict.fromkeys(matched_entities_to_remove))
    unmatched_entities_to_remove = list(dict.fromkeys(unmatched_entities_to_remove))

    for entity in matched_entities_to_remove:
        matched_entities.remove(entity)
    for entity in unmatched_entities_to_remove:
        unmatched_entities.remove(entity)

    return matched_entities, unmatched_entities

def highlight_entities(article_content,summary_output):
   
    markdown_start_red = "<mark class=\"entity\" style=\"background: rgb(238, 135, 135);\">"
    markdown_start_green = "<mark class=\"entity\" style=\"background: rgb(121, 236, 121);\">"
    markdown_end = "</mark>"

    matched_entities, unmatched_entities = get_and_compare_entities(article_content,summary_output)
    
    print(summary_output)

    for entity in matched_entities:
        summary_output = re.sub(f'({entity})(?![^rgb\(]*\))',markdown_start_green + entity + markdown_end,summary_output)

    for entity in unmatched_entities:
        summary_output = re.sub(f'({entity})(?![^rgb\(]*\))',markdown_start_red + entity + markdown_end,summary_output)
    
    print("")
    print(summary_output)
    
    print("")
    print(summary_output)
    
    soup = BeautifulSoup(summary_output, features="html.parser")

    return HTML_WRAPPER.format(soup)


def clean_text(text,doc=False,plain_text=False,url=False):
    """Return clean text from the various input sources"""

    if url:
        is_url = validators.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)
        
            return article_title, chunks
        
    elif doc:
        
       clean_text = chunk_clean_text(preprocess_plain_text(extract_text_from_file(text)))
       
       return None, clean_text
    
    elif plain_text:
        
        clean_text = chunk_clean_text(preprocess_plain_text(text))
        
        return None, clean_text
        

@st.experimental_singleton(suppress_st_warning=True)
def get_spacy():
    nlp = en_core_web_lg.load()
    return nlp
    
@st.experimental_singleton(suppress_st_warning=True)
def facebook_model():
    model_name = 'facebook/bart-large-cnn'
    summarizer = pipeline('summarization',model=model_name,tokenizer=model_name,
    device=0 if torch.cuda.is_available() else -1)
    return summarizer
    
@st.experimental_singleton(suppress_st_warning=True)
def schleifer_model():
    model_name = 'sshleifer/distilbart-cnn-12-6'
    summarizer = pipeline('summarization',model=model_name, tokenizer=model_name,
    device=0 if torch.cuda.is_available() else -1)
    return summarizer

@st.experimental_singleton(suppress_st_warning=True)    
def google_model():
    model_name = 'google/pegasus-large'
    summarizer = pipeline('summarization',model=model_name, tokenizer=model_name,
    device=0 if torch.cuda.is_available() else -1)
    return summarizer
    
@st.experimental_singleton(suppress_st_warning=True)
def get_sentence_embedding_model():
    return SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
    
@st.experimental_singleton(suppress_st_warning=True)
def get_ner_pipeline():
    tokenizer = AutoTokenizer.from_pretrained("xlm-roberta-large-finetuned-conll03-english")
    model = AutoModelForTokenClassification.from_pretrained("xlm-roberta-large-finetuned-conll03-english")
    return pipeline("ner", model=model, tokenizer=tokenizer, grouped_entities=True)

# Load all different models (cached) at start time of the hugginface space
sentence_embedding_model = get_sentence_embedding_model()
ner_model = get_ner_pipeline()
nlp = get_spacy()
    
#Streamlit App
    
st.title("Article Text and Link Extractive Summarizer with Entity Matching 📝")

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

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

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

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.
    - Google Pegasus, trained on large C4 and HugeNews articles"""
)

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("---")

if "text_area" not in st.session_state:
    st.session_state.text_area = ''
    
if "summ_area" not in st.session_state:
    st.session_state.summ_area = ''
        
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_area("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"
)

if url_text:
    article_title, cleaned_text = clean_text(url_text, url=True)
    st.session_state.text_area = cleaned_text[0]
    
elif plain_text:
    article_title, cleaned_text = clean_text(plain_text,plain_text=True)
    st.session_state.text_area = ''.join(cleaned_text)

elif upload_doc:
   article_title, cleaned_text = clean_text(upload_doc,doc=True)
   st.session_state.text_area = ''.join(cleaned_text)
   
article_text = st.text_area(
    label='Full Article Text',
    placeholder="Full article text will be displayed here..",
    height=250,
    key='text_area'
)
    
summarize = st.button("Summarize")

# called on toggle button [summarize]
if summarize:
    if model_type == "Facebook-Bart":
        if url_text:
            text_to_summarize =cleaned_text[0]
        else:
            text_to_summarize = cleaned_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,clean_up_tokenization_spaces=True,no_repeat_ngram_size=4)
            summarized_text = ' '.join([summ['summary_text'] for summ in summarized_text])
           
    
    elif model_type == "Sshleifer-DistilBart":
        if url_text:
            text_to_summarize = cleaned_text[0]
        else:
            text_to_summarize = cleaned_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,clean_up_tokenization_spaces=True,no_repeat_ngram_size=4)
            summarized_text = ' '.join([summ['summary_text'] for summ in summarized_text])
            
    elif model_type == "Google-Pegasus":
        if url_text:
            text_to_summarize = cleaned_text[0]
            
        else:
            text_to_summarize = cleaned_text

        with st.spinner(
            text="Loading Google-Pegasus Model and Extracting summary. This might take a few seconds depending on the length of your text..."
        ):
            summarizer_model = google_model()
            summarized_text = summarizer_model(text_to_summarize, max_length=max_len, min_length=min_len,clean_up_tokenization_spaces=True,no_repeat_ngram_size=4)
            summarized_text = ' '.join([summ['summary_text'] for summ in summarized_text])
    
    with st.spinner("Calculating and matching entities, this takes a few seconds..."):
        entity_match_html = highlight_entities(text_to_summarize,summarized_text)
        st.markdown("####")
        print(entity_match_html)
        
        if article_title:

            # view summarized text (expander)
            st.markdown(f"Article title: {article_title}")
        
        st.session_state.summ_area = summarized_text
        
        st.subheader('Summarized Text with no Entity Matching')
        
        summarized_text = st.text_area(
        label = '',
        placeholder="Full summarized text will be displayed here..",
        height=250,
        key='summ_area'
        )
        
        st.markdown("####")     
        
        st.subheader("Summarized text with matched entities in Green and mismatched entities in Red relative to the Original Text")
   
        st.write(entity_match_html, unsafe_allow_html=True)
        
        st.markdown("####")     
    
        summary_downloader(summarized_text)


st.markdown("""
            """)
                        
st.markdown("![visitor badge](https://visitor-badge.glitch.me/badge?page_id=nickmuchi-article-text-summarizer)")
# In[ ]: