Update app.py
Browse files
app.py
CHANGED
@@ -1,90 +1,53 @@
|
|
1 |
-
import
|
2 |
import pandas as pd
|
|
|
3 |
import yfinance as yf
|
4 |
-
import tensorflow as tf
|
5 |
-
from tensorflow import keras
|
6 |
-
from tensorflow.keras import layers
|
7 |
-
import gradio as gr
|
8 |
-
import matplotlib.pyplot as plt
|
9 |
from sklearn.preprocessing import MinMaxScaler
|
10 |
-
import
|
11 |
-
|
12 |
-
# Function to fetch and preprocess data
|
13 |
-
def fetch_data(ticker, start_date, end_date):
|
14 |
-
data = yf.download(ticker, start=start_date, end=end_date)
|
15 |
-
data = data[['Close']]
|
16 |
-
return data
|
17 |
-
|
18 |
-
# Function to create datasets for training
|
19 |
-
def create_dataset(data, time_step=1):
|
20 |
-
X, y = [], []
|
21 |
-
for i in range(len(data) - time_step - 1):
|
22 |
-
a = data[i:(i + time_step), 0]
|
23 |
-
X.append(a)
|
24 |
-
y.append(data[i + time_step, 0])
|
25 |
-
return np.array(X), np.array(y)
|
26 |
-
|
27 |
-
# Function to build and train the LSTM model
|
28 |
-
def train_model(data, time_step=10):
|
29 |
-
# Scale the data
|
30 |
-
scaler = MinMaxScaler(feature_range=(0, 1))
|
31 |
-
scaled_data = scaler.fit_transform(data)
|
32 |
-
|
33 |
-
# Create datasets
|
34 |
-
X, y = create_dataset(scaled_data, time_step)
|
35 |
-
X = X.reshape(X.shape[0], X.shape[1], 1) # Reshape for LSTM input
|
36 |
-
|
37 |
-
# Split the data into training and testing sets
|
38 |
-
split = int(len(X) * 0.8)
|
39 |
-
X_train, X_test = X[:split], X[split:]
|
40 |
-
y_train, y_test = y[:split], y[split:]
|
41 |
-
|
42 |
-
# Build the LSTM model
|
43 |
-
model = keras.Sequential([
|
44 |
-
layers.LSTM(50, return_sequences=True, input_shape=(X_train.shape[1], 1)),
|
45 |
-
layers.LSTM(50, return_sequences=False),
|
46 |
-
layers.Dense(1)
|
47 |
-
])
|
48 |
-
|
49 |
-
model.compile(optimizer='adam', loss='mean_squared_error')
|
50 |
-
model.fit(X_train, y_train, epochs=50, batch_size=32, verbose=1)
|
51 |
-
|
52 |
-
return model, scaler
|
53 |
-
|
54 |
-
# Function to predict the stock price
|
55 |
-
def predict_price(ticker, start_date, end_date):
|
56 |
-
data = fetch_data(ticker, start_date, end_date)
|
57 |
-
model, scaler = train_model(data.values)
|
58 |
-
last_30_days = data['Close'][-30:].values
|
59 |
-
last_30_days_scaled = scaler.transform(last_30_days.reshape(-1, 1))
|
60 |
-
|
61 |
-
X_test = []
|
62 |
-
X_test.append(last_30_days_scaled)
|
63 |
-
X_test = np.array(X_test)
|
64 |
-
X_test = X_test.reshape(X_test.shape[0], X_test.shape[1], 1)
|
65 |
-
|
66 |
-
predicted_price = model.predict(X_test)
|
67 |
-
predicted_price = scaler.inverse_transform(predicted_price)
|
68 |
-
|
69 |
-
return predicted_price[0][0]
|
70 |
|
71 |
-
#
|
72 |
-
|
73 |
-
|
74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
# Create the Gradio interface
|
77 |
-
|
78 |
-
choices=["AAPL", "GOOGL", "MSFT", "AMZN", "TSLA"
|
79 |
label="Select Stock Ticker"
|
80 |
)
|
81 |
-
start_date_input = gr.inputs.Date(label="Start Date")
|
82 |
-
end_date_input = gr.inputs.Date(label="End Date")
|
83 |
|
84 |
-
gr.
|
85 |
-
|
86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
outputs="text",
|
88 |
-
|
89 |
description="Enter the stock ticker and date range to predict the stock price."
|
90 |
-
)
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
import yfinance as yf
|
|
|
|
|
|
|
|
|
|
|
5 |
from sklearn.preprocessing import MinMaxScaler
|
6 |
+
from tensorflow import keras
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
+
# Load your trained model
|
9 |
+
model = keras.models.load_model('your_model.h5')
|
10 |
+
|
11 |
+
# Function to predict stock prices
|
12 |
+
def predict_stock_price(stock_ticker, start_date, end_date):
|
13 |
+
# Fetch data
|
14 |
+
data = yf.download(stock_ticker, start=start_date, end=end_date)
|
15 |
+
|
16 |
+
# Preprocess data
|
17 |
+
scaler = MinMaxScaler()
|
18 |
+
scaled_data = scaler.fit_transform(data['Close'].values.reshape(-1, 1))
|
19 |
+
|
20 |
+
# Prepare input for the model
|
21 |
+
# This assumes your model expects a certain shape of input
|
22 |
+
input_data = scaled_data[-60:] # Use the last 60 days of data
|
23 |
+
input_data = input_data.reshape((1, input_data.shape[0], 1))
|
24 |
+
|
25 |
+
# Predict stock prices
|
26 |
+
prediction = model.predict(input_data)
|
27 |
+
predicted_price = scaler.inverse_transform(prediction) # Rescale back to original price
|
28 |
+
|
29 |
+
return f"Predicted stock price for tomorrow: ${predicted_price[0][0]:.2f}"
|
30 |
|
31 |
# Create the Gradio interface
|
32 |
+
stock_ticker_input = gr.Dropdown(
|
33 |
+
choices=["AAPL", "GOOGL", "MSFT", "AMZN", "TSLA"],
|
34 |
label="Select Stock Ticker"
|
35 |
)
|
|
|
|
|
36 |
|
37 |
+
start_date_input = gr.Date(label="Start Date")
|
38 |
+
end_date_input = gr.Date(label="End Date")
|
39 |
+
|
40 |
+
iface = gr.Interface(
|
41 |
+
fn=predict_stock_price,
|
42 |
+
inputs=[
|
43 |
+
stock_ticker_input,
|
44 |
+
start_date_input,
|
45 |
+
end_date_input
|
46 |
+
],
|
47 |
outputs="text",
|
48 |
+
title="Stock Price Prediction App",
|
49 |
description="Enter the stock ticker and date range to predict the stock price."
|
50 |
+
)
|
51 |
+
|
52 |
+
# Launch the Gradio app
|
53 |
+
iface.launch()
|