public static void Main(string[] args)
        {
            // You will need to set you email and the account key as the input parameters.

            // You can get your account key from https://datamarket.azure.com/account if
            // you signed up for the service using Data Market.
            // If you signed up for the service using the Azure Management Portal, you can
            // get the Account Key there.
            if (args.Length != 2)
            {
                throw new Exception(
                    "Invalid usage: expecting 2 parameters (in this order) email and DataMarket primary account key");
            }

            var email = args[0];
            var accountKey = args[1];

            Console.WriteLine("Invoking Azure ML Sample app for user {0}\n", email);

            //Note: if you run the app flow consecutively you need to change the model name, otherwise the invocation will fail.
            const string modelName = "demo_model";

            //Initialization 
            var recommendationsApp = new RecommendationsSampleApp();
            recommendationsApp.Init(email, accountKey);

            //Create a model container
            Console.WriteLine("Creating a new model container {0}...", modelName);
            var modelId = recommendationsApp.CreateModel(modelName);
            Console.WriteLine("Model '{0}' created with ID: {1}", modelName, modelId);

            //Import data to the container
            Console.WriteLine("Importing catalog and usage data...");
            var resourcesDir = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Resources");
            Console.WriteLine("Import catalog...");
            var report = recommendationsApp.ImportFile(modelId, Path.Combine(resourcesDir, "catalog_small.txt"), Uris.ImportCatalog);
            Console.WriteLine("{0}", report);

            Console.WriteLine("Import usage...");
            report = recommendationsApp.ImportFile(modelId, Path.Combine(resourcesDir, "usage_small.txt"), Uris.ImportUsage);
            Console.WriteLine("{0}", report);


            //Trigger a build to produce a recommendation model.
            Console.WriteLine("Triggering build for model '{0}'", modelId);
            var buildId = recommendationsApp.BuildModel(modelId, "build of " + DateTime.UtcNow.ToString("yyyyMMddHHmmss"));
            Console.WriteLine("Triggered build id '{0}'", buildId);


            Console.WriteLine("Monitoring build '{0}'\n", buildId);
            //monitor the current triggered build
            var status = BuildStatus.Create;
            bool monitor = true;
            while (monitor)
            {
                status = recommendationsApp.GetBuildStatus(modelId, buildId);

                Console.Write("Build '{0}' (model '{1}'): status {2}\n", buildId, modelId, status);
                if (status != BuildStatus.Error && status != BuildStatus.Cancelled && status != BuildStatus.Success)
                {
                    Console.WriteLine(" --> will check in 5 sec...");
                    Thread.Sleep(5000);
                }
                else
                {
                    monitor = false;
                }
            }

            Console.WriteLine("Build {0} ended with status {1}", buildId, status);

            //The below api is more meaningful when you want to give a cetain build id to be an active build.
            //currently this app has a single build which is already active.
            Console.WriteLine("Updating model description to 'book model' and set active build");

            recommendationsApp.UpdateModel(modelId, "book model", buildId);

            if (status != BuildStatus.Success)
            {
                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;
            }

            // we deliberatly add delay in order to propagate the model updates from the build...
            Console.WriteLine("Waiting for 20 sec for propagation of the built model...");
            Thread.Sleep(20000);

            Console.WriteLine("Getting some recommendations...");
            // get recommendations
            var seedItems = new List<CatalogItem>
            {
                // These item data were extracted from the catalog file in the resource folder.
                new CatalogItem() {Id = "2406e770-769c-4189-89de-1c9283f93a96", Name = "Clara Callan"},
                new CatalogItem() {Id = "552a1940-21e4-4399-82bb-594b46d7ed54", Name = "Restraint of Beasts"}
            };
            Console.WriteLine("\t for single seed item");

            // show usage for single item
            recommendationsApp.InvokeRecommendations(modelId, seedItems, false);


            Console.WriteLine("\n\n\t for a set of seed item");
            // show usage for a list of items
            recommendationsApp.InvokeRecommendations(modelId, seedItems, true);


            Console.WriteLine("\nGetting user recommendations...");
            // the following user id was extracted from the sample usage file in the resource folder
            recommendationsApp.InvokeUserRecommendations(modelId, "85526");
            Console.WriteLine("Press any key to end");
            Console.ReadKey();
        }
