Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import streamlit as st
|
2 |
import tweepy as tw
|
3 |
import pandas as pd
|
|
|
4 |
from transformers import pipeline
|
5 |
import os
|
6 |
|
@@ -8,50 +9,54 @@ consumer_key = 'OCgWzDW6PaBvBeVimmGBqdAg1'
|
|
8 |
consumer_secret = 'tBKnmyg5Jfsewkpmw74gxHZbbZkGIH6Ee4rsM0lD1vFL7SrEIM'
|
9 |
access_token = '1449663645412065281-LNjZoEO9lxdtxPcmLtM35BRdIKYHpk'
|
10 |
access_token_secret = 'FL3SGsUWSzPVFnG7bNMnyh4vYK8W1SlABBNtdF7Xcbh7a'
|
11 |
-
|
12 |
auth = tw.OAuthHandler(consumer_key, consumer_secret)
|
13 |
auth.set_access_token(access_token, access_token_secret)
|
14 |
api = tw.API(auth, wait_on_rate_limit=True)
|
15 |
classifier = pipeline('sentiment-analysis')
|
16 |
-
|
17 |
-
# Define file name and headers
|
18 |
FILE_NAME = 'query_history.csv'
|
19 |
HEADERS = ['Search Query', 'Number of Tweets', 'Results', 'Date']
|
20 |
|
21 |
-
# Create file if it does not exist
|
22 |
if not os.path.isfile(FILE_NAME):
|
23 |
df = pd.DataFrame(columns=HEADERS)
|
24 |
df.to_csv(FILE_NAME, index=False)
|
25 |
|
26 |
-
# Make program take up full width of screen
|
27 |
st.set_page_config(page_title='π Twitter Sentiment Analysis', layout='wide')
|
28 |
|
29 |
-
# Define function to display history
|
30 |
def display_history():
|
31 |
df = pd.read_csv(FILE_NAME)
|
32 |
st.dataframe(df.style.highlight_max(axis=0))
|
33 |
|
34 |
-
# Define main function
|
35 |
def run():
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
submit_button = st.form_submit_button(label='Submit')
|
40 |
|
41 |
if submit_button:
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
with open(FILE_NAME, mode='a', newline='') as file:
|
49 |
df.to_csv(file, header=False, index=False)
|
50 |
-
|
51 |
if st.button('Clear History'):
|
52 |
os.remove(FILE_NAME)
|
53 |
st.write('History has been cleared.')
|
54 |
-
|
55 |
if st.button('Display History'):
|
56 |
display_history()
|
57 |
|
|
|
1 |
import streamlit as st
|
2 |
import tweepy as tw
|
3 |
import pandas as pd
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
from transformers import pipeline
|
6 |
import os
|
7 |
|
|
|
9 |
consumer_secret = 'tBKnmyg5Jfsewkpmw74gxHZbbZkGIH6Ee4rsM0lD1vFL7SrEIM'
|
10 |
access_token = '1449663645412065281-LNjZoEO9lxdtxPcmLtM35BRdIKYHpk'
|
11 |
access_token_secret = 'FL3SGsUWSzPVFnG7bNMnyh4vYK8W1SlABBNtdF7Xcbh7a'
|
|
|
12 |
auth = tw.OAuthHandler(consumer_key, consumer_secret)
|
13 |
auth.set_access_token(access_token, access_token_secret)
|
14 |
api = tw.API(auth, wait_on_rate_limit=True)
|
15 |
classifier = pipeline('sentiment-analysis')
|
|
|
|
|
16 |
FILE_NAME = 'query_history.csv'
|
17 |
HEADERS = ['Search Query', 'Number of Tweets', 'Results', 'Date']
|
18 |
|
|
|
19 |
if not os.path.isfile(FILE_NAME):
|
20 |
df = pd.DataFrame(columns=HEADERS)
|
21 |
df.to_csv(FILE_NAME, index=False)
|
22 |
|
|
|
23 |
st.set_page_config(page_title='π Twitter Sentiment Analysis', layout='wide')
|
24 |
|
|
|
25 |
def display_history():
|
26 |
df = pd.read_csv(FILE_NAME)
|
27 |
st.dataframe(df.style.highlight_max(axis=0))
|
28 |
|
|
|
29 |
def run():
|
30 |
+
search_words = st.text_input('Enter a word or phrase you want to know about')
|
31 |
+
number_of_tweets = st.number_input('How many tweets do you want to see? (maximum 50)', 1, 50, 50)
|
32 |
+
submit_button = st.form_submit_button(label='Submit')
|
|
|
33 |
|
34 |
if submit_button:
|
35 |
+
unique_tweets, tweet_list, sentiment_list = set(), [], []
|
36 |
+
tweets = tw.Cursor(api.search_tweets, q=search_words, lang="en").items(number_of_tweets)
|
37 |
+
for tweet in tweets:
|
38 |
+
if tweet.text not in unique_tweets:
|
39 |
+
unique_tweets.add(tweet.text)
|
40 |
+
tweet_list.append(tweet.text)
|
41 |
+
p = classifier(tweet.text)
|
42 |
+
sentiment_list.append(p[0]['label'])
|
43 |
+
|
44 |
+
df = pd.DataFrame(list(zip(tweet_list, sentiment_list)), columns=['Tweets', 'Sentiment'])
|
45 |
+
st.write(df)
|
46 |
+
|
47 |
+
summary = df.groupby('Sentiment').size().reset_index(name='Counts')
|
48 |
+
fig, ax = plt.subplots()
|
49 |
+
ax.pie(summary['Counts'], labels=summary['Sentiment'], autopct='%1.1f%%', startangle=90)
|
50 |
+
ax.axis('equal')
|
51 |
+
st.pyplot(fig)
|
52 |
+
|
53 |
with open(FILE_NAME, mode='a', newline='') as file:
|
54 |
df.to_csv(file, header=False, index=False)
|
55 |
+
|
56 |
if st.button('Clear History'):
|
57 |
os.remove(FILE_NAME)
|
58 |
st.write('History has been cleared.')
|
59 |
+
|
60 |
if st.button('Display History'):
|
61 |
display_history()
|
62 |
|