Storing Machine Learning Model in Anvil

I have a model built in Jupyter that I currently have an Anvil app calling thru Uplink. I would like to store the model and it’s attributes in Anvil so that I may shutdown the Jupyter instance. Is this possible? or are there more efficient ways to accomplish this?

Hello and welcome,

You may be able to use the joblib library to save and load your model object. It is a part of SciPy and therefore is installed on the Anvil servers. For example:

On your local machine:

# save the model to disk on your local machine
import joblib
.
.
.
joblib.dump(model, 'my_model.sav')
 
# Save that file into an Anvil datatable:
# You could write uplink code to transfer the file to Anvil or
# use the DataTables IDE to manually upload it or
# use a FileLoader component

# example of saving it to a data table using code (untested and from memory!):
anvil.server.connect(<uplink key>)

with open('my_model.sav', 'rb') as file:
    media = anvil.BlobMedia(content_type='application/octet-streamf', content=file.read(), name='my_model_name')
    app_tables.my_table.add_row(media_column=media)

On the Anvil Server:

import joblib
.
.
.
# load the model from disk (now on the Anvil server)
loaded_model = joblib.load('my_model.sav')
result = loaded_model.score(X_test, Y_test)

Similarly, if you can pickle your model, then you may be able to use Anvil’s cloudpickle package to load the model back in. There may be other ways but this is what comes to mind.

1 Like

Thank you campopianoa. I have the media object in a table, however I am having problems retrieving with joblib or any app_table commands. Any suggestions?

Is it possible to share more information? For example, what is the error?

If you could demonstrate what you have tried with code or an example app clone, that would be great. I’m sure you are almost there.

This is the error I am receiving:
FileNotFoundError: [Errno 2] No such file or directory: ‘get_pred_churn.sav’ at /usr/local/lib/python3.7/site-packages/joblib/numpy_pickle.py, line 567 called from [ServerModule1, line 53](javascript:void(0)) called from [Form1, line 118](javascript:void(0))

and the code:
@anvil.server.callable
def get_model():
model = joblib.load(‘get_pred_churn.sav’)
return model

I am pretty new to this

No problem we’re happy to help. I’m away from a computer at the moment but I forgot to mention that you probably need to save the media object to a temporary file and then read it in as normal.

For now until I get back to the computer perhaps search the docs and forum for things like “temp file media object”, “temp directory”, etc. Pretty sure there are some great doc pages about this.

1 Like

Okay, I’m back to a computer. Give this a try to see if you can read in your model.

import anvil.media

# in a server function
with anvil.media.TempFile(your_media_object) as file_name:
  # Now there is a file in the filesystem with the contents of media object
  # The file_name variable is a string of its full path.

  my_model = joblib.load(file_name)

Here are the docs I was referring to that detail dealing with media objects and the filesystem.

2 Likes