Deploy your Machine Learning Python Codes with Azure Functions

lalitha raghavan
Towards Data Science
3 min readJan 30, 2021

--

Credits
Photo by Romain V on Unsplash

Azure Functions are code triggered events that scale well owing to their serverless capabilities. The triggering mechanisms are diverse — time based (CRON functions), http, storage triggers (blob, queue) etc. Functions are also unique in that they can be coded in a variety of languages like C#, Python, Javascript etc. In this article, I will be demonstrating a simple ML python script that is triggered using Azure functions using VS code. You can check my git repo here.

Requirements:

  • VS Code with Azure extensions loaded
  • Azure subscription (free credits available)
  • Azure function CLI installed. Please follow https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local?tabs=windows%2Ccsharp%2Cbash
  • A pickled ML model. Here I have picked a simple model that uses a regression algorithm to predict the price of a piece of real estate based on factors like age of the building, the distance from the city, the number of stores around the place, latitude and longitude. The focus has been on the implementation of the model in Azure functions and not the accuracy of the model itself.

Place the python code and pkl files in a separate folder. Recommended to use a virtual environment for installing the necessary files. Freeze the requirements file in the same folder.

Launch VS code. Fire up the function app from the extension, choose http trigger as the trigger and choose Anonymous as the trigger type. Launch the workspace and folder to where the python code and the pkl files are stored — requirements file is automatically created. The necessary files needed for running the code are further added, which include the packages necessary for running the pkl file. Launching the function automatically creates an init.py and a function.json file apart from other helpers.

Let’s dig a little deeper into what the init.py file is actually doing — in this basic code, the init.py file is the script that contains the main code for the function app, that triggers the http and has the post and get requests. Results of the json dump are returned as a numpy array after the prediction on the model has been performed. The function.json file is simply a binder (that would change based on the trigger used) that points to the init.py file. Please note the anonymous authentication level used.

Based on your requirement, you can change these settings. In case you are accessing based on a file change to your blob, for example, make sure you have your secure SAS keys in place.

That’s about it, really. Now it’s time to test your own function app. In the command prompt initialize the function with:

func init
func host start

You should be able to see some ascii characters. You will be directed to a link on the local. Allow it to be. This shows that the function app is actually working without flaws until now.

Open up git bash and run the following command:

curl -w '\n' 'http://localhost:7071/api/real_estate?Age=25&Dist=251&Num_stores=5&Lat=24.9756&Long=121.5521'

You can of course change the values for age, dist etc. On running this bash command, a success in the python terminal should be shown along with the predicted value in the bash terminal.

Deploying it to Azure via the vscode is the easiest way. This is pretty straightforward — use the deployment commands as prompted from the Functions extension. Your app should be ready. Use the weblink as deployed and add the same parts to the http link (?Age=25&Dist=251&Num_stores=5&Lat=24.9756&Long=121.5521).

You should see the same response in your weblink as well.

Alright, that’s pretty much it!!! — this is now an end to end pipeline to trigger a python script using vscode and azure functions. Azure functions are pretty versatile and can be put up in a pipeline with datafactory for more complex works.

--

--