Reputation: 111
I have an existing machine learning model saved on my local system. I want to deploy this model as a web service so I can consume this model as a request-response i.e. send an HTTP request to the model and get back a predicted response.
When attempting to deploy this model on AzureML I run into a few problems
The model needs to be initialized in an entry script int the init() function, but for initializing my model I have a custom class and require few txt files to be loaded.
below is the code to initialize the model object
from model_file import MyModelClass # this is the file which contains the model class
def init():
global robert_model
my_model = MyModelClass(vocab_path='<path-to-text-files>',
model_paths=['<path-to-model-file>'],
iterations=5,
min_error_probability=0.0,
min_probability=0.0,
weigths=None)
def run(json_data):
try:
data = json.loads(json_data)
preds, cnt = my_model.handle_batch([sentence.split()])
return {'output': pred, 'count': cnt}
except Exception as e:
error = str(e)
return error
I don't know how to import those class files and text files in the entry script
I don't know much about azure, and I am having a hard time figuring this out. Please help.
Upvotes: 4
Views: 4740
Reputation: 217
The additional files related to a model can be uploaded directly to Azure ML Workspace(with GUI) in source-directory, which is HOME > Notebooks. (Refer to the image).
Add files into this(source-directory) using, Add Files button
The uploaded files can be accessed os.getEnv() method.
from azureml.core import Workspace
from azureml.core import Environment
import os
# Connect to WS
ws = Workspace(subscription_id="sid",
resource_group="rg",
workspace_name="wsname")
# Creating inference Config for demo
env = Environment(name='environment_name')
inf_config = InferenceConfig(environment=env, source_directory=os.getenv('ENV_NAME'), entry_script='./score.py')
Upvotes: 1
Reputation: 111
I found the solution:
The solution was right there I just had to read more in the official documentation. Below I have explained the solution to my problem.
When deploying a model on Azure Machine Learning Studio we have to prepare 3 things:
Entry Script is the actual python script that makes predictions.
Deployment Configuration can be thought of as the computer where your model will run.
Inference Configuration defines the software dependencies of your model. It is here, where we can also provide text/static files.
The inferece_config
takes an argument called source_directory
. We can provide a path to a folder to the source_directory argument like below
inference_config = InferenceConfig(
entry_script="<entry-script.py>",
environment=myenv,
source_directory="<path-to-your-folder>",
)
We can place all our requirements in this folder, including text, static, CSV, python scripts, etc files. The entry script must also be placed in this folder. This folder will be directly copied by azure in the deployment image that gets created. This way we can access these files in the entry script using regular python syntax.
For my solution:
If I have my inference_config
like this
inference_config = InferenceConfig(
entry_script="score.py",
environment=myenv,
source_directory='./assets',
)
And have this in the ./assets
folder
assets
├── my_model_class.py
├── score.py
└── data
└── vocaublary.txt
1 directory, 3 files
Then my entry script (score.py) will look something like this:
from assets.my_model_class import MyModelClass
def init():
global my_model
model_path = os.path.join(os.getenv('AZUREML_MODEL_DIR'),
'my_model.pkl',)
my_model = MyModelClass(vocab_path='assets/data/vocaublary.txt',
model_path=model_path,)
def run(json_data):
try:
data = json.loads(json_data)
prediction = my_model.predict(data)
return {'data': prediction}
except Exception as e:
// do error handling
return error
Note: We have to import from assets.my_model_class
even though score.py
is present in assets
folder because, after deployment the CWD(Current Working Directory) changes to be the parent folder of assets
folder
Upvotes: 6
Reputation: 4174
For txt/static files - you could upload to Azure Blobs and consume them on fly. And for classes, you can encapsulate your classes inside the modules.
There are 2 options you could do here
Option 1 :
You could publish your module to pip and then consume it.
Option 2 :
The "Private wheel files" solution as described in this article. The below is the sample environment and pls look closely under the pip section.
name: project_environment
dependencies:
# The python interpreter version.
# Currently Azure ML only supports 3.5.2 and later.
- python=3.6.2
- pip:
- scikit-learn==0.22.1
- azureml-defaults
- https://myworkspaceid.blob.core/azureml/Environment/azureml-private-packages/my-wheel-file-name.whl
channels:
- conda-forge
Saw a reference code and similar discussion in this thread.
Upvotes: 0