We walk through how to successfully connect Microsoft Flow (and PowerApps) to an Azure Machine Learning Web Service.
Recently, Centric Consulting hosted a Hackathon to get our consultants a bit more exposure to Machine Learning. It was a day and a half of different teams putting together something that could demonstrate what Machine Learning is and how it can be connected to Microsoft Flow and PowerApps.
In the case of my team, we used some data to try and predict how long an employee might stay at a certain organization.
This was my first foray into Machine learning, so it was not a complex model by any means. I used the Azure Machine Learning studio to build the model, and one of the things I noticed was it could also set up a Predictive Web Service to interact with the model. By default, you can use Excel to input data into the model and get a prediction as an output, but that was way too bland and boring for me!
Getting Prepared
I’m going to use PowerApps and Flow for this. We need to have something ready in a day and a half, so I am absolutely firing up these two to get something out there fast. But, with connecting to an ML web Service, there are some caveats:
- There currently is no Azure ML Connector.
- This means it has to be a Custom Connector.
- Microsoft yanked the ability to use Custom Connectors from the licensing model included with Office 365. Which means you need a P1 or P2 license.
Now, since I only needed this to work for a short amount of time, I was able to use a P1 trial, but be aware of the above if you want to work with Custom Connectors.
Let’s Start with Flow
I wanted Flow to handle the input/output of the data for a few reasons. Flow is much more flexible with sending and receiving JSON data, and it’s much easier to troubleshoot when connecting to a web service. So, we’re going to need to create a custom connector to bridge the gap between the model and Flow.
We need to go to Flow, expand the Data menu, and click on “Custom Connectors.” On this screen, we can now “Create a Custom Connector.” When you click this button, you receive a few options on how to get the endpoint details. The Azure ML endpoint uses Swagger to define the endpoint. In my testing, I found that “Import an OpenAPI” from URL just errors out, so I used “Import a Postman Collection.
But, this also means we now need to create a Postman Collection.
Please Mr. Postman
Postman is a tool if you do any development for API’s, and it’s free! I’m not going to go into the full functionality of the product in this article, but you can download it here.
Once installed, open up Postman, and in the top bar, click on “Import.”
Now we need to give Postman the URL to our Azure ML Service Swagger doc. You can get the endpoint from your Azure ML Service Dashboard:
In this case, I need to use the Request-Response endpoint (as this sample PowerApp won’t be doing batch requests). I need to take the URL they provided me, but I need to replace the /execute?api-version=s.0&format=swagger and replace it with /swagger.json This is the correct URL to my swagger document in the service. Once I click Import, Postman connects the endpoint, reads the swagger document, and provides full details about my API.
Since we now have connectivity to the API, we can test it to ensure it works. I’m going to test the POST functionality of the Request-Response endpoint. Now, if I click “Send” to send a request to the endpoint, I just get back:
{
“error”: {
“code”: “Unauthorized”,
“message”: “Request is unauthorized to access resource.”,
“details”: [
{
“code”: “ScoreRequestUnauthorized”,
“message”: “Invalid credentials provided.”
}
]
}
}
The Azure ML Endpoint uses an API key to allow authorization for requests. But, how do I add that authorization to my Postman request? Well, the Azure ML service provides a handy Document to tell us the Headers, Body, and responses to expect. Right below where I found the URL for my Request-Response endpoint, there is a link called “API Help.” Clicking on it will bring you to a full document that provides assistance with the API:
So, in this document, I can see I need an authorization header, that includes Bearer and my API Key. I can add a header in Postman that looks like this (I’ve blocked out most of my API key, so hopefully you get the idea):
Now, when I click Send, I no longer get an authorization error. But, I get an error nonetheless:
{
“error”: {
“code”: “BadArgument”,
“message”: “Invalid argument provided.”,
“details”: [
{
“code”: “RequestBodyInvalid”,
“message”: “No request body provided or error in deserializing the request body.”
}
]
}
}
Now, let’s give our Request a body. The easiest way to do this is to go back to the API Help Document, scroll down a bit, and you see a sample Body request the API expects.
Now, just copy and paste everything in the Sample Request Box to the Body of the Postman Request and click Send. Awesome, we got a response back.
We now know the endpoint works, so we can save this as a Postman Collection. To do so, just click on the ellipses next to the collection name, and click “Export:”
Currently, Flow and PowerApps will only read a Postman v1 collection, so make sure that is set, and click Export. Save the file to a location you can remember:
Our data and analytics experts share six challenges that derail the productivity of your machine learning initiatives and why applying MLOps can help solve them.
Time to Connect the ML Service to Flow
Head back over to Flow, click on “Create Custom Connector” and click “Import from Postman File.” Provide a name for your Custom Connector and click Import. Find the Postman File we created and click “Continue”
Once Postman file is imported, we can now further define the Connector. You can add a custom Icon or Description on the General Page:
On the Security Page, we need to define the security settings users need to use to connect to this. Since we’re using an API key, choose that option from the Authentication Type Drop down. I named my Parameter label “API Key” but you can choose whatever is more descriptive for you. For Parameter name, enter in “Authorization.” The location is “Header”
On the Definition tab, you can see all of the information gleaned from the endpoint. I recommend accepting the defaults here, but you can play around with some of the namings if so desired:
Finally, click on “Create Connector” to complete the process. (Optional: Once the Connector has been created, you can try testing it from the “Test Tab”)
Success!
Congratulations! You’ve now successfully connected Microsoft Flow (and PowerApps) to an Azure Machine Learning Web Service. In Part 2, we’ll create a sample Flow and PowerApp that takes advantage of this new Custom Connector!
Part four of a Women in Tech series and originally posted on Jo’s blog, here.