|
1 | | -from torchtext.utils import ( |
2 | | - download_from_url, |
3 | | -) |
| 1 | +from torchtext._internal.module_utils import is_module_available |
| 2 | +from typing import Union, Tuple |
| 3 | + |
| 4 | +if is_module_available("torchdata"): |
| 5 | + from torchdata.datapipes.iter import FileOpener, HttpReader, IterableWrapper |
| 6 | + |
4 | 7 | from torchtext.data.datasets_utils import ( |
5 | | - _RawTextIterableDataset, |
6 | 8 | _wrap_split_argument, |
7 | 9 | _add_docstring_header, |
8 | 10 | _create_dataset_directory, |
9 | | - _create_data_from_csv, |
10 | 11 | ) |
11 | 12 | import os |
12 | 13 |
|
|
30 | 31 |
|
31 | 32 | @_add_docstring_header(num_lines=NUM_LINES, num_classes=4) |
32 | 33 | @_create_dataset_directory(dataset_name=DATASET_NAME) |
33 | | -@_wrap_split_argument(('train', 'test')) |
34 | | -def AG_NEWS(root, split): |
35 | | - path = download_from_url(URL[split], root=root, |
36 | | - path=os.path.join(root, split + ".csv"), |
37 | | - hash_value=MD5[split], |
38 | | - hash_type='md5') |
39 | | - return _RawTextIterableDataset(DATASET_NAME, NUM_LINES[split], |
40 | | - _create_data_from_csv(path)) |
| 34 | +@_wrap_split_argument(("train", "test")) |
| 35 | +def AG_NEWS(root: str, split: Union[Tuple[str], str]): |
| 36 | + if not is_module_available("torchdata"): |
| 37 | + raise ModuleNotFoundError("Package `torchdata` not found. Please install following instructions at `https://github.com/pytorch/data`") |
| 38 | + |
| 39 | + url_dp = IterableWrapper([URL[split]]) |
| 40 | + http_dp = HttpReader(url_dp) |
| 41 | + cache_dp = http_dp.on_disk_cache( |
| 42 | + filepath_fn=lambda x: os.path.join(root, split + ".csv"), |
| 43 | + hash_dict={os.path.join(root, split + ".csv"): MD5[split]}, |
| 44 | + hash_type="md5" |
| 45 | + ).end_caching(mode="w", same_filepath_fn=True) |
| 46 | + |
| 47 | + cache_dp = FileOpener(cache_dp, mode="r") |
| 48 | + return cache_dp.parse_csv().map(fn=lambda t: (int(t[0]), " ".join(t[1:]))) |
0 commit comments