Table of Contents

Class OnnxModelDownloader

Namespace
AiDotNet.Onnx
Assembly
AiDotNet.dll

Downloads ONNX models from HuggingFace Hub and other repositories.

public class OnnxModelDownloader : IOnnxModelDownloader, IDisposable
Inheritance
OnnxModelDownloader
Implements
Inherited Members

Remarks

This class provides functionality to download pre-trained ONNX models from HuggingFace Hub with automatic caching, progress reporting, and resume support.

For Beginners: Instead of manually downloading model files, use this class:

var downloader = new OnnxModelDownloader();

// Download Whisper model from HuggingFace var modelPath = await downloader.DownloadAsync( "openai/whisper-base", "model.onnx", progress: new Progress<double>(p => Console.WriteLine($"{p:P0}")));

var model = new OnnxModel<float>(modelPath);

Constructors

OnnxModelDownloader(HttpClient, string?, bool)

Creates a new OnnxModelDownloader with a custom HttpClient.

public OnnxModelDownloader(HttpClient httpClient, string? cacheDirectory = null, bool ownsHttpClient = false)

Parameters

httpClient HttpClient

The HttpClient to use for downloads.

cacheDirectory string

Optional custom cache directory.

ownsHttpClient bool

Whether to dispose the HttpClient when this downloader is disposed.

OnnxModelDownloader(string?)

Creates a new OnnxModelDownloader with default settings.

public OnnxModelDownloader(string? cacheDirectory = null)

Parameters

cacheDirectory string

Optional custom cache directory. Defaults to ~/.aidotnet/models

Fields

HuggingFaceBaseUrl

The base URL for HuggingFace Hub.

public const string HuggingFaceBaseUrl = "https://huggingface.co"

Field Value

string

Methods

ClearCache(string?)

Clears the local cache for a specific model or all models.

public void ClearCache(string? modelId = null)

Parameters

modelId string

Optional model identifier. If null, clears all cached models.

Dispose()

Disposes the downloader and the HttpClient if owned.

public void Dispose()

Dispose(bool)

Disposes managed and unmanaged resources.

protected virtual void Dispose(bool disposing)

Parameters

disposing bool

True if called from Dispose(), false if from finalizer.

DownloadAsync(string, string?, IProgress<double>?, CancellationToken)

Downloads an ONNX model from a remote repository.

public Task<string> DownloadAsync(string modelId, string? fileName = null, IProgress<double>? progress = null, CancellationToken cancellationToken = default)

Parameters

modelId string

The model identifier (e.g., "openai/whisper-base").

fileName string

Optional specific file name within the model repository.

progress IProgress<double>

Optional progress reporter (0.0 to 1.0).

cancellationToken CancellationToken

Cancellation token.

Returns

Task<string>

The local file path to the downloaded model.

DownloadFromUrlAsync(string, IProgress<double>?, CancellationToken)

Downloads a model from a direct URL (not HuggingFace).

public Task<string> DownloadFromUrlAsync(string url, IProgress<double>? progress = null, CancellationToken cancellationToken = default)

Parameters

url string

The direct URL to the ONNX model file.

progress IProgress<double>

Optional progress reporter.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<string>

The local path to the downloaded model.

DownloadMultipleAsync(string, IEnumerable<string>, IProgress<double>?, CancellationToken)

Downloads multiple ONNX files from a model repository.

public Task<IReadOnlyDictionary<string, string>> DownloadMultipleAsync(string modelId, IEnumerable<string> fileNames, IProgress<double>? progress = null, CancellationToken cancellationToken = default)

Parameters

modelId string

The model identifier.

fileNames IEnumerable<string>

The file names to download.

progress IProgress<double>

Optional progress reporter (0.0 to 1.0).

cancellationToken CancellationToken

Cancellation token.

Returns

Task<IReadOnlyDictionary<string, string>>

Dictionary mapping file names to local paths.

GetCacheSize()

Gets the total size of the local cache in bytes.

public long GetCacheSize()

Returns

long

GetCachedFiles(string)

Gets the files cached for a specific model.

public IReadOnlyList<string> GetCachedFiles(string modelId)

Parameters

modelId string

The model identifier.

Returns

IReadOnlyList<string>

A list of cached file names.

GetCachedPath(string, string?)

Checks if a model is already cached locally.

public string? GetCachedPath(string modelId, string? fileName = null)

Parameters

modelId string

The model identifier.

fileName string

Optional specific file name.

Returns

string

The local path if cached, null otherwise.

ListCachedModels()

Lists all cached models.

public IReadOnlyList<string> ListCachedModels()

Returns

IReadOnlyList<string>

A list of cached model IDs.