cahya commited on
Commit
6423f2a
·
1 Parent(s): 10dfda1

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +45 -16
README.md CHANGED
@@ -40,8 +40,37 @@ import torchaudio
40
  from datasets import load_dataset
41
  from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
42
 
43
- ## TODO: load from OpenSLR https://openslr.org/44/
44
- ##
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
  processor = Wav2Vec2Processor.from_pretrained("cahya/wav2vec2-large-xlsr-sundanese")
47
  model = Wav2Vec2ForCTC.from_pretrained("cahya/wav2vec2-large-xlsr-sundanese")
@@ -51,15 +80,15 @@ resampler = torchaudio.transforms.Resample(48_000, 16_000)
51
  # Preprocessing the datasets.
52
  # We need to read the aduio files as arrays
53
  def speech_file_to_array_fn(batch):
54
- \tspeech_array, sampling_rate = torchaudio.load(batch["path"])
55
- \tbatch["speech"] = resampler(speech_array).squeeze().numpy()
56
- \treturn batch
57
 
58
  test_dataset = test_dataset.map(speech_file_to_array_fn)
59
  inputs = processor(test_dataset["speech"][:2], sampling_rate=16_000, return_tensors="pt", padding=True)
60
 
61
  with torch.no_grad():
62
- \tlogits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
63
 
64
  predicted_ids = torch.argmax(logits, dim=-1)
65
 
@@ -86,31 +115,31 @@ processor = Wav2Vec2Processor.from_pretrained("cahya/wav2vec2-large-xlsr-sundane
86
  model = Wav2Vec2ForCTC.from_pretrained("cahya/wav2vec2-large-xlsr-sundanese")
87
  model.to("cuda")
88
 
89
- chars_to_ignore_regex = '[\\,\\?\\.\\!\\-\\;\\:\\"\\“\\%\\‘\\'\\”]'
90
 
91
  resampler = torchaudio.transforms.Resample(48_000, 16_000)
92
 
93
  # Preprocessing the datasets.
94
  # We need to read the aduio files as arrays
95
  def speech_file_to_array_fn(batch):
96
- \tbatch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower()
97
- \tspeech_array, sampling_rate = torchaudio.load(batch["path"])
98
- \tbatch["speech"] = resampler(speech_array).squeeze().numpy()
99
- \treturn batch
100
 
101
  test_dataset = test_dataset.map(speech_file_to_array_fn)
102
 
103
  # Preprocessing the datasets.
104
  # We need to read the aduio files as arrays
105
  def evaluate(batch):
106
- \tinputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
107
 
108
- \twith torch.no_grad():
109
- \t\tlogits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
110
 
111
  pred_ids = torch.argmax(logits, dim=-1)
112
- \tbatch["pred_strings"] = processor.batch_decode(pred_ids)
113
- \treturn batch
114
 
115
  result = test_dataset.map(evaluate, batched=True, batch_size=8)
116
 
 
40
  from datasets import load_dataset
41
  from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
42
 
43
+
44
+ def load_dataset_sundanese():
45
+ root_dir = Path("/dataset/ASR/sundanese")
46
+ url_sundanese_female = "https://www.openslr.org/resources/44/su_id_female.zip"
47
+ url_sundanese_male = "https://www.openslr.org/resources/44/su_id_male.zip"
48
+ data_dirs = [ root_dir/"su_id_female/wavs", root_dir/"su_id_male/wavs" ]
49
+ filenames = [ root_dir/"su_id_female/line_index.tsv", root_dir/"su_id_male/line_index.tsv" ]
50
+
51
+ if not (root_dir/"su_id_female").exists():
52
+ !wget -P {root_dir} {url_sundanese_female}
53
+ !unzip {root_dir}/su_id_female.zip -d {root_dir}
54
+ if not (root_dir/"su_id_male").exists():
55
+ !wget -P {root_dir} {url_sundanese_male}
56
+ !unzip {root_dir}/su_id_male.zip -d {root_dir}
57
+
58
+ dfs = []
59
+
60
+ dfs.append(pd.read_csv(filenames[0], sep='\t\t', names=["path", "sentence"]))
61
+ dfs.append(pd.read_csv(filenames[1], sep='\t\t', names=["path", "sentence"]))
62
+
63
+ for i, dir in enumerate(data_dirs):
64
+ dfs[i]["path"] = dfs[i].apply(lambda row: str(data_dirs[i]) + "/" + row + ".wav", axis=1)
65
+ df = pd.concat(dfs)
66
+ # df = df.sample(frac=1, random_state=1).reset_index(drop=True)
67
+ dataset = Dataset.from_pandas(df)
68
+ dataset = dataset.remove_columns('__index_level_0__')
69
+
70
+ return dataset.train_test_split(test_size=0.1, seed=1)
71
+
72
+ dataset = load_dataset_sundanese()
73
+ test_dataset = dataset['test']
74
 
75
  processor = Wav2Vec2Processor.from_pretrained("cahya/wav2vec2-large-xlsr-sundanese")
76
  model = Wav2Vec2ForCTC.from_pretrained("cahya/wav2vec2-large-xlsr-sundanese")
 
80
  # Preprocessing the datasets.
81
  # We need to read the aduio files as arrays
82
  def speech_file_to_array_fn(batch):
83
+ \\tspeech_array, sampling_rate = torchaudio.load(batch["path"])
84
+ \\tbatch["speech"] = resampler(speech_array).squeeze().numpy()
85
+ \\treturn batch
86
 
87
  test_dataset = test_dataset.map(speech_file_to_array_fn)
88
  inputs = processor(test_dataset["speech"][:2], sampling_rate=16_000, return_tensors="pt", padding=True)
89
 
90
  with torch.no_grad():
91
+ \\tlogits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
92
 
93
  predicted_ids = torch.argmax(logits, dim=-1)
94
 
 
115
  model = Wav2Vec2ForCTC.from_pretrained("cahya/wav2vec2-large-xlsr-sundanese")
116
  model.to("cuda")
117
 
118
+ chars_to_ignore_regex = '[\\\\,\\\\?\\\\.\\\\!\\\\-\\\\;\\\\:\\\\"\\\\“\\\\%\\\\‘\\\\'\\\\”]'
119
 
120
  resampler = torchaudio.transforms.Resample(48_000, 16_000)
121
 
122
  # Preprocessing the datasets.
123
  # We need to read the aduio files as arrays
124
  def speech_file_to_array_fn(batch):
125
+ \\tbatch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower()
126
+ \\tspeech_array, sampling_rate = torchaudio.load(batch["path"])
127
+ \\tbatch["speech"] = resampler(speech_array).squeeze().numpy()
128
+ \\treturn batch
129
 
130
  test_dataset = test_dataset.map(speech_file_to_array_fn)
131
 
132
  # Preprocessing the datasets.
133
  # We need to read the aduio files as arrays
134
  def evaluate(batch):
135
+ \\tinputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
136
 
137
+ \\twith torch.no_grad():
138
+ \\t\\tlogits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
139
 
140
  pred_ids = torch.argmax(logits, dim=-1)
141
+ \\tbatch["pred_strings"] = processor.batch_decode(pred_ids)
142
+ \\treturn batch
143
 
144
  result = test_dataset.map(evaluate, batched=True, batch_size=8)
145