2017-07-12 1 views
1

En .NET-Core C#, j'utilise Googles ml API pour interagir avec le moteur. Le code que j'ai pour ma prédire méthode est ici.NET-Core Méthode GoogleCloudMlV1PredictRequest Execture Retourne la réponse

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(); 

Cependant, je reçois cette réponse en arrière sur l'objet GoogleApiHttpBody:

All null fields in GoogleApiHttpBody response object

Quelqu'un sait ce qui se passe?

Répondre

3

Après avoir vérifié l'API Google et vu les demandes qui arrivaient, j'ai pensé que quelque chose n'allait pas dans la bibliothèque. J'ai posté des détails sur mon propre blog ici: .NET-Core GoogleCloudMlV1PredictRequest Execture Method Returns null Response

Qu'est-ce qui se passe est que GoogleApiHttpBody ne sérialise pas l'objet "instances" où le point de terminaison: prédire l'attend. Je me suis dit que quand je l'ai lu et vu le flux cette réponse:

{"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\\\"}]}\"}}"} 

Alors, je simplement changé mon code comme suit, et maintenant je le prédis résultat Retour correctement

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

Vous avez raison - Vous devez envoyer la demande brute. Dans votre exemple 'toSend' est non lié, donc je suppose qu'il y a un bug dans le code tel que présenté? – rhaertel80

+0

Vous avez raison! J'ai essayé de le nettoyer pour StackOverflow et je n'ai pas recompilé. Merci @ rhaertel80 –