Spaces:
Runtime error
Runtime error
| 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 | |
| import matplotlib.pyplot as plt | |
| import streamlit as st | |
| from huggingface_hub import hf_hub_download | |
| from datetime import date | |
| #cpi.update() | |
| # from huggingface_hub import notebook_login | |
| # notebook_login() | |
| # m = hf_hub_download(repo_id="marvmk/model-test", filename="model.pkl") | |
| # model = pickle.load(open(m, 'rb')) | |
| with open("model.pkl", "rb") as f: | |
| model = pickle.load(f) | |
| # downloading the last 10 days to make the prediction | |
| 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) | |
| 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()) | |
| 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] | |
| 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) |