File size: 3,474 Bytes
95528fd |
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 |
import gradio as gr
import matplotlib.pyplot as plt
import numpy as np
import hopsworks
from tensorflow.keras.layers import LSTM, Dense
from tensorflow.keras.models import load_model
from DataLoader import DataLoader
from sklearn.preprocessing import MinMaxScaler
# Function to generate a sine wave plot
def predict(index_name="^OMX"):
# Load the model
project = hopsworks.login(api_key_value="pwWjyzF8SYsYJGQp.uZRknwAGCDPMe2covG1e4uVY4LsJXhAyKYgUNADOGY3H67mRAzoBtEJGlskTWE8h")
mr = project.get_model_registry()
model = mr.get_model("FinanceModel", version=9)
saved_model_dir = model.download()
model = load_model(saved_model_dir + "/model.keras")
#Fetch the data used to train the model
time_period_news = '30d'
time_period_price = '3mo' #Needed to make sure we get 30 days of price data. Stock markets are closed on weekends and holidays
data_loader = DataLoader(index_name, time_period_news, time_period_price)
data = data_loader.get_data()
#Get the previous closing price
previous_closing_price = data['Close'].values
#Remove uneccessary data and scale the data
#The modell only takes the latest 30 days of data
data = data[-30:]
#Load the input and output scalers used to train the model
input_scaler = MinMaxScaler()
output_scaler = MinMaxScaler()
#Create a fake output data to fit the scaler
output_scaler.fit_transform(previous_closing_price.reshape(-1, 1))
#Scale the data
data = input_scaler.fit_transform(data)
#Format the data to be used by the model. The model expects the data to be in the shape (1, 30, 7)
data = data.reshape(1, 30, 7)
prediction = model.predict(data)
#Inverse the scaling
prediction = output_scaler.inverse_transform(prediction)[0]
print(prediction)
# predicted_value = index_close_price_list[-1] + 10
# Create the plot
fig, ax = plt.subplots(figsize=(8, 4))
ax.plot(range(len(previous_closing_price)), previous_closing_price, label="True Values", color="blue")
predicted_indices = np.arange(len(previous_closing_price), len(previous_closing_price) + len(prediction))
ax.scatter(predicted_indices, prediction, color="red", label="Predicted Value")
ax.axvline(len(previous_closing_price) - 1, linestyle="--", color="gray", alpha=0.6)
ax.set_title(f"Prediction for {index_name}")
ax.set_xlabel("Time")
ax.set_ylabel("Index Value")
ax.legend()
""" fig, ax = plt.subplots(figsize=(8, 4))
ax.plot(previous_closing_price, label='Previous Closing Prices', linestyle='--',)
# Create an array of indices for the predicted values, right after the last index of prev_closing
predicted_indices = np.arange(len(previous_closing_price), len(previous_closing_price) + len(prediction))
# Plot the predicted closing prices in red, using the correct indices
ax.plot(predicted_indices, prediction, color='red', label='Predicted Prices',linestyle='--',) """
return fig
# Create the Gradio interface
interface = gr.Interface(
fn=predict,
inputs=gr.Textbox(label="Financial Index Name", placeholder="Enter the name of the financial index..."),
outputs=gr.Plot(label="Index Prediction Plot"),
title="Financial Index Predictor",
description="Enter the name of a financial index to generate a plot showing true values for the past 30 days and the predicted value."
)
# Launch the app
if __name__ == "__main__":
interface.launch()
|