diarray commited on
Commit
6cdd5c7
·
1 Parent(s): f9ba6f2

Update dataset loading script

Browse files
Files changed (1) hide show
  1. jeli-asr.py +27 -29
jeli-asr.py CHANGED
@@ -14,8 +14,8 @@ See the License for the specific language governing permissions and
14
  limitations under the License.
15
  """
16
  # jeli_asr.py
17
- import os
18
  import csv
 
19
 
20
  import datasets
21
  from datasets import Split, SplitGenerator
@@ -37,7 +37,7 @@ the cleaned subset of Oza's Bambara-ASR with the Jeli-ASR-RMAI subset.
37
 
38
  _HOMEPAGE = "https://huggingface.co/datasets/RobotsMali/jeli-asr"
39
  _LICENSE = "CC-BY-4.0"
40
-
41
 
42
  class JeliASRConfig(datasets.BuilderConfig):
43
  """BuilderConfig for different subsets of the Jeli-ASR dataset."""
@@ -50,17 +50,17 @@ class JeliASR(datasets.GeneratorBasedBuilder):
50
  BUILDER_CONFIGS = [
51
  JeliASRConfig(
52
  name="jeli-asr-rmai",
53
- version=datasets.Version("1.0.0"),
54
  description="Load only the Jeli-ASR RMAI subset (files under jeli-asr-rmai/)",
55
  ),
56
  JeliASRConfig(
57
  name="bam-asr-oza",
58
- version=datasets.Version("1.0.0"),
59
  description="Load only the Bam-ASR Oza subset (files under bam-asr-oza/)",
60
  ),
61
  JeliASRConfig(
62
  name="jeli-asr",
63
- version=datasets.Version("1.0.0"),
64
  description="Combined data: jeli-asr-rmai + bam-asr-oza (all rows).",
65
  ),
66
  ]
@@ -73,7 +73,7 @@ class JeliASR(datasets.GeneratorBasedBuilder):
73
  description=_DESCRIPTION,
74
  features=datasets.Features(
75
  {
76
- # We'll load the audio from local paths (relative to data_dir + file_name).
77
  "audio": datasets.Audio(sampling_rate=16_000),
78
  "duration": datasets.Value("float32"),
79
  "bam": datasets.Value("string"),
@@ -88,15 +88,15 @@ class JeliASR(datasets.GeneratorBasedBuilder):
88
 
89
  def _split_generators(self, dl_manager):
90
  """
91
- We assume metadata.csv is in data_dir. We'll generate two splits:
92
- - TRAIN: file paths containing "/train/"
93
- - TEST: file paths containing "/test/"
94
  """
95
- metadata_path = "metadata.csv"
 
 
96
 
97
- # We do not need to call dl_manager.download_and_extract here because
98
- # the audio + CSV are already on the Hub. The Datasets library will handle
99
- # fetching them automatically if not present locally.
100
 
101
  return [
102
  SplitGenerator(
@@ -104,6 +104,7 @@ class JeliASR(datasets.GeneratorBasedBuilder):
104
  gen_kwargs={
105
  "metadata_path": metadata_path,
106
  "split": "train",
 
107
  },
108
  ),
109
  SplitGenerator(
@@ -111,50 +112,47 @@ class JeliASR(datasets.GeneratorBasedBuilder):
111
  gen_kwargs={
112
  "metadata_path": metadata_path,
113
  "split": "test",
 
114
  },
115
  ),
116
  ]
117
 
118
- def _generate_examples(self, metadata_path, split):
119
  """
120
- Read metadata.csv row by row.
121
- Filter rows by:
122
  1) Config name (jeli-asr-rmai, bam-asr-oza, or jeli-asr).
123
- 2) Split substring (train/test).
124
- Yield each example with features: audio, duration, bam, french.
125
  """
126
  with open(metadata_path, "r", encoding="utf-8") as f:
127
  reader = csv.DictReader(f)
128
  idx = 0
129
  for row in reader:
130
- file_path = row["file_name"] # e.g. "bam-asr-oza/train/split1/oza75-bam-asr-0.wav"
131
 
132
- # 1) Filter by config name
133
  if self.config.name == "jeli-asr-rmai":
134
- # Keep only rows with 'jeli-asr-rmai/' in the file path
135
  if "jeli-asr-rmai/" not in file_path:
136
  continue
137
  elif self.config.name == "bam-asr-oza":
138
- # Keep only rows with 'bam-asr-oza/' in the file path
139
  if "bam-asr-oza/" not in file_path:
140
  continue
141
- # If config.name == "jeli-asr", we include all rows.
142
 
143
- # 2) Filter by split (train/test)
144
  if split == "train" and "/train/" not in file_path:
145
  continue
146
  if split == "test" and "/test/" not in file_path:
147
  continue
148
 
149
- # Construct the full path to the audio file, relative to self.config.data_dir
150
- # e.g. "jeli-asr/bam-asr-oza/train/..."
151
- #audio_path = os.path.join(self.config.data_dir, file_path)
152
 
153
- # Yield a dictionary matching the schema in _info()
154
  yield idx, {
155
- "audio": file_path,
156
  "duration": float(row["duration"]),
157
  "bam": row["bam"],
158
  "french": row["french"],
159
  }
160
  idx += 1
 
 
14
  limitations under the License.
15
  """
