rajesh1729 commited on
Commit
51c254d
1 Parent(s): e639871

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -0
app.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import tweepy as tw
3
+ import pandas as pd
4
+ from transformers import pipeline
5
+
6
+
7
+ consumer_key = 'OCgWzDW6PaBvBeVimmGBqdAg1'
8
+ consumer_secret = 'tBKnmyg5Jfsewkpmw74gxHZbbZkGIH6Ee4rsM0lD1vFL7SrEIM'
9
+ access_token = '1449663645412065281-LNjZoEO9lxdtxPcmLtM35BRdIKYHpk'
10
+ access_token_secret = 'FL3SGsUWSzPVFnG7bNMnyh4vYK8W1SlABBNtdF7Xcbh7a'
11
+ auth = tw.OAuthHandler(consumer_key, consumer_secret)
12
+ auth.set_access_token(access_token, access_token_secret)
13
+ api = tw.API(auth, wait_on_rate_limit=True)
14
+ classifier = pipeline('sentiment-analysis')
15
+
16
+ st.title('Live Twitter Sentiment Analysis with Tweepy and HuggingFace Transformers')
17
+ 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.')
18
+
19
+ def run():
20
+
21
+ with st.form(key='Enter name'):
22
+ search_words = st.text_input('Enter the name for which you want to know the sentiment')
23
+ 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)
24
+ submit_button = st.form_submit_button(label='Submit')
25
+ if submit_button:
26
+ tweets =tw.Cursor(api.search_tweets,q=search_words,lang="en").items(number_of_tweets)
27
+ tweet_list = [i.text for i in tweets]
28
+ p = [i for i in classifier(tweet_list)]
29
+ q=[p[i]['label'] for i in range(len(p))]
30
+ df = pd.DataFrame(list(zip(tweet_list, q)),columns =['Latest '+str(number_of_tweets)+' Tweets'+' on '+search_words, 'sentiment'])
31
+ st.write(df)
32
+
33
+ if __name__=='__main__':
34
+ run()