2017-07-12 30 views
1

在.NET-Core C#中,我使用Googles ml API與引擎進行交互。我有我的預測方法的代碼是在這裏.NET核心GoogleCloudMlV1PredictRequest執行方法返回null響應

string credPath = @".\appkey.json"; 
var json = File.ReadAllText(credPath); 
PersonalServiceAccountCred cr = JsonConvert.DeserializeObject(json); 

// Create an explicit ServiceAccountCredential credential 
var xCred = new ServiceAccountCredential(new 
ServiceAccountCredential.Initializer(cr.ClientEmail) 
{ 
    Scopes = new [] { 
     CloudMachineLearningEngineService.Scope.CloudPlatform 
    } 
}.FromPrivateKey(cr.PrivateKey)); 

var service = new CloudMachineLearningEngineService(new BaseClientService.Initializer { 
    HttpClientInitializer = xCred 
}); 

ProjectsResource.PredictRequest req = new ProjectsResource.PredictRequest(service, new GoogleCloudMlV1PredictRequest { 
    HttpBody = new GoogleApiHttpBody { 
     Data = "{\"instances\": [{\"age\": 25, \"workclass\": \" Private\", \"education\": \" 11th\", \"education_num\": 7, \"marital_status\": \" Never - married\", \"occupation\": \" Machine - op - inspct\", \"relationship\": \" Own - child\", \"race\": \" Black\", \"gender\": \" Male\", \"capital_gain\": 0, \"capital_loss\": 0, \"hours_per_week\": 40, \"native_country\": \" United - States\"}]}" 
}, "projects/{project_name}/models/census/versions/v1"); 

GoogleApiHttpBody body = req.Execute(); 

不過,我得到這個響應回GoogleApiHttpBody對象:

All null fields in GoogleApiHttpBody response object

是否有人知道這是怎麼回事?

回答

3

在檢查Google API並看到要求通過後,我認爲圖書館可能有問題。我已經在自己的博客上發佈了詳細信息:.NET-Core GoogleCloudMlV1PredictRequest Execture Method Returns null Response

發生了什麼事情是GoogleApiHttpBody不會序列化預測端點預期的「實例」對象。我想通了這一點,當我讀到流,看到這樣的響應:

{"error": "<strong>Missing \"instances\" field in request body</strong>: {\"httpBody\":{\"data\":\"{\\\"instances\\\":[{\\\"age\\\":25,\\\"workclass\\\":\\\" Private\\\",\\\"education\\\":\\\" 11th\\\",\\\"education_num\\\":7,\\\"marital_status\\\":\\\" Never - married\\\",\\\"occupation\\\":\\\" Machine - op - inspct\\\",\\\"relationship\\\":\\\" Own - child\\\",\\\"race\\\":\\\" Black\\\",\\\"gender\\\":\\\" Male\\\",\\\"capital_gain\\\":0,\\\"capital_loss\\\":0,\\\"hours_per_week\\\":40,\\\"native_country\\\":\\\" United - States\\\"}]}\"}}"} 

所以,我簡單的改變了我的代碼如下所示,現在我拿到地預測結果回到正確

string credPath = @".\appkey.json"; 
var json = File.ReadAllText(credPath); 
PersonalServiceAccountCred cr = JsonConvert.DeserializeObject(json); 

// Create an explicit ServiceAccountCredential credential 
var xCred = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(cr.ClientEmail) { 
    Scopes = new [] { 
     CloudMachineLearningEngineService.Scope.CloudPlatform 
    } 
}.FromPrivateKey(cr.PrivateKey)); 

var service = new CloudMachineLearningEngineService(new BaseClientService.Initializer { 
    HttpClientInitializer = xCred 
}); 

ProjectsResource.PredictRequest req = new ProjectsResource.PredictRequest(service, new GoogleCloudMlV1PredictRequest(), "projects/{project-name}/models/census/versions/v1"); 

string requestPath = req.Service.BaseUri + 
CloudMachineLearningEngineService.Version + "/" + req.Name + ":" + req.MethodName; 
Task result = service.HttpClient.PostAsync(requestPath, new StringContent("{\"instances\": [{\"age\": 25, \"workclass\": \" Private\", \"education\": \" 11th\", \"education_num\": 7, \"marital_status\": \" Never - married\", \"occupation\": \" Machine - op - inspct\", \"relationship\": \" Own - child\", \"race\": \" Black\", \"gender\": \" Male\", \"capital_gain\": 0, \"capital_loss\": 0, \"hours_per_week\": 40, \"native_country\": \" United - States\"}]}")); 
Task.WaitAll(result); 

HttpResponseMessage responseMessage = result.Result; 

Task responseStreamTask = responseMessage.Content.ReadAsStringAsync(); 
Task.WaitAll(responseStreamTask); 

string responseText = responseStreamTask.Result; 
+1

你是正確的 - 您需要發送原始請求。在你的例子中'toSend'沒有綁定,所以我假設代碼中存在一個錯誤? – rhaertel80

+0

你是對的!我試圖清理它的StackOverflow並沒有重新編譯。謝謝@ rhaertel80 –