16
  # jeli_asr.py
 
17
  import csv
18
+ import os
19
 
20
  import datasets
21
  from datasets import Split, SplitGenerator
 
37
 
38
  _HOMEPAGE = "https://huggingface.co/datasets/RobotsMali/jeli-asr"
39
  _LICENSE = "CC-BY-4.0"
40
+ VERSION = datasets.Version("1.0.0")
41
 
42
  class JeliASRConfig(datasets.BuilderConfig):
43
  """BuilderConfig for different subsets of the Jeli-ASR dataset."""
 
50
  BUILDER_CONFIGS = [
51
  JeliASRConfig(
52
  name="jeli-asr-rmai",
53
+ version=VERSION,
54
  description="Load only the Jeli-ASR RMAI subset (files under jeli-asr-rmai/)",
55
  ),
56
  JeliASRConfig(
57
  name="bam-asr-oza",
58
+ version=VERSION,
59
  description="Load only the Bam-ASR Oza subset (files under bam-asr-oza/)",
60
  ),
61
  JeliASRConfig(
62
  name="jeli-asr",
63
+ version=VERSION,
64
  description="Combined data: jeli-asr-rmai + bam-asr-oza (all rows).",
65
  ),
66
  ]
 
73
  description=_DESCRIPTION,
74
  features=datasets.Features(
75
  {
76
+ # We'll load the audio from local paths.
77
  "audio": datasets.Audio(sampling_rate=16_000),
78
  "duration": datasets.Value("float32"),
79
  "bam": datasets.Value("string"),
 
88
 
89
  def _split_generators(self, dl_manager):
90
  """
91
+ We'll generate two splits by scanning metadata.csv for rows
92
+ containing '/train/' or '/test/' in the file path.
 
93
  """
94
+ # If the user doesn't pass data_dir, self.config.data_dir might be empty or None,
95
+ # so let's handle that gracefully:
96
+ base_dir = self.config.data_dir or ""
97
 
98
+ # metadata.csv is assumed to be in the repo root (or base_dir if there's a subfolder).
99
+ metadata_path = os.path.join(base_dir, "metadata.csv")
 
100
 
101
  return [
102
  SplitGenerator(
 
104
  gen_kwargs={
105
  "metadata_path": metadata_path,
106
  "split": "train",
107
+ "base_dir": base_dir
108
  },
109
  ),
110
  SplitGenerator(
 
112
  gen_kwargs={
113
  "metadata_path": metadata_path,
114
  "split": "test",
115
+ "base_dir": base_dir
116
  },
117
  ),
118
  ]
119
 
120
+ def _generate_examples(self, metadata_path, split, base_dir):
121
  """
122
+ Read metadata.csv row-by-row. Filter by:
 
123
  1) Config name (jeli-asr-rmai, bam-asr-oza, or jeli-asr).
124
+ 2) 'train' vs 'test' in the file path.
125
+ Yield each example with audio, duration, bam, and french.
126
  """
127
  with open(metadata_path, "r", encoding="utf-8") as f:
128
  reader = csv.DictReader(f)
129
  idx = 0
130
  for row in reader:
131
+ file_path = row["file_name"] # e.g. 'bam-asr-oza/train/split1/oza75-bam-asr-0.wav'
132
 
133
+ # Filter by config
134
  if self.config.name == "jeli-asr-rmai":
 
135
  if "jeli-asr-rmai/" not in file_path:
136
  continue
137
  elif self.config.name == "bam-asr-oza":
 
138
  if "bam-asr-oza/" not in file_path:
139
  continue
140
+ # If config is "jeli-asr", include everything.
141
 
142
+ # Filter by train/test
143
  if split == "train" and "/train/" not in file_path:
144
  continue
145
  if split == "test" and "/test/" not in file_path:
146
  continue
147
 
148
+ # Build the absolute local path to the audio file (within the cloned repo)
149
+ audio_path = os.path.join(base_dir, file_path)
 
150
 
 
151
  yield idx, {
152
+ "audio": audio_path,
153
  "duration": float(row["duration"]),
154
  "bam": row["bam"],
155
  "french": row["french"],
156
  }
157
  idx += 1
158
+