sohoso commited on
Commit
55e2f2b
1 Parent(s): 17fced7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -40
app.py CHANGED
@@ -5,6 +5,7 @@ import numpy as np
5
  import yfinance as yf
6
  import plotly.express as px
7
  import plotly.graph_objects as go
 
8
 
9
  from sklearn.preprocessing import MinMaxScaler
10
  from tensorflow.keras.models import Sequential
@@ -21,11 +22,16 @@ end_date = st.sidebar.date_input('End Date')
21
  # --- MAIN PAGE
22
  st.header('Cryptocurrency Prediction')
23
 
24
- col1, col2, = st.columns([1,9])
25
  with col1:
26
- st.image('icons/'+ ticker +'.png', width=75)
 
 
 
 
 
27
  with col2:
28
- st.write(f" ## {ticker}")
29
 
30
  ticker_obj = yf.Ticker(ticker)
31
 
@@ -63,7 +69,7 @@ split_ratio = 0.8 # Ratio of training set to total data
63
  zero_base = True
64
  lstm_neurons = 50
65
  epochs = 100
66
- batch_size = 128 #32
67
  loss = 'mean_squared_error'
68
  dropout = 0.24
69
  optimizer = 'adam'
@@ -91,8 +97,12 @@ model = build_lstm_model(X_train, output_size=1, neurons=lstm_neurons, dropout=d
91
  # Saved Weights
92
  file_path = "./LSTM_" + ticker + "_weights.h5"
93
 
94
- # Loads the weights
95
- model.load_weights(file_path)
 
 
 
 
96
 
97
  # Step 4: Make predictions
98
  preds = model.predict(X_test)
@@ -110,13 +120,13 @@ y_test = scaler.inverse_transform(y_test)
110
 
111
  fig = px.line(x=model_data.index[-len(y_test):],
112
  y=[y_test.flatten(), preds.flatten()])
113
- newnames = {'wide_variable_0':'Real Values', 'wide_variable_1': 'Predictions'}
114
- fig.for_each_trace(lambda t: t.update(name = newnames[t.name],
115
- legendgroup = newnames[t.name],
116
- hovertemplate = t.hovertemplate.replace(t.name, newnames[t.name])))
117
  fig.update_layout(
118
  xaxis_title="Date",
119
- yaxis_title=ticker+" Price",
120
  legend_title=" ")
121
  st.write(fig)
122
 
@@ -125,34 +135,34 @@ st.write(fig)
125
  about_data, news = st.tabs(["About", "News"])
126
 
127
  with about_data:
128
- # Candlestick
129
- raw_data = ticker_obj.history(start=start_date, end=end_date)
130
- fig = go.Figure(data=[go.Candlestick(x=raw_data.index,
131
- open=raw_data['Open'],
132
- high=raw_data['High'],
133
- low=raw_data['Low'],
134
- close=raw_data['Close'])])
135
- fig.update_layout(
136
- title=ticker + " candlestick : Open, High, Low and Close",
137
- yaxis_title=ticker + ' Price')
138
- st.plotly_chart(fig)
139
-
140
- # Table
141
- history_data = raw_data.copy()
142
-
143
- # Formating index Date
144
- history_data.index = pd.to_datetime(history_data.index, format='%Y-%m-%d %H:%M:%S').date
145
- history_data.index.name = "Date"
146
- history_data.sort_values(by='Date', ascending=False, inplace=True)
147
- st.write(history_data)
148
 
149
  with news:
150
- sNews = StockNews(ticker, save_news=False)
151
- sNews_df = sNews.read_rss()
152
-
153
- # Showing most recent news
154
- for i in range(10):
155
- st.subheader(f"{i+1} - {sNews_df['title'][i]}")
156
- st.write(sNews_df['summary'][i])
157
- date_object = datetime.strptime(sNews_df['published'][i], '%a, %d %b %Y %H:%M:%S %z')
158
- st.write(f"_{date_object.strftime('%A')}, {date_object.date()}_")
 
5
  import yfinance as yf
6
  import plotly.express as px
7
  import plotly.graph_objects as go
8
+ import os
9
 
10
  from sklearn.preprocessing import MinMaxScaler
11
  from tensorflow.keras.models import Sequential
 
22
  # --- MAIN PAGE
23
  st.header('Cryptocurrency Prediction')
24
 
25
+ col1, col2 = st.columns([1, 9])
26
  with col1:
27
+ image_path = 'icons/' + ticker + '.png'
28
+ if os.path.exists(image_path):
29
+ st.image(image_path, width=75)
30
+ else:
31
+ st.warning(f"Image for {ticker} not found.")
32
+
33
  with col2:
34
+ st.write(f" ## {ticker}")
35
 
36
  ticker_obj = yf.Ticker(ticker)
37
 
 
69
  zero_base = True
70
  lstm_neurons = 50
71
  epochs = 100
72
+ batch_size = 128
73
  loss = 'mean_squared_error'
74
  dropout = 0.24
75
  optimizer = 'adam'
 
97
  # Saved Weights
98
  file_path = "./LSTM_" + ticker + "_weights.h5"
99
 
100
+ # Check if weights file exists
101
+ if os.path.exists(file_path):
102
+ # Loads the weights
103
+ model.load_weights(file_path)
104
+ else:
105
+ st.warning(f"Weights file for {ticker} not found.")
106
 
107
  # Step 4: Make predictions
108
  preds = model.predict(X_test)
 
120
 
121
  fig = px.line(x=model_data.index[-len(y_test):],
122
  y=[y_test.flatten(), preds.flatten()])
123
+ newnames = {'wide_variable_0': 'Real Values', 'wide_variable_1': 'Predictions'}
124
+ fig.for_each_trace(lambda t: t.update(name=newnames[t.name],
125
+ legendgroup=newnames[t.name],
126
+ hovertemplate=t.hovertemplate.replace(t.name, newnames[t.name])))
127
  fig.update_layout(
128
  xaxis_title="Date",
129
+ yaxis_title=ticker + " Price",
130
  legend_title=" ")
131
  st.write(fig)
132
 
 
135
  about_data, news = st.tabs(["About", "News"])
136
 
137
  with about_data:
138
+ # Candlestick
139
+ raw_data = ticker_obj.history(start=start_date, end=end_date)
140
+ fig = go.Figure(data=[go.Candlestick(x=raw_data.index,
141
+ open=raw_data['Open'],
142
+ high=raw_data['High'],
143
+ low=raw_data['Low'],
144
+ close=raw_data['Close'])])
145
+ fig.update_layout(
146
+ title=ticker + " candlestick : Open, High, Low and Close",
147
+ yaxis_title=ticker + ' Price')
148
+ st.plotly_chart(fig)
149
+
150
+ # Table
151
+ history_data = raw_data.copy()
152
+
153
+ # Formating index Date
154
+ history_data.index = pd.to_datetime(history_data.index, format='%Y-%m-%d %H:%M:%S').date
155
+ history_data.index.name = "Date"
156
+ history_data.sort_values(by='Date', ascending=False, inplace=True)
157
+ st.write(history_data)
158
 
159
  with news:
160
+ sNews = StockNews(ticker, save_news=False)
161
+ sNews_df = sNews.read_rss()
162
+
163
+ # Showing most recent news
164
+ for i in range(10):
165
+ st.subheader(f"{i+1} - {sNews_df['title'][i]}")
166
+ st.write(sNews_df['summary'][i])
167
+ date_object = datetime.strptime(sNews_df['published'][i], '%a, %d %b %Y %H:%M:%S %z')
168
+ st.write(f"_{date_object.strftime('%A')}, {date_object.date()}_")