Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import joblib
|
| 3 |
+
from huggingface_hub import HfApi
|
| 4 |
+
import pickle
|
| 5 |
+
import yfinance as yf
|
| 6 |
+
from datetime import datetime, timedelta
|
| 7 |
+
from forex_python.converter import get_rate
|
| 8 |
+
import pandas as pd
|
| 9 |
+
import numpy as np
|
| 10 |
+
import cpi
|
| 11 |
+
from sklearn.preprocessing import MinMaxScaler
|
| 12 |
+
import matplotlib.pyplot as plt
|
| 13 |
+
import streamlit as st
|
| 14 |
+
|
| 15 |
+
#cpi.update()
|
| 16 |
+
|
| 17 |
+
# from huggingface_hub import notebook_login
|
| 18 |
+
# notebook_login()
|
| 19 |
+
|
| 20 |
+
from huggingface_hub import hf_hub_download
|
| 21 |
+
m = hf_hub_download(repo_id="marvmk/model-test", filename="model.pkl")
|
| 22 |
+
model = pickle.load(open(m, 'rb'))
|
| 23 |
+
|
| 24 |
+
# downloading the last 10 days to make the prediction
|
| 25 |
+
from datetime import date
|
| 26 |
+
|
| 27 |
+
today = date.today()
|
| 28 |
+
days_ago = today - timedelta(days=20)
|
| 29 |
+
|
| 30 |
+
# we get the last 20 days and keep just the last 10 working days, which have prices
|
| 31 |
+
nasdaq = yf.Ticker("^IXIC")
|
| 32 |
+
hist = nasdaq.history(start=days_ago, end=today)
|
| 33 |
+
hist = hist.drop(columns=['Dividends', 'Stock Splits'])
|
| 34 |
+
|
| 35 |
+
# keeping the last 10 data points
|
| 36 |
+
hist = hist[-10:]
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
inflation = []
|
| 40 |
+
for t in hist.index:
|
| 41 |
+
inflation.append(get_rate("USD", "EUR", t))
|
| 42 |
+
|
| 43 |
+
cpi_items_df = cpi.series.get(seasonally_adjusted=False).to_dataframe()
|
| 44 |
+
cpi_items_df = cpi_items_df[cpi_items_df['period_type']=='monthly']
|
| 45 |
+
cpi_items_df['date'] = pd.to_datetime(cpi_items_df['date'])
|
| 46 |
+
cpi_items_df = cpi_items_df.set_index('date')
|
| 47 |
+
cpi_df = cpi_items_df['value'].loc['2022':'2023']
|
| 48 |
+
|
| 49 |
+
cpi_col = []
|
| 50 |
+
for x in hist.index:
|
| 51 |
+
# ts = datetime(x.year, x.month, 1)
|
| 52 |
+
|
| 53 |
+
# just adding the latest inflation rate
|
| 54 |
+
cpi_col.append(cpi_df[-1])
|
| 55 |
+
|
| 56 |
+
hist['Inflation'] = inflation
|
| 57 |
+
hist['CPI'] = cpi_col
|
| 58 |
+
|
| 59 |
+
hist['Quarter_end'] = np.where(hist.index.month%3==0,1,0)
|
| 60 |
+
|
| 61 |
+
s = hf_hub_download(repo_id="marvmk/scalable_project", filename="scaler.save", repo_type='dataset')
|
| 62 |
+
scaler = joblib.load(s)
|
| 63 |
+
|
| 64 |
+
inp = scaler.transform(hist.to_numpy())
|
| 65 |
+
|
| 66 |
+
df = inp
|
| 67 |
+
temp_df = pd.DataFrame(inp, columns = ['Open','High','Low','Close','Volume','Inflation', 'CPI', 'Quarter_end'])
|
| 68 |
+
ds = []
|
| 69 |
+
ds.append(temp_df[0:10])
|
| 70 |
+
ds = np.array(ds)
|
| 71 |
+
|
| 72 |
+
predictions = model.predict(ds)
|
| 73 |
+
predictions
|
| 74 |
+
p = predictions[0][0]
|
| 75 |
+
print(p)
|
| 76 |
+
a = np.array([0,0,0,p,0,0,0,0])
|
| 77 |
+
a = scaler.inverse_transform(a.reshape(1,-1))
|
| 78 |
+
final_prediction = a[-1][3]
|
| 79 |
+
|
| 80 |
+
prediction = []
|
| 81 |
+
#prediction.append(final_prediction)
|
| 82 |
+
close = hist['Close'].to_list()
|
| 83 |
+
print(close)
|
| 84 |
+
for c in close:
|
| 85 |
+
prediction.append(c)
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
prediction.append(final_prediction)
|
| 89 |
+
print(prediction)
|
| 90 |
+
plt.figure(figsize = (20,10))
|
| 91 |
+
plt.plot(prediction, label="Prediction")
|
| 92 |
+
plt.plot(hist['Close'].to_list()[-10:], label="Previous")
|
| 93 |
+
plt.ylabel('Price US$', fontsize = 15 )
|
| 94 |
+
plt.xlabel('Working Days', fontsize = 15 )
|
| 95 |
+
plt.title("NASDAQ Stock Prediction", fontsize = 20)
|
| 96 |
+
plt.legend()
|
| 97 |
+
plt.grid()
|
| 98 |
+
|
| 99 |
+
st.pyplot(plt)
|