Jainesh212 commited on
Commit
ba57d74
ยท
1 Parent(s): fe711c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +179 -45
app.py CHANGED
@@ -1,59 +1,193 @@
1
- from textblob import TextBlob
2
- import pandas as pd
3
  import streamlit as st
4
- import cleantext
5
 
 
 
 
 
 
 
 
6
 
7
- st.header('Sentiment Analysis')
8
- with st.expander('Analyze Text'):
9
- text = st.text_input('Text here: ')
10
- if text:
11
- blob = TextBlob(text)
12
- st.write('Polarity: ', round(blob.sentiment.polarity,2))
13
- st.write('Subjectivity: ', round(blob.sentiment.subjectivity,2))
14
 
 
 
 
 
 
15
 
16
- pre = st.text_input('Clean Text: ')
17
- if pre:
18
- st.write(cleantext.clean(pre, clean_all= False, extra_spaces=True ,
19
- stopwords=True ,lowercase=True ,numbers=True , punct=True))
20
 
21
- with st.expander('Analyze CSV'):
22
- upl = st.file_uploader('Upload file')
23
 
24
- def score(x):
25
- blob1 = TextBlob(x)
26
- return blob1.sentiment.polarity
27
 
28
- #
29
- def analyze(x):
30
- if x >= 0.5:
31
- return 'Positive'
32
- elif x <= -0.5:
33
- return 'Negative'
34
- else:
35
- return 'Neutral'
36
 
37
- #
38
- if upl:
39
- df = pd.read_excel(upl)
40
- del df['Unnamed: 0']
41
- df['score'] = df['tweets'].apply(score)
42
- df['analysis'] = df['score'].apply(analyze)
43
- st.write(df.head(10))
44
 
45
- @st.cache
46
- def convert_df(df):
47
- # IMPORTANT: Cache the conversion to prevent computation on every rerun
48
- return df.to_csv().encode('utf-8')
49
 
