Store model credentials in .env files
Learn how to store model identifier credentials in an .env file instead of using inline credentials, allowing you to follow best practices for security when running Jupyter Notebooks.
Prerequisites
1. Store credentials in an .env file
In the ValidMind Platform, retrieve the code snippet for your model:
In the left sidebar, click Inventory.
Select a model by clicking on it or find your model by applying a filter or searching for it.3
In the left sidebar that appears for your model, click Getting Started.
Select the document you want to automatically upload test results to.4
Click Copy snippet to clipboard.
Create a new file in the same folder as your notebook and name it
.env.This is a hidden file, so you may need to change your settings to view it.
Use the code snippet from your clipboard to build the credentials in your
.envfile in the following format:5VM_API_HOST=<api_host> VM_API_KEY=<api_key> VM_API_SECRET=<api_secret> VM_API_MODEL=<model>
5 For example, if your credentials look like this:
import validmind as vm
vm.init(
api_host = "https://api.prod.validmind.ai/api/v1/tracking",
api_key = "key123",
api_secret = "secret456",
document = "document-key",
model = "model789"
)Then your .env file should look like this:
VM_API_HOST=https://api.prod.validmind.ai/api/v1/tracking
VM_API_KEY=key123
VM_API_SECRET=secret456
VM_API_MODEL=model7892. Use credentials in your Jupyter Notebook
After installing the ValidMind Library within your Jupyter Notebook,7 insert this code snippet in a code cell above your model identifier credentials:
%load_ext dotenv %dotenv dev.envRemove the inline credentials from
vm.init()after importing the library:Example updated code cell:
%load_ext dotenv %dotenv .env import validmind as vm vm.init( document="document-key", )Example updated code cell including ongoing monitoring:8
%load_ext dotenv %dotenv .env import validmind as vm vm.init( document="monitoring", monitoring=True )Run the cell.
Instead of using inline credentials, this code cell will now load your model credentials from a
.envfile.