|
from pathlib import Path |
|
from datasets import load_dataset, load_from_disk |
|
from dataclasses import dataclass, field |
|
from huggingface_hub import HfApi |
|
from transformers import AutoModel, AutoTokenizer, HfArgumentParser |
|
from typing import Optional, List |
|
|
|
|
|
@dataclass |
|
class DownloadArgs: |
|
model_cache_dir: str = field( |
|
default='/share/LMs', |
|
metadata={'help': 'Default path to save language models'} |
|
) |
|
model_name_or_path: Optional[str] = field( |
|
default=None, |
|
metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models'} |
|
) |
|
dataset_cache_dir: str = field( |
|
default='/share/peitian/Data/Datasets/huggingface', |
|
metadata={'help': 'Default path to save huggingface datasets'} |
|
) |
|
dataset_name_or_path: Optional[str] = field( |
|
default=None, |
|
metadata={'help': 'Dataset name'} |
|
) |
|
data_files: Optional[dict] = field( |
|
default=None, |
|
metadata={'help': 'Data files for json dataset.'} |
|
) |
|
dataset_from_disk: bool = field( |
|
default=False, |
|
metadata={'help': 'Load dataset from disk?'} |
|
) |
|
|
|
file: Optional[str] = field( |
|
default=None, |
|
metadata={'help': 'File to upload.'} |
|
) |
|
file_in_repo: Optional[str] = field( |
|
default=None, |
|
metadata={'help': 'File name in repository.'} |
|
) |
|
|
|
hub_name: Optional[str] = field( |
|
default=None, |
|
metadata={'help': 'Name of the huggingface repo.'} |
|
) |
|
|
|
revision: str = field( |
|
default=None, |
|
metadata={'help': 'Remote code revision'} |
|
) |
|
resume_download: bool = field( |
|
default=True, |
|
metadata={'help': 'Resume downloading'} |
|
) |
|
def __post_init__(self): |
|
|
|
if self.model_name_or_path is not None: |
|
tokenizer = AutoTokenizer.from_pretrained(self.model_name_or_path, cache_dir=self.model_cache_dir, trust_remote_code=True) |
|
model = AutoModel.from_pretrained(self.model_name_or_path, cache_dir=self.model_cache_dir, trust_remote_code=True) |
|
|
|
while 1: |
|
try: |
|
tokenizer.push_to_hub(self.hub_name) |
|
break |
|
except: |
|
pass |
|
while 1: |
|
try: |
|
model.push_to_hub(self.hub_name) |
|
break |
|
except: |
|
pass |
|
|
|
if self.dataset_name_or_path is not None: |
|
if self.dataset_from_disk: |
|
dataset = load_from_disk(self.dataset_name_or_path) |
|
else: |
|
dataset = load_dataset(self.dataset_name_or_path, data_files=self.data_files, cache_dir=self.dataset_cache_dir) |
|
|
|
while 1: |
|
try: |
|
dataset.push_to_hub(self.hub_name) |
|
break |
|
except: |
|
pass |
|
|
|
if self.file is not None: |
|
api = HfApi() |
|
if self.file_in_repo is None: |
|
self.file_in_repo = Path(self.file).name |
|
api.upload_file( |
|
path_or_fileobj=self.file, |
|
path_in_repo=self.file_in_repo, |
|
repo_id=self.hub_name, |
|
repo_type="dataset", |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
parser = HfArgumentParser([DownloadArgs]) |
|
args, = parser.parse_args_into_dataclasses() |
|
|
|
|
|
|