|
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 |
|
|
|
|
|
def predict(index_name="^OMX"): |
|
|
|
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") |
|
|
|
|
|
time_period_news = '30d' |
|
time_period_price = '3mo' |
|
data_loader = DataLoader(index_name, time_period_news, time_period_price) |
|
data = data_loader.get_data() |
|
|
|
|
|
previous_closing_price = data['Close'].values |
|
|
|
|
|
data = data[-30:] |
|
|
|
|
|
input_scaler = MinMaxScaler() |
|
output_scaler = MinMaxScaler() |
|
|
|
|
|
output_scaler.fit_transform(previous_closing_price.reshape(-1, 1)) |
|
|
|
|
|
data = input_scaler.fit_transform(data) |
|
|
|
|
|
data = data.reshape(1, 30, 7) |
|
prediction = model.predict(data) |
|
|
|
|
|
prediction = output_scaler.inverse_transform(prediction)[0] |
|
print(prediction) |
|
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
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." |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
interface.launch() |
|
|