Class OnnxModelDownloader
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
httpClientHttpClientThe HttpClient to use for downloads.
cacheDirectorystringOptional custom cache directory.
ownsHttpClientboolWhether to dispose the HttpClient when this downloader is disposed.
OnnxModelDownloader(string?)
Creates a new OnnxModelDownloader with default settings.
public OnnxModelDownloader(string? cacheDirectory = null)
Parameters
cacheDirectorystringOptional custom cache directory. Defaults to ~/.aidotnet/models
Fields
HuggingFaceBaseUrl
The base URL for HuggingFace Hub.
public const string HuggingFaceBaseUrl = "https://huggingface.co"
Field Value
Methods
ClearCache(string?)
Clears the local cache for a specific model or all models.
public void ClearCache(string? modelId = null)
Parameters
modelIdstringOptional 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
disposingboolTrue 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
modelIdstringThe model identifier (e.g., "openai/whisper-base").
fileNamestringOptional specific file name within the model repository.
progressIProgress<double>Optional progress reporter (0.0 to 1.0).
cancellationTokenCancellationTokenCancellation token.
Returns
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
urlstringThe direct URL to the ONNX model file.
progressIProgress<double>Optional progress reporter.
cancellationTokenCancellationTokenCancellation token.
Returns
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
modelIdstringThe model identifier.
fileNamesIEnumerable<string>The file names to download.
progressIProgress<double>Optional progress reporter (0.0 to 1.0).
cancellationTokenCancellationTokenCancellation 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
GetCachedFiles(string)
Gets the files cached for a specific model.
public IReadOnlyList<string> GetCachedFiles(string modelId)
Parameters
modelIdstringThe 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
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.