Download ds_batch_ocr/storage.py from Gflorent/deepseek-ocr-job-code: direct link, hf CLI and curl.
- Browser
- Download file 10.2 kB
-
https://huggingface.co/datasets/Gflorent/deepseek-ocr-job-code/resolve/main/ds_batch_ocr/storage.py
- Command line
-
hf download hf://datasets/Gflorent/deepseek-ocr-job-code/ds_batch_ocr/storage.py
-
curl -L -o storage.py https://huggingface.co/datasets/Gflorent/deepseek-ocr-job-code/resolve/main/ds_batch_ocr/storage.py
10.2 kB
| """Unified storage abstraction for dataset I/O. | |
| This module provides a common interface for saving/loading datasets, | |
| abstracting away whether we're using HuggingFace Hub or S3. | |
| Usage: | |
| from .storage import get_storage | |
| storage = get_storage() | |
| storage.save_dataset(dataset, "my_dataset") | |
| dataset = storage.load_dataset() | |
| """ | |
| from __future__ import annotations | |
| import logging | |
| from abc import ABC, abstractmethod | |
| from pathlib import Path | |
| from typing import TYPE_CHECKING, Optional | |
| from .config import env | |
| if TYPE_CHECKING: | |
| from datasets import Dataset | |
| LOGGER = logging.getLogger(__name__) | |
| class DatasetStorage(ABC): | |
| """Abstract base class for dataset storage backends.""" | |
| def upload_files(self, output_dir: Path, path_prefix: str = "") -> bool: | |
| """Upload files from output_dir to storage. | |
| Args: | |
| output_dir: Local directory containing files to upload | |
| path_prefix: Prefix/path within the storage destination | |
| Returns: | |
| True if upload succeeded | |
| """ | |
| pass | |
| def save_dataset(self, dataset: "Dataset", name: str) -> bool: | |
| """Save a HuggingFace dataset to storage. | |
| Args: | |
| dataset: HuggingFace Dataset to save | |
| name: Name/identifier for the dataset | |
| Returns: | |
| True if save succeeded | |
| """ | |
| pass | |
| def load_dataset(self, split: str = "train") -> Optional["Dataset"]: | |
| """Load a HuggingFace dataset from storage. | |
| Args: | |
| split: Dataset split to load | |
| Returns: | |
| Loaded Dataset or None if not available | |
| """ | |
| pass | |
| def is_configured(self) -> bool: | |
| """Check if this storage backend is configured.""" | |
| pass | |
| class HFHubStorage(DatasetStorage): | |
| """HuggingFace Hub storage backend.""" | |
| def __init__( | |
| self, | |
| repo_id: Optional[str] = None, | |
| path_in_repo: str = "", | |
| branch: Optional[str] = None, | |
| commit_message: Optional[str] = None, | |
| ): | |
| self.repo_id = repo_id or env("HF_REPO_ID") | |
| self.path_in_repo = path_in_repo or env("HF_PATH_IN_REPO", "") | |
| self.branch = branch or env("HF_BRANCH") | |
| self.commit_message = commit_message or env("HF_COMMIT_MESSAGE") | |
| self._token = env("HF_TOKEN") | |
| def is_configured(self) -> bool: | |
| return bool(self.repo_id) | |
| def upload_files(self, output_dir: Path, path_prefix: str = "") -> bool: | |
| if not self.is_configured: | |
| LOGGER.debug("HF Hub not configured, skipping file upload") | |
| return False | |
| try: | |
| from .hf_io import maybe_upload_dataset | |
| prefix = path_prefix or self.path_in_repo | |
| maybe_upload_dataset( | |
| output_dir=output_dir, | |
| repo_id=self.repo_id, | |
| path_in_repo=prefix, | |
| commit_message=self.commit_message, | |
| revision=self.branch, | |
| ) | |
| LOGGER.info("Uploaded files to HF Hub: %s", self.repo_id) | |
| return True | |
| except Exception as exc: | |
| LOGGER.exception("HF Hub file upload failed: %s", exc) | |
| return False | |
| def save_dataset(self, dataset: "Dataset", name: str) -> bool: | |
| if not self.is_configured: | |
| LOGGER.debug("HF Hub not configured, skipping dataset save") | |
| return False | |
| try: | |
| dataset.push_to_hub( | |
| self.repo_id, | |
| token=self._token, | |
| revision=self.branch, | |
| commit_message=self.commit_message or f"Add {name}", | |
| ) | |
| LOGGER.info("Pushed dataset to HF Hub: %s", self.repo_id) | |
| return True | |
| except Exception as exc: | |
| LOGGER.exception("HF Hub dataset push failed: %s", exc) | |
| return False | |
| def load_dataset(self, split: str = "train") -> Optional["Dataset"]: | |
| if not self.is_configured: | |
| LOGGER.debug("HF Hub not configured, cannot load dataset") | |
| return None | |
| try: | |
| from datasets import load_dataset | |
| LOGGER.info("Loading dataset from HF Hub: %s", self.repo_id) | |
| return load_dataset(self.repo_id, split=split, token=self._token) | |
| except Exception as exc: | |
| LOGGER.exception("HF Hub dataset load failed: %s", exc) | |
| return None | |
| class S3Storage(DatasetStorage): | |
| """Amazon S3 storage backend using Parquet format.""" | |
| def __init__( | |
| self, | |
| output_uri: Optional[str] = None, | |
| input_uri: Optional[str] = None, | |
| ): | |
| self.output_uri = output_uri or env("S3_OUTPUT_URI") | |
| self.input_uri = input_uri or env("S3_INPUT_URI") | |
| def is_configured(self) -> bool: | |
| return bool(self.output_uri or self.input_uri) | |
| def upload_files(self, output_dir: Path, path_prefix: str = "") -> bool: | |
| if not self.output_uri: | |
| LOGGER.debug("S3 output URI not configured, skipping file upload") | |
| return False | |
| try: | |
| from .sm_io import upload_files_to_s3 | |
| upload_files_to_s3( | |
| output_dir=output_dir, | |
| s3_uri=self.output_uri, | |
| path_prefix=path_prefix, | |
| ) | |
| LOGGER.info("Uploaded files to S3: %s", self.output_uri) | |
| return True | |
| except ImportError as exc: | |
| LOGGER.warning("S3 upload failed (missing dependency): %s", exc) | |
| return False | |
| except Exception as exc: | |
| LOGGER.exception("S3 file upload failed: %s", exc) | |
| return False | |
| def save_dataset(self, dataset: "Dataset", name: str) -> bool: | |
| if not self.output_uri: | |
| LOGGER.debug("S3 output URI not configured, skipping dataset save") | |
| return False | |
| try: | |
| from .sm_io import save_dataset_to_s3 | |
| save_dataset_to_s3(dataset, self.output_uri, name) | |
| return True | |
| except ImportError as exc: | |
| LOGGER.warning("S3 save failed (missing dependency): %s", exc) | |
| return False | |
| except Exception as exc: | |
| LOGGER.exception("S3 dataset save failed: %s", exc) | |
| return False | |
| def load_dataset(self, split: str = "train") -> Optional["Dataset"]: | |
| if not self.input_uri: | |
| LOGGER.debug("S3 input URI not configured, cannot load dataset") | |
| return None | |
| try: | |
| from .sm_io import load_dataset_from_s3 | |
| return load_dataset_from_s3(self.input_uri, split=split) | |
| except ImportError as exc: | |
| # This can happen if sm_io is missing OR if s3fs is not installed | |
| LOGGER.warning("S3 load failed (missing dependency): %s. Install with: pip install s3fs fsspec", exc) | |
| return None | |
| except Exception as exc: | |
| LOGGER.exception("S3 dataset load failed: %s", exc) | |
| return None | |
| class CompositeStorage(DatasetStorage): | |
| """Composite storage that tries multiple backends. | |
| Saves to all configured backends, loads from the first available. | |
| """ | |
| def __init__(self, backends: list[DatasetStorage]): | |
| self._backends = [b for b in backends if b.is_configured] | |
| def is_configured(self) -> bool: | |
| return len(self._backends) > 0 | |
| def upload_files(self, output_dir: Path, path_prefix: str = "") -> bool: | |
| success = False | |
| for backend in self._backends: | |
| if backend.upload_files(output_dir, path_prefix): | |
| success = True | |
| return success | |
| def save_dataset(self, dataset: "Dataset", name: str) -> bool: | |
| success = False | |
| for backend in self._backends: | |
| if backend.save_dataset(dataset, name): | |
| success = True | |
| return success | |
| def load_dataset(self, split: str = "train") -> Optional["Dataset"]: | |
| for backend in self._backends: | |
| dataset = backend.load_dataset(split) | |
| if dataset is not None: | |
| return dataset | |
| return None | |
| def get_storage( | |
| *, | |
| prefer_s3: bool = False, | |
| repo_id: Optional[str] = None, | |
| s3_output_uri: Optional[str] = None, | |
| s3_input_uri: Optional[str] = None, | |
| ) -> DatasetStorage: | |
| """Get the appropriate storage backend based on configuration. | |
| By default, returns a CompositeStorage that saves to all configured backends. | |
| Set prefer_s3=True to prioritize S3 over HF Hub. | |
| Args: | |
| prefer_s3: If True, prioritize S3 storage | |
| repo_id: Override HF repo ID | |
| s3_output_uri: Override S3 output URI | |
| s3_input_uri: Override S3 input URI | |
| Returns: | |
| Configured DatasetStorage instance | |
| """ | |
| s3 = S3Storage(output_uri=s3_output_uri, input_uri=s3_input_uri) | |
| hf = HFHubStorage(repo_id=repo_id) | |
| if s3.is_configured: | |
| if hf.is_configured: | |
| backends = [s3, hf] if prefer_s3 else [hf, s3] | |
| return CompositeStorage(backends) | |
| return s3 | |
| return hf | |
| def get_source_storage( | |
| *, | |
| source_repo_id: Optional[str] = None, | |
| ) -> DatasetStorage: | |
| """Get storage backend for loading source data. | |
| Checks S3_INPUT_URI first, then falls back to HF Hub. | |
| Args: | |
| source_repo_id: HF repo ID to load from (falls back to SOURCE_REPO_ID env var) | |
| Returns: | |
| Configured DatasetStorage instance for loading | |
| """ | |
| s3_input = env("S3_INPUT_URI") | |
| if s3_input: | |
| return S3Storage(input_uri=s3_input) | |
| repo_id = source_repo_id or env("SOURCE_REPO_ID") or env("HF_REPO_ID") | |
| return HFHubStorage(repo_id=repo_id) | |
| __all__ = [ | |
| "DatasetStorage", | |
| "HFHubStorage", | |
| "S3Storage", | |
| "CompositeStorage", | |
| "get_storage", | |
| "get_source_storage", | |
| ] | |