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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -9
app.py CHANGED
@@ -2,7 +2,7 @@ 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'
@@ -14,22 +14,46 @@ 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
- st.title('Live Twitter Sentiment Analysis with Tweepy and HuggingFace Transformers')
18
- 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.')
 
19
 
20
- def run():
 
 
 
 
 
 
21
 
 
 
 
 
 
 
 
22
  with st.form(key='Enter name'):
23
- search_words = st.text_input('Enter the name for which you want to know the sentiment')
24
- 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)
25
  submit_button = st.form_submit_button(label='Submit')
 
26
  if submit_button:
27
  tweets =tw.Cursor(api.search_tweets,q=search_words,lang="en").items(number_of_tweets)
28
  tweet_list = [i.text for i in tweets]
29
  p = [i for i in classifier(tweet_list)]
30
  q=[p[i]['label'] for i in range(len(p))]
31
- df = pd.DataFrame(list(zip(tweet_list, q)),columns =['Latest '+str(number_of_tweets)+' Tweets'+' on '+search_words, 'sentiment'])
32
- st.write(df)
 
 
 
 
 
 
 
 
 
33
 
34
  if __name__=='__main__':
35
- run()
 
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'
 
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()