Last active
September 24, 2021 17:31
-
-
Save zeryx/52d4e9592c9e0c51b63dad1adbbd4550 to your computer and use it in GitHub Desktop.
An algorithm that attempts to reload it's model file (if it's been updated) every 5 minutes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Algorithmia | |
from time import time | |
import pickle | |
from src.data import data | |
client = Algorithmia.client() | |
DATA_MODEL_DIR = "data://.my/example" | |
MODEL_NAME = "example.pkl" | |
TIME_0 = 0 | |
LAST_MODIFIED = "" | |
MODEL = None | |
DATA_MODEL_DIR = client.dir(DATA_MODEL_DIR) | |
def maybe_load(): | |
global TIME_0, MODEL, DATA_MODEL_DIR, LAST_MODIFIED | |
TIME_1 = time() | |
# Lets only trigger our maybe load operation every 5 minutes; except for the first time this function is called. | |
if TIME_1 - TIME_0 > 300: | |
TIME_0 = TIME_1 | |
current_modified = None | |
# Since the only way to get the status of a file in the data API without downloading is via iteration of the | |
# directory; this lets us know if the model file has been changed without downloading it. | |
for f in DATA_MODEL_DIR.files(): | |
if f.getName() == MODEL_NAME: | |
current_modified = f.last_modified | |
# If we haven't loaded a model yet, we should go and do it now | |
if MODEL is None and DATA_MODEL_DIR.file(MODEL_NAME).exists(): | |
local_file = DATA_MODEL_DIR.file(MODEL_NAME).getFile().name | |
with open(local_file, 'rb') as f: | |
MODEL = pickle.load(f) | |
# However if we already have a model; lets make sure that the file's been changed before we redownload and | |
# load into memory | |
elif current_modified is not None and current_modified != LAST_MODIFIED: | |
LAST_MODIFIED = current_modified | |
local_file = DATA_MODEL_DIR.file(MODEL_NAME).getFile().name | |
with open(local_file, 'rb') as f: | |
MODEL = pickle.load(f) | |
# Dummy apply function expecting a scikit-learn model | |
def apply(input): | |
maybe_load() | |
result = MODEL.predict(input) | |
return result | |
maybe_load() | |
if __name__ == "__main__": | |
print(apply([1, 1, 1])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment