Skip to content

Commit

Permalink
Model Artifact Download Example (#3417)
Browse files Browse the repository at this point in the history
* DownloadModel example

* spacing

* whitespace

---------

Co-authored-by: Nalin Gaddis <nalingaddis@microsoft.com>
  • Loading branch information
nalingaddis and Nalin Gaddis authored Oct 11, 2024
1 parent c7b8161 commit 43d5f13
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
46 changes: 46 additions & 0 deletions sdk/dotnet/AzureML-Samples-CSharp/Assets/Model/ModelOperations.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using Azure.Core;
using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.MachineLearning;
using Azure.ResourceManager.MachineLearning.Models;
using Azure.ResourceManager.Resources;
using Azure.Storage.Blobs;

namespace Azure.MachineLearning.Samples.Assets.Model;

Expand Down Expand Up @@ -57,4 +59,48 @@ public static async Task<MachineLearningModelVersionResource> GetOrCreateModelVe
}
// </GetOrCreateModelVersionAsync>

/// <summary>
/// Download the model artifact from the workspace datastore.
/// </summary>
// <DownloadLatestModelVersion>
public static async Task DownloadModelVersion(
string subscriptionId,
string resourceGroupName,
string workspaceName,
string modelName,
string version,
string downloadToPath)
{
var cred = new DefaultAzureCredential();
var armClient = new ArmClient(cred);

Console.WriteLine("Getting model version data ...");
var modelId = MachineLearningModelVersionResource.CreateResourceIdentifier(subscriptionId, resourceGroupName, workspaceName, modelName, version);
var modelResult = await armClient.GetMachineLearningModelVersionResource(modelId).GetAsync();
var modelData = modelResult.Value.Data;
Console.WriteLine($"Succeeded on id: {modelData.Id}");

Console.WriteLine("Getting workspace datastore ...");
var datastoreName = "workspaceblobstore";
var datastoreId = MachineLearningDatastoreResource.CreateResourceIdentifier(subscriptionId, resourceGroupName, workspaceName, datastoreName);
var datastoreResult = await armClient.GetMachineLearningDatastoreResource(datastoreId).GetAsync();
var datastoreData = datastoreResult.Value.Data;
Console.WriteLine($"Succeeded on id: {datastoreData.Id}");
Console.WriteLine(datastoreData);

var blobName = modelData.Properties.ModelUri.AbsolutePath.Split("/paths/").Last();
Console.WriteLine($"Model blob name: {blobName}");

var datastoreProperties = (MachineLearningAzureBlobDatastore)datastoreData.Properties;
var storageEndpoint = $"https://{datastoreProperties.AccountName}.blob.core.windows.net/{datastoreProperties.ContainerName}";
Console.WriteLine($"Storage endpoint: {storageEndpoint}");

var modelUri = new Uri($"{storageEndpoint}/{blobName}");
Console.WriteLine($"Downloading model from {modelUri} ...");

var blobClient = new BlobClient(modelUri, cred);
blobClient.DownloadTo(downloadToPath);
Console.WriteLine($"Succeded on downloading model to {downloadToPath}");
}
// </DownloadLatestModelVersion>
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.12.1" />
<PackageReference Include="Azure.ResourceManager.MachineLearning" Version="1.2.1" />
<PackageReference Include="Azure.Storage.Blobs" Version="12.22.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

Expand Down

0 comments on commit 43d5f13

Please sign in to comment.