Commit
·
1271e80
1
Parent(s):
08bdd0b
Upload download.py with huggingface_hub
Browse files- download.py +71 -0
download.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import glob
|
| 3 |
+
from datasets import load_dataset, get_dataset_config_names
|
| 4 |
+
from dataclasses import dataclass, field
|
| 5 |
+
from transformers import AutoModel, AutoTokenizer, AutoModelForCausalLM, AutoModelForSeq2SeqLM, HfArgumentParser
|
| 6 |
+
from typing import Optional, List
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
@dataclass
|
| 10 |
+
class DownloadArgs:
|
| 11 |
+
model_cache_dir: str = field(
|
| 12 |
+
default='/share/LMs',
|
| 13 |
+
metadata={'help': 'Default path to save language models'}
|
| 14 |
+
)
|
| 15 |
+
model_name_or_path: Optional[str] = field(
|
| 16 |
+
default=None,
|
| 17 |
+
metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models'}
|
| 18 |
+
)
|
| 19 |
+
use_lm_class: bool = field(
|
| 20 |
+
default=False,
|
| 21 |
+
metadata={'help': 'Call .from_pretrained from AutoModelForCausalLM? Useful when downloading remote-code based lms.'}
|
| 22 |
+
)
|
| 23 |
+
dataset_cache_dir: str = field(
|
| 24 |
+
default='/share/peitian/Data/Datasets/huggingface',
|
| 25 |
+
metadata={'help': 'Default path to save huggingface datasets'}
|
| 26 |
+
)
|
| 27 |
+
dataset_name_or_path: Optional[str] = field(
|
| 28 |
+
default=None,
|
| 29 |
+
metadata={'help': 'Dataset name'}
|
| 30 |
+
)
|
| 31 |
+
dataset_subset: Optional[str] = field(
|
| 32 |
+
default=None,
|
| 33 |
+
metadata={'help': 'Dataset subset name'}
|
| 34 |
+
)
|
| 35 |
+
dataset_split: Optional[str] = field(
|
| 36 |
+
default=None,
|
| 37 |
+
metadata={'help': 'Dataset split'}
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
revision: str = field(
|
| 41 |
+
default=None,
|
| 42 |
+
metadata={'help': 'Remote code revision'}
|
| 43 |
+
)
|
| 44 |
+
resume_download: bool = field(
|
| 45 |
+
default=False,
|
| 46 |
+
metadata={'help': 'Resume downloading'}
|
| 47 |
+
)
|
| 48 |
+
def __post_init__(self):
|
| 49 |
+
# folder or model not exists
|
| 50 |
+
if self.model_name_or_path is not None:
|
| 51 |
+
kwargs = {
|
| 52 |
+
'revision': self.revision,
|
| 53 |
+
'resume_download': self.resume_download
|
| 54 |
+
}
|
| 55 |
+
AutoTokenizer.from_pretrained(self.model_name_or_path, cache_dir=self.model_cache_dir, trust_remote_code=True, **kwargs)
|
| 56 |
+
if self.use_lm_class:
|
| 57 |
+
AutoModelForCausalLM.from_pretrained(self.model_name_or_path, cache_dir=self.model_cache_dir, trust_remote_code=True, **kwargs)
|
| 58 |
+
else:
|
| 59 |
+
AutoModel.from_pretrained(self.model_name_or_path, cache_dir=self.model_cache_dir, trust_remote_code=True, **kwargs)
|
| 60 |
+
if self.dataset_name_or_path is not None:
|
| 61 |
+
if self.dataset_subset is None:
|
| 62 |
+
dataset_subsets = get_dataset_config_names(self.dataset_name_or_path)
|
| 63 |
+
for dataset_subset in dataset_subsets:
|
| 64 |
+
load_dataset(self.dataset_name_or_path, name=dataset_subset, split=self.dataset_split, cache_dir=self.dataset_cache_dir)
|
| 65 |
+
else:
|
| 66 |
+
load_dataset(self.dataset_name_or_path, name=self.dataset_subset, split=self.dataset_split, cache_dir=self.dataset_cache_dir)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
if __name__ == "__main__":
|
| 70 |
+
parser = HfArgumentParser([DownloadArgs])
|
| 71 |
+
args, = parser.parse_args_into_dataclasses()
|