dlxj commited on
Commit ·
1f91d44
1
Parent(s): f3a69db
add convert_eo.py
Browse files- convert_eo.py +83 -0
convert_eo.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
from dataclasses import is_dataclass
|
| 4 |
+
from datasets import load_dataset
|
| 5 |
+
from datasets.utils.logging import set_verbosity_info, enable_progress_bar
|
| 6 |
+
from omegaconf import OmegaConf
|
| 7 |
+
|
| 8 |
+
# 开启 datasets 进度条和日志
|
| 9 |
+
set_verbosity_info()
|
| 10 |
+
enable_progress_bar()
|
| 11 |
+
|
| 12 |
+
# 添加脚本所在路径到 sys.path
|
| 13 |
+
sys.path.append(os.path.join(os.path.dirname(__file__), 'scripts', 'speech_recognition'))
|
| 14 |
+
|
| 15 |
+
from convert_hf_dataset_to_nemo import HFDatasetConversionConfig, prepare_output_dirs, process_dataset
|
| 16 |
+
|
| 17 |
+
def convert_eo():
|
| 18 |
+
# 本地数据集路径
|
| 19 |
+
dataset_path = os.path.join(os.path.dirname(__file__), "data", "common_voice_11_0")
|
| 20 |
+
# 输出路径
|
| 21 |
+
output_dir = os.path.join(os.path.dirname(__file__), "data", "nemo_eo")
|
| 22 |
+
|
| 23 |
+
# 构建配置
|
| 24 |
+
cfg = HFDatasetConversionConfig(
|
| 25 |
+
output_dir=output_dir,
|
| 26 |
+
path=dataset_path,
|
| 27 |
+
name="eo",
|
| 28 |
+
ensure_ascii=False,
|
| 29 |
+
use_auth_token=False,
|
| 30 |
+
num_proc=8, # 使用多进程加快速度
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
# 转换为 OmegaConf
|
| 34 |
+
if is_dataclass(cfg):
|
| 35 |
+
cfg = OmegaConf.structured(cfg)
|
| 36 |
+
|
| 37 |
+
# 准备输出目录
|
| 38 |
+
prepare_output_dirs(cfg)
|
| 39 |
+
|
| 40 |
+
print(f"开始加载数据集 {cfg.path},语言: {cfg.name}...")
|
| 41 |
+
print("此过程会进行数据的准备和解压,请耐心等待(可以通过命令行查看进度条)...")
|
| 42 |
+
|
| 43 |
+
# 加载数据集
|
| 44 |
+
try:
|
| 45 |
+
dataset = load_dataset(
|
| 46 |
+
path=cfg.path,
|
| 47 |
+
name=cfg.name,
|
| 48 |
+
split=cfg.split,
|
| 49 |
+
cache_dir=None,
|
| 50 |
+
streaming=cfg.streaming,
|
| 51 |
+
token=cfg.use_auth_token,
|
| 52 |
+
trust_remote_code=True,
|
| 53 |
+
download_mode="force_redownload",
|
| 54 |
+
)
|
| 55 |
+
print("数据集加载完成!")
|
| 56 |
+
except Exception as e:
|
| 57 |
+
import traceback
|
| 58 |
+
print("Failed to load dataset. Traceback:")
|
| 59 |
+
print(traceback.format_exc())
|
| 60 |
+
sys.exit(1)
|
| 61 |
+
|
| 62 |
+
print("开始进行格式转换 (HF -> NeMo)...")
|
| 63 |
+
# 处理数据集
|
| 64 |
+
if isinstance(dataset, dict):
|
| 65 |
+
print(f"\nMultiple splits found for dataset {cfg.path}: {list(dataset.keys())}")
|
| 66 |
+
keys = list(dataset.keys())
|
| 67 |
+
for key in keys:
|
| 68 |
+
ds_split = dataset[key]
|
| 69 |
+
print(f"Processing split {key} for dataset {cfg.path}")
|
| 70 |
+
cfg.split_output_dir = os.path.join(cfg.resolved_output_dir, key)
|
| 71 |
+
process_dataset(ds_split, cfg)
|
| 72 |
+
del dataset[key], ds_split
|
| 73 |
+
|
| 74 |
+
cfg.split_output_dir = None
|
| 75 |
+
else:
|
| 76 |
+
print(f"Single split found for dataset {cfg.path} | Split chosen = {cfg.split}")
|
| 77 |
+
if cfg.split is not None:
|
| 78 |
+
cfg.split_output_dir = os.path.join(cfg.resolved_output_dir, cfg.split)
|
| 79 |
+
|
| 80 |
+
process_dataset(dataset, cfg)
|
| 81 |
+
|
| 82 |
+
if __name__ == '__main__':
|
| 83 |
+
convert_eo()
|