Spaces:
Runtime error
Runtime error
| import pandas as pd | |
| import joblib | |
| from huggingface_hub import HfApi | |
| import pickle | |
| import yfinance as yf | |
| import keras | |
| 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 | |
| import gradio as gr | |
| from huggingface_hub import notebook_login | |
| notebook_login() | |
| import hopsworks | |
| from datetime import date | |
| import matplotlib.pyplot as plt | |
| import streamlit as st | |
| st.write(""" | |
| # Stock Price Prediction | |
| Shown is the stock prediction of the next working day taking into account the last 10 working days | |
| """) | |
| model = keras.models.load_model('model_stock_prices.h5') | |
| working_days = st.sidebar.slider("Show the historical data of the following last working days", min_value = 10, max_value=20) | |
| working_days = int(working_days) | |
| # 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) | |
| p = predictions[0][0][0] | |
| p = float(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) | |
| st.write(""" | |
| # Historical prices data | |
| Shown is the historical data of the prices (can be adapted with the values from the sidebar) | |
| """) | |
| today = date.today() | |
| days_ago = today - timedelta(days=25) | |
| # we get the last 30 days and keep just the last 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 working days data points | |
| hist = hist[-working_days:] | |
| 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) | |
| hist | |