Metadata-Version: 2.5
Name: 3lc-compute-plugin-huggingface
Version: 0.1.7
Summary: Browse Hugging Face datasets, import them into 3LC tables, and share 3LC tables back as Hugging Face datasets.
License: Apache-2.0
License-File: LICENSE
Classifier: License :: OSI Approved :: Apache Software License
Requires-Python: >=3.10
Requires-Dist: 3lc-compute-plugin-sdk<0.7.0,>=0.5.0.20261009132039.36.1
Requires-Dist: 3lc<4.0.0,>=3.5.0.dev149866
Provides-Extra: huggingface
Requires-Dist: 3lc[pandas]<4.0.0,>=3.5.0.dev149866; extra == 'huggingface'
Requires-Dist: datasets>=3.0; extra == 'huggingface'
Requires-Dist: huggingface-hub>=0.26; extra == 'huggingface'
Requires-Dist: pillow>=10; extra == 'huggingface'
Requires-Dist: pyarrow>=15; extra == 'huggingface'
Requires-Dist: requests>=2.31; extra == 'huggingface'
Description-Content-Type: text/markdown

# 3lc-compute-plugin-huggingface

Hugging Face datasets inside the 3LC Hub: browse the Hub with its own vocabularies, preview any dataset without
downloading it, import a split into a 3LC table with the media copied to your storage or referenced in place, and
share a 3LC table back as a Hugging Face dataset with a card and a licence.

A 3LC compute-service plugin (`kind` compute, section *Data Ops*, `id = "huggingface"`), isolated in its own venv
(`provision_extra = "huggingface"`).

## What the page does

- **Datasets.** Search plus the Hub's own filters — modality (image, video, audio, text, tabular, document, 3D,
  geospatial, time series), size category, format, licence, task — sorted by trending, downloads, likes or updated.
  The result list and the detail pane share one screen; the page never scrolls.
- **Preview.** Configs and splits with row counts and parquet sizes, rows with thumbnails, per-split filtering
  (`"label"=3`, numeric ranges) and text search where the Hub offers it, class distributions. All of it comes from
  Hugging Face's datasets-server; nothing is downloaded to look.
- **Import.** Tick the columns in the preview header; the drawer takes project, dataset and table names, where the
  project goes (this computer or the cloud root), all rows / the first N / the current filter, and — for image
  columns — *copy to storage* (a folder or bucket URL, with a URL alias) or *reference Hugging Face files* when
  the dataset stores each image as a file. The licence is acknowledged on every import. The job reads the dataset's
  parquet branch (`refs/convert/parquet`) with column projection and filters, in batches.
- **Share a table.** A 3LC table → a new dataset repository: parquet, a README dataset card with the licence you
  choose, private by default, images included as files or left as URLs.

## Token

The person's own token is a Hugging Face **Connection** they choose on the page (the Hub's
`PLUGIN_API.chooseCredential("huggingface")`); the host hands its value to this plugin's jobs and to the
routes `plugin.toml` lists under `credential_routes`. After it: `HF_TOKEN` / `HUGGINGFACE_HUB_TOKEN` as the
worker's environment had it at start, then the compute service's Hugging Face CLI login
(`~/.cache/huggingface/token`), then — for one release — a token an earlier version of the page saved in the
plugin's settings. None is ever returned to the browser or logged. A token is needed for gated or private
datasets and for sharing (write access).

## Routes (mounted under `/api/plugins/huggingface/`)

| Route | Answer |
| --- | --- |
| `GET /hf/vocab` | `{modality, size_categories, format, license, task_categories}` as `[{id, label}]` |
| `GET /hf/search?q&modality&size&format&license&task&sort&page&limit` | `{items:[card…], page, limit, has_more}` |
| `GET /hf/dataset?id=` | licence, card, configs → splits (rows, bytes), features per config, parquet files, `importable` + reason |
| `GET /hf/rows?id&config&split&offset&length&where&q` | `{features, rows:[{idx, cells}], total, partial, filtered, note}`; image cells `{type:"image", src, width, height}` |
| `GET /hf/statistics?id&config&split` | `{statistics:[{column, type, stats}]}` |
| `GET /hf/token` | `{token_set, token_source}` for the caller (`connection`, `env`, `hub`, `settings` or `""`) |
| `GET/POST /hf/settings` | the same without the caller's Connection; POST `{"token": "-"}` removes a saved token |
| `GET /project-root`, `GET /browse`, `POST /upload-temp` | the SDK's shared data-source routes |

Jobs go through the generic `POST /run` with `mode: "import"` (dataset, config, split, parquet_files, columns,
row_limit, where, project/dataset/table names, project_root_url, media `copy`/`reference`, media_target,
alias_token, license) or `mode: "export"` (table_url, repo_id, private, license, include_media, description).

## Dev setup

```bash
uv sync --extra huggingface
uv pip install --python .venv/bin/python -e ../3lc-compute-plugin-sdk   # until the SDK with /project-root ships
uv run --no-sync pytest
```

Tests never reach Hugging Face: every HTTP call goes through `hub_api._http_get`, every file read through the
importer's seams. Point a dev compute at this checkout with `TLC_COMPUTE_EXTERNAL_PLUGIN_DIRS=.../src`.

## POC development and publishing

Staged 3LC builds (the 3lc core and SDK 0.5) resolve from the private staging index
(`pypi.3lc.ai/repositories/prereleases`), declared as the explicit `staging` index in
`pyproject.toml`; everything else comes from PyPI. Set only the index credentials in your shell
(keep them out of source files):

```bash
export UV_INDEX_STAGING_USERNAME="<staging index username>"
export UV_INDEX_STAGING_PASSWORD="<staging index token>"
uv sync --locked --group dev --extra huggingface
uv run --no-sync pytest
```

The Release workflow builds verified artifacts on manual runs. `publish=true` uploads them
to the private staging index (`prereleases`) using the `CLOUDREPO_USERNAME` /
`CLOUDREPO_PASSWORD` secrets; publishing is
disabled by default. The timestamp, run and
attempt suffix stamps the distribution and manifest together.

The separate `v*` tag path provides PyPI Trusted Publishing plumbing. Configure the PyPI
project's trusted publisher before using that path; no public release is needed during POC.
