HuggingFaceModelLoader<T>
Models & Types · AiDotNet.ModelLoading
Downloads and caches models from HuggingFace Hub.
Implements: IDisposable
For Beginners
HuggingFace Hub is like a library of pretrained AI models.
Instead of training models yourself (which requires huge amounts of data and compute), you can download models that others have already trained. This class handles:
- Downloading model files from HuggingFace
- Caching them locally so you don’t re-download every time
- Loading the weights into your model
Example usage:
var loader = new HuggingFaceModelLoader<float>();
// Download and cache a pretrained VAE
var files = await loader.DownloadModelAsync("stabilityai/sd-vae-ft-mse");
// Load weights into your model
var vae = new VAEEncoder<float>();
loader.LoadWeights(vae, files["diffusion_pytorch_model.safetensors"]);
Constructors
| Constructor | Summary |
|---|---|
HuggingFaceModelLoader(string cacheDir, string apiToken, bool verbose) |
Initializes a new instance of the HuggingFaceModelLoader class. |
Methods
| Method | Summary |
|---|---|
LoadResult LoadWeights(IWeightLoadable<T> model, string weightsPath, Func<string, string> mapping, bool strict) |
Loads weights using a WeightMapping instance. |
LoadResult LoadWeights(IWeightLoadable<T> model, string weightsPath, WeightMapping mapping, bool strict) |
Loads weights using a WeightMapping instance. |
Task<Dictionary<string, string>> DownloadModelAsync(string repoId, string revision, IEnumerable<string> filePatterns, CancellationToken cancellationToken) |
Downloads a model from HuggingFace Hub. |
Task<LoadResult> DownloadAndLoadAsync(IWeightLoadable<T> model, string repoId, string weightsFile, Func<string, string> mapping, string revision, bool strict, CancellationToken cancellationToken) |
Downloads a model and loads its weights in one operation. |
Task<string> DownloadFileAsync(string repoId, string revision, string fileName, CancellationToken cancellationToken) |
Downloads a single file from HuggingFace Hub. |
bool IsCached(string repoId, string weightsFile, string revision) |
Checks if a model is already cached locally. |
string GetCachePath(string repoId, string revision) |
Gets the local cache path for a repository. |
void ClearAllCache() |
Clears all cached models. |
void ClearCache(string repoId) |
Clears the cache for a specific model. |
void Dispose() |
Releases the HTTP client this loader created. |