Views
No views yet
1!pip install datasets
2!pip install transformers
3
4from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
5
6import torch
7import librosa
8from datasets import load_dataset
9
10test_dataset = load_dataset("common_voice", "ta", split="test[:2%]").
11
12processor = Wav2Vec2Processor.from_pretrained("Gobee/Wav2vec2-Large-XLSR-Tamil")
13model = Wav2Vec2ForCTC.from_pretrained("Gobee/Wav2vec2-Large-XLSR-Tamil")
14
15resampler = torchaudio.transforms.Resample(48_000, 16_000)
16
17# Preprocessing the datasets.
18# We need to read the audio files as arrays
19def speech_file_to_array_fn(batch):
20 speech_array, sampling_rate = librosa.load(batch["path"], sr=16_000)
21 batch["speech"] = speech_array
22 batch["sentence"] = batch["sentence"].upper()
23 return batch
24
25test_dataset = test_dataset.map(speech_file_to_array_fn)
26inputs = processor(test_dataset["speech"][:2], sampling_rate=16_000, return_tensors="pt", padding=True)
27
28with torch.no_grad():
29 logits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
30
31predicted_ids = torch.argmax(logits, dim=-1)
32
33print("Prediction:", processor.batch_decode(predicted_ids))
34print("Reference:", test_dataset["sentence"][:2])1!pip install datasets
2!pip install transformers
3!pip install jiwer
4
5from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
6
7import torch
8import librosa
9from datasets import load_dataset, load_metric
10import re
11
12test_dataset = load_dataset("common_voice", "ta", split="test")
13wer = load_metric("wer")
14
15processor = Wav2Vec2Processor.from_pretrained("Gobee/Wav2vec2-Large-XLSR-Tamil")
16model = Wav2Vec2ForCTC.from_pretrained("Gobee/Wav2vec2-Large-XLSR-Tamil")
17model.to("cuda")
18
19chars_to_ignore_regex = '[\,\?\.\!\-\;\:\"\“\%\‘\”\ \’\–\(\)]'
20
21# Preprocessing the datasets.
22# We need to read the aduio files as arrays
23def speech_file_to_array_fn(batch):
24 batch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower()
25 speech_array, sampling_rate = librosa.load(batch["path"], sr=16_000)
26 batch["speech"] = speech_array
27 return batch
28
29test_dataset = test_dataset.map(speech_file_to_array_fn)
30
31# Preprocessing the datasets.
32# We need to read the aduio files as arrays
33def evaluate(batch):
34 inputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
35
36 with torch.no_grad():
37 logits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
38
39 pred_ids = torch.argmax(logits, dim=-1)
40 batch["pred_strings"] = processor.batch_decode(pred_ids)
41 return batch
42
43result = test_dataset.map(evaluate, batched=True, batch_size=8)
44
45print("WER: {:2f}".format(100 * wer.compute(predictions=result["pred_strings"], references=result["sentence"])))train, validation datasets were used for training.