Update README.md
Browse files
README.md
CHANGED
@@ -42,10 +42,10 @@ import torchaudio
|
|
42 |
from datasets import load_dataset
|
43 |
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
44 |
|
45 |
-
test_dataset = load_dataset("common_voice", "
|
46 |
|
47 |
-
processor = Wav2Vec2Processor.from_pretrained("
|
48 |
-
model = Wav2Vec2ForCTC.from_pretrained("
|
49 |
|
50 |
resampler = torchaudio.transforms.Resample(48_000, 16_000)
|
51 |
|
@@ -66,6 +66,7 @@ predicted_ids = torch.argmax(logits, dim=-1)
|
|
66 |
|
67 |
print("Prediction:", processor.batch_decode(predicted_ids))
|
68 |
print("Reference:", test_dataset["sentence"][:2])
|
|
|
69 |
```
|
70 |
|
71 |
#### Results:
|
@@ -93,30 +94,30 @@ processor = Wav2Vec2Processor.from_pretrained("gagan3012/wav2vec2-xlsr-punjabi")
|
|
93 |
model = Wav2Vec2ForCTC.from_pretrained("gagan3012/wav2vec2-xlsr-punjabi")
|
94 |
model.to("cuda")
|
95 |
|
96 |
-
chars_to_ignore_regex = '[
|
97 |
resampler = torchaudio.transforms.Resample(48_000, 16_000)
|
98 |
|
99 |
# Preprocessing the datasets.
|
100 |
# We need to read the aduio files as arrays
|
101 |
def speech_file_to_array_fn(batch):
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
|
107 |
test_dataset = test_dataset.map(speech_file_to_array_fn)
|
108 |
|
109 |
# Preprocessing the datasets.
|
110 |
# We need to read the aduio files as arrays
|
111 |
def evaluate(batch):
|
112 |
-
|
113 |
|
114 |
-
|
115 |
-
|
116 |
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
|
121 |
result = test_dataset.map(evaluate, batched=True, batch_size=8)
|
122 |
|
|
|
42 |
from datasets import load_dataset
|
43 |
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
44 |
|
45 |
+
test_dataset = load_dataset("common_voice", "pa-IN", split="test")
|
46 |
|
47 |
+
processor = Wav2Vec2Processor.from_pretrained("gagan3012/wav2vec2-xlsr-punjabi")
|
48 |
+
model = Wav2Vec2ForCTC.from_pretrained("gagan3012/wav2vec2-xlsr-punjabi")
|
49 |
|
50 |
resampler = torchaudio.transforms.Resample(48_000, 16_000)
|
51 |
|
|
|
66 |
|
67 |
print("Prediction:", processor.batch_decode(predicted_ids))
|
68 |
print("Reference:", test_dataset["sentence"][:2])
|
69 |
+
|
70 |
```
|
71 |
|
72 |
#### Results:
|
|
|
94 |
model = Wav2Vec2ForCTC.from_pretrained("gagan3012/wav2vec2-xlsr-punjabi")
|
95 |
model.to("cuda")
|
96 |
|
97 |
+
chars_to_ignore_regex = '[\\,\\?\\.\\!\\-\\;\\:\\"\\“]' # TODO: adapt this list to include all special characters you removed from the data
|
98 |
resampler = torchaudio.transforms.Resample(48_000, 16_000)
|
99 |
|
100 |
# Preprocessing the datasets.
|
101 |
# We need to read the aduio files as arrays
|
102 |
def speech_file_to_array_fn(batch):
|
103 |
+
\tbatch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower()
|
104 |
+
\tspeech_array, sampling_rate = torchaudio.load(batch["path"])
|
105 |
+
\tbatch["speech"] = resampler(speech_array).squeeze().numpy()
|
106 |
+
\treturn batch
|
107 |
|
108 |
test_dataset = test_dataset.map(speech_file_to_array_fn)
|
109 |
|
110 |
# Preprocessing the datasets.
|
111 |
# We need to read the aduio files as arrays
|
112 |
def evaluate(batch):
|
113 |
+
\tinputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
|
114 |
|
115 |
+
\twith torch.no_grad():
|
116 |
+
\t\tlogits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
|
117 |
|
118 |
+
\tpred_ids = torch.argmax(logits, dim=-1)
|
119 |
+
\tbatch["pred_strings"] = processor.batch_decode(pred_ids)
|
120 |
+
\treturn batch
|
121 |
|
122 |
result = test_dataset.map(evaluate, batched=True, batch_size=8)
|
123 |
|