Beispiel #1
0
 static void ProcessResults(BatchScoreStatus status)
 {
     foreach (var output in status.Results)
     {
         var blobLocation = output.Value;
         Console.WriteLine(string.Format("The result '{0}' is available at the following Azure Storage location:", output.Key));
         Console.WriteLine(string.Format("BaseLocation: {0}", blobLocation.BaseLocation));
         Console.WriteLine(string.Format("RelativeLocation: {0}", blobLocation.RelativeLocation));
         Console.WriteLine(string.Format("SasBlobToken: {0}", blobLocation.SasBlobToken));
         Console.WriteLine();
     }
 }
Beispiel #2
0
        static void ProcessResults(BatchScoreStatus status)
        {
            bool first = true;

            foreach (var output in status.Results)
            {
                var blobLocation = output.Value;
                Console.WriteLine(string.Format("The result '{0}' is available at the following Azure Storage location:", output.Key));
                Console.WriteLine(string.Format("BaseLocation: {0}", blobLocation.BaseLocation));
                Console.WriteLine(string.Format("RelativeLocation: {0}", blobLocation.RelativeLocation));
                Console.WriteLine(string.Format("SasBlobToken: {0}", blobLocation.SasBlobToken));
                Console.WriteLine();

                // Save the first output to disk
                if (first)
                {
                    first = false;
                    SaveBlobToFile(blobLocation, string.Format("The results for {0}", output.Key));
                }
            }
        }
Beispiel #3
0
        static async Task InvokeBatchExecutionService()
        {
            // How this works:
            //
            // 1. Assume the input is present in a local file (if the web service accepts input)
            // 2. Upload the file to an Azure blob - you'd need an Azure storage account
            // 3. Call the Batch Execution Service to process the data in the blob. Any output is written to Azure blobs.
            // 4. Download the output blob, if any, to local file

            const string BaseUrl = "https://ussouthcentral.services.azureml.net/workspaces/a84f9ba84d5c48a582bfac621d24ff3e/services/86fe4131d2d645fbadd233fa6f8e45db/jobs";

            const string StorageAccountName      = "mystorageacct";                     // Replace this with your Azure Storage Account name
            const string StorageAccountKey       = "Dx9WbMIThAvXRQWap/aLnxT9LV5txxw=="; // Replace this with your Azure Storage Key
            const string StorageContainerName    = "mycontainer";                       // Replace this with your Azure Storage Container name
            string       storageConnectionString = string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", StorageAccountName, StorageAccountKey);
            const string apiKey = "abc123";                                             // Replace this with the API key for the web service

            // set a time out for polling status
            const int TimeOutInMilliseconds = 120 * 1000; // Set a timeout of 2 minutes



            UploadFileToBlob("..\\azure-machine-learning-getting-started\\3-azure-machine-learning-getting-started-m3-exercise-files\\german.data.csv" /*Replace this with the location of your input file*/,
                             "input1datablob.csv" /*Replace this with the name you would like to use for your Azure blob; this needs to have the same extension as the input file */,
                             StorageContainerName, storageConnectionString);

            using (HttpClient client = new HttpClient())
            {
                var request = new BatchExecutionRequest()
                {
                    Inputs = new Dictionary <string, AzureBlobDataReference>()
                    {
                        {
                            "input1",
                            new AzureBlobDataReference()
                            {
                                ConnectionString = storageConnectionString,
                                RelativeLocation = string.Format("{0}/input1datablob.csv", StorageContainerName)
                            }
                        },
                    },

                    Outputs = new Dictionary <string, AzureBlobDataReference>()
                    {
                        {
                            "output1",
                            new AzureBlobDataReference()
                            {
                                ConnectionString = storageConnectionString,
                                RelativeLocation = string.Format("/{0}/output1results.csv", StorageContainerName)
                            }
                        },
                    },
                    GlobalParameters = new Dictionary <string, string>()
                    {
                    }
                };

                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);

                // WARNING: The 'await' statement below can result in a deadlock if you are calling this code from the UI thread of an ASP.Net application.
                // One way to address this would be to call ConfigureAwait(false) so that the execution does not attempt to resume on the original context.
                // For instance, replace code such as:
                //      result = await DoSomeTask()
                // with the following:
                //      result = await DoSomeTask().ConfigureAwait(false)


                Console.WriteLine("Submitting the job...");

                // submit the job
                var response = await client.PostAsJsonAsync(BaseUrl + "?api-version=2.0", request);

                if (!response.IsSuccessStatusCode)
                {
                    await WriteFailedResponse(response);

                    return;
                }

                string jobId = await response.Content.ReadAsAsync <string>();

                Console.WriteLine(string.Format("Job ID: {0}", jobId));


                // start the job
                Console.WriteLine("Starting the job...");
                response = await client.PostAsync(BaseUrl + "/" + jobId + "/start?api-version=2.0", null);

                if (!response.IsSuccessStatusCode)
                {
                    await WriteFailedResponse(response);

                    return;
                }

                string    jobLocation = BaseUrl + "/" + jobId + "?api-version=2.0";
                Stopwatch watch       = Stopwatch.StartNew();
                bool      done        = false;
                while (!done)
                {
                    Console.WriteLine("Checking the job status...");
                    response = await client.GetAsync(jobLocation);

                    if (!response.IsSuccessStatusCode)
                    {
                        await WriteFailedResponse(response);

                        return;
                    }

                    BatchScoreStatus status = await response.Content.ReadAsAsync <BatchScoreStatus>();

                    if (watch.ElapsedMilliseconds > TimeOutInMilliseconds)
                    {
                        done = true;
                        Console.WriteLine(string.Format("Timed out. Deleting job {0} ...", jobId));
                        await client.DeleteAsync(jobLocation);
                    }
                    switch (status.StatusCode)
                    {
                    case BatchScoreStatusCode.NotStarted:
                        Console.WriteLine(string.Format("Job {0} not yet started...", jobId));
                        break;

                    case BatchScoreStatusCode.Running:
                        Console.WriteLine(string.Format("Job {0} running...", jobId));
                        break;

                    case BatchScoreStatusCode.Failed:
                        Console.WriteLine(string.Format("Job {0} failed!", jobId));
                        Console.WriteLine(string.Format("Error details: {0}", status.Details));
                        done = true;
                        break;

                    case BatchScoreStatusCode.Cancelled:
                        Console.WriteLine(string.Format("Job {0} cancelled!", jobId));
                        done = true;
                        break;

                    case BatchScoreStatusCode.Finished:
                        done = true;
                        Console.WriteLine(string.Format("Job {0} finished!", jobId));

                        ProcessResults(status);
                        break;
                    }

                    if (!done)
                    {
                        Thread.Sleep(1000); // Wait one second
                    }
                }
            }
        }
