Enable data streaming
#1
by
lhoestq
HF staff
- opened
- AmericanStories.py +14 -23
AmericanStories.py
CHANGED
@@ -1,11 +1,6 @@
|
|
1 |
import json
|
2 |
-
import tarfile
|
3 |
from datasets import DatasetInfo, DatasetBuilder, DownloadManager, BuilderConfig, SplitGenerator, Split, Version
|
4 |
import datasets
|
5 |
-
import os
|
6 |
-
import requests
|
7 |
-
import re
|
8 |
-
from tqdm import tqdm
|
9 |
|
10 |
SUPPORTED_YEARS = ["1774"]
|
11 |
# Add years from 1798 to 1964 to the supported years
|
@@ -19,14 +14,12 @@ def make_year_file_splits():
|
|
19 |
dict: A dictionary mapping each year to its corresponding file URL.
|
20 |
list: A list of years.
|
21 |
"""
|
22 |
-
base_url = "https://huggingface.co/datasets/dell-research-harvard/AmericanStories/resolve/main/"
|
23 |
|
24 |
# Make a list of years from 1774 to 1960
|
25 |
year_list = [str(year) for year in range(1774, 1960)]
|
26 |
data_files = [f"faro_{year}.tar.gz" for year in year_list]
|
27 |
-
url_list = [base_url + file for file in data_files]
|
28 |
|
29 |
-
splits = {year: file for year, file in zip(year_list,
|
30 |
years = year_list
|
31 |
|
32 |
return splits, years
|
@@ -46,7 +39,7 @@ _FILE_DICT, _YEARS = make_year_file_splits()
|
|
46 |
class CustomBuilderConfig(datasets.BuilderConfig):
|
47 |
"""BuilderConfig for AmericanStories dataset with different configurations."""
|
48 |
|
49 |
-
def __init__(self, year_list=None,
|
50 |
"""
|
51 |
BuilderConfig for AmericanStories dataset.
|
52 |
|
@@ -56,7 +49,6 @@ class CustomBuilderConfig(datasets.BuilderConfig):
|
|
56 |
"""
|
57 |
super(CustomBuilderConfig, self).__init__(**kwargs)
|
58 |
self.year_list = year_list
|
59 |
-
self.features = features
|
60 |
|
61 |
|
62 |
class AmericanStories(datasets.GeneratorBasedBuilder):
|
@@ -92,6 +84,8 @@ class AmericanStories(datasets.GeneratorBasedBuilder):
|
|
92 |
]
|
93 |
DEFAULT_CONFIG_NAME = "subset_years"
|
94 |
|
|
|
|
|
95 |
def _info(self):
|
96 |
"""
|
97 |
Specifies the DatasetInfo object for the AmericanStories dataset.
|
@@ -152,21 +146,22 @@ class AmericanStories(datasets.GeneratorBasedBuilder):
|
|
152 |
urls = {year: urls[year] for year in self.config.year_list}
|
153 |
year_list = self.config.year_list
|
154 |
|
155 |
-
|
156 |
|
157 |
# Return a list of splits, where each split corresponds to a year
|
158 |
return [
|
159 |
datasets.SplitGenerator(
|
160 |
name=year,
|
161 |
gen_kwargs={
|
162 |
-
"
|
|
|
163 |
"split": year,
|
164 |
"associated": True if not self.config.name.endswith("content_regions") else False,
|
165 |
},
|
166 |
) for year in year_list
|
167 |
]
|
168 |
|
169 |
-
def _generate_examples(self, year_dir,split, associated):
|
170 |
"""
|
171 |
Generates examples for the specified year and split.
|
172 |
|
@@ -177,12 +172,11 @@ class AmericanStories(datasets.GeneratorBasedBuilder):
|
|
177 |
Yields:
|
178 |
tuple: The key-value pair containing the example ID and the example data.
|
179 |
"""
|
180 |
-
print("Associated: " + str(associated))
|
181 |
if associated:
|
182 |
-
for filepath in
|
183 |
-
|
184 |
try :
|
185 |
-
data = json.
|
186 |
except:
|
187 |
print("Error loading file: " + filepath)
|
188 |
continue
|
@@ -206,18 +200,15 @@ class AmericanStories(datasets.GeneratorBasedBuilder):
|
|
206 |
"article": article["article"],
|
207 |
}
|
208 |
else:
|
209 |
-
|
210 |
-
|
211 |
-
with open(os.path.join(year_dir, filepath), encoding="utf-8") as f:
|
212 |
try :
|
213 |
-
data = json.
|
214 |
except:
|
215 |
# print("Error loading file: " + filepath)
|
216 |
continue
|
217 |
###Convert json to strng
|
218 |
data=json.dumps(data)
|
219 |
-
print((data))
|
220 |
-
print(type(data))
|
221 |
scan_id=filepath.split('.')[0]
|
222 |
##Yield the scan id and the raw data string
|
223 |
yield scan_id, {
|
|
|
1 |
import json
|
|
|
2 |
from datasets import DatasetInfo, DatasetBuilder, DownloadManager, BuilderConfig, SplitGenerator, Split, Version
|
3 |
import datasets
|
|
|
|
|
|
|
|
|
4 |
|
5 |
SUPPORTED_YEARS = ["1774"]
|
6 |
# Add years from 1798 to 1964 to the supported years
|
|
|
14 |
dict: A dictionary mapping each year to its corresponding file URL.
|
15 |
list: A list of years.
|
16 |
"""
|
|
|
17 |
|
18 |
# Make a list of years from 1774 to 1960
|
19 |
year_list = [str(year) for year in range(1774, 1960)]
|
20 |
data_files = [f"faro_{year}.tar.gz" for year in year_list]
|
|
|
21 |
|
22 |
+
splits = {year: file for year, file in zip(year_list, data_files)}
|
23 |
years = year_list
|
24 |
|
25 |
return splits, years
|
|
|
39 |
class CustomBuilderConfig(datasets.BuilderConfig):
|
40 |
"""BuilderConfig for AmericanStories dataset with different configurations."""
|
41 |
|
42 |
+
def __init__(self, year_list=None, **kwargs):
|
43 |
"""
|
44 |
BuilderConfig for AmericanStories dataset.
|
45 |
|
|
|
49 |
"""
|
50 |
super(CustomBuilderConfig, self).__init__(**kwargs)
|
51 |
self.year_list = year_list
|
|
|
52 |
|
53 |
|
54 |
class AmericanStories(datasets.GeneratorBasedBuilder):
|
|
|
84 |
]
|
85 |
DEFAULT_CONFIG_NAME = "subset_years"
|
86 |
|
87 |
+
BUILDER_CONFIG_CLASS = CustomBuilderConfig
|
88 |
+
|
89 |
def _info(self):
|
90 |
"""
|
91 |
Specifies the DatasetInfo object for the AmericanStories dataset.
|
|
|
146 |
urls = {year: urls[year] for year in self.config.year_list}
|
147 |
year_list = self.config.year_list
|
148 |
|
149 |
+
archive = dl_manager.download(urls)
|
150 |
|
151 |
# Return a list of splits, where each split corresponds to a year
|
152 |
return [
|
153 |
datasets.SplitGenerator(
|
154 |
name=year,
|
155 |
gen_kwargs={
|
156 |
+
"files": dl_manager.iter_archive(archive[year]),
|
157 |
+
"year_dir": "/".join(["mnt", "122a7683-fa4b-45dd-9f13-b18cc4f4a187", "ca_rule_based_fa_clean", "faro_" + year]),
|
158 |
"split": year,
|
159 |
"associated": True if not self.config.name.endswith("content_regions") else False,
|
160 |
},
|
161 |
) for year in year_list
|
162 |
]
|
163 |
|
164 |
+
def _generate_examples(self, files, year_dir, split, associated):
|
165 |
"""
|
166 |
Generates examples for the specified year and split.
|
167 |
|
|
|
172 |
Yields:
|
173 |
tuple: The key-value pair containing the example ID and the example data.
|
174 |
"""
|
|
|
175 |
if associated:
|
176 |
+
for filepath, f in files:
|
177 |
+
if filepath.startswith(year_dir):
|
178 |
try :
|
179 |
+
data = json.loads(f.read().decode("utf-8"))
|
180 |
except:
|
181 |
print("Error loading file: " + filepath)
|
182 |
continue
|
|
|
200 |
"article": article["article"],
|
201 |
}
|
202 |
else:
|
203 |
+
for filepath, f in files:
|
204 |
+
if filepath.startswith(year_dir):
|
|
|
205 |
try :
|
206 |
+
data = json.loads(f.read().decode("utf-8"))
|
207 |
except:
|
208 |
# print("Error loading file: " + filepath)
|
209 |
continue
|
210 |
###Convert json to strng
|
211 |
data=json.dumps(data)
|
|
|
|
|
212 |
scan_id=filepath.split('.')[0]
|
213 |
##Yield the scan id and the raw data string
|
214 |
yield scan_id, {
|