Consuming O16N Web Services from Azure Functions
Operationalization feature of Microsoft Machine Learning Server allows us to publish R/Python models and code in the form of web services and the consume these services within client applications. This article outlines step-by-step details of consuming the published web service (R language) using Azure Functions (C# TimerTrigger). Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud without worrying about a whole application or the infrastructure to run it.
Step 1: Deploy Machine Learning Server
Step 2: Publish Web Service
Let us publish a sample web service ManualTransmission to the above deployed configuration and generate a C# client to consume the web service using the following R code :
<Replace parameters in remoteLogin() with your own parameters>
Step 3: Use Visual Studio to Develop and Deploy Azure Function
Install Visual Studio 17 Update 3 (or later).
Open ManualTransmissionClient.sln from the Client Code Location from step 2 in VS17
Right Click ManualTransmissionClient Solution and Add --> Add New Project.
Choose Azure Functions from Visual C# Cloud, provide a name for the project “ManualTransmissionFunctionApp” and click OK
Now you will see a third project in our solution
In Solution Explorer, right-click on ManualTransmissionFunctionApp project and select Add --> New Item. Select Azure Function, type a Name for the class ManualTransmissionFunction.cs, and click Add.
Depending on your use case, you can choose the type of trigger. To make things simple, let us choose a timer trigger – a function which is triggered every 5 minutes.
Now we will add ManualTransmissionClient as a reference project so that we use client code in ManualTransmissionFunction.cs
Add Newtonsoft.Json and RestSharp Nuget Package for ManualTransmissionFunctionApp project using Manage Nuget Packages (since these nuget packages are used by the client project).
Once references and nuget packages are added , the dependencies will look similar to this :
Now add C# code to ManualTranmissionFunction.cs to call the web service : https://github.com/Microsoft/microsoft-r/blob/master/ManualTransmissionAzureFunction/ManualTransmissionFunctionApp/ManualTransmissionFunction.cs
To run this Azure Function locally, we need to define a storage account connection string for AzureWebJobStorage in local.settings.json file. Create a storage account (or) use an existing storage account connection string.
After updating the local.settings.json file, Set ManualTransmissionFunctionApp as Startup project, build the solution and run the app.
You will see that the function is called every 5th minute and the prediction output is printed in the console. You can also debug this locally, by setting a breakpoint in the code.
Now lets publish this app from Visual Studio to Azure. Right Click ManualTransmissionFunctionApp and choose Publish.
Once you have published to Azure successfully, you can view the console output of Azure Function from Azure Portal as well.
The Visual Studio Solution is available in Github :
Other possible ideas using Azure Functions
- Azure function supports continuous deployment and it integrates with Dropbox, Github, Onedrive, VSTS etc. We can develop an Azure function (C# code) to update a published web service with new model/code if new code/model is checked-in to Github/VSTS.
- Azure Functions have blob trigger and Cosmos DB Trigger. So whenever new data is added to a blob or CosmosDB, Azure function can be triggered to “Score” the new data and store/perform subsequent action based on the scoring results.
- Exporting an Azure-hosted API to PowerApps and Microsoft Flow.
- Periodically score new incoming data in Azure SQL Database using Machine Learning Services.