awacke1 commited on
Commit
8066f29
·
1 Parent(s): 1ed60ff

Create new file

Browse files
Files changed (1) hide show
  1. app.py +32 -0
app.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import tweepy as tw
3
+ import pandas as pd
4
+ from transformers import pipeline
5
+ consumer_key = 'OCgWzDW6PaBvBeVimmGBqdAg1'
6
+ consumer_secret = 'tBKnmyg5Jfsewkpmw74gxHZbbZkGIH6Ee4rsM0lD1vFL7SrEIM'
7
+ access_token = '1449663645412065281-LNjZoEO9lxdtxPcmLtM35BRdIKYHpk'
8
+ access_token_secret = 'FL3SGsUWSzPVFnG7bNMnyh4vYK8W1SlABBNtdF7Xcbh7a'
9
+ auth = tw.OAuthHandler(consumer_key, consumer_secret)
10
+ auth.set_access_token(access_token, access_token_secret)
11
+ api = tw.API(auth, wait_on_rate_limit=True)
12
+ classifier = pipeline('sentiment-analysis')
13
+
14
+ st.title('Live Twitter Sentiment Analysis with Tweepy and HuggingFace Transformers')
15
+ st.markdown('This app uses tweepy to get tweets from twitter based on the input name/phrase. It then processes the tweets through HuggingFace transformers pipeline function for sentiment analysis. The resulting sentiments and corresponding tweets are then put in a dataframe for display which is what you see as result.')
16
+
17
+ def run():
18
+
19
+ with st.form(key='Enter name'):
20
+ search_words = st.text_input('Enter the name for which you want to know the sentiment')
21
+ number_of_tweets = st.number_input('Enter the number of latest tweets for which you want to know the sentiment(Maximum 50 tweets)', 0,50,10)
22
+ submit_button = st.form_submit_button(label='Submit')
23
+ if submit_button:
24
+ tweets =tw.Cursor(api.search_tweets,q=search_words,lang="en").items(number_of_tweets)
25
+ tweet_list = [i.text for i in tweets]
26
+ p = [i for i in classifier(tweet_list)]
27
+ q=[p[i]['label'] for i in range(len(p))]
28
+ df = pd.DataFrame(list(zip(tweet_list, q)),columns =['Latest '+str(number_of_tweets)+' Tweets'+' on '+search_words, 'sentiment'])
29
+ st.write(df)
30
+
31
+ if __name__=='__main__':
32
+ run()