Hub documentation
ストリーミングデータセット
ストリーミングデータセット
統合ライブラリ
If a dataset on the Hub is compatible with a supported library that allows streaming from Hugging Face, streaming the dataset can be done in just a few lines. For information on accessing the dataset, you can click on the “Use this dataset” button on the dataset page to see how to do so. For example, knkarthick/samsum shows how to do so with datasets below.


Hugging Face クライアントライブラリを使用する
huggingface_hub ライブラリを使用して、リポジトリからファイルの作成、削除、アクセスを行うことができます。例えば、Python で allenai/c4 データセットをストリーミングするには、ライブラリをインストール(最新バージョンの使用を推奨します)し、以下のコードを実行するだけです。
pip install -U huggingface_hub
from huggingface_hub import hffs
repo_id = "allenai/c4"
path_in_repo = "en/c4-train.00000-of-01024.json.gz"
# Stream the file
with hffs.open(f"datasets/{repo_id}/{path_in_repo}", "r", compression="gzip") as f:
print(f.readline()) # read only the first line
# {"text":"Beginners BBQ Class Taking Place in Missoula!...}詳細については、HfFileSystemのドキュメントを参照してください。
ご自身のライブラリに統合することもできます。例えば、Pandasを使ってCSVデータセットをバッチ単位で素早くストリーミングできます。
import pandas as pd
repo_id = "YOUR_REPO_ID"
path_in_repo = "data.csv"
batch_size = 5
# Stream the file
with hffs.open(f"datasets/{repo_id}/{path_in_repo}") as f:
for df in pd.read_csv(f, iterator=True, chunksize=batch_size): # read 5 lines at a time
print(len(df)) # 5ストリーミングは、Hugging Face上の大きなファイルを段階的に読み込む場合や、その一部のみを読み込む場合に特に有用です。
例えば、tarfileはTARアーカイブ内のファイルを反復処理でき、zipfileはZIPアーカイブからファイルを読み取れ、pyarrowはParquetファイルの行グループにアクセスできます。
Rust による同等のファイルシステム実装が OpenDAL で利用できます。
cURLの使用
Hub上のすべてのファイルはHTTP経由で利用可能なため、cURLを使用してファイルをストリーミングできます。
>>> curl -L https://huggingface.co/datasets/fka/awesome-chatgpt-prompts/resolve/main/prompts.csv | head -n 5
"act","prompt"
"An Ethereum Developer","Imagine you are an experienced Ethereum developer tasked with creating...
"SEO Prompt","Using WebPilot, create an outline for an article that will be 2,000 words on the ...
"Linux Terminal","I want you to act as a linux terminal. I will type commands and you will repl...
"English Translator and Improver","I want you to act as an English translator, spelling correct...範囲リクエストを使用して、ファイルの特定の部分にアクセスします:
>>> curl -r 40-88 -L https://huggingface.co/datasets/fka/awesome-chatgpt-prompts/resolve/main/prompts.csv
Imagine you are an experienced Ethereum developerアクセストークンを使用して、プライベートリポジトリからストリーミングします:
>>> export HF_TOKEN=hf_xxx
>>> curl -H "Authorization: Bearer $HF_TOKEN" -L https://huggingface.co/...Parquetのストリーミング
ParquetはAIデータセットに最適なフォーマットです。優れた圧縮率、効率的な処理や投影(projection)のためのカラム型構造、高速なフィルタリングのための多層メタデータを提供し、あらゆるサイズのデータセットに適しています。
Parquetファイルは、それぞれ約100MB程度のサイズであることが多い行グループに分割されています。これにより、データローダーやデータ処理フレームワークは、行グループを反復しながらデータを段階的にストリーミングできます。
row group の内部には個々のカラムがあり、これらはページに分割されています。ページは実際のデータを含む、約 1MB の圧縮ブロックです。

ストリーム Row Groups
Hugging Face上のParquetファイルからPyArrowを使用して行グループをストリーミングします:
import pyarrow.parquet as pq
repo_id = "HuggingFaceFW/finewiki"
path_in_repo = "data/enwiki/000_00000.parquet"
# Stream the Parquet file row group per row group
with pq.ParquetFile(f"hf://datasets/{repo_id}/{path_in_repo}") as pf:
for row_group_idx in range(pf.num_row_groups):
row_group_table = pf.read_row_group(row_group_idx)
df = row_group_table.to_pandas()PyArrowは
hf://パスを標準でサポートしており、HfFileSystemを自動的に使用します
PyArrowのドキュメントで詳細を確認してください。
効率的なランダムアクセス
Row group はさらにカラムに、カラムはページに分割されます。ページは通常 1MB 程度で、Parquet におけるデータの最小単位です。圧縮が適用されるのはこのページ単位だからです。ページへのアクセスにより、Row group 全体を読み込むことなく特定の行のみを読み込むことが可能になります。ただし、これは Parquet ファイルにページインデックスが存在する場合に限られます。また、すべての Parquet フレームワークがページレベルでの読み取りをサポートしているわけではありません。例えば PyArrow はサポートしていませんが、Rust の parquet クレートはサポートしています:
use std::sync::Arc;
use object_store::path::Path;
use object_store_opendal::OpendalStore;
use opendal::services::Huggingface;
use opendal::Operator;
use parquet::arrow::async_reader::ParquetObjectReader;
use parquet::arrow::ParquetRecordBatchStreamBuilder;
use futures::TryStreamExt;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let repo_id = "HuggingFaceFW/finewiki";
let path_in_repo = Path::from("data/enwiki/000_00000.parquet");
let offset = 0;
let limit = 10;
let builder = Huggingface::default().repo_type("dataset").repo_id(repo_id);
let operator = Operator::new(builder)?.finish();
let store = Arc::new(OpendalStore::new(operator));
let reader = ParquetObjectReader::new(store, path_in_repo.clone());
let batch_stream =
ParquetRecordBatchStreamBuilder::new(reader).await?
.with_offset(offset as usize)
.with_limit(limit as usize)
.build()?;
let results = batch_stream.try_collect::<Vec<_>>().await?;
println!("Read {} batches", results.len());
Ok(())
}Rustでは、Pythonの
HfFileSystemに相当するOpenDALのHuggingfaceサービスを使用します。
PyArrow で write_page_index=True を渡すことで、効率的なランダムアクセスを可能にするページインデックスを含めることができます。
これにより、Hugging Face の Parquet メタデータビューア で確認できる Parquet のカラムに “offset_index_offset” と “offset_index_length” が追加されます。
ページインデックスは Hugging Face Dataset Viewer の速度も向上させ、行グループのサイズ制限なしでデータを表示できるようにします。