Sh1man commited on
Commit
b6dbfac
·
1 Parent(s): 16a68b8

Update golos_opus.py

Browse files
Files changed (1) hide show
  1. golos_opus.py +34 -19
golos_opus.py CHANGED
@@ -117,51 +117,66 @@ class GolosOpus(datasets.GeneratorBasedBuilder):
117
  audio_archives = dl_manager.download(audio_urls)
118
  transcript_paths = dl_manager.download_and_extract(transcript_urls)
119
 
 
 
 
 
120
  return [
121
  datasets.SplitGenerator(
122
  name=datasets.Split.TRAIN,
123
  gen_kwargs={
124
- "archives": [dl_manager.iter_archive(path) for path in audio_archives["train"]],
125
  "transcript_path": transcript_paths["train"],
126
- "split": "train",
127
  },
128
  ),
129
  datasets.SplitGenerator(
130
  name=datasets.Split.TEST,
131
  gen_kwargs={
132
- "archives": [dl_manager.iter_archive(path) for path in audio_archives["test"]],
133
  "transcript_path": transcript_paths["test"],
134
- "split": "test",
135
  },
136
  ),
137
  ]
138
 
139
- def _generate_examples(self, archives, transcript_path, split):
140
  """Yields examples."""
 
 
 
141
  # Загружаем метаданные из TSV
142
  metadata = {}
143
  with open(transcript_path, encoding="utf-8") as f:
144
- reader = csv.DictReader(f, delimiter="\t")
145
  for row in reader:
 
 
 
 
 
 
 
 
 
146
  metadata[row["path"]] = row
147
-
148
  # Проходим по всем архивам и извлекаем файлы
149
- example_idx = 0
150
  for archive_idx, archive in enumerate(archives):
151
  for file_path, file_obj in archive:
152
- # Получаем имя файла из пути без использования os.path
153
- # Используем самый простой способ - разделение по '/' и взятие последнего элемента
154
  filename = file_path.split("/")[-1]
155
 
156
  # Если файл есть в метаданных, создаем пример
157
  if filename in metadata:
158
  row = metadata[filename]
159
- example = {
160
- "id": row["id"],
161
- "path": filename,
162
- "text": row["text"],
163
- "duration": float(row["duration"]) if row["duration"] else 0.0,
164
- "audio": {"path": file_path, "bytes": file_obj.read()},
165
- }
166
- yield example_idx, example
167
- example_idx += 1
 
 
 
 
117
  audio_archives = dl_manager.download(audio_urls)
118
  transcript_paths = dl_manager.download_and_extract(transcript_urls)
119
 
120
+ # Создаем архивы для каждого разделения
121
+ train_archives = [dl_manager.iter_archive(path) for path in audio_archives["train"]]
122
+ test_archives = [dl_manager.iter_archive(path) for path in audio_archives["test"]]
123
+
124
  return [
125
  datasets.SplitGenerator(
126
  name=datasets.Split.TRAIN,
127
  gen_kwargs={
128
+ "archives": train_archives,
129
  "transcript_path": transcript_paths["train"],
 
130
  },
131
  ),
132
  datasets.SplitGenerator(
133
  name=datasets.Split.TEST,
134
  gen_kwargs={
135
+ "archives": test_archives,
136
  "transcript_path": transcript_paths["test"],
 
137
  },
138
  ),
139
  ]
140
 
141
+ def _generate_examples(self, archives, transcript_path):
142
  """Yields examples."""
143
+ # Получаем список всех ожидаемых полей
144
+ data_fields = list(self._info().features.keys())
145
+
146
  # Загружаем метаданные из TSV
147
  metadata = {}
148
  with open(transcript_path, encoding="utf-8") as f:
149
+ reader = csv.DictReader(f, delimiter="\t", quoting=csv.QUOTE_NONE)
150
  for row in reader:
151
+ # Если нужно добавить к имени файла расширение
152
+ if not row["path"].endswith(".opus"):
153
+ row["path"] += ".opus"
154
+
155
+ # Заполняем отсутствующие поля пустыми строками
156
+ for field in data_fields:
157
+ if field not in row and field != "audio":
158
+ row[field] = ""
159
+
160
  metadata[row["path"]] = row
161
+
162
  # Проходим по всем архивам и извлекаем файлы
 
163
  for archive_idx, archive in enumerate(archives):
164
  for file_path, file_obj in archive:
165
+ # Получаем имя файла из пути
 
166
  filename = file_path.split("/")[-1]
167
 
168
  # Если файл есть в метаданных, создаем пример
169
  if filename in metadata:
170
  row = metadata[filename]
171
+
172
+ # Копируем данные из метаданных
173
+ result = dict(row)
174
+
175
+ # Добавляем аудио-данные
176
+ result["audio"] = {"path": file_path, "bytes": file_obj.read()}
177
+
178
+ # Устанавливаем путь
179
+ result["path"] = filename
180
+
181
+ # Как в Common Voice: ключ = путь к файлу, значение = запись
182
+ yield file_path, result