Running Machine Learning Model in Azure Function
Tags: machinelearning, python, cloud, azurefunctions
Published: 2023-08-25 14:49:30

<p class="text-center">☀️☀️☀️</p>
<p>Hi! How's your weekend? I hope it's great. I've been taking a long break since I moved into a new place. Anyway, I'd like to show you how I managed to utilize Azure Function to run amachine learning model. Hopefully, by the end of this post, you're able to understand how it works.</p>

<hr/>

<h3>What's Azure Function?</h3>
<p>Azure Function is just another service among other good services offered by Microsoft Azure. It's known as a serverless solution. That is, it allow us to focus on writing code instead of worrying about server infrastructure.</p>

<p>So what are the differences between running application on Azure Function and Virtual Machine? This is a good question. Firstly, Azure Function is more cost-effective. In fact, our application will only be charged depending on its running time. Secondly, it's also easier to manage. No need to configure extra settings whatsoever to run application.</p>

<h3>Requirements</h3>
<ol>
<li><a href="https://www.python.org/downloads/release/python-370/">Python 3.7</a></li>
<li><a href="https://go.microsoft.com/fwlink/?linkid=2174087">Azure Functions Core</a> (Not a hard requirement, but will make life easier)</li>
<li><a href="https://code.visualstudio.com/">Visual Studio Code</a> + <a href="https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-azurefunctions">Azure Function Extension</a> (Again, not a hard requirement)</li>
<li><a href="https://learn.microsoft.com/en-us/azure/azure-functions/functions-overview?pivots=programming-language-python">Azure Function Service</a></li>
<li><a href="https://learn.microsoft.com/en-us/azure/storage/common/storage-account-overview">Azure Storage Account</a></li>
<li>A machine learning model</li>
</ol>

<h3>Steps</h3>
<p>In this article, we're making use of Graph Convolutional Network (GCN) machine learning model. To give more detail, it's a specific type of neural network model that accepts a graph data as an input. It then outputs a visualization of the graph's node embeddings. If you're interested to learn more about GCN, feel free to check out this <a href="https://tkipf.github.io/graph-convolutional-networks/">blog</a> first. Otherwise, go ahead clone the repository <a href="https://github.com/wildanazz/gcn.git">here</a>.</p>

<p>Open the __init__.py and read the code carefully.</p>
<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d2res7865ycvr3efsejg.png"/>
<p>Notice there are HTTP request and response in the code. That is because our function is making use of a HTTP trigger. If you don't know what HTTP trigger is, it's simply a method to let us invoke our function using HTTP request. There are several triggers available to use in Azure Function such as HTTP, timer, and blob.</p>

<p>Open the local.settings.json.</p>
<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ra2ixh2wussnmyy1sesa.png"/>
<p>Add your storage account connection string. So what's storage account? storage account is an object storage solution offered by the Azure cloud. We're using this storage to store our function's result.</p>

<p>Use the Azure Functions Core tool to initialize a Python function app.</p>
<pre><code>$ func init</code></pre>
<p>Run the following commands to create and activate a virtual environment named .venv. </p>
<pre><code>$ py -3.7 -m venv .venv</code></pre>
<pre><code>$ .venv\scripts\activate</code></pre>
<p>Install the dependencies by running the following command. Installation may take a few minutes.</p>
<pre><code>$ pip install --no-cache-dir -r requirements.txt</code></pre>
<p>Start the function by starting the local Azure Function runtime host.</p>
<pre><code>$ func start</code></pre>
<h4>Note:</h4>
<p>If encounter any errors, make sure the correct version of python is already installed. Otherwise, try upgrading the python pip and install the dependencies again. If the errors still persist, <a href="https://www.google.com/">Google</a> is the best place to ask questions.</p>

<h4>Extra steps (Nice to follow!)</h4>
<p>We may now test our function. Download graph dataset <a>here</a>. What is this dataset? It's a preprocessed dataset for our model.</p>
<p>Open the request.py and change the files location to your dataset file location.</p>
<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vlfrjceuaw7ghl7pp9vf.png"/>
<p>Send a POST request to the runtime host on http://localhost:7071/api/nn by executing the code below. The process may take several minutes to complete.</p>
<pre><code>$ py -3.7 request.py</code></pre>
<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tr1ydkw13x438mbxhviz.png"/>

<p>In order to get the function's public endpoint, we need to deploy our function on cloud. Check out the complete documentation on how to <a href="https://learn.microsoft.com/en-us/azure/azure-functions/functions-deployment-technologies?tabs=windows">here</a>.</p>

<h3>Conclusion</h3>
<p>We've learned how to run a machine learning model with Azure Function by following the steps above. I hope you understand how Azure Function works in general by now. It's also important to note that it doesn't necessarily have to be a complex application, such as machine learning model, to run with Azure Function. We can use it for as simple as APIs that update a single row or column in database.</p>