Beispiel #4
0
        static async Task InvokeBatchExecutionService()
        {
            // How this works:
            //
            // 1. Assume the input is present in a local file (if the web service accepts input)
            // 2. Upload the file to an Azure blob - you'd need an Azure storage account
            // 3. Call the Batch Execution Service to process the data in the blob. Any output is written to Azure blobs.
            // 4. Download the output blob, if any, to local file

            const string BaseUrl = "https://ussouthcentral.services.azureml.net/subscriptions/3ad4a5841cf644859e2f3dc64fc51528/services/ad5cd4eaf3f041589164680d5679ff01/jobs";

            const string StorageAccountName      = "mlstorage33";                                                                              // Replace this with your Azure Storage Account name
            const string StorageAccountKey       = "CRCbe2eBK0b1/XWDvoPipZigM/CEO+AAel8i3gNsixDxwEFW2QcNJH7n9/nhqSw1hZ6QORZcywbvfOotmgqtYQ=="; // Replace this with your Azure Storage Key
            const string StorageContainerName    = "mldata";                                                                                   // Replace this with your Azure Storage Container name
            string       storageConnectionString = string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", StorageAccountName, StorageAccountKey);

            const string apiKey = "Swo+LRPS+vgc7HsUNeDg5Ql5Xyptnb/Zll8+on3qeGdQPizu8rNAlZr0BMJtLomdnV4bywh+pl1SXK/5JAcOcg=="; // Replace this with the API key for the web service

            // set a time out for polling status
            const int TimeOutInMilliseconds = 120 * 1000; // Set a timeout of 2 minutes

            UploadFileToBlob("E:\\Download\\Train Data\\traindatanocopy.csv" /*Replace this with the location of your input file, and valid file extension (usually .csv)*/,
                             "input1datablob.csv" /*Replace this with the name you would like to use for your Azure blob; this needs to have the same extension as the input file */,
                             StorageContainerName, storageConnectionString);
            UploadFileToBlob("E:\\Download\\Train Data\\channels.csv" /*Replace this with the location of your input file, and valid file extension (usually .csv)*/,
                             "input2datablob.csv" /*Replace this with the name you would like to use for your Azure blob; this needs to have the same extension as the input file */,
                             StorageContainerName, storageConnectionString);

            using (HttpClient client = new HttpClient())
            {
                var request = new BatchExecutionRequest()
                {
                    Inputs = new Dictionary <string, AzureBlobDataReference>()
                    {
                        {
                            "input1",
                            new AzureBlobDataReference()
                            {
                                ConnectionString = storageConnectionString,
                                RelativeLocation = string.Format("{0}/input1datablob.csv", StorageContainerName)
                            }
                        },
                        {
                            "input2",
                            new AzureBlobDataReference()
                            {
                                ConnectionString = storageConnectionString,
                                RelativeLocation = string.Format("{0}/input2datablob.csv", StorageContainerName)
                            }
                        },
                    },

                    Outputs = new Dictionary <string, AzureBlobDataReference>()
                    {
                        {
                            "output1",
                            new AzureBlobDataReference()
                            {
                                ConnectionString = storageConnectionString,
                                RelativeLocation = string.Format("{0}/output1results.csv", StorageContainerName) /*Replace this with the location you would like to use for your output file, and valid file extension (usually .csv for scoring results, or .ilearner for trained models)*/
                            }
                        },
                        {
                            "output2",
                            new AzureBlobDataReference()
                            {
                                ConnectionString = storageConnectionString,
                                RelativeLocation = string.Format("{0}/output2results.ilearner", StorageContainerName) /*Replace this with the location you would like to use for your output file, and valid file extension (usually .csv for scoring results, or .ilearner for trained models)*/
                            }
                        },
                    },

                    GlobalParameters = new Dictionary <string, string>()
                    {
                    }
                };

                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);

                // WARNING: The 'await' statement below can result in a deadlock
                // if you are calling this code from the UI thread of an ASP.Net application.
                // One way to address this would be to call ConfigureAwait(false)
                // so that the execution does not attempt to resume on the original context.
                // For instance, replace code such as:
                //      result = await DoSomeTask()
                // with the following:
                //      result = await DoSomeTask().ConfigureAwait(false)

                Console.WriteLine("Submitting the job...");

                // submit the job
                var response = await client.PostAsJsonAsync(BaseUrl + "?api-version=2.0", request);

                if (!response.IsSuccessStatusCode)
                {
                    await WriteFailedResponse(response);

                    return;
                }

                string jobId = await response.Content.ReadAsAsync <string>();

                Console.WriteLine(string.Format("Job ID: {0}", jobId));

                // start the job
                Console.WriteLine("Starting the job...");
                response = await client.PostAsync(BaseUrl + "/" + jobId + "/start?api-version=2.0", null);

                if (!response.IsSuccessStatusCode)
                {
                    await WriteFailedResponse(response);

                    return;
                }

                string    jobLocation = BaseUrl + "/" + jobId + "?api-version=2.0";
                Stopwatch watch       = Stopwatch.StartNew();
                bool      done        = false;
                while (!done)
                {
                    Console.WriteLine("Checking the job status...");
                    response = await client.GetAsync(jobLocation);

                    if (!response.IsSuccessStatusCode)
                    {
                        await WriteFailedResponse(response);

                        return;
                    }

                    BatchScoreStatus status = await response.Content.ReadAsAsync <BatchScoreStatus>();

                    if (watch.ElapsedMilliseconds > TimeOutInMilliseconds)
                    {
                        done = true;
                        Console.WriteLine(string.Format("Timed out. Deleting job {0} ...", jobId));
                        await client.DeleteAsync(jobLocation);
                    }
                    switch (status.StatusCode)
                    {
                    case BatchScoreStatusCode.NotStarted:
                        Console.WriteLine(string.Format("Job {0} not yet started...", jobId));
                        break;

                    case BatchScoreStatusCode.Running:
                        Console.WriteLine(string.Format("Job {0} running...", jobId));
                        break;

                    case BatchScoreStatusCode.Failed:
                        Console.WriteLine(string.Format("Job {0} failed!", jobId));
                        Console.WriteLine(string.Format("Error details: {0}", status.Details));
                        done = true;
                        break;

                    case BatchScoreStatusCode.Cancelled:
                        Console.WriteLine(string.Format("Job {0} cancelled!", jobId));
                        done = true;
                        break;

                    case BatchScoreStatusCode.Finished:
                        done = true;
                        Console.WriteLine(string.Format("Job {0} finished!", jobId));
                        ProcessResults(status);
                        break;
                    }

                    if (!done)
                    {
                        Thread.Sleep(1000); // Wait one second
                    }
                }
            }
        }
        static void ProcessResults(BatchScoreStatus status)
        {
            var blobLocation = status.Result;

            SaveBlobToFile(blobLocation, "The results");
        }
