Search code examples
c#asp.netazureazure-machine-learning-servicecortana-intelligence

Azure ML Web Service request not working in C#


I have created an Azure ML Web service which outputs JSON response on request, and the structure of the sample request is as following:

{
  "Inputs": {
    "input1": {
      "ColumnNames": [
        "gender",
        "age",
        "income"
      ],
      "Values": [
        [
          "value",
          "0",
          "0"
        ],
        [
          "value",
          "0",
          "0"
        ]
      ]
    }
  },
  "GlobalParameters": {}
}

And the input parameters are supposedly like this:

gender String
age Numeric
income Numeric

My Post method looks like this:

    [HttpPost]
        public ActionResult GetPredictionFromWebService()
        {
            var gender = Request.Form["gender"];
            var age = Request.Form["age"];


            if (!string.IsNullOrEmpty(gender) && !string.IsNullOrEmpty(age))
            {
                var resultResponse = _incomeWebService.InvokeRequestResponseService<ResultOutcome>(gender, age).Result;


                if (resultResponse != null)
                {
                    var result = resultResponse.Results.Output1.Value.Values;
                    PersonResult = new Person
                    {
                        Gender = result[0, 0],
                        Age = Int32.Parse(result[0, 1]),
                        Income = Int32.Parse(result[0, 2])
                    };
                }
            }




            return RedirectToAction("index");
        }

But for whatever reason; the Azure ML Webservice doesn’t seem to respond anything to my request. Does anyone know what the reason might be? I see no error or anything, just an empty response.


Solution

  • The answer to your problem is that the “Numeric” datatype which is written in the input parameters in Azure ML is in fact a float and not an integer for your income measure. So when trying to request a response from Azure ML, you are not providing it the “adequate” information needed in the right format for it to respond correctly, resulting in it not giving you any response.

    I believe your model would look something similar to this based on your input parameters:

    public class Person
        {
            public string Gender { get; set; }
            public int Age { get; set; }
            public int Income { get; set; }
    
    
            public override string ToString()
            {
                return Gender + "," + Age + "," + Income;
            }
        }
    

    You would have to change your Income datatype into float like so:

    public class Person
    {
        public string Gender { get; set; }
        public int Age { get; set; }
        public float Income { get; set; }
    
        public override string ToString()
        {
            return Gender + "," + Age + "," + Income;
        }
    }
    

    And then your post-method would look something like this:

        [HttpPost]
        public ActionResult GetPredictionFromWebService()
        {
            var gender = Request.Form["gender"];
            var age = Request.Form["age"];
    
            if (!string.IsNullOrEmpty(gender) && !string.IsNullOrEmpty(age))
            {
                var resultResponse = _incomeWebService.InvokeRequestResponseService<ResultOutcome>(gender, age).Result;
    
                    if (resultResponse != null)
                    {
                        var result = resultResponse.Results.Output1.Value.Values;
                        PersonResult = new Person
                        {
                            Gender = result[0, 0],
                            Age = Int32.Parse(result[0, 1]),
                            Income = float.Parse(result[0, 3], CultureInfo.InvariantCulture.NumberFormat)
                    };
                }
            }
    
            ViewBag.myData = PersonResult.Income.ToString();
            return View("Index");
        }
    

    The key here is simply:

    Income = float.Parse(result[0, 3], CultureInfo.InvariantCulture.NumberFormat)
    

    Rather than your legacy

    Income = Int32.Parse(result[0, 2])