mistermprah commited on
Commit
3176257
·
verified ·
1 Parent(s): 149c511

Upload 4 files

Browse files
Files changed (4) hide show
  1. app.py +99 -0
  2. lstm_model.h5 +3 -0
  3. requirements.txt +9 -0
  4. scaler.joblib +3 -0
app.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import gradio as gr
3
+ import numpy as np
4
+ import pandas as pd
5
+ import yfinance as yf
6
+ from datetime import datetime
7
+ from tensorflow.keras.models import load_model
8
+ from joblib import load
9
+
10
+ # Load the saved LSTM model and scaler
11
+ lstm_model = load_model('/kaggle/input/madel-and-scaler/lstm_model.h5')
12
+ scaler = load('/kaggle/input/madel-and-scaler/scaler.joblib')
13
+
14
+ # Define the list of stocks
15
+ stock_list = ['GOOG', 'AAPL', 'TSLA', 'AMZN', 'MSFT']
16
+
17
+ # Function to get the last row of stock data
18
+ def get_last_stock_data(ticker):
19
+ try:
20
+ start_date = '2010-01-01'
21
+ end_date = datetime.now().strftime('%Y-%m-%d')
22
+ data = yf.download(ticker, start=start_date, end=end_date)
23
+ last_row = data.iloc[-1]
24
+ return last_row.to_dict()
25
+ except Exception as e:
26
+ return str(e)
27
+
28
+ # Function to make predictions
29
+ def predict_stock_price(ticker, open_price, high_price, low_price, close_price, adj_close_price, volume):
30
+ try:
31
+ start_date = '2010-01-01'
32
+ end_date = datetime.now().strftime('%Y-%m-%d')
33
+ data = yf.download(ticker, start=start_date, end=end_date)
34
+
35
+ # Prepare the data
36
+ data = data[['Close']]
37
+ dataset = data.values
38
+ scaled_data = scaler.transform(dataset)
39
+
40
+ # Append the user inputs as the last row in the data
41
+ user_input = np.array([[close_price]])
42
+ user_input_scaled = scaler.transform(user_input)
43
+ scaled_data = np.vstack([scaled_data, user_input_scaled])
44
+
45
+ # Prepare the data for LSTM
46
+ x_test_lstm = []
47
+ for i in range(60, len(scaled_data)):
48
+ x_test_lstm.append(scaled_data[i-60:i])
49
+
50
+ x_test_lstm = np.array(x_test_lstm)
51
+ x_test_lstm = np.reshape(x_test_lstm, (x_test_lstm.shape[0], x_test_lstm.shape[1], 1))
52
+
53
+ # LSTM Predictions
54
+ lstm_predictions = lstm_model.predict(x_test_lstm)
55
+ lstm_predictions = scaler.inverse_transform(lstm_predictions)
56
+ next_day_lstm_price = lstm_predictions[-1][0]
57
+
58
+ result = f"Predicted future price for {ticker}: ${next_day_lstm_price:.2f}"
59
+
60
+ return result
61
+ except Exception as e:
62
+ return str(e)
63
+
64
+ # Set up Gradio interface
65
+ ticker_input = gr.Dropdown(choices=stock_list, label="Stock Ticker")
66
+
67
+ def get_user_inputs(ticker):
68
+ last_data = get_last_stock_data(ticker)
69
+ if isinstance(last_data, str):
70
+ return gr.Textbox.update(value=last_data)
71
+ else:
72
+ return gr.update(inputs=[
73
+ gr.Number(value=last_data['Open'], label='Open'),
74
+ gr.Number(value=last_data['High'], label='High'),
75
+ gr.Number(value=last_data['Low'], label='Low'),
76
+ gr.Number(value=last_data['Close'], label='Close'),
77
+ gr.Number(value=last_data['Adj Close'], label='Adj Close'),
78
+ gr.Number(value=last_data['Volume'], label='Volume')
79
+ ])
80
+
81
+ iface = gr.Interface(
82
+ fn=predict_stock_price,
83
+ inputs=[
84
+ ticker_input,
85
+ gr.Number(label="Open"),
86
+ gr.Number(label="High"),
87
+ gr.Number(label="Low"),
88
+ gr.Number(label="Close"),
89
+ gr.Number(label="Adj Close"),
90
+ gr.Number(label="Volume")
91
+ ],
92
+ outputs=gr.Textbox(),
93
+ title="Stock Price Predictor",
94
+ description="Select the stock ticker and input the last recorded values to predict the closing price using the LSTM model."
95
+ )
96
+
97
+ iface.launch()
98
+
99
+
lstm_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e7812e428cc87e438b827fe557e3153355a1905c22800d146dc26b976b4c9311
3
+ size 408600
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+
2
+ gradio
3
+ yfinance
4
+ tensorflow
5
+ joblib
6
+ matplotlib
7
+ numpy
8
+ pandas
9
+ scikit-learn
scaler.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ac234b6fe95df3b478938c9c61c63f7a79419279dce91d80fbdb1a330fb90030
3
+ size 719