Update app.py
Browse files
app.py
CHANGED
@@ -1,82 +1,118 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
import matplotlib.pyplot as plt
|
3 |
import numpy as np
|
4 |
import hopsworks
|
5 |
-
from tensorflow.keras.layers import LSTM, Dense
|
6 |
from tensorflow.keras.models import load_model
|
7 |
from DataLoader import DataLoader
|
8 |
from sklearn.preprocessing import MinMaxScaler
|
9 |
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
# Load the model
|
13 |
-
project = hopsworks.login(
|
|
|
|
|
14 |
mr = project.get_model_registry()
|
15 |
-
model = mr.get_model("FinanceModel", version=
|
16 |
saved_model_dir = model.download()
|
|
|
17 |
model = load_model(saved_model_dir + "/model.keras")
|
18 |
-
|
19 |
-
#Fetch the data used to train the model
|
20 |
-
time_period_news =
|
21 |
-
time_period_price =
|
22 |
-
data_loader = DataLoader(
|
23 |
data = data_loader.get_data()
|
24 |
|
25 |
-
#Get the previous closing price
|
26 |
-
|
27 |
-
|
28 |
-
#The modell only takes the latest 30 days of data
|
29 |
data = data[-30:]
|
30 |
|
31 |
-
#Load the input and output scalers used to train the model
|
32 |
input_scaler = MinMaxScaler()
|
33 |
output_scaler = MinMaxScaler()
|
34 |
|
35 |
-
#
|
36 |
-
output_scaler.fit_transform(
|
37 |
|
38 |
-
#Scale the data
|
39 |
data = input_scaler.fit_transform(data)
|
40 |
|
41 |
-
#Format the data to be used by the model. The model expects the data to be in the shape (1, 30, 7)
|
42 |
-
data = data.reshape(1, 30,
|
43 |
prediction = model.predict(data)
|
|
|
44 |
|
45 |
-
#Inverse the scaling
|
46 |
prediction = output_scaler.inverse_transform(prediction)[0]
|
47 |
-
print(prediction)
|
48 |
-
|
49 |
-
# predicted_value = index_close_price_list[-1] + 10
|
50 |
|
51 |
# Create the plot
|
52 |
fig, ax = plt.subplots(figsize=(8, 4))
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
ax.set_xlabel("Time")
|
59 |
-
ax.set_ylabel("
|
60 |
ax.legend()
|
61 |
|
62 |
-
""" fig, ax = plt.subplots(figsize=(8, 4))
|
63 |
-
ax.plot(previous_closing_price, label='Previous Closing Prices', linestyle='--',)
|
64 |
-
|
65 |
-
# Create an array of indices for the predicted values, right after the last index of prev_closing
|
66 |
-
predicted_indices = np.arange(len(previous_closing_price), len(previous_closing_price) + len(prediction))
|
67 |
-
|
68 |
-
# Plot the predicted closing prices in red, using the correct indices
|
69 |
-
ax.plot(predicted_indices, prediction, color='red', label='Predicted Prices',linestyle='--',) """
|
70 |
-
|
71 |
return fig
|
72 |
|
73 |
# Create the Gradio interface
|
74 |
interface = gr.Interface(
|
75 |
fn=predict,
|
76 |
-
inputs=gr.
|
|
|
|
|
|
|
|
|
77 |
outputs=gr.Plot(label="Index Prediction Plot"),
|
78 |
title="Financial Index Predictor",
|
79 |
-
description="Enter the name of a financial index to generate a plot showing true values for the past 30 days and the predicted value."
|
80 |
)
|
81 |
|
82 |
# Launch the app
|
|
|
1 |
+
import os
|
2 |
import gradio as gr
|
3 |
import matplotlib.pyplot as plt
|
4 |
import numpy as np
|
5 |
import hopsworks
|
|
|
6 |
from tensorflow.keras.models import load_model
|
7 |
from DataLoader import DataLoader
|
8 |
from sklearn.preprocessing import MinMaxScaler
|
9 |
|
10 |
+
name_to_ticker = {
|
11 |
+
"ABB Ltd": "ABB.ST",
|
12 |
+
"Alfa Laval": "ALFA.ST",
|
13 |
+
"Assa Abloy B": "ASSA-B.ST",
|
14 |
+
"Astra Zeneca": "AZN.ST",
|
15 |
+
"Atlas Copco A": "ATCO-A.ST",
|
16 |
+
"Atlas Copco B": "ATCO-B.ST",
|
17 |
+
"Boliden": "BOL.ST",
|
18 |
+
"Electrolux B": "ELUX-B.ST",
|
19 |
+
"Ericsson B": "ERIC-B.ST",
|
20 |
+
"Essity B": "ESSITY-B.ST",
|
21 |
+
"Evolution": "EVO.ST",
|
22 |
+
"Getinge B": "GETI-B.ST",
|
23 |
+
"Hennes & Mauritz B": "HM-B.ST",
|
24 |
+
"Hexagon AB": "HEXA-B.ST",
|
25 |
+
"Investor B": "INVE-B.ST",
|
26 |
+
"Kinnevik B": "KINV-B.ST",
|
27 |
+
"Nordea Bank Abp": "NDA-SE.ST",
|
28 |
+
"Sandvik": "SAND.ST",
|
29 |
+
"Sinch B": "SINCH.ST",
|
30 |
+
"SEB A": "SEB-A.ST",
|
31 |
+
"Skanska B": "SKA-B.ST",
|
32 |
+
"SKF B": "SKF-B.ST",
|
33 |
+
"SCA B": "SCA-B.ST",
|
34 |
+
"Svenska Handelsbanken A": "SHB-A.ST",
|
35 |
+
"Swedbank A": "SWED-A.ST",
|
36 |
+
"Tele2 B": "TEL2-B.ST",
|
37 |
+
"Telia": "TELIA.ST",
|
38 |
+
"Volvo B": "VOLV-B.ST",
|
39 |
+
}
|
40 |
+
|
41 |
+
def predict(stock_name):
|
42 |
+
ticker = name_to_ticker[stock_name]
|
43 |
+
print(f"TICKER VALUE: {ticker}")
|
44 |
# Load the model
|
45 |
+
project = hopsworks.login(
|
46 |
+
api_key_value=os.environ['Hopsworks_API_Key']
|
47 |
+
)
|
48 |
mr = project.get_model_registry()
|
49 |
+
model = mr.get_model("FinanceModel", version=11)
|
50 |
saved_model_dir = model.download()
|
51 |
+
print(saved_model_dir)
|
52 |
model = load_model(saved_model_dir + "/model.keras")
|
53 |
+
|
54 |
+
# Fetch the data used to train the model
|
55 |
+
time_period_news = "30d"
|
56 |
+
time_period_price = "3mo" # Needed to make sure we get 30 days of price data. Stock markets are closed on weekends and holidays
|
57 |
+
data_loader = DataLoader(ticker, time_period_news, time_period_price)
|
58 |
data = data_loader.get_data()
|
59 |
|
60 |
+
# Get the previous closing price
|
61 |
+
previous_closing_prices = data["Close"].values
|
62 |
+
|
|
|
63 |
data = data[-30:]
|
64 |
|
65 |
+
# Load the input and output scalers used to train the model
|
66 |
input_scaler = MinMaxScaler()
|
67 |
output_scaler = MinMaxScaler()
|
68 |
|
69 |
+
# Scale output to specific stock
|
70 |
+
output_scaler.fit_transform(previous_closing_prices.reshape(-1, 1))
|
71 |
|
72 |
+
# Scale the data
|
73 |
data = input_scaler.fit_transform(data)
|
74 |
|
75 |
+
# Format the data to be used by the model. The model expects the data to be in the shape (1, 30, 7)
|
76 |
+
data = data.reshape(1, 30, 8)
|
77 |
prediction = model.predict(data)
|
78 |
+
print(f"PREDICTION: {prediction}")
|
79 |
|
80 |
+
# Inverse the scaling
|
81 |
prediction = output_scaler.inverse_transform(prediction)[0]
|
|
|
|
|
|
|
82 |
|
83 |
# Create the plot
|
84 |
fig, ax = plt.subplots(figsize=(8, 4))
|
85 |
+
previous_indices = range(len(previous_closing_prices))
|
86 |
+
ax.plot(
|
87 |
+
previous_indices,
|
88 |
+
previous_closing_prices,
|
89 |
+
label="True Values",
|
90 |
+
color="blue",
|
91 |
+
)
|
92 |
+
predicted_indices = np.arange(
|
93 |
+
len(previous_closing_prices), len(previous_closing_prices) + len(prediction)
|
94 |
+
)
|
95 |
+
ax.plot([previous_indices[-1], predicted_indices[0]], [previous_closing_prices[-1], prediction[0]], color="red")
|
96 |
+
ax.plot(predicted_indices, prediction, color="red", label="Predicted Values")
|
97 |
+
ax.axvline(len(previous_closing_prices) - 1, linestyle="--", color="gray", alpha=0.6)
|
98 |
+
ax.set_title(f"Prediction for {stock_name}")
|
99 |
ax.set_xlabel("Time")
|
100 |
+
ax.set_ylabel("Price")
|
101 |
ax.legend()
|
102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
return fig
|
104 |
|
105 |
# Create the Gradio interface
|
106 |
interface = gr.Interface(
|
107 |
fn=predict,
|
108 |
+
inputs=gr.Dropdown(
|
109 |
+
label="Stock name",
|
110 |
+
choices=list(name_to_ticker.keys()),
|
111 |
+
value="ABB.ST", # Default value
|
112 |
+
),
|
113 |
outputs=gr.Plot(label="Index Prediction Plot"),
|
114 |
title="Financial Index Predictor",
|
115 |
+
description="Enter the name of a financial index to generate a plot showing true values for the past 30 days and the predicted value.",
|
116 |
)
|
117 |
|
118 |
# Launch the app
|