Search code examples
c#google-cloud-automl

How to create new AutoML DataSet for simple classification (C#)


As part of ML automation process I want to dynamically create new AutoML model. I'm using C# (.net framework) and Google.Cloud.AutoML.V1.

After trying to run CreateDataSet code:

var autoMlClient = AutoMlClient.Create();
var parent = LocationName.FromProjectLocation(_projectId, _locationId);

var dataset = new Google.Cloud.AutoML.V1.Dataset();
dataset.DisplayName = "NewDataSet";    
var response = autoMlClient.CreateDataset(parent, dataset);

I get the following error:

Field: dataset.dataset_metadata; Message: Required field not set

According to this user manual I should set Dataset Metadata Type, but the list contains only specific types of classifications (Translation/ImageClassifications etc.), I can't find a simple classification type. How do I create a simple classification data set with the API ? in the AutoML UI its just with a simple button click ("NEW DATASET") - and have to provide only name & region - no classification type.

I also tried to set:

dataset.TextClassificationDatasetMetadata = 
     new TextClassificationDatasetMetadata() { ClassificationType = ClassificationType.Multiclass };

But I was unable to import data to it (got too many errors of invalid inputs from the input CSV file), I guess its related to the reason that the input format is not suitable for Text Classification.

UPDATE

I've just notice that the Nuget works with AutoML v1 but v1 beta does contains TablesDatasetMetadata Dataset Metadata Type for normal classifications. I'm speechless.


Solution

  • After @RajithaWarusavitarana comment, and my last question update , below is the code that did the trick. The token is being generated by GoogleClientAPI nuget and AutoML is handled by REST.

    string GcpGlobalEndPointUrl = "https://automl.googleapis.com";
    string GcpGlobalLocation = "us-central1";   // api "parent" parameter
    
    public string GetToken(string jsonFilePath)
    {
        var serviceAccountCredentialFileContents = System.IO.File.ReadAllText(jsonFilePath);
    
        var credentialParameters = NewtonsoftJsonSerializer.Instance.Deserialize<JsonCredentialParameters>(serviceAccountCredentialFileContents);
        var initializer = new ServiceAccountCredential.Initializer(credentialParameters.ClientEmail)
        {
            Scopes = new List<string> { "https://www.googleapis.com/auth/cloud-platform" }
        };
        var cred = new ServiceAccountCredential(initializer.FromPrivateKey(credentialParameters.PrivateKey));
    
        string accessToken = cred.GetAccessTokenForRequestAsync("https://oauth2.googleapis.com/token").Result;
        return accessToken;
    }
    
    public void GetDataSetList(string projectId, string token)
    {
        var restClient = new RestClient(GcpGlobalEndPointUrl);
    
        var createDataSetReqUrl = $"v1beta1/projects/{projectId}/locations/{GcpGlobalLocation}/datasets";
        var createDataSetReq = new RestRequest(createDataSetReqUrl, Method.GET);
        createDataSetReq.AddHeader("Authorization", $"Bearer {token}");
    
        var createDatasetResponse = restClient.Execute(createDataSetReq);
        createDatasetResponse.Dump();
    }
    

    I took the token generation code from google-api-dotnet-client Test File