awacke1 commited on
Commit
d8ea28c
Β·
1 Parent(s): 8e91ba0

Create v1-app.py

Browse files
Files changed (1) hide show
  1. v1-app.py +59 -0
v1-app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import tweepy as tw
3
+ import pandas as pd
4
+ from transformers import pipeline
5
+ import os
6
+
7
+ 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
+ with st.form(key='Enter name'):
37
+ search_words = st.text_input('Enter a word or phrase you want to know about')
38
+ number_of_tweets = st.number_input('How many tweets do you want to see? (maximum 50)', 0,50,10)
39
+ submit_button = st.form_submit_button(label='Submit')
40
+
41
+ if submit_button:
42
+ tweets =tw.Cursor(api.search_tweets,q=search_words,lang="en").items(number_of_tweets)
43
+ tweet_list = [i.text for i in tweets]
44
+ p = [i for i in classifier(tweet_list)]
45
+ q=[p[i]['label'] for i in range(len(p))]
46
+ df = pd.DataFrame(list(zip([search_words], [number_of_tweets], [tweet_list], [pd.Timestamp.now()])),columns = HEADERS)
47
+ st.write(pd.DataFrame(list(zip(tweet_list, q)),columns =['Latest '+str(number_of_tweets)+' Tweets'+' on '+search_words, 'sentiment']))
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
+
58
+ if __name__=='__main__':
59
+ run()