StockProject / app.py
Victorlopo21's picture
Update app.py
5bfe1bf
raw
history blame
4.49 kB
import pandas as pd
import joblib
from huggingface_hub import HfApi
import pickle
import yfinance as yf
from datetime import datetime, timedelta
from forex_python.converter import get_rate
import pandas as pd
import numpy as np
import cpi
from sklearn.preprocessing import MinMaxScaler
from huggingface_hub import hf_hub_download
#cpi.update()
import gradio as gr
from huggingface_hub import notebook_login
notebook_login()
import hopsworks
# from huggingface_hub import hf_hub_download
# m = hf_hub_download(repo_id="marvmk/model-test", filename="model.pkl")
project = hopsworks.login(api_key_value="4CY1rwa8iz8Yu6gG.TwayrYmsX4GQfhSp3LNKYTLvyFMfqAvnzNUQp5ae9K5HhfYxb5mcnLAutm1K18zV")
fs = project.get_feature_store()
mr = project.get_model_registry()
model = mr.get_model("stock_price_modal")
#model_dir = model.download()
#model = joblib.load(model_dir + "/stock_price_model.pkl")
#from huggingface_hub import hf_hub_download
#m = hf_hub_download(repo_id="marvmk/model-test", filename="model.pkl")
#model = pickle.load(open(m, 'rb'))
#m=model_dir + "stock_model.pkl"
#with open(m, "rb") as f:
# model = pickle.load(f)
#model = pickle.load(open(m, 'rb'))
# downloading the last 10 days to make the prediction
from datetime import date
today = date.today()
days_ago = today - timedelta(days=20)
# we get the last 20 days and keep just the last 10 working days, which have prices
nasdaq = yf.Ticker("^IXIC")
hist = nasdaq.history(start=days_ago, end=today)
hist = hist.drop(columns=['Dividends', 'Stock Splits'])
# keeping the last 10 data points
hist = hist[-10:]
inflation = []
for t in hist.index:
inflation.append(get_rate("USD", "EUR", t))
cpi_items_df = cpi.series.get(seasonally_adjusted=False).to_dataframe()
cpi_items_df = cpi_items_df[cpi_items_df['period_type']=='monthly']
cpi_items_df['date'] = pd.to_datetime(cpi_items_df['date'])
cpi_items_df = cpi_items_df.set_index('date')
cpi_df = cpi_items_df['value'].loc['2022':'2023']
cpi_col = []
for x in hist.index:
# ts = datetime(x.year, x.month, 1)
# just adding the latest inflation rate
cpi_col.append(cpi_df[-1])
hist['Inflation'] = inflation
hist['CPI'] = cpi_col
hist['Quarter_end'] = np.where(hist.index.month%3==0,1,0)
# def build_sequences(nump, target_labels=['Close'], window=10, stride=1, telescope=1):
# # Sanity check to avoid runtime errors
# df = pd.DataFrame(nump, columns = ['Open','High','Low','Close','Volume','Inflation', 'CPI', 'Quarter_end'])
# assert window % stride == 0
# dataset = []
# labels = []
# temp_df = df.copy().values
# temp_label = df[target_labels].copy().values
# padding_len = len(df)%window
# if(padding_len != 0):
# # Compute padding length
# padding_len = window - len(df)%window
# padding = np.zeros((padding_len,temp_df.shape[1]), dtype='float64')
# temp_df = np.concatenate((padding,df))
# padding = np.zeros((padding_len,temp_label.shape[1]), dtype='float64')
# temp_label = np.concatenate((padding,temp_label))
# assert len(temp_df) % window == 0
# for idx in np.arange(0,len(temp_df)-window-telescope,stride):
# dataset.append(temp_df[idx:idx+window])
# labels.append(temp_label[idx+window:idx+window+telescope])
# dataset = np.array(dataset)
# labels = np.array(labels)
# return dataset, labels
s = hf_hub_download(repo_id="marvmk/scalable_project", filename="scaler.save", repo_type='dataset')
scaler = joblib.load(s)
inp = scaler.transform(hist.to_numpy())
#inp = scaler.inverse_transform(inp)
df = inp
temp_df = pd.DataFrame(inp, columns = ['Open','High','Low','Close','Volume','Inflation', 'CPI', 'Quarter_end'])
ds = []
ds.append(temp_df[0:10])
ds = np.array(ds)
predictions = model.predict(ds)
predictions
p = predictions[0][0]
print(p)
a = np.array([0,0,0,p,0,0,0,0])
a = scaler.inverse_transform(a.reshape(1,-1))
final_prediction = a[-1][3]
import matplotlib.pyplot as plt
import streamlit as st
prediction = []
#prediction.append(final_prediction)
close = hist['Close'].to_list()
print(close)
for c in close:
prediction.append(c)
prediction.append(final_prediction)
print(prediction)
plt.figure(figsize = (20,10))
plt.plot(prediction, label="Prediction")
plt.plot(hist['Close'].to_list()[-10:], label="Previous")
plt.ylabel('Price US$', fontsize = 15 )
plt.xlabel('Working Days', fontsize = 15 )
plt.title("NASDAQ Stock Prediction", fontsize = 20)
plt.legend()
plt.grid()
st.pyplot(plt)