public Stream ReadModelDataFile(string userId, string name) { var model = GetModel(userId, name); if (model != null && model.DataFileBlobName != null) { return(blobClient.GetBlobReader(ModelBlobContainerName, model.DataFileBlobName)); } return(null); }
/// <summary> /// Shows how to get item-to-item recommendations in batch. /// You can learn more about batch scoring at https://azure.microsoft.com/en-us/documentation/articles/cognitive-services-recommendations-batch-scoring/ /// Before you can use this method, you need to provide your blob account name, blob account key, and the input container name. /// </summary> /// <param name="recommender">Wrapper that maintains API key</param> /// <param name="modelId">Model ID</param> /// <param name="buildId">Build ID</param> public static void GetRecommendationsBatch(RecommendationsApiWrapper recommender, string modelId, long buildId) { #region setup // Set storage credentials and copy input file that defines items we want to get recommendations to the Blob Container. string blobStorageAccountName = ""; // enter your account name here. string blobStorageAccountKey = ""; // enter your account key here const string containerName = ""; // enter your container name here string outputContainerName = containerName; string baseLocation = "https://" + blobStorageAccountName + ".blob.core.windows.net/"; string inputFileName = "batchInput.json"; // the batch input string outputFileName = "batchOutput.json"; // the batch output string errorFileName = "batchError.json"; // the batch error // Validate user entered credentials. if (String.IsNullOrEmpty(blobStorageAccountKey) || String.IsNullOrEmpty(blobStorageAccountKey) || String.IsNullOrEmpty(containerName)) { Console.WriteLine("GetRecommendationsBatch: Provide your blob account name, blob account key, and the input container name."); Console.WriteLine("Press any key to continue."); Console.ReadKey(); } string connectionString = "DefaultEndpointsProtocol=https;AccountName=" + blobStorageAccountName + ";AccountKey=" + blobStorageAccountKey; // Copy input file from resources directory to blob storate var sourceStorageAccount = CloudStorageAccount.Parse(connectionString); BlobHelper bh = new BlobHelper(sourceStorageAccount, containerName); var resourcesDir = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Resources"); bh.PutBlockBlob(containerName, inputFileName, File.ReadAllText(Path.Combine(resourcesDir, inputFileName))); var inputSas = BlobHelper.GenerateBlobSasToken(connectionString, containerName, inputFileName); var outputSas = BlobHelper.GenerateBlobSasToken(connectionString, outputContainerName, outputFileName); var errorSas = BlobHelper.GenerateBlobSasToken(connectionString, outputContainerName, errorFileName); // Now we need to define the batch job to perform. BatchJobsRequestInfo batchJobsRequestInfo = new BatchJobsRequestInfo { Input = new StorageBlobInfo { AuthenticationType = "PublicOrSas", BaseLocation = baseLocation, RelativeLocation = containerName + "/" + inputFileName, SasBlobToken = inputSas }, Output = new StorageBlobInfo { AuthenticationType = "PublicOrSas", BaseLocation = baseLocation, RelativeLocation = containerName + "/" + outputFileName, SasBlobToken = outputSas }, Error = new StorageBlobInfo { AuthenticationType = "PublicOrSas", BaseLocation = baseLocation, RelativeLocation = containerName + "/" + errorFileName, SasBlobToken = errorSas }, // You may modify the information below to meet your request needs. // Note that currently only "ItemRecommend" is supported. Job = new JobInfo { ApiName = "ItemRecommend", ModelId = modelId, //staging model id for books BuildId = buildId, NumberOfResults = 10, IncludeMetadata = false, MinimalScore = 0 } }; #endregion #region start the job, wait for completion // kick start the batch job. string operationLocationHeader = ""; var jobId = recommender.StartBatchJob(batchJobsRequestInfo, out operationLocationHeader); // Monitor the batch job and wait for completion. Console.WriteLine("Monitoring batch job {0}", jobId); var batchInfo = recommender.WaitForOperationCompletion <BatchJobInfo>(RecommendationsApiWrapper.GetOperationId(operationLocationHeader)); Console.WriteLine("Batch {0} ended with status {1}.\n", jobId, batchInfo.Status); if (String.Compare(batchInfo.Status, "Succeeded", StringComparison.OrdinalIgnoreCase) != 0) { Console.WriteLine("Batch job {0} did not end successfully, the sample app will stop here.", jobId); Console.WriteLine("Press any key to end"); Console.ReadKey(); return; } else { // Copy the output file from blob starage into the local machine. Stream reader = bh.GetBlobReader(outputContainerName, outputFileName); string outputFullPath = Path.Combine(resourcesDir, outputFileName); using (var fileStream = File.Create(outputFullPath)) { reader.Seek(0, SeekOrigin.Begin); reader.CopyTo(fileStream); } Console.WriteLine("The output of the blob operation has been saved to: {0}", outputFullPath); } #endregion }