Spaces:
Sleeping
Sleeping
mistermprah
commited on
Commit
•
59137fe
1
Parent(s):
8ff0005
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,8 @@
|
|
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 |
|
@@ -26,7 +25,7 @@ def get_last_stock_data(ticker):
|
|
26 |
return str(e)
|
27 |
|
28 |
# Function to make predictions
|
29 |
-
def predict_stock_price(ticker, open_price,
|
30 |
try:
|
31 |
start_date = '2010-01-01'
|
32 |
end_date = datetime.now().strftime('%Y-%m-%d')
|
@@ -55,48 +54,95 @@ def predict_stock_price(ticker, open_price, high_price, low_price, close_price,
|
|
55 |
lstm_predictions = scaler.inverse_transform(lstm_predictions)
|
56 |
next_day_lstm_price = lstm_predictions[-1][0]
|
57 |
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
result = f"Predicted future price for {ticker}: ${next_day_lstm_price:.2f}"
|
62 |
|
63 |
return result
|
64 |
except Exception as e:
|
65 |
return str(e)
|
66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
# Set up Gradio interface
|
68 |
ticker_input = gr.Dropdown(choices=stock_list, label="Stock Ticker")
|
69 |
|
70 |
-
def get_user_inputs(ticker):
|
71 |
-
last_data = get_last_stock_data(ticker)
|
72 |
-
if isinstance(last_data, str):
|
73 |
-
return gr.Textbox.update(value=last_data)
|
74 |
-
else:
|
75 |
-
return gr.update(inputs=[
|
76 |
-
gr.Number(value=last_data['Open'], label='Open'),
|
77 |
-
gr.Number(value=last_data['High'], label='High'),
|
78 |
-
gr.Number(value=last_data['Low'], label='Low'),
|
79 |
-
gr.Number(value=last_data['Close'], label='Close'),
|
80 |
-
gr.Number(value=last_data['Adj Close'], label='Adj Close'),
|
81 |
-
gr.Number(value=last_data['Volume'], label='Volume')
|
82 |
-
])
|
83 |
-
|
84 |
iface = gr.Interface(
|
85 |
fn=predict_stock_price,
|
86 |
inputs=[
|
87 |
ticker_input,
|
88 |
gr.Number(label="Open"),
|
89 |
-
gr.Number(label="
|
90 |
-
gr.Number(label="Low"),
|
91 |
-
gr.Number(label="Close"),
|
92 |
-
gr.Number(label="Adj Close"),
|
93 |
-
gr.Number(label="Volume")
|
94 |
],
|
95 |
outputs=gr.Textbox(),
|
96 |
title="Stock Price Predictor",
|
97 |
description="Select the stock ticker and input the last recorded values to predict the closing price using the LSTM model."
|
98 |
)
|
99 |
|
100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
import pandas as pd
|
4 |
import yfinance as yf
|
5 |
+
from datetime import datetime, timedelta
|
6 |
from tensorflow.keras.models import load_model
|
7 |
from joblib import load
|
8 |
|
|
|
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')
|
|
|
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):
|
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 |
+
# Prepare the data for LSTM
|
76 |
+
x_test_lstm = []
|
77 |
+
for i in range(60, len(scaled_data)):
|
78 |
+
x_test_lstm.append(scaled_data[i-60:i])
|
79 |
+
|
80 |
+
x_test_lstm = np.array(x_test_lstm)
|
81 |
+
x_test_lstm = np.reshape(x_test_lstm, (x_test_lstm.shape[0], x_test_lstm.shape[1], 1))
|
82 |
+
|
83 |
+
# Predicting the next 30 days
|
84 |
+
predictions = []
|
85 |
+
for _ in range(30):
|
86 |
+
pred = lstm_model.predict(x_test_lstm[-1].reshape(1, 60, 1))
|
87 |
+
predictions.append(pred)
|
88 |
+
x_test_lstm = np.append(x_test_lstm, pred.reshape(1, 1, 1), axis=1)
|
89 |
+
x_test_lstm = x_test_lstm[:, 1:, :]
|
90 |
+
|
91 |
+
predictions = np.array(predictions).reshape(-1, 1)
|
92 |
+
next_month_predictions = scaler.inverse_transform(predictions)
|
93 |
+
next_month_price = next_month_predictions[-1][0]
|
94 |
+
|
95 |
+
result = f"Predicted price for {ticker} next month: ${next_month_price:.2f}"
|
96 |
+
|
97 |
+
return result
|
98 |
+
except Exception as e:
|
99 |
+
return str(e)
|
100 |
+
|
101 |
+
# Function to display historical data
|
102 |
+
def display_historical_data(ticker):
|
103 |
+
try:
|
104 |
+
start_date = '2010-01-01'
|
105 |
+
end_date = datetime.now().strftime('%Y-%m-%d')
|
106 |
+
data = yf.download(ticker, start=start_date, end=end_date)
|
107 |
+
return data.tail(30)
|
108 |
+
except Exception as e:
|
109 |
+
return str(e)
|
110 |
+
|
111 |
# Set up Gradio interface
|
112 |
ticker_input = gr.Dropdown(choices=stock_list, label="Stock Ticker")
|
113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
iface = gr.Interface(
|
115 |
fn=predict_stock_price,
|
116 |
inputs=[
|
117 |
ticker_input,
|
118 |
gr.Number(label="Open"),
|
119 |
+
gr.Number(label="Close")
|
|
|
|
|
|
|
|
|
120 |
],
|
121 |
outputs=gr.Textbox(),
|
122 |
title="Stock Price Predictor",
|
123 |
description="Select the stock ticker and input the last recorded values to predict the closing price using the LSTM model."
|
124 |
)
|
125 |
|
126 |
+
next_month_iface = gr.Interface(
|
127 |
+
fn=predict_next_month_price,
|
128 |
+
inputs=[ticker_input],
|
129 |
+
outputs=gr.Textbox(),
|
130 |
+
title="Next Month Stock Price Predictor",
|
131 |
+
description="Select the stock ticker to predict the closing price for the next month using the LSTM model."
|
132 |
+
)
|
133 |
|
134 |
+
historical_data_iface = gr.Interface(
|
135 |
+
fn=display_historical_data,
|
136 |
+
inputs=[ticker_input],
|
137 |
+
outputs=gr.Dataframe(),
|
138 |
+
title="Historical Data Viewer",
|
139 |
+
description="Select the stock ticker to view the historical data."
|
140 |
+
)
|
141 |
+
|
142 |
+
# Combine interfaces
|
143 |
+
app = gr.TabbedInterface(
|
144 |
+
interface_list=[iface, next_month_iface, historical_data_iface],
|
145 |
+
tab_names=["Predict Today's Price", "Predict Next Month's Price", "View Historical Data"]
|
146 |
+
)
|
147 |
|
148 |
+
app.launch()
|