-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
146 lines (121 loc) · 4.99 KB
/
app.py
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
# import libraries
from sklearn.feature_extraction.text import TfidfVectorizer
from sumy.parsers.plaintext import PlaintextParser
from sumy.summarizers.lsa import LsaSummarizer
from sumy.nlp.tokenizers import Tokenizer
from sumy.nlp.stemmers import Stemmer
from sumy.utils import get_stop_words
from bs4 import BeautifulSoup
import streamlit as st
import numpy as np
import tldextract
import requests
import spacy
# import en_core_web_sm
# basic things to define as variables and constants
nlp = spacy.load('en_core_web_sm', disable = ['tagger', 'ner'])
# nlp = en_core_web_sm.load(disable = ['tagger', 'ner'])
viable_domain = ['medium', 'towardsdatascience', 'fritz']
LANGUAGE = 'english'
# COUNT = 10
correct = 'Looks like a medium post'
warning = 'This url does not look like it is coming from medium. This may affect the quality of the highlights of the post. Try a medium post url'
error = 'This url does not seem correct. Please paste the correct url'
# to make sure the url is correct
def validate_url(url):
""" Make sure the url given is correct and from medium"""
if requests.get(url).status_code == 200:
ext = tldextract.extract(url)
if ext.domain in viable_domain:
message = correct
else:
message = warning
else:
message = error
return message
#scrape the url given
def scrape_url(url):
"""Scrape the url to get the content of the article"""
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
topic = soup.find('h1')
head = topic.text
fetch = soup.find_all('p')
new = [i.text for i in fetch]
content = ' '.join(new)[:-11]
return head, content
# Summarize the content in the article
@st.cache
def summarize(string, COUNT=10):
""" Summarize the string using sumy"""
new_string = string.replace('.', '. ').strip()
lsa = LsaSummarizer(Stemmer(LANGUAGE))
lsa.stop_words = get_stop_words(LANGUAGE)
parser = PlaintextParser.from_string(new_string, Tokenizer(LANGUAGE))
lsa_summary = lsa(parser.document, COUNT)
lsa_s = [str(sent) for sent in lsa_summary]
summary = ' '.join(lsa_s)
return summary
# Get the vectorize the words in the article
def vectorize(string):
"""Convert string to vector using TDIDF"""
work = nlp(string)
clean_text = []
for word in work:
if not word.is_stop:
if word.is_alpha:
clean_text.append(word.lemma_)
new_string = ' '.join(clean_text)
tf = TfidfVectorizer(stop_words='english', lowercase= False)
# tf = TfidfVectorizer()
out = tf.fit_transform([new_string])
feature_names = np.array(tf.get_feature_names())
return out, feature_names
# Get the key words in the article
def get_top_tf_idf_words(response, top_n, feature_names):
"""get keywords from the td-idf vectors"""
sorted_nzs = np.argsort(response.data)[:-(top_n+1):-1]
return feature_names[response.indices[sorted_nzs]]
# The actual function powering the frontend
def main():
"""Summarizing app"""
st.title('Medium Post Summarizer')
html_temp = """
<div style="background-color:skyblue;padding:15px">
<h2> About Summarizer ML App</h2>
<p> This <b>Extractive Summarizer</b> gives a highlight-summary of an article based on each sentence importance as well as the keywords used. Works best on medium posts</p>
</div>
<br>
"""
st.markdown(html_temp, unsafe_allow_html=True)
url = st.text_input("Enter a Medium post URL")
highlight = st.radio("Change the length of highlights ",("Short Highlights","Long Highlights"))
if st.button("Summarize"):
if url == '':
st.error('Please input a url')
else:
try :
message = validate_url(url)
if message == 'This url does not seem correct. Please paste the correct url':
st.error(message)
elif message == 'This url does not look like it is coming from medium. This may affect the quality of the highlights of the post. Try a medium post url':
st.warning(message)
else:
head, content = scrape_url(url)
out , feature_names = vectorize(content)
keywords = [get_top_tf_idf_words(response,5, feature_names ) for response in out]
if highlight == 'Short Highlights':
summary = summarize(content, 5)
elif highlight == 'Long Highlights':
summary = summarize(content, 10)
st.text(f'\n\n\n')
st.subheader(f'Title : {head} \n\nSummary')
st.info(summary)
st.warning(f'Keywords : {keywords[0][0]}, {keywords[0][1]}, {keywords[0][2]}, {keywords[0][3]}, {keywords[0][4]}')
st.markdown(f'Read full article [here]({url})')
except:
st.error('Check what you pasted')
if st.button("Thanks"):
st.balloons()
if __name__ == '__main__':
main()