Haleel commited on
Commit
ad6f551
1 Parent(s): f57bb40

cryptweet file added

Browse files
Files changed (2) hide show
  1. app.py +16 -2
  2. cryptweet.py +66 -0
app.py CHANGED
@@ -1,4 +1,18 @@
1
  import streamlit as st
 
 
2
 
3
- x = st.slider('Select a value')
4
- st.write(x, 'squared is', x * x)
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import cryptweet
3
+ st.set_page_config(page_title="Cryptweets",page_icon='https://preview.pixlr.com/images/800wm/100/1/1001457242.jpg')
4
 
5
+ st.title('Cryptweets')
6
+ st.header("Using Twitter API to predict future movement of cryptocurrencies and stocks")
7
+ test='''It is known that social media platforms like twitter plays an imortant role in
8
+ determining the price of financial asset platforms like stock market and crytpocurrencies. It is
9
+ expected to increase the price of Doge after a tweet from Elon musk. Here the sentiment of public
10
+ about a cryptocoin or stock (or any current issue going on!) could be calculated with the help of
11
+ twitter api and huggingface transformers and a little mathematics'''
12
+
13
+ st.subheader(test)
14
+ key = st.text_input(label="Enter the keyword eg:#bitcoin ",)
15
+
16
+ while key!='':
17
+ st.subheader('the given keyword is '+cryptweet.sentimentanalyser(key))
18
+ break
cryptweet.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tweepy
2
+ import json
3
+ !pip -q install transformers
4
+ !pip3 install emoji
5
+ import emoji
6
+ import transformers
7
+ from transformers import pipeline
8
+
9
+ sentimentmodel=pipeline(model="finiteautomata/bertweet-base-sentiment-analysis")
10
+
11
+
12
+
13
+ # authenticate the api key and token with twitter
14
+
15
+ from tweepy.auth import OAuthHandler
16
+
17
+ auth = OAuthHandler('t6d8uj4w9P6DjrNSmfSd42gEI', 'ZLcVXunCbN6NO4rUup6vTR33rO32epm0LFHLkzFLZnhjbQmQzZ')
18
+ auth.set_access_token('1405483945379074054-ktOMmQ6HUcZwzOxiHfjcFmP74hkTn5', 'hV792qfEAuJcvZmkzmoMf61qaaSnoV8D2YiIFYjzAgr06')
19
+
20
+
21
+ api = tweepy.API(auth,wait_on_rate_limit=True)
22
+
23
+
24
+ # extract the tweets with the given keyword
25
+
26
+ def tweextractor(keyword):
27
+ tweet=[] #list to store tweets extracted
28
+
29
+ # search_tweets method extract atmost 100 tweets with given keyword
30
+ # use it with cursor to extract more than that in one go
31
+
32
+ # take 500 popular tweets and 500 recent tweets to keep consistency in the result
33
+
34
+ for i in tweepy.Cursor(api.search_tweets, keyword,tweet_mode="extended",lang='en',result_type='recent',count=100).items(500):
35
+ i = json.dumps(i._json)
36
+ i = json.loads(i)
37
+ if i['full_text'] not in tweet:
38
+ tweet.append(i['full_text'])
39
+ for i in tweepy.Cursor(api.search_tweets, keyword,tweet_mode="extended",lang='en',result_type='popular',count=100).items(500):
40
+ i = json.dumps(i._json)
41
+ i = json.loads(i)
42
+ if i['full_text'] not in tweet and len(i['full_text'])<500:
43
+ tweet.append(i['full_text'])
44
+ print(len(tweet),'tweets total tweets found')
45
+ return tweet
46
+
47
+
48
+
49
+ # method to take keyword and find overall sentiment
50
+
51
+ def sentimentanalyser(keyword):
52
+ tweets=tweextractor(keyword)
53
+ score=[]
54
+ mood=sentimentmodel(tweet)
55
+ for i in mood:
56
+ if i['label']=='POS':
57
+ score.append(i['score'])
58
+ elif i['label']=='NEG':
59
+ score.append(-i['score'])
60
+ sentiment=sum(score)/len(score)
61
+ if sentiment>0:
62
+ return 'Sentiment of public is positive with probability '+sentiment
63
+ else: return 'Sentiment of public is negative with probability'+(-sentiment)
64
+
65
+
66
+