-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
129 lines (109 loc) · 4.12 KB
/
app.py
File metadata and controls
129 lines (109 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from flask import Flask, render_template, request, redirect, url_for
import pandas as pd
import numpy as np
import yfinance as yf
from keras.models import load_model
from sklearn.preprocessing import MinMaxScaler
import matplotlib
import matplotlib.pyplot as plt
import io
import base64
from datetime import datetime
# Set Matplotlib to non-interactive backend
matplotlib.use('Agg')
app = Flask(__name__)
# Load Pre-trained Model
model = load_model("model.keras")
# Helper Function to Convert Matplotlib Plots to HTML
def plot_to_html(fig):
buf = io.BytesIO()
fig.savefig(buf, format="png")
buf.seek(0)
data = base64.b64encode(buf.getbuffer()).decode("ascii")
buf.close()
return f"data:image/png;base64,{data}"
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "POST":
stock = request.form.get("stock")
no_of_days = int(request.form.get("no_of_days"))
return redirect(url_for("predict", stock=stock, no_of_days=no_of_days))
return render_template("index.html")
@app.route("/predict")
def predict():
stock = request.args.get("stock", "BTC-USD")
no_of_days = int(request.args.get("no_of_days", 10))
# Fetch Stock Data
end = datetime.now()
start = datetime(end.year - 10, end.month, end.day)
stock_data = yf.download(stock, start, end)
if stock_data.empty:
return render_template("result.html", error="Invalid stock ticker or no data available.")
# Data Preparation
splitting_len = int(len(stock_data) * 0.9)
x_test = stock_data[['Close']][splitting_len:]
scaler = MinMaxScaler(feature_range=(0, 1))
scaled_data = scaler.fit_transform(x_test)
x_data = []
y_data = []
for i in range(100, len(scaled_data)):
x_data.append(scaled_data[i - 100:i])
y_data.append(scaled_data[i])
x_data = np.array(x_data)
y_data = np.array(y_data)
# Predictions
predictions = model.predict(x_data)
inv_predictions = scaler.inverse_transform(predictions)
inv_y_test = scaler.inverse_transform(y_data)
# Prepare Data for Plotting
plotting_data = pd.DataFrame({
'Original Test Data': inv_y_test.flatten(),
'Predicted Test Data': inv_predictions.flatten()
}, index=x_test.index[100:])
# Generate Plots
# Plot 1: Original Closing Prices
fig1 = plt.figure(figsize=(15, 6))
plt.plot(stock_data['Close'], 'b', label='Close Price')
plt.title("Closing Prices Over Time")
plt.xlabel("Date")
plt.ylabel("Close Price")
plt.legend()
original_plot = plot_to_html(fig1)
# Plot 2: Original vs Predicted Test Data
fig2 = plt.figure(figsize=(15, 6))
plt.plot(plotting_data['Original Test Data'], label="Original Test Data")
plt.plot(plotting_data['Predicted Test Data'], label="Predicted Test Data", linestyle="--")
plt.legend()
plt.title("Original vs Predicted Closing Prices")
plt.xlabel("Date")
plt.ylabel("Close Price")
predicted_plot = plot_to_html(fig2)
# Plot 3: Future Predictions
last_100 = stock_data[['Close']].tail(100)
last_100_scaled = scaler.transform(last_100)
future_predictions = []
last_100_scaled = last_100_scaled.reshape(1, -1, 1)
for _ in range(no_of_days):
next_day = model.predict(last_100_scaled)
future_predictions.append(scaler.inverse_transform(next_day))
last_100_scaled = np.append(last_100_scaled[:, 1:, :], next_day.reshape(1, 1, -1), axis=1)
future_predictions = np.array(future_predictions).flatten()
fig3 = plt.figure(figsize=(15, 6))
plt.plot(range(1, no_of_days + 1), future_predictions, marker='o', label="Predicted Future Prices", color="purple")
plt.title("Future Close Price Predictions")
plt.xlabel("Days Ahead")
plt.ylabel("Predicted Close Price")
plt.grid(alpha=0.3)
plt.legend()
future_plot = plot_to_html(fig3)
return render_template(
"result.html",
stock=stock,
original_plot=original_plot,
predicted_plot=predicted_plot,
future_plot=future_plot,
enumerate =enumerate,
future_predictions=future_predictions
)
if __name__ == "__main__":
app.run(debug=True)