brianyan918 commited on
Commit
bcc76bb
·
1 Parent(s): 7f2a678

add loader

Browse files
Files changed (1) hide show
  1. cs_fleurs.py +74 -0
cs_fleurs.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ from datasets import DatasetInfo, GeneratorBasedBuilder, BuilderConfig, SplitGenerator, Split, Features, Value
4
+
5
+ class CSFLEURSConfig(BuilderConfig):
6
+ """BuilderConfig for a subset of CS-FLEURS."""
7
+ def __init__(self, subset_name=None, **kwargs):
8
+ super().__init__(**kwargs)
9
+ self.subset_name = subset_name # e.g., "read", "xtts", "mms"
10
+
11
+ class CSFLEURS(GeneratorBasedBuilder):
12
+ BUILDER_CONFIGS = [
13
+ CSFLEURSConfig(name="default", subset_name="all", description="All splits"),
14
+ CSFLEURSConfig(name="read", subset_name="read", description="Read subset"),
15
+ CSFLEURSConfig(name="xtts", subset_name="xtts", description="XTTS subset"),
16
+ CSFLEURSConfig(name="mms", subset_name="mms", description="MMS subset"),
17
+ ]
18
+
19
+ def _info(self):
20
+ return DatasetInfo(
21
+ description="CS-FLEURS: Code-switched speech dataset",
22
+ features=Features({
23
+ "id": Value("string"),
24
+ "file_name": Value("string"),
25
+ "text": Value("string"),
26
+ "duration": Value("float"),
27
+ "language": Value("string"),
28
+ "speaker": Value("string"),
29
+ }),
30
+ supervised_keys=None,
31
+ )
32
+
33
+ def _split_generators(self, dl_manager):
34
+ """Dynamically generate splits from subdirectories."""
35
+ base_path = os.path.abspath(".")
36
+ subset_dir = os.path.join(base_path, self.config.subset_name)
37
+
38
+ if not os.path.exists(subset_dir):
39
+ raise FileNotFoundError(f"Subset directory not found: {subset_dir}")
40
+
41
+ splits = []
42
+ for split_name in os.listdir(subset_dir):
43
+ split_path = os.path.join(subset_dir, split_name)
44
+ if os.path.isdir(split_path):
45
+ # Map folder names to Hugging Face Split enum if possible
46
+ hf_split_name = Split.TRAIN if split_name == "train" else Split.TEST
47
+ splits.append(
48
+ SplitGenerator(
49
+ name=hf_split_name if split_name in ["train", "test"] else split_name,
50
+ gen_kwargs={"data_dir": split_path}
51
+ )
52
+ )
53
+
54
+ return splits
55
+
56
+ def _generate_examples(self, data_dir):
57
+ """Yields examples from metadata.jsonl in the given directory."""
58
+ metadata_path = os.path.join(data_dir, "metadata.jsonl")
59
+ if not os.path.exists(metadata_path):
60
+ raise FileNotFoundError(f"Metadata file not found: {metadata_path}")
61
+
62
+ with open(metadata_path, "r", encoding="utf-8") as f:
63
+ for line in f:
64
+ entry = json.loads(line)
65
+ # Make file_name absolute
66
+ file_path = os.path.join(data_dir, entry["file_name"]) if not os.path.isabs(entry["file_name"]) else entry["file_name"]
67
+ yield entry["id"], {
68
+ "id": entry["id"],
69
+ "file_name": file_path,
70
+ "text": entry["text"],
71
+ "duration": float(entry["duration"]),
72
+ "language": entry["language"],
73
+ "speaker": entry["speaker"],
74
+ }