Mythili Sridhar commited on
Commit
d598c34
1 Parent(s): 8816ce4

initial commit

Browse files
README.md CHANGED
@@ -1,8 +1,8 @@
1
  ---
2
  title: TeslaInSpace
3
  emoji: 😻
4
- colorFrom: yellow
5
- colorTo: purple
6
  sdk: streamlit
7
  sdk_version: 1.10.0
8
  app_file: app.py
1
  ---
2
  title: TeslaInSpace
3
  emoji: 😻
4
+ colorFrom: purple
5
+ colorTo: yellow
6
  sdk: streamlit
7
  sdk_version: 1.10.0
8
  app_file: app.py
app.py ADDED
@@ -0,0 +1,207 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # author sridhar iyer
2
+
3
+ from datetime import date
4
+ from datetime import datetime
5
+ import re
6
+
7
+ import numpy as np
8
+ import pandas as pd
9
+ from PIL import Image
10
+ import plotly.express as px
11
+ import plotly.graph_objects as go
12
+ import streamlit as st
13
+ import time
14
+
15
+ from plotly.subplots import make_subplots
16
+
17
+ # Read CSV file into pandas and extract timestamp data
18
+ dfSentiment = pd.read_csv('sentiment_data.csv')### YOUR LINE OF CODE HERE
19
+ dfSentiment['timestamp'] = [datetime.strptime(dt, '%Y-%m-%d') for dt in dfSentiment['timestamp'].tolist()]
20
+
21
+ # Multi-select columns to build chart
22
+ col_list = dfSentiment.columns.values.tolist()### YOUR LINE OF CODE HERE #### Extract columns into a list
23
+
24
+ r_sentiment = re.compile(".*sentiment")
25
+ sentiment_cols = list(filter(r_sentiment.match, col_list))### YOUR LINE OF CODE HERE
26
+
27
+ r_post = re.compile(".*post")
28
+ post_list = list(filter(r_post.match, col_list))### YOUR LINE OF CODE HERE
29
+
30
+ r_perc= re.compile(".*perc")
31
+ perc_list = list(filter(r_perc.match, col_list))
32
+
33
+ r_close = re.compile(".*close")
34
+ close_list = list(filter(r_close.match, col_list))
35
+
36
+ r_volume = re.compile(".*volume")
37
+ volume_list = list(filter(r_volume.match, col_list))
38
+
39
+ sentiment_cols = sentiment_cols + post_list
40
+ stocks_cols = close_list + volume_list ### YOUR LINE OF CODE HERE
41
+
42
+ # Config for page
43
+ st.set_page_config(
44
+ page_title= 'TSLA Sentiment Analyzer Using Huggingface and StreamLit App',### YOUR LINE OF CODE HERE
45
+ page_icon='✅',
46
+ layout='wide',
47
+ )
48
+
49
+ with st.sidebar:
50
+ # FourthBrain logo to sidebar
51
+ fourthbrain_logo = Image.open('./images/fourthbrain_logo.png')
52
+ st.image([fourthbrain_logo], width=300)
53
+
54
+ # Date selection filters
55
+ start_date_filter = st.date_input(
56
+ ### YOUR LINE OF CODE HERE
57
+ 'Start Date',
58
+ min(dfSentiment['timestamp']),
59
+ min_value=min(dfSentiment['timestamp']),
60
+ max_value=max(dfSentiment['timestamp'])
61
+ )
62
+
63
+
64
+ end_date_filter = st.date_input(
65
+ 'End Date',
66
+ max(dfSentiment['timestamp']),
67
+ min_value=min(dfSentiment['timestamp']),
68
+ max_value=max(dfSentiment['timestamp'])
69
+ )
70
+
71
+ sentiment_select = st.selectbox('Select Sentiment Data', sentiment_cols) ### YOUR LINE OF CODE HERE
72
+ stock_select = st.selectbox('Select Stock Data', stocks_cols) ### YOUR LINE OF CODE HERE
73
+
74
+ # Banner with TSLA and Reddit images
75
+ tsla_logo = Image.open('./images/tsla_logo.png')### YOUR LINE OF CODE HERE
76
+ reddit_logo = Image.open('./images/reddit_logo.png')
77
+ st.image([tsla_logo, reddit_logo], width=200)
78
+
79
+ # dashboard title
80
+ ### YOUR LINE OF CODE HERE
81
+ st.title('TSLA Dashboard')
82
+
83
+ ## dataframe filter
84
+ # start date
85
+ dfSentiment = dfSentiment[dfSentiment['timestamp'] >= datetime(start_date_filter.year, start_date_filter.month, start_date_filter.day)]
86
+
87
+ # end date
88
+ dfSentiment = dfSentiment[dfSentiment['timestamp'] <= datetime(end_date_filter.year, end_date_filter.month, end_date_filter.day)]
89
+ dfSentiment = dfSentiment.reset_index(drop=True)
90
+
91
+
92
+ # creating a single-element container
93
+ placeholder = st.empty()### YOUR LINE OF CODE HERE
94
+
95
+ # near real-time / live feed simulation
96
+ for i in range(1, len(dfSentiment)-1):
97
+
98
+ # creating KPIs
99
+ last_close = dfSentiment['close'][i]
100
+ last_close_lag1 = dfSentiment['close'][i-1]
101
+ last_sentiment = dfSentiment['sentiment_score'][i] ### YOUR LINE OF CODE HERE
102
+ last_sentiment_lag1 = dfSentiment['sentiment_score'][i-1]### YOUR LINE OF CODE HERE
103
+
104
+
105
+ with placeholder.container():
106
+
107
+ # create columns
108
+ kpi1, kpi2 = st.columns(2)
109
+
110
+ # fill in those three columns with respective metrics or KPIs
111
+ kpi1.metric(
112
+ label='Sentiment Score',
113
+ value=round(last_sentiment, 3),
114
+ delta=round(last_sentiment_lag1, 3),
115
+ )
116
+
117
+ kpi2.metric(
118
+ label='Last Closing Price',
119
+ ### YOUR LINE 1 OF CODE HERE
120
+ ### YOUR LINE 2 OF CODE HERE
121
+ value=round(last_close),
122
+ delta=round(last_close - last_close_lag1)
123
+ )
124
+
125
+
126
+ # create two columns for charts
127
+ fig_col1, fig_col2 = st.columns(2)
128
+
129
+ with fig_col1:
130
+ # Add traces
131
+ fig=make_subplots(specs=[[{"secondary_y":True}]])
132
+
133
+
134
+ fig.add_trace(
135
+ go.Scatter(
136
+ x=dfSentiment['timestamp'][0:i],
137
+ y=dfSentiment[sentiment_select][0:i],
138
+ name=sentiment_select,
139
+ mode='lines',
140
+ hoverinfo='none',
141
+ )
142
+ )
143
+
144
+ if sentiment_select.startswith('perc') == True:
145
+ yaxis_label = '% Change Sentiment'
146
+
147
+ elif sentiment_select in sentiment_cols:
148
+ yaxis_label = 'Sentiment Score'
149
+
150
+ elif sentiment_select in post_list:
151
+ yaxis_label = 'Volume'
152
+
153
+ fig.layout.yaxis.title=yaxis_label
154
+
155
+ if stock_select.startswith('perc') == True:
156
+ fig.add_trace(
157
+ go.Scatter(
158
+ x=dfSentiment['timestamp'][0:i],
159
+ y=dfSentiment[stock_select][0:i],
160
+ name=stock_select,
161
+ mode='lines',
162
+ hoverinfo='none',
163
+ yaxis='y2',
164
+ )
165
+ )
166
+ fig.layout.yaxis2.title='% Change Stock Price ($US)'
167
+
168
+ elif stock_select == 'volume':
169
+ fig.add_trace(
170
+ go.Scatter(
171
+ x=dfSentiment['timestamp'][0:i],
172
+ y=dfSentiment[stock_select][0:i],
173
+ name=stock_select,
174
+ mode='lines',
175
+ hoverinfo='none',
176
+ yaxis='y2',
177
+ )
178
+ )
179
+
180
+ fig.layout.yaxis2.title="Shares Traded"
181
+
182
+
183
+ else:
184
+ fig.add_trace(
185
+ go.Scatter(
186
+ x=dfSentiment['timestamp'][0:i],
187
+ y=dfSentiment[stock_select][0:i],
188
+ name=stock_select,
189
+ mode='lines',
190
+ hoverinfo='none',
191
+ yaxis='y2',
192
+ )
193
+ )
194
+
195
+ fig.layout.yaxis2.title='Stock Price ($USD)'
196
+
197
+
198
+ fig.layout.xaxis.title='Timestamp'
199
+
200
+ # write the figure throught streamlit
201
+ ### YOUR LINE OF CODE HERE
202
+ st.write(fig)
203
+
204
+
205
+ st.markdown('### Detailed Data View')
206
+ st.dataframe(dfSentiment.iloc[:, 1:][0:i])
207
+ time.sleep(1)
images/fourthbrain_logo.png ADDED
images/reddit_logo.png ADDED
images/tsla_logo.png ADDED
requirements.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ loguru==0.6.0
2
+ matplotlib==3.5.2
3
+ numpy==1.23.0
4
+ pandas==1.4.2
5
+ Pillow==9.2.0
6
+ plotly==5.9.0
7
+ praw==7.6.0
8
+ pydantic==1.9.1
9
+ requests==2.28.1
10
+ streamlit==1.10.0
11
+ transformers==4.19.4
sentiment_data.csv ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
1
+ ,timestamp,counter,close,volume,sentiment_score,close_lag1,perc_change_close,sentiment_score_lag1,perc_change_sentiment,sentiment_SMA3mo
2
+ 1,2022-07-02,361,0.0,,0.8339518653719049,681.78998,-1.0,0.9120607078075409,-0.0856399598919222,0.0
3
+ 2,2022-07-03,289,0.0,,0.8884219457121456,0.0,0.0,0.8339518653719049,0.06531561664647095,0.8781448396305306
4
+ 3,2022-07-04,784,0.0,,0.919358315212386,0.0,0.0,0.8884219457121456,0.034821707916548865,0.8805773754321455
5
+ 4,2022-07-05,4,699.20001,28193700,0.9892416596412659,0.0,inf,0.919358315212386,0.07601317492052678,0.9323406401885991
6
+ 5,2022-07-06,289,695.20001,23951200,0.9044164594481973,699.20001,-0.0057208237168074405,0.9892416596412659,-0.08574770316873759,0.937672144767283
7
+ 6,2022-07-07,1,0.0,,0.8769211769104004,695.20001,-1.0,0.9044164594481973,-0.03040113019899295,0.9235264319999544
8
+ 7,2022-07-08,169,0.0,,0.9258403640526992,0.0,0.0,0.8769211769104004,0.055785158837938685,0.9023926668037657
9
+ 8,2022-07-09,324,0.0,,0.9318263298935361,0.0,0.0,0.9258403640526992,0.006465440558925727,0.9115292902855452