|
| 1 | +================== |
| 2 | +Hugging Face Hub |
| 3 | +================== |
| 4 | + |
| 5 | +Vortex reads Hugging Face Hub repositories over ``hf://`` URLs. A Hub repository is a set of files |
| 6 | +behind an HTTP endpoint that honours range requests, so no cloud SDK is involved and no extra build |
| 7 | +feature is needed. |
| 8 | + |
| 9 | +.. list-table:: |
| 10 | + :header-rows: 1 |
| 11 | + |
| 12 | + * - URL |
| 13 | + - Repository kind |
| 14 | + * - ``hf://datasets/<owner>/<name>[@<revision>][/<path>]`` |
| 15 | + - Dataset |
| 16 | + * - ``hf://spaces/<owner>/<name>[@<revision>][/<path>]`` |
| 17 | + - Space |
| 18 | + * - ``hf://<owner>/<name>[@<revision>][/<path>]`` |
| 19 | + - Model |
| 20 | + |
| 21 | +``<revision>`` is a branch, tag or commit, defaulting to ``main``. A revision containing ``/`` must |
| 22 | +be percent-encoded, e.g. ``hf://datasets/org/name@refs%2Fconvert%2Fparquet/data/train.vortex``. |
| 23 | + |
| 24 | +Configuration comes from the same environment variables ``huggingface_hub`` reads: |
| 25 | + |
| 26 | +.. list-table:: |
| 27 | + :header-rows: 1 |
| 28 | + |
| 29 | + * - Variable |
| 30 | + - Meaning |
| 31 | + * - ``HF_TOKEN`` |
| 32 | + - API token for private and gated repositories. Falls back to the token file at |
| 33 | + ``HF_TOKEN_PATH``, then ``$HF_HOME/token``, then ``$HOME/.cache/huggingface/token``. |
| 34 | + * - ``HF_ENDPOINT`` |
| 35 | + - Hub endpoint, defaulting to ``https://huggingface.co``. |
| 36 | + |
| 37 | +Reading from the Hub |
| 38 | +==================== |
| 39 | + |
| 40 | +Pass an ``hf://`` URL directly. Public repositories need no credentials; private and gated ones |
| 41 | +authenticate from ``HF_TOKEN`` or the saved login: |
| 42 | + |
| 43 | +.. code-block:: python |
| 44 | +
|
| 45 | + import vortex as vx |
| 46 | +
|
| 47 | + vxf = vx.open("hf://datasets/org/name/data/train.vortex") |
| 48 | + for batch in vxf.to_arrow(): |
| 49 | + ... |
| 50 | +
|
| 51 | +:class:`vortex.store.HfStore` |
| 52 | +============================= |
| 53 | + |
| 54 | +.. py:class:: vortex.store.HfStore(repo_id, *, repo_type="dataset", revision=None, token=None, endpoint=None) |
| 55 | +
|
| 56 | + A Hugging Face Hub object store, rooted at one repository and revision. |
| 57 | + |
| 58 | + A URL is enough for most reads, so reach for this class only for the two things a URL cannot |
| 59 | + express: a token held in a variable rather than the environment, and a read that must stay |
| 60 | + anonymous even though the environment offers credentials. |
| 61 | + |
| 62 | + Because the store is rooted at the repository and revision, the path passed alongside it is a |
| 63 | + path *within* the repository. |
| 64 | + |
| 65 | + :param repo_id: The repository, as ``"<owner>/<name>"``. |
| 66 | + :param repo_type: ``"dataset"``, ``"model"`` or ``"space"``. Defaults to ``"dataset"``. |
| 67 | + :param revision: A branch, tag or commit. Defaults to ``main``. Unlike in a URL, a revision |
| 68 | + containing ``/`` is passed literally — the store percent-encodes it. |
| 69 | + :param token: ``None`` (the default) or ``True`` authenticates from ``HF_TOKEN`` or the saved |
| 70 | + login; ``False`` forces an anonymous read even when credentials are available; a string is |
| 71 | + used as the token directly. |
| 72 | + :param endpoint: Hub endpoint. Defaults to ``HF_ENDPOINT``, then ``https://huggingface.co``. |
| 73 | + |
| 74 | +.. code-block:: python |
| 75 | +
|
| 76 | + import vortex as vx |
| 77 | + from vortex.store import HfStore |
| 78 | +
|
| 79 | + store = HfStore("org/name", revision="refs/convert/parquet", token="hf_...") |
| 80 | +
|
| 81 | + # With `store=`, the path is a path within the repository. |
| 82 | + vxf = vx.open("data/train.vortex", store=store) |
| 83 | +
|
| 84 | +Listing |
| 85 | +======= |
| 86 | + |
| 87 | +The Hub does not implement WebDAV ``PROPFIND``, which is how object-store HTTP listing works, so a |
| 88 | +Hub store cannot list a prefix. Opening a known path works, since that is a ``HEAD`` plus ranged |
| 89 | +``GET``. To expand a glob, list the repository through the Hub's own API first — which is what |
| 90 | +``vortex.datasets.load_dataset`` does — and then open each path it returns. |
| 91 | + |
| 92 | +Hugging Face Datasets |
| 93 | +===================== |
| 94 | + |
| 95 | +``vortex.datasets.load_dataset`` builds on this to load Vortex files from the Hub as Hugging Face |
| 96 | +``Datasets`` objects, expanding globs and pushing projections, filters and row limits into each |
| 97 | +scan. See :doc:`../datasets`. |
0 commit comments