Upload operators.py with huggingface_hub
Browse files35 unmodified lines3637383940414212 unmodified lines55565758596061622 unmodified lines656667686970717273747075767715 unmodified lines93949592969798999 unmodified lines1091101111081121131141151672 unmodified lines17881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184135 unmodified linesimport operatorimport osimport uuidimport zipfilefrom abc import abstractmethodfrom collections import Counterfrom copy import deepcopy12 unmodified lines Union,)
import requests
from .artifact import Artifact, fetch_artifactfrom .dataclass import NonPositionalFieldfrom .dict_utils import dict_delete, dict_get, dict_set, is_subpath2 unmodified lines MultiStreamOperator, PagedStreamOperator, SequentialOperator, SideEffectOperator, SingleStreamOperator, SingleStreamReducer, SourceOperator, StreamingOperator, StreamInitializerOperator, StreamInstanceOperator, StreamSource,)from .random_utils import new_random_generatorfrom .stream import Stream15 unmodified lines return MultiStream.from_iterables(iterables)
class IterableSource(StreamSource):class IterableSource(SourceOperator): """Creates a MultiStream from a dict of named iterables.
It is a callable.9 unmodified lines
iterables: Dict[str, Iterable]
def __call__(self) -> MultiStream: def process(self) -> MultiStream: return MultiStream.from_iterables(self.iterables)
1672 unmodified lines if total_len < val: return i return i + 1
class DownloadError(Exception): def __init__( self, message, ): self.__super__(message)
class UnexpectedHttpCodeError(Exception): def __init__(self, http_code): self.__super__(f"unexpected http code {http_code}")
class DownloadOperator(SideEffectOperator): """Operator for downloading a file from a given URL to a specified local path.
Attributes: source (str): URL of the file to be downloaded. target (str): Local path where the downloaded file should be saved. """
source: str target: str
def process(self): try: response = requests.get(self.source, allow_redirects=True) except Exception as e: raise DownloadError(f"Unabled to download {self.source}") from e if response.status_code != 200: raise UnexpectedHttpCodeError(response.status_code) with open(self.target, "wb") as f: f.write(response.content)
class ExtractZipFile(SideEffectOperator): """Operator for extracting files from a zip archive.
Attributes: zip_file (str): Path of the zip file to be extracted. target_dir (str): Directory where the contents of the zip file will be extracted. """
zip_file: str target_dir: str
def process(self): with zipfile.ZipFile(self.zip_file) as zf: zf.extractall(self.target_dir)operators.py+58-3