While Model APIs are commonly used when you need an API to query your model in near real-time, you may run into a situation where you want your Model API to write a file back to the Domino File System when it's called. Using the Domino python package, you can upload your created file(s) back to your project!
Setup
Model APIs do not inherit environment variables, so in order to use the Domino python package you'll need to specify environment variables for DOMINO_API_HOST and DOMINO_USER_API_KEY within your Model API. These can be added under your Model settings > Environment.
If you're unsure of these variables, one easy way to grab them is from a workspace:
import os
print(os.environ['DOMINO_API_HOST'])
print(os.environ['DOMINO_USER_API_KEY'])
For this example, we'll be writing a simple set of results to a .json file in a subfolder "model_output", and use the timestamp for a filename.
Example Python script:
from domino import Domino import time import json def create_file(): file_name = time.strftime('%Y-%m-%d_%H%M%S.json') results_folder = "model_output/" file_path = results_folder + file_name # Data to be written results = { "name": "Domino", "test_date": time.strftime('%Y-%m-%d'), "result_1": 56, "result_2": 8.6 } with open(file_name, "w") as f: f.write(json.dumps(results)) domino = Domino("<project-owner>/<project-name>") f = open(file_name, 'rb') r = domino.files_upload("/" + file_path, f)
In the call to the Domino python package, you'll replace <project-owner>/<project-name> with your actual project owner name and project name.
We can now call this model API, and we'll see our .json file populated in /mnt/model_output
! You can adapt this to your own needs, including any file type, file name, and file content, just remember to call the domino.files_upload()
in your script.
*Note
If you're running Domino 5.0 - 5.2, the code above will result in a permission error, which can be seen in the Results:
{"error":{"message":"[Errno 13] Permission denied: '2022-10-07_181404.json'"},
"model_time_in_ms":0,"
release":{"harness_version":"0.1","model_version":"63406a9ac51c7570972943ae","model_version_number":1},
"request_id":"NSG6LPDTI1V4KQA5","timing":0.33402442932128906}
or the Instance Logs:
PermissionError: [Errno 13] Permission denied: '2022-10-07_181404.json'
This is due to a Model API permissions change to these specific Domino versions. More information is available here: https://tickets.dominodatalab.com/hc/en-us/articles/8125256142996-Model-permissions-change-for-Domino-version-5-0-5-2-
One quick workaround for this is to create your initial file in a temp directory (/tmp
), which will have different write permissions, before uploading your final file to your model_output
. Modifying the script from above, we can add /tmp/
to our initial file path where the .json file is written, before uploading to our final location:
from domino import Domino import time import json def create_file(): file_name = time.strftime('%Y-%m-%d_%H%M%S.json') results_folder = "model_output/" temp_file = "/tmp/" + file_name file_path = results_folder + file_name # Data to be written results = { "name": "Domino", "test_date": time.strftime('%Y-%m-%d'), "result_1": 56, "result_2": 8.6 } with open(temp_file, "w") as f: f.write(json.dumps(results)) domino = Domino("<project-owner>/<project-name>") f = open(temp_file, 'rb') r = domino.files_upload("/" + file_path, f)
Comments
0 comments
Please sign in to leave a comment.