Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import numpy as np
|
3 |
+
import pandas as pd
|
4 |
+
import yfinance as yf
|
5 |
+
from datetime import datetime
|
6 |
+
from tensorflow.keras.models import load_model
|
7 |
+
from joblib import load
|
8 |
+
|
9 |
+
# Load the saved LSTM model and scaler
|
10 |
+
lstm_model = load_model('lstm_model.h5')
|
11 |
+
scaler = load('scaler.joblib')
|
12 |
+
|
13 |
+
# Define the list of stocks
|
14 |
+
stock_list = ['GOOG', 'AAPL', 'TSLA', 'AMZN', 'MSFT']
|
15 |
+
|
16 |
+
# Function to get the last row of stock data
|
17 |
+
def get_last_stock_data(ticker):
|
18 |
+
try:
|
19 |
+
start_date = '2010-01-01'
|
20 |
+
end_date = datetime.now().strftime('%Y-%m-%d')
|
21 |
+
data = yf.download(ticker, start=start_date, end=end_date)
|
22 |
+
last_row = data.iloc[-1]
|
23 |
+
return last_row.to_dict()
|
24 |
+
except Exception as e:
|
25 |
+
return str(e)
|
26 |
+
|
27 |
+
# Function to make predictions
|
28 |
+
def predict_stock_price(ticker, open_price, close_price):
|
29 |
+
try:
|
30 |
+
start_date = '2010-01-01'
|
31 |
+
end_date = datetime.now().strftime('%Y-%m-%d')
|
32 |
+
data = yf.download(ticker, start=start_date, end=end_date)
|
33 |
+
|
34 |
+
# Prepare the data
|
35 |
+
data = data[['Close']]
|
36 |
+
dataset = data.values
|
37 |
+
scaled_data = scaler.transform(dataset)
|
38 |
+
|
39 |
+
# Append the user inputs as the last row in the data
|
40 |
+
user_input = np.array([[close_price]])
|
41 |
+
user_input_scaled = scaler.transform(user_input)
|
42 |
+
scaled_data = np.vstack([scaled_data, user_input_scaled])
|
43 |
+
|
44 |
+
# Prepare the data for LSTM
|
45 |
+
x_test_lstm = []
|
46 |
+
for i in range(60, len(scaled_data)):
|
47 |
+
x_test_lstm.append(scaled_data[i-60:i])
|
48 |
+
|
49 |
+
x_test_lstm = np.array(x_test_lstm)
|
50 |
+
x_test_lstm = np.reshape(x_test_lstm, (x_test_lstm.shape[0], x_test_lstm.shape[1], 1))
|
51 |
+
|
52 |
+
# LSTM Predictions
|
53 |
+
lstm_predictions = lstm_model.predict(x_test_lstm)
|
54 |
+
lstm_predictions = scaler.inverse_transform(lstm_predictions)
|
55 |
+
next_day_lstm_price = lstm_predictions[-1][0]
|
56 |
+
|
57 |
+
result = f"Predicted future price for {ticker}: ${next_day_lstm_price:.2f}"
|
58 |
+
|
59 |
+
return result
|
60 |
+
except Exception as e:
|
61 |
+
return str(e)
|
62 |
+
|
63 |
+
# Function to predict next month's price
|
64 |
+
def predict_next_month_price(ticker, close_price):
|
65 |
+
try:
|
66 |
+
start_date = '2010-01-01'
|
67 |
+
end_date = datetime.now().strftime('%Y-%m-%d')
|
68 |
+
data = yf.download(ticker, start=start_date, end=end_date)
|
69 |
+
|
70 |
+
# Prepare the data
|
71 |
+
data = data[['Close']]
|
72 |
+
dataset = data.values
|
73 |
+
scaled_data = scaler.transform(dataset)
|
74 |
+
|
75 |
+
# Append the user inputs as the last row in the data
|
76 |
+
user_input = np.array([[close_price]])
|
77 |
+
user_input_scaled = scaler.transform(user_input)
|
78 |
+
scaled_data = np.vstack([scaled_data, user_input_scaled])
|
79 |
+
|
80 |
+
# Prepare the data for LSTM
|
81 |
+
x_test_lstm = []
|
82 |
+
for i in range(60, len(scaled_data)):
|
83 |
+
x_test_lstm.append(scaled_data[i-60:i])
|
84 |
+
|
85 |
+
x_test_lstm = np.array(x_test_lstm)
|
86 |
+
x_test_lstm = np.reshape(x_test_lstm, (x_test_lstm.shape[0], x_test_lstm.shape[1], 1))
|
87 |
+
|
88 |
+
# Predicting the next 30 days
|
89 |
+
predictions = []
|
90 |
+
for _ in range(30):
|
91 |
+
pred = lstm_model.predict(x_test_lstm[-1].reshape(1, 60, 1))
|
92 |
+
predictions.append(pred[0])
|
93 |
+
new_input = np.append(x_test_lstm[-1][1:], pred)
|
94 |
+
x_test_lstm = np.append(x_test_lstm, new_input.reshape(1, 60, 1), axis=0)
|
95 |
+
|
96 |
+
predictions = np.array(predictions)
|
97 |
+
next_month_predictions = scaler.inverse_transform(predictions)
|
98 |
+
next_month_price = next_month_predictions[-1][0]
|
99 |
+
|
100 |
+
result = f"Predicted price for {ticker} next month: ${next_month_price:.2f}"
|
101 |
+
|
102 |
+
return result
|
103 |
+
except Exception as e:
|
104 |
+
return str(e)
|
105 |
+
|
106 |
+
# Function to display historical data
|
107 |
+
def display_historical_data(ticker):
|
108 |
+
try:
|
109 |
+
start_date = '2010-01-01'
|
110 |
+
end_date = datetime.now().strftime('%Y-%m-%d')
|
111 |
+
data = yf.download(ticker, start=start_date, end=end_date)
|
112 |
+
return data.tail(30)
|
113 |
+
except Exception as e:
|
114 |
+
return str(e)
|
115 |
+
|
116 |
+
# Streamlit interface
|
117 |
+
st.title("Stock Price Predictor")
|
118 |
+
|
119 |
+
tab1, tab2, tab3 = st.tabs(["Predict Today's Price", "Predict Next Month's Price", "View Historical Data"])
|
120 |
+
|
121 |
+
with tab1:
|
122 |
+
st.header("Predict Today's Price")
|
123 |
+
ticker_input = st.selectbox("Stock Ticker", stock_list)
|
124 |
+
open_price = st.number_input("Open Price", value=0.0)
|
125 |
+
close_price = st.number_input("Close Price", value=0.0)
|
126 |
+
if st.button("Predict Today's Price"):
|
127 |
+
result = predict_stock_price(ticker_input, open_price, close_price)
|
128 |
+
st.write(result)
|
129 |
+
|
130 |
+
with tab2:
|
131 |
+
st.header("Predict Next Month's Price")
|
132 |
+
next_month_ticker_input = st.selectbox("Stock Ticker", stock_list)
|
133 |
+
next_month_close_price = st.number_input("Close Price", value=0.0)
|
134 |
+
if st.button("Predict Next Month's Price"):
|
135 |
+
result = predict_next_month_price(next_month_ticker_input, next_month_close_price)
|
136 |
+
st.write(result)
|
137 |
+
|
138 |
+
with tab3:
|
139 |
+
st.header("View Historical Data")
|
140 |
+
historical_ticker_input = st.selectbox("Stock Ticker", stock_list)
|
141 |
+
if st.button("View Data"):
|
142 |
+
data = display_historical_data(historical_ticker_input)
|
143 |
+
st.dataframe(data)
|