assapin / secure_inference

Example for how to obfuscate source code and models
1 stars 1 forks source link

Steps to use it #1

Open elisim opened 2 years ago

elisim commented 2 years ago

Hi @assapin,

Thanks for the great example. Can you please elaborate which steps should I do in order to obfuscate my model?

I serve my model similar to Steven Cutting's blog:

https://www.stevencuttingblog.com/notes/practical-packaging-for-machine-learning-solutions

TL;DR: I have the model pickled in my repo, and I release a pip package with a predict method to run my inference.

Thanks, Eli

assapin commented 2 years ago

in your project, add at least the following python packages .github (and all its sub-folders) - directly in the root of the project

And the python packages:

config encryption key test .env

should go under the iris_classifier subfolder

Changes you will need to make:

  1. in the .env, edit the properties indicating the name of your .pkl file (just name, not path)
  2. add the methods that load the decrypted model from inference.py into iris.py
  3. if you place the model pickle in a different package (not model), then you will need to edit the methods in (2) that rely on the package to find the model pkl file (asterixes for emphasis only)

    def load_model(is_secure=None, model_file_name=None):
    import **data**
    secure = is_secure or settings.use_secure
    file_to_load = model_file_name or settings.get_model_file_name()
    
    with pkg_resources.path(**data**, file_to_load) as p:
        path = p
    if secure:
        path = BytesIO(decrypt_file_inner(key=fernet_key.key, source_path=path))
        path.seek(0)
    return joblib.load(path)
  4. You will need to edit the github-actions-secure.yaml to reflect your project's paths (e.g. replace src with iris_classifier, replace /model/ with /data/) for example (asterixes for emphasis only)

run: echo "key = '"$ENCRYPTION_KEY"'" > ./**src**/key/fernet_key.py --> run: echo "key = '"$ENCRYPTION_KEY"'" > ./**iris_classifier**/key/fernet_key.py

elisim commented 2 years ago

Thank you for the detailed explanation. I'll do the steps and will update here 🙂

Thanks again, Eli