50
- csv = convert_df(df)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
- st.download_button(
53
- label="Download data as CSV",
54
- data=csv,
55
- file_name='sentiment.csv',
56
- mime='text/csv',
57
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
 
59
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
 
2
 
3
+ import matplotlib.pyplot as plt
4
+ import numpy as np
5
+ import pandas as pd
6
+ from PIL import Image
7
+ import io
8
+ from wordcloud import STOPWORDS, ImageColorGenerator, WordCloud
9
+ import warnings
10
 
11
+ #### Main functions import from utils: ###
 
 
 
 
 
 
12
 
13
+ from utils.get_text import get_text
14
+ from utils.preprocess_text import preprocess_text
15
+ from utils.sentiment_analysis import sentiment_analysis
16
+ from utils.name_ent_recognition import name_ent_recognition
17
+ from utils.screenshot import screenshot
18
 
 
 
 
 
19
 
20
+ ## Warnings ignore ###
 
21
 
22
+ warnings.filterwarnings(action='ignore')
23
+ st.set_option('deprecation.showfileUploaderEncoding', False)
24
+ st.set_option('deprecation.showPyplotGlobalUse', False)
25
 
 
 
 
 
 
 
 
 
26
 
 
 
 
 
 
 
 
27
 
28
+ ### App layout ###
 
 
 
29
 
30
+ def main():
31
+ # create a wide layout
32
+ st.set_page_config(
33
+ page_title="Scrapify ยฆ Web Page Analysis For Content Writers",
34
+ page_icon="๐Ÿ“ฐ",
35
+ initial_sidebar_state="expanded",
36
+ layout="wide")
37
+
38
+
39
+ # create a sidebar menu
40
+ option = st.sidebar.selectbox("Select an option", ["Home", "Webpage Text Scraper", "Word Cloud", "Sentiment Analysis", "Name Entity Recognition", "Screenshot"])
41
+
42
+
43
+ if option == "Home":
44
+
45
+
46
+ # add an image
47
+ display = image = Image.open("img/Sport+Performance+Analysis+-+Web+Scraping+21.png")
48
+ display = np.array(display)
49
+ st.image(display, width=500)
50
+ st.title("Scrapyfy\n")
51
+ st.success('**Web Page Text Analysys App** for Content Writers, Bloggers, to monitor the topics that are being discussed on the webpage.')
52
+
53
+ st.markdown('''This streamlit app will be used to scrape webpage, extract the text and create a wordcloud from it, the wordcloud can be also saved as an image file.
54
+ The app also will be used to create a sentiment analysis of the text and Name Entity Recognition. The multiple features are accessible from the sidebar menu on the left.\n
55
+ The app will also be used to create a screenshot and download it. The app is useful for content writers, bloggers, and anyone who wants to create a wordcloud from a webpage,
56
+ to monitor the topics that are being discussed in the webpage.\n''')
57
 
58
+ st.subheader('''**๐Ÿงพ The features of the app you will find on the sidebar menu on the left are:**\n''')
59
+
60
+ st.success('''- **Webpage Text Scraper:** Scrape the text from the webpage and show the first ten lines of the text in a table.\n
61
+ - **Word Cloud:** Create a Wordcloud from the text scraped from the webpage.\n
62
+ - **Sentiment Analysis:** Create a sentiment analysis of the text scraped from the webpage.\n
63
+ - **Name Entity Recognition:** Create a name entity recognition of the text scraped from the webpage.\n
64
+ - **Screenshot:** Create a screenshot of the webpage and download it.\n''')
65
+ st.info('''๐Ÿ‘ˆ๐Ÿ‘ˆStart playing selecting the option from the sidebar menu on the left.๐Ÿ‘ˆ๐Ÿ‘ˆ\n''')
66
+
67
+ # create a footer
68
+ st.caption('''App created by [**@marcellodichiera**](https://marcello-personal-website.netlify.app/)''')
69
+ ## Webpage Text Scraper
70
+ elif option == "Webpage Text Scraper":
71
+ st.title("Webpage Scraper test")
72
+ st.markdown('##### This option lets you scrape a webpage, extract the text from it and show the first 10 lines in a table and download the text as a csv file.')
73
+
74
+ # create a text box to enter the URL
75
+ URL = st.text_input("Enter the URL of the webpage you want to scrape")
76
+ # create a button to scrape the text from the webpage
77
+ if URL is not None:
78
+ if st.button("Scrape"):
79
+ text = get_text(URL)
80
+ df = pd.DataFrame(text.splitlines(), columns=["Webpage_text"], index=None)
81
+ # show the text in the dataframe
82
+ st.markdown('## Showing the first ten lines of the text')
83
+ st.dataframe(df.head(10))
84
+ # download the text as a csv file
85
+ st.info('''Download the text as a csv file if you like.''')
86
+ st.download_button(label="Download the text as a csv file",
87
+ data=df.to_csv(index=False, encoding='utf-8'),
88
+ file_name='webpage_text.csv',
89
+ mime='text/csv')
90
+
91
+ else:
92
+ st.warning("Please enter a valid URL")
93
+
94
+ # Word Cloud
95
+ elif option == "Word Cloud":
96
+ st.title("Word Cloud")
97
+ st.markdown('##### This option lets you create a wordcloud text from a webpage')
98
+
99
+ # create a text box to enter the URL
100
+ URL = st.text_input("Enter the URL of the webpage you want to scrape")
101
+ # create a button to scrape the text from the webpage
102
+ if URL is not None:
103
+
104
+ if st.button("Scrape"):
105
+ text = get_text(URL)
106
+ # create a wordcloud
107
+ wordcloud = WordCloud(width=800, height=400, background_color="white", stopwords=STOPWORDS).generate(text)
108
+ # wordcloud title
109
+ st.info('#### Wordcloud of {}'.format(URL))
110
+ plt.figure(figsize=(10, 5))
111
+ plt.imshow(wordcloud, interpolation="bilinear")
112
+ plt.axis("off")
113
+ # show the wordcloud
114
+ st.pyplot()
115
+
116
+
117
+ else:
118
+ st.warning("Please enter a valid URL")
119
+
120
+ # Sentiment Analysis
121
+
122
+ elif option == "Sentiment Analysis":
123
+ st.title("Sentiment Analysis")
124
+ st.markdown('##### This option lets you create a sentiment analysis of the text from a webpage')
125
+
126
+ # create a text box to enter the URL
127
+ URL = st.text_input("Enter the URL of the webpage you want to scrape")
128
+ # create a button to scrape the text from the webpage
129
+ if URL is not None:
130
+ if st.button("Scrape"):
131
+ text = get_text(URL)
132
+ # preprocess the text
133
+ #text = preprocess_text(text)
134
+ #get the sentiment score and label
135
+ sentiment_score, sentiment_label = sentiment_analysis(text)
136
+ # show the sentiment score rounded to 2 decimals and label
137
+ st.markdown('#### Sentiment score: {}'.format(round(sentiment_score, 2)))
138
+
139
+ st.markdown('#### Sentiment of web page content is: {}'.format(sentiment_label))
140
+ else:
141
+ st.warning("Please enter a valid URL")
142
+
143
+ # Name Entity Recognition
144
+
145
+ elif option == "Name Entity Recognition":
146
+ st.title("Name Entity Recognition")
147
+ st.markdown('##### This option lets you create a Name Entity Recognition of the text from a webpage')
148
+ st.markdown('Name Entity Recognition is a process of extracting the entities from the text, such as person, location, organization, etc.')
149
+
150
+ # create a text box to enter the URL
151
+ URL = st.text_input("Enter the URL of the webpage you want to scrape")
152
+ # create a button to scrape the text from the webpage
153
+ if URL is not None:
154
+ if st.button("Scrape"):
155
+ text = get_text(URL)
156
+ # preprocess the text
157
+ text = preprocess_text(text)
158
+ #get the name entities
159
+ name_entities = name_ent_recognition(text)
160
+ # show the name entities
161
+ st.markdown('#### Name entities of web page content are: {}'.format(name_entities))
162
+ else:
163
+ st.warning("Please enter a valid URL")
164
+
165
+
166
+
167
+ # Screenshot
168
+
169
+ elif option == 'Screenshot':
170
+ st.title("Screenshot")
171
+ st.markdown('##### This option lets you create a screenshot of a webpage and download it')
172
+
173
+ # create a text box to enter the URL
174
+ URL = st.text_input("Enter the URL of the webpage you want to scrape")
175
+ # create a button to scrape the text from the webpage
176
+ if URL is not None:
177
+ if st.button("Scrape"):
178
+ # get the screenshot
179
+ take_screen = screenshot(URL)
180
+ # show the screenshot
181
+ img_screen = Image.open('page_screen.jpg')
182
+
183
+ st.image(img_screen, caption='Screenshot of the webpage', use_column_width=True)
184
 
185
+
186
 
187
+ else:
188
+ st.warning("Please enter a valid URL")
189
+
190
+ # run the app
191
+
192
+ if __name__ == "__main__":
193
+ main()