public static long UploadDataAndTrainModel(string modelId, BuildType buildType = BuildType.Recommendation) { long buildId = -1; // Trigger a recommendation build. string operationLocationHeader; if (buildType == BuildType.Recommendation) { buildId = reco.CreateRecommendationsBuild(modelId, "Recommendation Build " + DateTime.UtcNow.ToString("yyyyMMddHHmmss"), enableModelInsights: false, operationLocationHeader: out operationLocationHeader); } else { buildId = reco.CreateFbtBuild(modelId, "Frequenty-Bought-Together Build " + DateTime.UtcNow.ToString("yyyyMMddHHmmss"), enableModelInsights: false, operationLocationHeader: out operationLocationHeader); } // Monitor the build and wait for completion. var buildInfo = reco.WaitForOperationCompletion <BuildInfo>(RecommendationsApiWrapper.GetOperationId(operationLocationHeader)); // Waiting in order to propagate the model updates from the build... Thread.Sleep(TimeSpan.FromSeconds(40)); reco.SetActiveBuild(modelId, buildId); return(buildId); }
/// <summary> /// Creates a model, upload catalog and usage files and trigger a build. /// Returns the Build ID of the trained build. /// </summary> /// <param name="recommender">Wrapper that maintains API key</param> /// <param name="buildType">The type of build. (Recommendation or FBT)</param> /// <param name="modelId">The model Id</param> public static long UploadDataAndTrainModel(string modelId, BuildType buildType = BuildType.Recommendation) { long buildId = -1; // Import data to the model. Console.WriteLine("Importing catalog files..."); int catalogFilesCount = 0; foreach (string catalog in Directory.GetFiles(ProductCatalogPath, "*.*")) { var catalogFile = new FileInfo(catalog); recommender.UploadCatalog(modelId, catalogFile.FullName, catalogFile.Name); catalogFilesCount++; } Console.WriteLine("Imported {0} catalog files.", catalogFilesCount); Console.WriteLine("Importing usage files..."); int usageFilesCount = 0; foreach (string usage in Directory.GetFiles(UsageFilesPath, "*.*")) { var usageFile = new FileInfo(usage); recommender.UploadUsage(modelId, usageFile.FullName, usageFile.Name); usageFilesCount++; } Console.WriteLine("Imported {0} usage files.", usageFilesCount); #region training // Trigger a recommendation build. string operationLocationHeader; Console.WriteLine("Triggering build for model '{0}'. \nThis will take a few minutes...", modelId); if (buildType == BuildType.Recommendation) { buildId = recommender.CreateRecommendationsBuild(modelId, "Recommendation Build " + DateTime.UtcNow.ToString("yyyyMMddHHmmss"), enableModelInsights: false, operationLocationHeader: out operationLocationHeader); } else { buildId = recommender.CreateFbtBuild(modelId, "Frequenty-Bought-Together Build " + DateTime.UtcNow.ToString("yyyyMMddHHmmss"), enableModelInsights: false, operationLocationHeader: out operationLocationHeader); } // Monitor the build and wait for completion. Console.WriteLine("Monitoring build {0}", buildId); var buildInfo = recommender.WaitForOperationCompletion <BuildInfo>(RecommendationsApiWrapper.GetOperationId(operationLocationHeader)); Console.WriteLine("Build {0} ended with status {1}.\n", buildId, buildInfo.Status); if (String.Compare(buildInfo.Status, "Succeeded", StringComparison.OrdinalIgnoreCase) != 0) { Console.WriteLine("Build {0} did not end successfully, the sample app will stop here.", buildId); Console.WriteLine("Press any key to end"); Console.ReadKey(); return(-1); } // Waiting in order to propagate the model updates from the build... Console.WriteLine("Waiting for 40 sec for propagation of the built model..."); Thread.Sleep(TimeSpan.FromSeconds(40)); // The below api is more meaningful when you want to give a certain build id to be an active build. // Currently this app has a single build which is already active. Console.WriteLine("Setting build {0} as active build.", buildId); recommender.SetActiveBuild(modelId, buildId); #endregion return(buildId); }