Upload stream.py with huggingface_hub
Browse files12348 unmodified lines13141515161716171819202126 unmodified lines484950505152535445 unmodified lines100101102102103104105106107108103104105106107108109110111112113114115116117118119import tempfilefrom typing import Dict, Iterable
from datasets import Dataset, DatasetDict, IterableDataset, IterableDatasetDict8 unmodified lines This class provides methods for generating, caching, and manipulating streaming data.
Attributes: generator (function): A generator function for streaming data. gen_kwargs (dict, optional): A dictionary of keyword arguments for the generator function. caching (bool): Whether the data is cached or not. generator (function): A generator function for streaming data. :no-index: gen_kwargs (dict, optional): A dictionary of keyword arguments for the generator function. :no-index: caching (bool): Whether the data is cached or not. :no-index: """
generator: callable26 unmodified lines def __iter__(self): return iter(self._get_stream())
def peak(self): def peek(self): return next(iter(self))
def take(self, n):45 unmodified lines for stream in self.values(): stream.copying = copying
def to_dataset(self) -> DatasetDict: return DatasetDict( { key: Dataset.from_generator(self.get_generator, gen_kwargs={"key": key}) for key in self.keys() } ) def to_dataset(self, disable_cache=True, cache_dir=None) -> DatasetDict: with tempfile.TemporaryDirectory() as dir_to_be_deleted: cache_dir = dir_to_be_deleted if disable_cache else cache_dir return DatasetDict( { key: Dataset.from_generator( self.get_generator, keep_in_memory=disable_cache, cache_dir=cache_dir, gen_kwargs={"key": key}, ) for key in self.keys() } )
def to_iterable_dataset(self) -> IterableDatasetDict: return IterableDatasetDict(stream.py+19-11