Beispiel #6
0
        static async Task InvokeBatchExecutionService()
        {
            // How this works:
            //
            // 1. Assume the input is present in a local file (if the web service accepts input)
            // 2. Upload the file to an Azure blob - you'd need an Azure storage account
            // 3. Call the Batch Execution Service to process the data in the blob. Any output is written to Azure blobs.
            // 4. Download the output blob, if any, to local file

            const string BaseUrl = "https://ussouthcentral.services.azureml.net/subscriptions/1a917ae725664354b19e4acf4039a381/services/c01a944d35a9426880392b47f3f21466/jobs";

            const string StorageAccountName      = "storage account"; // Replace this with your Azure Storage Account name
            const string StorageAccountKey       = "storage key";     // Replace this with your Azure Storage Key
            const string StorageContainerName    = "bescontainer";    // Replace this with your Azure Storage Container name
            string       storageConnectionString = string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", StorageAccountName, StorageAccountKey);

            const string apiKey = "API key"; // Replace this with the API key for the web service

            // set a time out for polling status
            const int TimeOutInMilliseconds = 120 * 1000; // Set a timeout of 2 minutes

            UploadFileToBlob("E:\\Labfiles\\Lab10\\Starter\\Binary Classification dataset.csv" /*Replace this with the location of your input file, and valid file extension (usually .csv)*/,
                             "BESdata.csv" /*Replace this with the name you would like to use for your Azure blob; this needs to have the same extension as the input file */,
                             StorageContainerName, storageConnectionString);

            using (HttpClient client = new HttpClient())
            {
                var request = new BatchExecutionRequest()
                {
                    Inputs = new Dictionary <string, AzureBlobDataReference>()
                    {
                        {
                            "input1",
                            new AzureBlobDataReference()
                            {
                                ConnectionString = storageConnectionString,
                                RelativeLocation = string.Format("{0}/BESdata.csv", StorageContainerName)
                            }
                        },
                    },

                    Outputs = new Dictionary <string, AzureBlobDataReference>()
                    {
                        {
                            "output1",
                            new AzureBlobDataReference()
                            {
                                ConnectionString = storageConnectionString,
                                RelativeLocation = string.Format("{0}/BESoutput.csv", StorageContainerName) /*Replace this with the location you would like to use for your output file, and valid file extension (usually .csv for scoring results, or .ilearner for trained models)*/
                            }
                        },
                    },

                    GlobalParameters = new Dictionary <string, string>()
                    {
                        {
                            "blobpath", "bescontainer/BESpredictions.csv"
                        },
                    }
                };

                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);

                // WARNING: The 'await' statement below can result in a deadlock
                // if you are calling this code from the UI thread of an ASP.Net application.
                // One way to address this would be to call ConfigureAwait(false)
                // so that the execution does not attempt to resume on the original context.
                // For instance, replace code such as:
                //      result = await DoSomeTask()
                // with the following:
                //      result = await DoSomeTask().ConfigureAwait(false)

                Console.WriteLine("Submitting the job...");

                // submit the job
                var response = await client.PostAsJsonAsync(BaseUrl + "?api-version=2.0", request);

                if (!response.IsSuccessStatusCode)
                {
                    await WriteFailedResponse(response);

                    return;
                }

                string jobId = await response.Content.ReadAsAsync <string>();

                Console.WriteLine(string.Format("Job ID: {0}", jobId));

                // start the job
                Console.WriteLine("Starting the job...");
                response = await client.PostAsync(BaseUrl + "/" + jobId + "/start?api-version=2.0", null);

                if (!response.IsSuccessStatusCode)
                {
                    await WriteFailedResponse(response);

                    return;
                }

                string    jobLocation = BaseUrl + "/" + jobId + "?api-version=2.0";
                Stopwatch watch       = Stopwatch.StartNew();
                bool      done        = false;
                while (!done)
                {
                    Console.WriteLine("Checking the job status...");
                    response = await client.GetAsync(jobLocation);

                    if (!response.IsSuccessStatusCode)
                    {
                        await WriteFailedResponse(response);

                        return;
                    }

                    BatchScoreStatus status = await response.Content.ReadAsAsync <BatchScoreStatus>();

                    if (watch.ElapsedMilliseconds > TimeOutInMilliseconds)
                    {
                        done = true;
                        Console.WriteLine(string.Format("Timed out. Deleting job {0} ...", jobId));
                        await client.DeleteAsync(jobLocation);
                    }
                    switch (status.StatusCode)
                    {
                    case BatchScoreStatusCode.NotStarted:
                        Console.WriteLine(string.Format("Job {0} not yet started...", jobId));
                        break;

                    case BatchScoreStatusCode.Running:
                        Console.WriteLine(string.Format("Job {0} running...", jobId));
                        break;

                    case BatchScoreStatusCode.Failed:
                        Console.WriteLine(string.Format("Job {0} failed!", jobId));
                        Console.WriteLine(string.Format("Error details: {0}", status.Details));
                        done = true;
                        break;

                    case BatchScoreStatusCode.Cancelled:
                        Console.WriteLine(string.Format("Job {0} cancelled!", jobId));
                        done = true;
                        break;

                    case BatchScoreStatusCode.Finished:
                        done = true;
                        Console.WriteLine(string.Format("Job {0} finished!", jobId));
                        ProcessResults(status);
                        break;
                    }

                    if (!done)
                    {
                        Thread.Sleep(1000); // Wait one second
                    }
                }
                Console.ReadLine();
            }
        }