-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtwitter_analysis.py
36 lines (28 loc) · 1.39 KB
/
twitter_analysis.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
import tweepy as tw
import streamlit as st
import pandas as pd
from transformers import pipeline
api_key = 'RsJhH0JDKWCPo1BNjfeUZ2y4o'
api_key_secret = 'UdDc0w0iRHOEERga0tLFf07a5UwBPrDXrBWqN00tfjqXJKwN7c'
access_token = '1330384842748473344-osAFdHj2ySYTOoDugmonG5RuC6XPKE'
access_token_secret = 'A6uF1RvoOn4oFa6LZ9o3p5Mtz9HvxI7LmMW1vJqhoCx7K'
auth = tw.OAuthHandler(api_key, api_key_secret)
auth.set_access_token(access_token, access_token_secret)
api = tw.API(auth, wait_on_rate_limit=True)
classifier = pipeline('sentiment-analysis')
st.title('Live Twitter Sentiment Analysis')
st.markdown('Get the sentiment labels of live tweets!')
def run():
with st.form(key='Enter name'):
search_words = st.text_input('Enter the topic for which you want to know the sentiment')
no_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)
submit_button = st.form_submit_button(label='Submit')
if submit_button:
tweets = tw.Cursor(api.search_tweets,q=search_words,lang="en").items(no_of_tweets)
tweet_list = [i.text for i in tweets]
output = [i for i in classifier(tweet_list)]
labels =[output[i]['label'] for i in range(len(output))]
df = pd.DataFrame(list(zip(tweet_list, labels)),columns =['Latest '+str(no_of_tweets)+' tweets'+' on '+search_words, 'Sentiment'])
st.write(df)
if __name__=='__main__':
run()