In previous blog I’ve announced free iAL extension for Visual Studio Code.

First version (0.1.0) allows you to get image for action.

Just type the name/description of your action, and iAL will find the best image for it, among 1100+ standard images using machine learning algorithms. 

Is this magic? =) Well, let’s see how it really works. 

Architecture

 

The Big picture of the process is the next:

1)    Developer creates Action on page, adds Image property, press Ctrl+Shift+P , select “iAL: Get image for action” (iAL extension should be installed), types the purpose of action (name or short description) and click Enter

2)    iAL extension function “GetImage” send HTTP request to Azure Function “get-nav-actionimage”

3)    Azure Function “get-nav-actionimage” send HTTP request to Azure Machine Learning web service “Get-NAV-ActionImage”

4)    Azure ML web service “Get-NAV-ActionImage” run experiment “Get-NAV-ActionImage”

5)    The result of experiment is “Image Value” (“Scored Value” in terms of Azure ML)

6)    Azure Function get “Image Value” in response from Azure ML Web Service

7)    iAL Extension get “Image Value” in response from Azure Function

8)    Developer get “Image Value” in VS Code editor

The whole process can take from 1 second to 20 seconds. Why there is so big difference in execution speed?

Because of Azure Functions.

Azure Functions

As you see we use Azure Functions here. Why? Because Azure ML WebService URL can be changed. 

I found that when you change experiment workplace then url changes. Also, if I want to create new experiment (for better speed/accuracy) then this also will result in different url.

So, I don’t want Azure ML URL to be hardcoded in VS Code extension. I want to be sure that every user use one url to get correct result. 

That is why I use Azure function which has static URL (which is hardcoded in VS Code extension).

And my Azure function has hardcoded URL to Azure ML web service, which I can change on the fly.

But what influence on Azure function execution time? This is “Always On” feature. When this feature is deactivated, then Azure functions app goes to sleep mode after some period of inactivity. And on free price tier “Always On” is turned off, and cannot be activated.

 

That is why, when developers use my extension rarely, then for every developer the process  take about 20 sec. But if developers will use this app more often, then time for every developer will be reduced up to 1 sec (azure function will not go to sleep mode). 

Yesterday I found Azure service “API management”. Did not tried it yet, may be it will solve this problem (//To Investigate). 

The next Azure Function code interact with Azure Machine Learning web service

declare var require: any

export function run(context: any, req: any): void 
{
    context.log("TypeScript HTTP trigger function processed a request.")

    if (req.query.alActionName || (req.body && req.body.alActionName)) 
    {
    } else 
    {
        context.res = 
        {
            status: 400,
            body: "Please pass an Action Name on the query string or in the request body"
        }
    }

    let request = require('request');

    const uri = 'https://europewest.services.azureml.net/workspaces/YOUR-WORKSPACE-ID/services/YOUR-WEB-SERVICE-ID/execute?api-version=2.0&format=swagger';
    const apiKey = 'YOUR-API-KEY';
    
    let data = 
        {
        "Inputs": 
            {
             "input1":
             [
                {
                    'Text on Action': (req.query.alActionName || req.body.alActionName)
                }
             ],
            },
            "GlobalParameters": {}
         }
    
    const reqOptions = 
    {
         uri: uri,
         method: "POST",
         headers: 
         {
             "Content-Type": "application/json",
             "Authorization": "Bearer " + apiKey,
         },
         body: JSON.stringify(data)
    }
    
    request(reqOptions, function (error, response, body) 
    {
        if (error !== undefined && error !== null) 
        {
            context.res = 
            {
                status: 400,
                body: 'Failed to read the content. Error: ' + error
            }
            return;
        }
        if (response.statusCode !== 200) 
        {
            context.res = 
            {
                status: response.statusCode,
                body: 'Failed to read the content. Status code ' + response.statusCode + ' and body: ' + body
            };
            return;
        }

        let jsonObject = JSON.parse(body);

        let alActionImage =  jsonObject.Results.output1[0]["Scored Labels"];
        context.res = 
        {
            status: response.statusCode,
            body: alActionImage
        };
        context.done();

    })    
}

To execute this code in Azure Functions you need also to

  1. Go to https://<function_app_name>.scm.azurewebsites.net.
  2. Click Debug Console > CMD.
  3. Go to D:\home\site\wwwroot, and then drag your package.json file to the wwwroot folder at the top half of the page.
  4. After the package.json file is uploaded, run the npm install request command in the Kudu remote execution console.
    This action downloads the packages indicated in the package.json file and restarts the function app.

https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-node 

What's next

In next blog I will describe how to create Azure Machine Learning web service to propose Image for Action. 

Try

Feel free to use iAL extension in your everyday work and please, rate it and leave feedback on Marketplace and create issues on ial github project.