Repository navigation
Expand file tree
/
Copy pathfetch_data.py
More file actions
241 lines (208 loc) · 7.95 KB
/
Copy pathfetch_data.py
File metadata and controls
241 lines (208 loc) · 7.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import os
import sys
import random
import argparse
import yaml
import pyarrow.parquet as pq
from huggingface_hub import hf_hub_download
# Local directories
REPO_ROOT = os.path.dirname(os.path.abspath(__file__))
DATA_DIR = os.path.join(REPO_ROOT, "data")
MONO_DIR = os.path.join(DATA_DIR, "monolingual")
# Remote Files (Hugging Face Hub)
# Fill this in with the datasets to download, e.g.:
#
# REMOTE_FILES = {
# "English": {
# "repo_id": "HuggingFaceFW/fineweb-edu",
# "repo_type": "dataset",
# "files": ["sample/10BT/000_00000.parquet"],
# },
# "German": {
# "repo_id": "HuggingFaceFW/fineweb-2",
# "repo_type": "dataset",
# "files": ["data/deu_Latn/train/001_00015.parquet"],
# },
# }
REMOTE_FILES = {
"Balinese": {
"repo_id": "HuggingFaceFW/fineweb-2",
"repo_type": "dataset",
"files": ["data/ban_Latn/train/000_00000.parquet"],
},
"Chinese": {
"repo_id": "HuggingFaceFW/fineweb-2",
"repo_type": "dataset",
"files": ["data/cmn_Hani/train/000_00050.parquet"],
},
"English": {
"repo_id": "HuggingFaceFW/fineweb-edu",
"repo_type": "dataset",
"files": ["sample/10BT/003_00000.parquet"],
},
"Filipino": {
"repo_id": "HuggingFaceFW/fineweb-2",
"repo_type": "dataset",
"files": ["data/fil_Latn/train/000_00000.parquet"],
},
"Indonesian": {
"repo_id": "HuggingFaceFW/fineweb-2",
"repo_type": "dataset",
"files": ["data/ind_Latn/train/001_00000.parquet"],
},
"Javanese": {
"repo_id": "HuggingFaceFW/fineweb-2",
"repo_type": "dataset",
"files": ["data/jav_Latn/train/000_00000.parquet"],
},
"Khmer": {
"repo_id": "HuggingFaceFW/fineweb-2",
"repo_type": "dataset",
"files": ["data/khm_Khmr/train/000_00000.parquet"],
},
"Laos": {
"repo_id": "HuggingFaceFW/fineweb-2",
"repo_type": "dataset",
"files": ["data/lao_Laoo/train/000_00000.parquet"],
},
"Burmese": {
"repo_id": "HuggingFaceFW/fineweb-2",
"repo_type": "dataset",
"files": ["data/mya_Mymr/train/000_00000.parquet"],
},
"Sudanese": {
"repo_id": "HuggingFaceFW/fineweb-2",
"repo_type": "dataset",
"files": ["data/sun_Latn/train/000_00000.parquet"],
},
"Tamil": {
"repo_id": "HuggingFaceFW/fineweb-2",
"repo_type": "dataset",
"files": ["data/tam_Taml/train/001_00000.parquet"],
},
"Thai": {
"repo_id": "HuggingFaceFW/fineweb-2",
"repo_type": "dataset",
"files": ["data/tha_Thai/train/002_00000.parquet"],
},
"Vietnamese": {
"repo_id": "HuggingFaceFW/fineweb-2",
"repo_type": "dataset",
"files": ["data/vie_Latn/train/001_00005.parquet"],
},
"Malay": {
"repo_id": "HuggingFaceFW/fineweb-2",
"repo_type": "dataset",
"files": ["data/zsm_Latn/train/001_00000.parquet"],
},
}
REQUIRED_COLUMN = "text"
def verify_parquet_schema(file_path, required_column=REQUIRED_COLUMN):
"""Checks (without loading the full file) that the parquet file has the required column."""
try:
schema = pq.ParquetFile(file_path).schema_arrow
except Exception as e:
print(f" FAILED to read parquet schema: {e}")
return False
if required_column not in schema.names:
print(f" FAILED: '{required_column}' column not found. Available columns: {schema.names}")
return False
print(f" Verified: '{required_column}' column present.")
return True
def print_sample_rows(file_path, column=REQUIRED_COLUMN, n=3):
"""Prints n randomly chosen values from the given column as a sanity check."""
try:
texts = pq.read_table(file_path, columns=[column]).column(column).to_pylist()
except Exception as e:
print(f" FAILED to read samples: {e}")
return
if not texts:
print(" No rows available to sample.")
return
sample = random.sample(texts, min(n, len(texts)))
print(f" Sample rows ({len(sample)} of {len(texts)}):")
for i, text in enumerate(sample, 1):
preview = (text or "").replace("\n", " ")[:200]
print(f" [{i}] {preview}")
def download_file(repo_id, filename, local_path, repo_type="dataset", revision=None):
print(f"--> Downloading {filename} from {repo_id}...")
try:
downloaded_path = hf_hub_download(
repo_id=repo_id,
filename=filename,
repo_type=repo_type,
revision=revision,
local_dir=os.path.dirname(local_path),
)
# hf_hub_download preserves the remote sub-directory structure under local_dir,
# so move/rename to the flat destination path we expect.
if os.path.abspath(downloaded_path) != os.path.abspath(local_path):
os.makedirs(os.path.dirname(local_path), exist_ok=True)
os.replace(downloaded_path, local_path)
print(" Done.")
return True
except Exception as e:
print(f" FAILED: {e}")
return False
def main():
parser = argparse.ArgumentParser(description="Fetch parquet data files from Hugging Face Hub.")
parser.add_argument("--languages", nargs="+", help="Specific languages to download (e.g. English German). Default: all.")
args = parser.parse_args()
if not REMOTE_FILES:
print("REMOTE_FILES is empty. Please populate it in fetch_data.py with the datasets to download.")
return
# Filter files based on user input
target_files = REMOTE_FILES
if args.languages:
lower_targets = set(l.lower() for l in args.languages)
target_files = {k: v for k, v in REMOTE_FILES.items() if k.lower() in lower_targets}
if not target_files:
print(f"No matching languages found for: {args.languages}")
print(f"Available: {list(REMOTE_FILES.keys())}")
return
os.makedirs(MONO_DIR, exist_ok=True)
# Track local paths for config update
local_data_map = {}
for lang, spec in target_files.items():
repo_id = spec["repo_id"]
repo_type = spec.get("repo_type", "dataset")
revision = spec.get("revision")
files = spec["files"]
lang_dir = os.path.join(MONO_DIR, lang.lower())
os.makedirs(lang_dir, exist_ok=True)
local_paths = []
for remote_filename in files:
filename = os.path.basename(remote_filename)
l_path = os.path.join(lang_dir, filename)
if os.path.exists(l_path):
print(f"Skipping {lang} (already exists): {l_path}")
elif not download_file(repo_id, remote_filename, l_path, repo_type=repo_type, revision=revision):
continue
if verify_parquet_schema(l_path):
print_sample_rows(l_path)
local_paths.append(os.path.relpath(l_path, REPO_ROOT))
else:
print(f" Discarding {l_path} (missing '{REQUIRED_COLUMN}' column).")
os.remove(l_path)
if local_paths:
local_data_map[lang] = local_paths
# Update Config
config_path = os.path.join(REPO_ROOT, "config", "config.yaml")
if os.path.exists(config_path):
with open(config_path, 'r') as f:
config = yaml.safe_load(f)
if 'monolingual_data' not in config:
config['monolingual_data'] = {}
for lang, paths in local_data_map.items():
# Use globs for local paths to match folder content
dir_path = os.path.dirname(paths[0])
config['monolingual_data'][lang] = [os.path.join(dir_path, "*.parquet")]
print("--> Updating config.yaml with local paths...")
with open(config_path, 'w') as f:
yaml.dump(config, f, sort_keys=False)
print(" Done.")
else:
print("Warning: config/config.yaml not found.")
print(f"\nDownload complete for: {list(target_files.keys())}")
if __name__ == "__main__":
main()