Victorlopo21 commited on
Commit
80726db
·
1 Parent(s): a0793b5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +149 -0
app.py ADDED
@@ -0,0 +1,149 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
13
+ #cpi.update()
14
+
15
+ import gradio as gr
16
+
17
+ from huggingface_hub import notebook_login
18
+ notebook_login()
19
+ import hopsworks
20
+
21
+ # from huggingface_hub import hf_hub_download
22
+ # m = hf_hub_download(repo_id="marvmk/model-test", filename="model.pkl")
23
+
24
+ project = hopsworks.login(api_key_value="4CY1rwa8iz8Yu6gG.TwayrYmsX4GQfhSp3LNKYTLvyFMfqAvnzNUQp5ae9K5HhfYxb5mcnLAutm1K18zV")
25
+ mr = project.get_model_registry()
26
+ model = mr.get_model("stock_modal", version=1)
27
+ model_dir = model.download()
28
+ model = joblib.load(model_dir + "/stock_model.pkl")
29
+
30
+ #model = pickle.load(open(m, 'rb'))
31
+
32
+ # downloading the last 10 days to make the prediction
33
+ from datetime import date
34
+
35
+ today = date.today()
36
+ days_ago = today - timedelta(days=20)
37
+
38
+ # we get the last 20 days and keep just the last 10 working days, which have prices
39
+ nasdaq = yf.Ticker("^IXIC")
40
+ hist = nasdaq.history(start=days_ago, end=today)
41
+ hist = hist.drop(columns=['Dividends', 'Stock Splits'])
42
+
43
+ # keeping the last 10 data points
44
+ hist = hist[-10:]
45
+
46
+
47
+ inflation = []
48
+ for t in hist.index:
49
+ inflation.append(get_rate("USD", "EUR", t))
50
+
51
+ cpi_items_df = cpi.series.get(seasonally_adjusted=False).to_dataframe()
52
+ cpi_items_df = cpi_items_df[cpi_items_df['period_type']=='monthly']
53
+ cpi_items_df['date'] = pd.to_datetime(cpi_items_df['date'])
54
+ cpi_items_df = cpi_items_df.set_index('date')
55
+ cpi_df = cpi_items_df['value'].loc['2022':'2023']
56
+
57
+ cpi_col = []
58
+ for x in hist.index:
59
+ # ts = datetime(x.year, x.month, 1)
60
+
61
+ # just adding the latest inflation rate
62
+ cpi_col.append(cpi_df[-1])
63
+
64
+ hist['Inflation'] = inflation
65
+ hist['CPI'] = cpi_col
66
+
67
+ hist
68
+
69
+ hist['Quarter_end'] = np.where(hist.index.month%3==0,1,0)
70
+
71
+ # def build_sequences(nump, target_labels=['Close'], window=10, stride=1, telescope=1):
72
+ # # Sanity check to avoid runtime errors
73
+ # df = pd.DataFrame(nump, columns = ['Open','High','Low','Close','Volume','Inflation', 'CPI', 'Quarter_end'])
74
+ # assert window % stride == 0
75
+ # dataset = []
76
+ # labels = []
77
+ # temp_df = df.copy().values
78
+ # temp_label = df[target_labels].copy().values
79
+ # padding_len = len(df)%window
80
+
81
+ # if(padding_len != 0):
82
+ # # Compute padding length
83
+ # padding_len = window - len(df)%window
84
+ # padding = np.zeros((padding_len,temp_df.shape[1]), dtype='float64')
85
+ # temp_df = np.concatenate((padding,df))
86
+ # padding = np.zeros((padding_len,temp_label.shape[1]), dtype='float64')
87
+ # temp_label = np.concatenate((padding,temp_label))
88
+ # assert len(temp_df) % window == 0
89
+
90
+ # for idx in np.arange(0,len(temp_df)-window-telescope,stride):
91
+ # dataset.append(temp_df[idx:idx+window])
92
+ # labels.append(temp_label[idx+window:idx+window+telescope])
93
+
94
+ # dataset = np.array(dataset)
95
+ # labels = np.array(labels)
96
+ # return dataset, labels
97
+
98
+ hist
99
+
100
+ s = hf_hub_download(repo_id="marvmk/scalable_project", filename="scaler.save", repo_type='dataset')
101
+ scaler = joblib.load(s)
102
+
103
+ inp = scaler.transform(hist.to_numpy())
104
+ #inp = scaler.inverse_transform(inp)
105
+
106
+ df = inp
107
+ temp_df = pd.DataFrame(inp, columns = ['Open','High','Low','Close','Volume','Inflation', 'CPI', 'Quarter_end'])
108
+ ds = []
109
+ ds.append(temp_df[0:10])
110
+ ds = np.array(ds)
111
+
112
+ ds
113
+
114
+ predictions = model.predict(ds)
115
+ predictions
116
+ p = predictions[0][0]
117
+ print(p)
118
+ a = np.array([0,0,0,p,0,0,0,0])
119
+
120
+ a = scaler.inverse_transform(a.reshape(1,-1))
121
+ a
122
+
123
+ final_prediction = a[-1][3]
124
+
125
+ final_prediction
126
+
127
+ import matplotlib.pyplot as plt
128
+ import streamlit as st
129
+
130
+ prediction = []
131
+ #prediction.append(final_prediction)
132
+ close = hist['Close'].to_list()
133
+ print(close)
134
+ for c in close:
135
+ prediction.append(c)
136
+
137
+
138
+ prediction.append(final_prediction)
139
+ print(prediction)
140
+ plt.figure(figsize = (20,10))
141
+ plt.plot(prediction, label="Prediction")
142
+ plt.plot(hist['Close'].to_list()[-10:], label="Previous")
143
+ plt.ylabel('Price US$', fontsize = 15 )
144
+ plt.xlabel('Working Days', fontsize = 15 )
145
+ plt.title("NASDAQ Stock Prediction", fontsize = 20)
146
+ plt.legend()
147
+ plt.grid()
148
+
149
+ st.pyplot(plt)