Views
No views yet
1import torch
2import torchaudio
3from datasets import load_dataset
4from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
5import pandas as pd
6# Evaluation notebook contains the procedure to download the data
7df = pd.read_csv("/content/te/test.tsv", sep="\t")
8df["path"] = "/content/te/clips/" + df["path"]
9test_dataset = Dataset.from_pandas(df)
10processor = Wav2Vec2Processor.from_pretrained("anuragshas/wav2vec2-large-xlsr-53-telugu")
11model = Wav2Vec2ForCTC.from_pretrained("anuragshas/wav2vec2-large-xlsr-53-telugu")
12resampler = torchaudio.transforms.Resample(48_000, 16_000)
13# Preprocessing the datasets.
14# We need to read the aduio files as arrays
15def speech_file_to_array_fn(batch):
16 speech_array, sampling_rate = torchaudio.load(batch["path"])
17 batch["speech"] = resampler(speech_array).squeeze().numpy()
18 return batch
19test_dataset = test_dataset.map(speech_file_to_array_fn)
20inputs = processor(test_dataset["speech"][:2], sampling_rate=16_000, return_tensors="pt", padding=True)
21with torch.no_grad():
22 logits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
23predicted_ids = torch.argmax(logits, dim=-1)
24print("Prediction:", processor.batch_decode(predicted_ids))
25print("Reference:", test_dataset["sentence"][:2])1import torch
2import torchaudio
3from datasets import Dataset, load_metric
4from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
5import re
6from sklearn.model_selection import train_test_split
7import pandas as pd
8# Evaluation notebook contains the procedure to download the data
9df = pd.read_csv("/content/te/test.tsv", sep="\t")
10df["path"] = "/content/te/clips/" + df["path"]
11test_dataset = Dataset.from_pandas(df)
12wer = load_metric("wer")
13processor = Wav2Vec2Processor.from_pretrained("anuragshas/wav2vec2-large-xlsr-53-telugu")
14model = Wav2Vec2ForCTC.from_pretrained("anuragshas/wav2vec2-large-xlsr-53-telugu")
15model.to("cuda")
16chars_to_ignore_regex = '[\,\?\.\!\-\_\;\:\"\“\%\‘\”\।\’\'\&]'
17resampler = torchaudio.transforms.Resample(48_000, 16_000)
18def normalizer(text):
19 # Use your custom normalizer
20 text = text.replace("\\n","\n")
21 text = ' '.join(text.split())
22 text = re.sub(r'''([a-z]+)''','',text,flags=re.IGNORECASE)
23 text = re.sub(r'''%'''," శాతం ", text)
24 text = re.sub(r'''(/|-|_)'''," ", text)
25 text = re.sub("ై","ై", text)
26 text = text.strip()
27 return text
28def speech_file_to_array_fn(batch):
29 batch["sentence"] = normalizer(batch["sentence"])
30 batch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower()+ " "
31 speech_array, sampling_rate = torchaudio.load(batch["path"])
32 batch["speech"] = resampler(speech_array).squeeze().numpy()
33 return batch
34test_dataset = test_dataset.map(speech_file_to_array_fn)
35# Preprocessing the datasets.
36# We need to read the aduio files as arrays
37def evaluate(batch):
38 inputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
39 with torch.no_grad():
40 logits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
41 pred_ids = torch.argmax(logits, dim=-1)
42 batch["pred_strings"] = processor.batch_decode(pred_ids)
43 return batch
44result = test_dataset.map(evaluate, batched=True, batch_size=8)
45print("WER: {:2f}".format(100 * wer.compute(predictions=result["pred_strings"], references=result["sentence"])))