jkot commited on
Commit
0c4fd82
1 Parent(s): e05ed3f

Update czech_parliament_plenary_hearings.py

Browse files
Files changed (1) hide show
  1. czech_parliament_plenary_hearings.py +43 -45
czech_parliament_plenary_hearings.py CHANGED
@@ -1,8 +1,10 @@
1
- from datasets import Dataset, GeneratorBasedBuilder, Features
2
  import os
3
  import tarfile
4
- import librosa
 
 
5
  import datasets
 
6
  _LICENSE = "https://creativecommons.org/licenses/by/4.0/"
7
  _HOMEPAGE = "https://lindat.mff.cuni.cz/repository/xmlui/handle/11234/1-3126"
8
  _DATASET_URL = "https://lindat.mff.cuni.cz/repository/xmlui/bitstream/handle/11234/1-3126/snemovna.tar.xz"
@@ -21,9 +23,8 @@ _CITATION = """\
21
  year = {2019} } """
22
 
23
 
24
- class CzechParliamentPlenaryHearings(GeneratorBasedBuilder):
25
- def __init__(self, **kwargs):
26
- super().__init__(**kwargs)
27
 
28
  def _info(self):
29
  return datasets.DatasetInfo(
@@ -31,8 +32,9 @@ class CzechParliamentPlenaryHearings(GeneratorBasedBuilder):
31
  features=datasets.Features(
32
  {
33
  "id": datasets.Value("string"),
34
- "audio": datasets.features.Audio(sampling_rate=16000),
35
- "transcription": datasets.Value("string")
 
36
  }
37
  ),
38
  supervised_keys=None,
@@ -42,44 +44,40 @@ class CzechParliamentPlenaryHearings(GeneratorBasedBuilder):
42
  )
43
 
44
  def _split_generators(self, dl_manager):
45
- archive_path = dl_manager.download(_DATASET_URL)
46
- data_dir = os.path.join(archive_path, 'ASR_DATA')
47
- splits = ("train", "dev", "test")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
- split_names = {
50
- "train": datasets.Split.TRAIN,
51
- "dev": datasets.Split.VALIDATION,
52
- "test": datasets.Split.TEST,
53
- }
54
- split_generators = []
55
- for split in splits:
56
- split_dir = os.path.join(data_dir, split)
57
- split_generators.append(
58
- datasets.SplitGenerator(
59
- name=split_names.get(split, split),
60
- gen_kwargs={'archive_path': archive_path, 'split_dir': split_dir})
61
- )
62
- return split_generators
63
 
64
- def _generate_examples(self, split_dir, archive_path):
65
- for folder_name in os.listdir(split_dir):
66
- folder_path = os.path.join(split_dir, folder_name)
67
- if os.path.isdir(folder_path):
68
- for file_name in os.listdir(folder_path):
69
- file_path = os.path.join(folder_path, file_name)
70
- if file_name.endswith('.trn'):
71
- with open(file_path, 'r') as f:
72
- transcription = f.read().strip()
73
- elif file_name.endswith('.wav'):
74
- with self._dl_manager.iter_archive(archive_path, filter_func=lambda x: x.path == file_path) as files:
75
- audio_file = next(files)
76
- audio_path = os.path.join(
77
- folder_name, audio_file.path.split('/')[-1])
78
- audio, sr = librosa.load(audio_file, sr=16000)
79
 
80
- yield f'{folder_name}/{audio_file.path.split("/")[-1]}', {
81
- 'audio': {
82
- 'path': audio_path, 'bytes': audio
83
- },
84
- 'transcription': transcription,
85
- }
 
 
1
  import os
2
  import tarfile
3
+
4
+ import soundfile as sf
5
+
6
  import datasets
7
+
8
  _LICENSE = "https://creativecommons.org/licenses/by/4.0/"
9
  _HOMEPAGE = "https://lindat.mff.cuni.cz/repository/xmlui/handle/11234/1-3126"
10
  _DATASET_URL = "https://lindat.mff.cuni.cz/repository/xmlui/bitstream/handle/11234/1-3126/snemovna.tar.xz"
 
23
  year = {2019} } """
24
 
25
 
26
+ class MyAudioDataset(datasets.GeneratorBasedBuilder):
27
+ VERSION = datasets.Version("1.0.0")
 
28
 
29
  def _info(self):
30
  return datasets.DatasetInfo(
 
32
  features=datasets.Features(
33
  {
34
  "id": datasets.Value("string"),
35
+ "audio": datasets.features.Audio(),
36
+ "transcription": datasets.Value("string"),
37
+ "audio_sampling_rate": datasets.Value("int32"),
38
  }
39
  ),
40
  supervised_keys=None,
 
44
  )
45
 
46
  def _split_generators(self, dl_manager):
47
+ archive_path = dl_manager.download_and_extract(_DATASET_URL)
48
+
49
+ return [
50
+ datasets.SplitGenerator(
51
+ name=datasets.Split.TRAIN,
52
+ gen_kwargs={"directory": os.path.join(
53
+ archive_path, "ASR_DATA", "train")},
54
+ ),
55
+ datasets.SplitGenerator(
56
+ name=datasets.Split.VALIDATION,
57
+ gen_kwargs={"directory": os.path.join(
58
+ archive_path, "ASR_DATA", "dev")},
59
+ ),
60
+ datasets.SplitGenerator(
61
+ name=datasets.Split.TEST,
62
+ gen_kwargs={"directory": os.path.join(
63
+ archive_path, "ASR_DATA", "test")},
64
+ ),
65
+ ]
66
 
67
+ def _generate_examples(self, directory):
68
+ for root, _, files in os.walk(directory):
69
+ for filename in files:
70
+ if filename.endswith(".wav"):
71
+ audio_path = os.path.join(root, filename)
72
+ transcription_path = os.path.join(root, filename + ".trn")
 
 
 
 
 
 
 
 
73
 
74
+ # Load audio and transcription
75
+ audio, sampling_rate = sf.read(audio_path)
76
+ with open(transcription_path, "r", encoding="utf-8") as f:
77
+ transcription = f.read().strip()
 
 
 
 
 
 
 
 
 
 
 
78
 
79
+ yield f"{audio_path}-{transcription}", {
80
+ "audio": audio,
81
+ "transcription": transcription,
82
+ "audio_sampling_rate": sampling_rate,
83
+ }