Exemplo n.º 2
0
        public static void Main(string[] args)
        {
            // You will need to set you email and the account key as the input parameters.

            // You can get your account key from https://datamarket.azure.com/account if
            // you signed up for the service using Data Market.
            // If you signed up for the service using the Azure Management Portal, you can
            // get the Account Key there.
            if (args.Length != 2)
            {
                throw new Exception(
                          "Invalid usage: expecting 2 parameters (in this order) email and DataMarket primary account key");
            }

            var email      = args[0];
            var accountKey = args[1];

            Console.WriteLine("Invoking Azure ML Sample app for user {0}\n", email);

            //Note: if you run the app flow consecutively you need to change the model name, otherwise the invocation will fail.
            const string modelName = "demo_model";

            //Initialization
            var recommendationsApp = new RecommendationsSampleApp();

            recommendationsApp.Init(email, accountKey);

            //Create a model container
            Console.WriteLine("Creating a new model container {0}...", modelName);
            var modelId = recommendationsApp.CreateModel(modelName);

            Console.WriteLine("Model '{0}' created with ID: {1}", modelName, modelId);

            //Import data to the container
            Console.WriteLine("Importing catalog and usage data...");
            var resourcesDir = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Resources");

            Console.WriteLine("Import catalog...");
            var report = recommendationsApp.ImportFile(modelId, Path.Combine(resourcesDir, "catalog_small.txt"), Uris.ImportCatalog);

            Console.WriteLine("{0}", report);

            Console.WriteLine("Import usage...");
            report = recommendationsApp.ImportFile(modelId, Path.Combine(resourcesDir, "usage_small.txt"), Uris.ImportUsage);
            Console.WriteLine("{0}", report);


            //Trigger a build to produce a recommendation model.
            Console.WriteLine("Triggering build for model '{0}'", modelId);
            var buildId = recommendationsApp.BuildModel(modelId, "build of " + DateTime.UtcNow.ToString("yyyyMMddHHmmss"));

            Console.WriteLine("Triggered build id '{0}'", buildId);


            Console.WriteLine("Monitoring build '{0}'\n", buildId);
            //monitor the current triggered build
            var  status  = BuildStatus.Create;
            bool monitor = true;

            while (monitor)
            {
                status = recommendationsApp.GetBuildStatus(modelId, buildId);

                Console.Write("Build '{0}' (model '{1}'): status {2}\n", buildId, modelId, status);
                if (status != BuildStatus.Error && status != BuildStatus.Cancelled && status != BuildStatus.Success)
                {
                    Console.WriteLine(" --> will check in 5 sec...");
                    Thread.Sleep(5000);
                }
                else
                {
                    monitor = false;
                }
            }

            Console.WriteLine("Build {0} ended with status {1}", buildId, status);

            //The below api is more meaningful when you want to give a cetain build id to be an active build.
            //currently this app has a single build which is already active.
            Console.WriteLine("Updating model description to 'book model' and set active build");

            recommendationsApp.UpdateModel(modelId, "book model", buildId);

            if (status != BuildStatus.Success)
            {
                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;
            }

            // we deliberatly add delay in order to propagate the model updates from the build...
            Console.WriteLine("Waiting for 20 sec for propagation of the built model...");
            Thread.Sleep(20000);

            Console.WriteLine("Getting some recommendations...");
            // get recommendations
            var seedItems = new List <CatalogItem>
            {
                // These item data were extracted from the catalog file in the resource folder.
                new CatalogItem()
                {
                    Id = "2406e770-769c-4189-89de-1c9283f93a96", Name = "Clara Callan"
                },
                new CatalogItem()
                {
                    Id = "552a1940-21e4-4399-82bb-594b46d7ed54", Name = "Restraint of Beasts"
                }
            };

            Console.WriteLine("\t for single seed item");

            // show usage for single item
            recommendationsApp.InvokeRecommendations(modelId, seedItems, false);


            Console.WriteLine("\n\n\t for a set of seed item");
            // show usage for a list of items
            recommendationsApp.InvokeRecommendations(modelId, seedItems, true);


            Console.WriteLine("\nGetting user recommendations...");
            // the following user id was extracted from the sample usage file in the resource folder
            recommendationsApp.InvokeUserRecommendations(modelId, "85526");
            Console.WriteLine("Press any key to end");
            Console.ReadKey();
        }