Repository navigation
Expand file tree
/
Copy path4_Model_Deployment_using_FastAPI.py
More file actions
248 lines (151 loc) · 4.75 KB
/
Copy path4_Model_Deployment_using_FastAPI.py
File metadata and controls
248 lines (151 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
#import modules
import os
import sqlite3
from glob import glob
import joblib
import pandas as pd
import requests
from arch.univariate.base import ARCHModelResult
from data import SQLRepository
# In[ ]:
#open connection to database
connection = sqlite3.connect(database=os.getenv('DB_NAME'), check_same_thread=False)
#instantiate sqlrepository from data module
repo = SQLRepository(connection=connection, ticker='MSFT')
# In[ ]:
from model import GarchModel
# Instantiate a `GarchModel`
gm_stock = GarchModel(ticker="MSFT", repo=repo, use_new_data=False)
# Does `gm_ambuja` have the correct attributes?
assert gm_stock.ticker == "MSFT"
assert gm_stock.repo == repo
assert not gm_stock.use_new_data
assert gm_stock.model_directory == os.getenv('MODEL_DIRECTORY')
# In[ ]:
# Instantiate `GarchModel`, use new data
model_stock = GarchModel(ticker="TSLA", repo=repo, use_new_data=True)
# Check that model doesn't have `data` attribute yet
assert not hasattr(model_stock, "data")
# Wrangle data
model_stock.wrangle_data(n_observations=1000)
# Does model now have `data` attribute?
assert hasattr(model_stock, "data")
# Is the `data` a Series?
assert isinstance(model_stock.data, pd.Series)
# Is Series correct shape?
assert model_stock.data.shape == (1000,)
model_stock.data.tail()
# In[ ]:
# Instantiate `GarchModel`, use old data
model_stock = GarchModel(ticker="AAPL", repo=repo, use_new_data=False)
# Wrangle data
model_stock.wrangle_data(n_observations=1000)
# Fit GARCH(1,1) model to data
model_stock.fit(p=1, q=1)
# Does `model_shop` have a `model` attribute now?
assert hasattr(model_stock, "model")
# Is model correct data type?
assert isinstance(model_stock.model, ARCHModelResult)
# Does model have correct parameters?
assert model_stock.model.params.index.tolist() == ["mu", "omega", "alpha[1]", "beta[1]"]
# Check model parameters
model_stock.model.summary()
# In[ ]:
# Generate prediction from `model_shop`
prediction = model_stock.predict_volatility(horizon=5)
# Is prediction a dictionary?
assert isinstance(prediction, dict)
# Are keys correct data type?
assert all(isinstance(k, str) for k in prediction.keys())
# Are values correct data type?
assert all(isinstance(v, float) for v in prediction.values())
prediction
# In[ ]:
# Save `model_shop` model, assign filename
filename = model_stock.dump()
# Is `filename` a string?
assert isinstance(filename, str)
# Does filename include ticker symbol?
assert model_stock.ticker in filename
# Does file exist?
assert os.path.exists(filename)
filename
# In[ ]:
model_stock = GarchModel(ticker="AAPL", repo=repo, use_new_data=False)
# Check that new `model_stock_test` doesn't have model attached
assert not hasattr(model_stock, "model")
# Load model
model_stock.load()
# Does `model_stock_test` have model attached?
assert hasattr(model_stock, "model")
model_stock.model.summary()
# In[ ]:
#get request to hello path
url = "http://127.0.0.1:8000/hello"
response = requests.get(url=url)
print('status code: ', response.status_code)
response.json()
# In[ ]:
#instantiate FitIn and FitOut
from main import FitIn, FitOut
fi = FitIn(ticker= 'MSFT', use_new_data=True, n_observations=2500, p=1, q=1)
print(fi)
fo = FitOut(ticker= 'MSFT', use_new_data= True, n_observations=2500, p=1, q=1, success=True, message='Nicely fitted')
print(fo)
# In[ ]:
#build model with build_model
from main import build_model
#instantiate GarchModel
model_stock = build_model(ticker='TSLA', use_new_data=False)
# Is `SQLRepository` attached to `model_shop`?
assert isinstance(model_stock.repo, SQLRepository)
# Is SQLite database attached to `SQLRepository`
assert isinstance(model_stock.repo.connection, sqlite3.Connection)
# Is `ticker` attribute correct?
assert model_stock.ticker == "TSLA"
# Is `use_new_data` attribute correct?
assert not model_stock.use_new_data
model_stock
# In[ ]:
# URL of `/fit` path
url = 'http://127.0.0.1:8000/fit'
# Data to send to path
json = {
'ticker':'META',
'n_observations': 2500,
'use_new_data': True,
'p':1,
'q':1
}
# Response of post request
response = requests.post(url=url, json=json)
# Inspect response
print("response code:", response.status_code)
response.json()
# In[ ]:
from main import PredictIn, PredictOut
pi = PredictIn(ticker="SHOPERSTOP.BSE", n_days=5)
print(pi)
po = PredictOut(
ticker="SHOPERSTOP.BSE", n_days=5, success=True, forecast={}, message="success"
)
print(po)
# In[ ]:
#get predictions
#url of '/predict' path
url = 'http://127.0.0.1:8000/predict'
# Data to send to path
json = {
'ticker':'META',
'n_days':6,
'use_new_data':False
}
#response of post request
response = requests.post(url=url, json=json)
# Inspect response
print("response code:", response.status_code)
response.json()
# In[ ]: