Repository navigation
fetchFromHuggingFace: add
#766
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| { | ||
| lib, | ||
| cacert, | ||
| python3, | ||
| stdenvNoCC, | ||
| }: | ||
|
|
||
| let | ||
| # `hf download` only accepts the `model`, `dataset` and `space` repository | ||
| # types, so we use `snapshot_download` (which the CLI wraps) directly. | ||
| python = python3.withPackages (ps: [ ps.huggingface-hub ]); | ||
| in | ||
|
|
||
| { | ||
| # Repository id, e.g. `kernels-community/relu`. | ||
| repoId, | ||
|
|
||
| # Repository type, e.g. `model`, `dataset` or `kernel`. | ||
| type ? "model", | ||
|
|
||
| # Tag or commit SHA. | ||
| revision, | ||
|
|
||
| # SRI hash of the repository snapshot. | ||
| hash, | ||
|
|
||
| # Derivation name, defaults to `<repoId>-<revision>`. | ||
| name ? null, | ||
| }: | ||
|
Comment on lines
+8
to
+29
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Feels illegal to write code this way! |
||
|
|
||
| stdenvNoCC.mkDerivation { | ||
| name = lib.strings.sanitizeDerivationName (if name != null then name else "${repoId}-${revision}"); | ||
|
|
||
| nativeBuildInputs = [ python ]; | ||
|
|
||
| dontUnpack = true; | ||
| dontConfigure = true; | ||
| dontBuild = true; | ||
| dontFixup = true; | ||
|
|
||
| installPhase = '' | ||
| runHook preInstall | ||
|
|
||
| # Downloading into a local directory bypasses the hub cache, but hf-xet | ||
| # still needs a writable home for its chunk cache. | ||
| export HOME="$NIX_BUILD_TOP" | ||
|
|
||
| # We use a custom script for now, since hf-cli does not support | ||
| # downloading the kernel repo type yet. | ||
| python ${./download.py} \ | ||
| ${lib.escapeShellArg repoId} \ | ||
| ${lib.escapeShellArg type} \ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we only allow "kernel" type repo downloads in this section? |
||
| ${lib.escapeShellArg revision} \ | ||
| "$out" | ||
|
|
||
| runHook postInstall | ||
| ''; | ||
|
|
||
| SSL_CERT_FILE = "${cacert}/etc/ssl/certs/ca-bundle.crt"; | ||
| HF_HUB_DISABLE_PROGRESS_BARS = "1"; | ||
| HF_HUB_DISABLE_TELEMETRY = "1"; | ||
|
|
||
| outputHash = hash; | ||
| outputHashAlgo = null; | ||
| outputHashMode = "recursive"; | ||
|
|
||
| impureEnvVars = lib.fetchers.proxyImpureEnvVars ++ [ "HF_TOKEN" ]; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import shutil | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| from huggingface_hub import snapshot_download | ||
|
|
||
| repo_id, repo_type, revision, local_dir = sys.argv[1:] | ||
|
|
||
| snapshot_download( | ||
| repo_id=repo_id, | ||
| repo_type=repo_type, | ||
| revision=revision, | ||
| local_dir=local_dir, | ||
| ) | ||
|
|
||
| # Downloading leaves per-file metadata (etags, timestamps) behind, which would | ||
| # make the hash of the snapshot unstable. | ||
| shutil.rmtree(Path(local_dir) / ".cache", ignore_errors=True) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit/question: is this safe? is there a case where the fetched repo legitimately contains files under **I think it should be safe to remove the whole dir but wanted to double check
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good question. I'm a bit worried about impurities that would arise because dependencies of |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we filter only "kernel" type repos?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like having a generic fetchers, maybe we can get it upstreamed in nixpkgs at some point 😁 .