Repository navigation
Correct way to use hf_hub_download to get absolute path to a cached folder? #10702
Replies: 2 comments
|
Your instinct is right that it feels wrong, but it is actually a legitimate idiom — A few refinements, depending on what you need: If you want the folder without downloading a heavyweight file: downloading a small file just to compute the path is fine — If you actually want the whole folder (all its files): from huggingface_hub import snapshot_download
snapshot_dir = snapshot_download(
repo_id="xxx/xxx",
allow_patterns=["models/image_encoder/*"],
)
encoder_dir = os.path.join(snapshot_dir, "models/image_encoder")If you want the files in your own directory instead of the cache (e.g. to bundle them with an app), pass snapshot_download(repo_id="xxx/xxx", local_dir="models/image_encoder",
allow_patterns=["models/image_encoder/*"])With Two things worth knowing about the cache path: it contains the resolved revision hash, so it stays stable across calls for the same |
|
import os
from huggingface_hub import snapshot_download
root = snapshot_download(repo_id="xxx/xxx", allow_patterns="models/image_encoder/*")
encoder_dir = os.path.join(root, "models/image_encoder")
— |
Uh oh!
There was an error while loading. Please reload this page.
I need to get absolute path to a folder,
Is this the correct way to do it? It looked so wrong to me.
All reactions