### Download Specific Byte Ranges with StreamingDownloadManager Source: https://context7.com/devadalat/hf-datasets/llms.txt Illustrates how to download only specific byte ranges from a remote file using HTTP range requests. This is highly efficient for accessing parts of large files without downloading the entire content. The example downloads bytes from a specified start to an optional end byte. ```rust use hf_datasets::download_manager::StreamingDownloadManager; use futures::stream::StreamExt; #[tokio::main] async fn main() -> Result<(), Box> { let manager = StreamingDownloadManager::new(); let url = "https://huggingface.co/datasets/facebook/research-plan-gen/resolve/main/data/train-00000-of-00001.parquet"; // Download only bytes 1000-5000 let start_byte = 1000; let end_byte = Some(5000); let mut stream = manager.download_range(url, start_byte, end_byte).await?; let mut downloaded = Vec::new(); while let Some(chunk_result) = stream.next().await { match chunk_result { Ok(chunk) => { downloaded.extend_from_slice(&chunk); } Err(e) => { eprintln!("Download error: {}", e); break; } } } println!("Downloaded {} bytes from range {}-{:?}", downloaded.len(), start_byte, end_byte); Ok(()) } ``` -------------------------------- ### Install hf-datasets and Tokio Dependencies in Cargo.toml Source: https://github.com/devadalat/hf-datasets/blob/master/README.md This snippet shows how to add the `hf-datasets` library and the `tokio` runtime to your project's `Cargo.toml` file for asynchronous operations. ```toml [dependencies] hf-datasets = "0.1.0" tokio = { version = "1.0", features = ["full"] } ``` -------------------------------- ### Stream File Downloads with StreamingDownloadManager Source: https://context7.com/devadalat/hf-datasets/llms.txt Demonstrates the low-level API for streaming file downloads from a URL, including support for HTTP range requests. This example shows how to iterate over downloaded chunks and handle potential download errors. It's useful for large files where downloading the entire file at once is not feasible. ```rust use hf_datasets::download_manager::StreamingDownloadManager; use futures::stream::StreamExt; #[tokio::main] async fn main() -> Result<(), Box> { let manager = StreamingDownloadManager::new(); // Stream a file from the beginning let url = "https://huggingface.co/datasets/facebook/research-plan-gen/resolve/main/data/train-00000-of-00001.parquet"; let mut stream = manager.download_file(url).await?; let mut total_bytes = 0; while let Some(chunk_result) = stream.next().await { match chunk_result { Ok(chunk) => { total_bytes += chunk.len(); println!("Downloaded chunk: {} bytes (total: {})", chunk.len(), total_bytes); // Stop after downloading 1MB for demo if total_bytes > 1_048_576 { break; } } Err(e) => { eprintln!("Download error: {}", e); break; } } } println!("Total downloaded: {} bytes", total_bytes); Ok(()) } ``` -------------------------------- ### Rust: Filter and Transform Data During Streaming Source: https://context7.com/devadalat/hf-datasets/llms.txt Demonstrates how to filter and transform data directly while streaming from a Hugging Face dataset. This example shows how to extract specific fields, apply criteria-based filtering, and collect the transformed data, optimizing data processing pipelines by performing operations on the fly. ```rust use hf_datasets::dataset::StreamingDatasetBuilder; use serde_json::Value; #[tokio::main] async fn main() -> Result<(), Box> { let mut dataset = StreamingDatasetBuilder::new("facebook/research-plan-gen") .split("train") .build() .await?; let mut filtered_records = Vec::new(); while let Some(example_result) = dataset.next().await { if let Ok(example) = example_result { // Extract only specific fields let text = example.get("text") .and_then(|v| v.as_str()) .unwrap_or(""); let label = example.get("label") .and_then(|v| v.as_i64()) .unwrap_or(-1); // Filter based on criteria if text.len() > 50 && label >= 0 { filtered_records.push((text.to_string(), label)); println!("Filtered record {}: label={}, text_len={}", filtered_records.len(), label, text.len()); } if filtered_records.len() >= 10 { break; } } } println!("Collected {} filtered records", filtered_records.len()); Ok(()) } ``` -------------------------------- ### Rust: Implement Batch Processing for HF Datasets Source: https://context7.com/devadalat/hf-datasets/llms.txt Illustrates how to process data from Hugging Face datasets in batches for improved efficiency. This pattern is useful for large datasets where processing individual examples might be too slow. It includes logic to handle full batches and remaining incomplete batches. ```rust use hf_datasets::dataset::StreamingDatasetBuilder; use std::collections::HashMap; #[tokio::main] async fn main() -> Result<(), Box> { let mut dataset = StreamingDatasetBuilder::new("facebook/research-plan-gen") .split("train") .build() .await?; let batch_size = 32; let mut batch: Vec> = Vec::new(); let mut total_processed = 0; while let Some(example_result) = dataset.next().await { match example_result { Ok(example) => { batch.push(example); // Process when batch is full if batch.len() >= batch_size { println!("Processing batch of {} examples", batch.len()); // Your batch processing logic here for (idx, record) in batch.iter().enumerate() { println!(" Batch item {}: {} fields", idx, record.len()); } total_processed += batch.len(); batch.clear(); // Process only first 100 examples for demo if total_processed >= 100 { break; } } } Err(e) => { eprintln!("Error in batch processing: {}", e); break; } } } // Process remaining items in incomplete batch if !batch.is_empty() { println!("Processing final batch of {} examples", batch.len()); total_processed += batch.len(); } println!("Total examples processed: {}", total_processed); Ok(()) } ``` -------------------------------- ### Stream Examples from a Hugging Face Dataset in Rust Source: https://github.com/devadalat/hf-datasets/blob/master/README.md Demonstrates how to use `StreamingDatasetBuilder` to create an asynchronous streaming dataset from a Hugging Face Hub ID, iterate through its records, and handle potential errors. Requires the `tokio` runtime. ```rust use hf_datasets::dataset::StreamingDatasetBuilder; #[tokio::main] async fn main() -> Result<(), Box> { // Create a streaming dataset let mut dataset = StreamingDatasetBuilder::new("facebook/research-plan-gen") .split("train") .build() .await?; // Stream examples let mut count = 0; while let Some(example_result) = dataset.next().await { match example_result { Ok(example) => { println!("Example {}: {:?}", count + 1, example); count += 1; if count >= 5 { break; } } Err(e) => { eprintln!("Error: {}", e); break; } } } Ok(()) } ``` -------------------------------- ### Rust Data Pipeline: Stream, Process, and Analyze Hugging Face Datasets Source: https://context7.com/devadalat/hf-datasets/llms.txt This Rust code demonstrates a full data processing pipeline using the `hf_datasets` library. It streams data from a Hugging Face dataset, processes records to count fields and data types, and includes basic error handling and progress indication. The example is limited to processing the first 100 records for demonstration purposes. It relies on `tokio` for async operations and `serde_json` for data type inspection. ```rust use hf_datasets::dataset::StreamingDatasetBuilder; use hf_datasets::error::HfDatasetsError; use std::collections::HashMap; use serde_json::Value; #[tokio::main] async fn main() -> Result<(), Box> { println!("=== Hugging Face Streaming Dataset Pipeline ===\n"); // Initialize dataset let dataset_id = "facebook/research-plan-gen"; println!("Loading dataset: {}", dataset_id); let mut dataset = StreamingDatasetBuilder::new(dataset_id) .split("train") .build() .await?; println!("Dataset initialized successfully!"); println!("Number of files: {}\n", dataset.file_urls().len()); // Statistics collection let mut stats = Statistics::new(); // Process streaming data while let Some(example_result) = dataset.next().await { match example_result { Ok(example) => { stats.total_records += 1; // Extract and analyze fields for (key, value) in &example { stats.field_counts.entry(key.clone()) .and_modify(|c| *c += 1) .or_insert(1); // Count data types let type_name = match value { Value::Null => "null", Value::Bool(_) => "bool", Value::Number(_) => "number", Value::String(_) => "string", Value::Array(_) => "array", Value::Object(_) => "object", }; stats.type_counts.entry(type_name.to_string()) .and_modify(|c| *c += 1) .or_insert(1); } // Process only first 100 records for demo if stats.total_records >= 100 { break; } } Err(e) => { stats.error_count += 1; eprintln!("Error processing record: {}", e); if stats.error_count > 10 { eprintln!("Too many errors, stopping pipeline"); break; } } } // Progress indicator if stats.total_records % 10 == 0 { println!("Processed {} records...", stats.total_records); } } // Print summary println!("\n=== Pipeline Summary ==="); println!("Total records processed: {}", stats.total_records); println!("Errors encountered: {}", stats.error_count); println!("\nField frequencies:"); for (field, count) in &stats.field_counts { println!(" {}: {}", field, count); } println!("\nData type distribution:"); for (dtype, count) in &stats.type_counts { println!(" {}: {}", dtype, count); } Ok(()) } struct Statistics { total_records: usize, error_count: usize, field_counts: HashMap, type_counts: HashMap, } impl Statistics { fn new() -> Self { Self { total_records: 0, error_count: 0, field_counts: HashMap::new(), type_counts: HashMap::new(), } } } ``` -------------------------------- ### Iterate Over a Streaming Dataset Source: https://github.com/devadalat/hf-datasets/blob/master/huggingface_streaming_guide.md Shows the process of iterating through a dataset loaded in streaming mode. Each iteration triggers the download of necessary data chunks, processing one example at a time. ```python for example in dataset: process(example) ``` -------------------------------- ### Get Streaming Dataset File URLs - Rust Source: https://context7.com/devadalat/hf-datasets/llms.txt Returns a list of Parquet file URLs being streamed from the Hugging Face Hub. This method is useful for inspecting the underlying data sources of the dataset. ```rust use hf_datasets::dataset::StreamingDatasetBuilder; #[tokio::main] async fn main() -> Result<(), Box> { let dataset = StreamingDatasetBuilder::new("facebook/research-plan-gen") .split("train") .build() .await?; // Inspect the underlying file structure let urls = dataset.file_urls(); println!("Dataset contains {} Parquet files:", urls.len()); for (idx, url) in urls.iter().enumerate() { println!(" File {}: {}", idx + 1, url); } Ok(()) } ``` -------------------------------- ### Fetch Dataset Metadata with MetadataFetcher Source: https://context7.com/devadalat/hf-datasets/llms.txt Retrieves comprehensive metadata for a given dataset from the Hugging Face Hub API. It utilizes the `MetadataFetcher` to get information such as dataset ID, download counts, likes, tags, and file details. This is useful for inspecting dataset properties before downloading. ```rust use hf_datasets::metadata::MetadataFetcher; #[tokio::main] async fn main() -> Result<(), Box> { let fetcher = MetadataFetcher::new(); // Fetch dataset information let dataset_info = fetcher.fetch_dataset_info("facebook/research-plan-gen").await?; println!("Dataset ID: {}", dataset_info.id); println!("Downloads: {}", dataset_info.downloads); println!("Likes: {}", dataset_info.likes); println!("Tags: {:?}", dataset_info.tags); println!("\nFiles:"); for file in &dataset_info.siblings { if file.rfilename.ends_with(".parquet") { let size_mb = file.size.unwrap_or(0) as f64 / 1_048_576.0; println!(" {} ({:.2} MB)", file.rfilename, size_mb); } } Ok(()) } ``` -------------------------------- ### Iterate Through Dataset Records - Rust Source: https://context7.com/devadalat/hf-datasets/llms.txt Retrieves the next record from the dataset stream, returning None when all records have been consumed. This function facilitates sequential processing of dataset examples and includes robust error handling for individual records. ```rust use hf_datasets::dataset::StreamingDatasetBuilder; use std::collections::HashMap; #[tokio::main] async fn main() -> Result<(), Box> { let mut dataset = StreamingDatasetBuilder::new("facebook/research-plan-gen") .split("train") .build() .await?; // Process records one at a time with full error handling let mut processed = 0; let mut errors = 0; while let Some(example_result) = dataset.next().await { match example_result { Ok(example) => { // Access fields from the HashMap if let Some(text) = example.get("text") { println!("Text field: {}", text); } if let Some(label) = example.get("label") { println!("Label: {}", label); } processed += 1; if processed >= 10 { break; } } Err(e) => { eprintln!("Failed to read example {}: {}", processed + 1, e); errors += 1; if errors > 5 { eprintln!("Too many errors, stopping"); break; } } } } println!("Successfully processed {} records", processed); Ok(()) } ``` -------------------------------- ### Streaming Dataset Structure and File Streaming (Go) Source: https://github.com/devadalat/hf-datasets/blob/master/huggingface_streaming_guide.md Defines a 'StreamingDataset' struct in Go with a slice of file paths and an HTTP client. It includes a 'streamFile' method to handle HTTP GET requests with a 'Range' header for byte-range fetching, returning an 'io.ReadCloser' for the response body. ```go import ( "net/http" "io" ) type StreamingDataset struct { files []string client *http.Client } func (ds *StreamingDataset) Next() (map[string]interface{}, error) { // Use bufio.Reader for streaming // Use parquet-go library for reading } func (ds *StreamingDataset) streamFile(url string) io.ReadCloser { req, _ := http.NewRequest("GET", url, nil) req.Header.Set("Range", "bytes=0-") resp, _ := ds.client.Do(req) return resp.Body } ``` -------------------------------- ### StreamingDatasetBuilder::build() Source: https://context7.com/devadalat/hf-datasets/llms.txt Builds and initializes the `StreamingDataset` from the configured builder. ```APIDOC ## POST /StreamingDatasetBuilder/{builder_id}/build ### Description Builds and initializes the `StreamingDataset` from the configured builder instance. ### Method POST ### Endpoint /StreamingDatasetBuilder/{builder_id}/build ### Parameters #### Path Parameters - **builder_id** (string) - Required - The identifier of the StreamingDatasetBuilder instance. ### Request Body (No request body needed for this operation) ### Response #### Success Response (200) - **dataset_id** (string) - A unique identifier for the created `StreamingDataset`. #### Response Example ```json { "dataset_id": "dataset_xyz789" } ``` ``` -------------------------------- ### Initialize StreamingDatasetBuilder - Rust Source: https://context7.com/devadalat/hf-datasets/llms.txt Creates a new builder instance for configuring and initializing a streaming dataset from Hugging Face Hub. This function is the entry point for creating a dataset builder. ```rust use hf_datasets::dataset::StreamingDatasetBuilder; #[tokio::main] async fn main() -> Result<(), Box> { // Create a dataset builder with a specific dataset ID let dataset = StreamingDatasetBuilder::new("facebook/research-plan-gen") .split("train") .build() .await?; println!("Dataset initialized with {} files", dataset.file_urls().len()); Ok(()) } ``` -------------------------------- ### StreamingDatasetBuilder::new() Source: https://context7.com/devadalat/hf-datasets/llms.txt Creates a new builder instance for configuring and initializing a streaming dataset from Hugging Face Hub. ```APIDOC ## POST /StreamingDatasetBuilder/new ### Description Creates a new builder instance for configuring and initializing a streaming dataset from Hugging Face Hub. ### Method POST ### Endpoint /StreamingDatasetBuilder/new ### Parameters #### Query Parameters - **dataset_id** (string) - Required - The identifier of the Hugging Face dataset. ### Request Body ```json { "dataset_id": "facebook/research-plan-gen" } ``` ### Response #### Success Response (200) - **builder_id** (string) - A unique identifier for the created builder instance. #### Response Example ```json { "builder_id": "builder_123abc" } ``` ``` -------------------------------- ### Streaming Dataset Structure (Rust) Source: https://github.com/devadalat/hf-datasets/blob/master/huggingface_streaming_guide.md Defines a basic structure for a streaming dataset in Rust. It includes a vector of file URLs and an HTTP client. The 'next_example' method is intended to provide the implementation for fetching the next data record, similar to the Python version. ```rust use reqwest::Client; use parquet::file::reader::SerializedFileReader; use std::io::Cursor; struct StreamingDataset { files: Vec, client: Client, } impl StreamingDataset { async fn next_example(&mut self) -> Option { // Implementation similar to Python version // Use tokio::fs for async file operations } } ``` -------------------------------- ### Select Dataset Split - Rust Source: https://context7.com/devadalat/hf-datasets/llms.txt Specifies which data split to load from the dataset (e.g., train, test, validation). This method allows users to target specific subsets of the dataset. ```rust use hf_datasets::dataset::StreamingDatasetBuilder; #[tokio::main] async fn main() -> Result<(), Box> { // Load the validation split instead of default train split let mut dataset = StreamingDatasetBuilder::new("glue") .split("validation") .build() .await?; let mut count = 0; while let Some(example_result) = dataset.next().await { match example_result { Ok(example) => { println!("Validation example {}: {:?}", count, example); count += 1; if count >= 3 { break; } } Err(e) => { eprintln!("Error: {}", e); break; } } } Ok(()) } ``` -------------------------------- ### Handle hf-datasets Errors in Rust Source: https://github.com/devadalat/hf-datasets/blob/master/README.md Illustrates how to gracefully handle various errors that can occur when working with the `hf-datasets` library, such as dataset not found, network issues, or Parquet parsing errors, using a `match` statement. ```rust use hf_datasets::error::HfDatasetsError; match result { Ok(data) => println!("Success: {:?}", data), Err(HfDatasetsError::DatasetNotFound(name)) => { eprintln!("Dataset '{}' not found", name); } Err(HfDatasetsError::NetworkError(msg)) => { eprintln!("Network error: {}", msg); } Err(HfDatasetsError::ParquetError(msg)) => { eprintln!("Parquet parsing error: {}", msg); } Err(_) => eprintln!("Other error occurred"), } ``` -------------------------------- ### Rust: Handle HfDatasetsError for Network and Dataset Failures Source: https://context7.com/devadalat/hf-datasets/llms.txt Demonstrates comprehensive error handling for various issues encountered when using the HfDatasets library. It specifically catches errors like DatasetNotFound, NetworkError, HttpError, ParquetError, and JsonError, providing user-friendly messages for each. ```rust use hf_datasets::dataset::StreamingDatasetBuilder; use hf_datasets::error::HfDatasetsError; #[tokio::main] async fn main() { // Attempt to load a non-existent dataset let result = StreamingDatasetBuilder::new("invalid/dataset-name") .split("train") .build() .await; match result { Ok(_) => println!("Dataset loaded successfully"), Err(HfDatasetsError::DatasetNotFound(name)) => { eprintln!("Dataset '{}' does not exist on Hugging Face Hub", name); } Err(HfDatasetsError::NetworkError(msg)) => { eprintln!("Network connection failed: {}", msg); } Err(HfDatasetsError::HttpError(e)) => { eprintln!("HTTP request error: {}", e); } Err(HfDatasetsError::ParquetError(msg)) => { eprintln!("Failed to parse Parquet format: {}", msg); } Err(HfDatasetsError::JsonError(e)) => { eprintln!("JSON parsing failed: {}", e); } Err(e) => { eprintln!("Unexpected error: {}", e); } } } ``` -------------------------------- ### Parquet Streaming with PyArrow Source: https://github.com/devadalat/hf-datasets/blob/master/huggingface_streaming_guide.md Explains how Parquet files are streamed using PyArrow. It highlights features like row group streaming, column projection, and predicate pushdown for efficient data reading. ```python import pyarrow.parquet as pq # Internal usage table = pq.read_table(file_obj, columns=requested_columns) for batch in table.to_batches(): for row in batch.to_pylist(): yield row ``` -------------------------------- ### StreamingDatasetBuilder::split() Source: https://context7.com/devadalat/hf-datasets/llms.txt Specifies which data split to load from the dataset (train, test, validation, etc.). ```APIDOC ## POST /StreamingDatasetBuilder/{builder_id}/split ### Description Specifies which data split to load from the dataset (train, test, validation, etc.) using the builder instance. ### Method POST ### Endpoint /StreamingDatasetBuilder/{builder_id}/split ### Parameters #### Path Parameters - **builder_id** (string) - Required - The identifier of the StreamingDatasetBuilder instance. #### Query Parameters - **split_name** (string) - Required - The name of the data split to load (e.g., 'train', 'validation'). ### Request Body ```json { "split_name": "validation" } ``` ### Response #### Success Response (200) - **builder_id** (string) - The identifier of the updated builder instance. #### Response Example ```json { "builder_id": "builder_123abc" } ``` ``` -------------------------------- ### Load Dataset with Streaming Enabled Source: https://github.com/devadalat/hf-datasets/blob/master/huggingface_streaming_guide.md Demonstrates how to load a Hugging Face dataset in streaming mode. This is the entry point for lazy loading, returning an IterableDataset that fetches data on demand. ```python from datasets import load_dataset dataset = load_dataset("dataset_name", split="train", streaming=True) ``` -------------------------------- ### Fetch Dataset Metadata (Python) Source: https://github.com/devadalat/hf-datasets/blob/master/huggingface_streaming_guide.md Retrieves metadata for a given Hugging Face dataset using the API. It requires the 'requests' library to make HTTP calls and returns a JSON object containing dataset information. ```python import requests def get_dataset_info(dataset_name): url = f"https://huggingface.co/api/datasets/{dataset_name}" response = requests.get(url) return response.json() ``` -------------------------------- ### StreamingDataset::file_urls() Source: https://context7.com/devadalat/hf-datasets/llms.txt Returns the list of Parquet file URLs being streamed from the Hugging Face Hub for the dataset. ```APIDOC ## GET /StreamingDataset/{dataset_id}/file_urls ### Description Returns the list of Parquet file URLs that the `StreamingDataset` is configured to stream from the Hugging Face Hub. ### Method GET ### Endpoint /StreamingDataset/{dataset_id}/file_urls ### Parameters #### Path Parameters - **dataset_id** (string) - Required - The identifier of the `StreamingDataset`. ### Request Body (No request body needed for this operation) ### Response #### Success Response (200) - **urls** (array of strings) - A list of URLs pointing to the Parquet files. #### Response Example ```json { "urls": [ "https://huggingface.co/datasets/facebook/research-plan-gen/resolve/main/data/file1.parquet", "https://huggingface.co/datasets/facebook/research-plan-gen/resolve/main/data/file2.parquet" ] } ``` ``` -------------------------------- ### Streaming Dataset Implementation (JavaScript/Node.js) Source: https://github.com/devadalat/hf-datasets/blob/master/huggingface_streaming_guide.md Provides an asynchronous iterator implementation for streaming datasets in JavaScript. It uses 'axios' for HTTP requests and 'parquetjs' for reading Parquet files. The class fetches metadata and streams file content chunk by chunk. ```javascript const axios = require('axios'); const { ParquetReader } = require('parquetjs'); class StreamingDataset { constructor(datasetName) { this.files = await this.fetchMetadata(datasetName); } async *[Symbol.asyncIterator]() { for (const file of this.files) { const stream = await this.streamFile(file.url); const reader = new ParquetReader(stream); for await (const row of reader) { yield row; } } } async streamFile(url) { const response = await axios.get(url, { responseType: 'stream', headers: { 'Range': 'bytes=0-' } }); return response.data; } } ``` -------------------------------- ### StreamingDataset::next() Source: https://context7.com/devadalat/hf-datasets/llms.txt Retrieves the next record from the dataset stream, returning None when all records have been consumed. ```APIDOC ## GET /StreamingDataset/{dataset_id}/next ### Description Retrieves the next record from the dataset stream. Returns `None` when all records have been consumed or an error occurs during fetching. ### Method GET ### Endpoint /StreamingDataset/{dataset_id}/next ### Parameters #### Path Parameters - **dataset_id** (string) - Required - The identifier of the `StreamingDataset`. ### Request Body (No request body needed for this operation) ### Response #### Success Response (200) - **record** (object | null) - A JSON object representing the next record, or null if the stream is exhausted. - Fields within the record object will vary based on the dataset schema. #### Response Example ```json { "record": { "text": "This is an example text.", "label": 0 } } ``` #### Error Response (e.g., 500) - **error** (string) - A message describing the error that occurred. #### Error Response Example ```json { "error": "Failed to fetch record from source." } ``` ``` -------------------------------- ### Streaming Dataset Iterator Implementation (Python) Source: https://github.com/devadalat/hf-datasets/blob/master/huggingface_streaming_guide.md Implements an iterator for streaming dataset files. It fetches dataset metadata, iterates through files, streams content using 'stream_file', and processes Parquet data using 'read_parquet_stream'. Handles file exhaustion by moving to the next file. ```python class StreamingDataset: def __init__(self, dataset_name, split="train"): self.metadata = get_dataset_info(dataset_name) self.files = self.metadata["splits"][split]["files"] self.current_file_idx = 0 def __iter__(self): return self def __next__(self): while self.current_file_idx < len(self.files): file_info = self.files[self.current_file_idx] try: for example in self._read_file(file_info): return example except StopIteration: self.current_file_idx += 1 raise StopIteration def _read_file(self, file_info): stream = stream_file(file_info["url"]) for example in read_parquet_stream(stream): yield example ``` -------------------------------- ### Stream File Content with Range Requests (Python) Source: https://github.com/devadalat/hf-datasets/blob/master/huggingface_streaming_guide.md Streams file content using HTTP Range requests to fetch specific byte ranges. It utilizes the 'requests' library with 'stream=True' for efficient handling of large files and returns an iterator for content chunks. ```python import requests def stream_file(url, start_byte=0, end_byte=None): headers = {"Range": f"bytes={start_byte}-{end_byte or ''}"} response = requests.get(url, headers=headers, stream=True) return response.iter_content(chunk_size=8192) ``` -------------------------------- ### StreamingDownloadManager File Access Source: https://github.com/devadalat/hf-datasets/blob/master/huggingface_streaming_guide.md Illustrates how StreamingDownloadManager uses fsspec to provide file-like objects for remote files. This enables lazy access to data without downloading the entire file. ```python import fsspec class StreamingDownloadManager: def download(self, url): # Instead of downloading, returns a file-like object return fsspec.open(url).open() ``` -------------------------------- ### Process Parquet Stream (Python) Source: https://github.com/devadalat/hf-datasets/blob/master/huggingface_streaming_guide.md Reads a stream of Parquet data, supporting column projection. This function is a placeholder and assumes a 'parquet_reader' function is available, which handles the actual Parquet parsing from the provided file stream. ```python # Assume parquet_reader is a function/library that can read a stream def read_parquet_stream(file_stream, columns=None): reader = parquet_reader(file_stream) for row_group in reader.row_groups: for row in row_group.rows: yield {col: row[col] for col in columns or row.keys()} ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.