Views
No views yet
1from huggingsound import SpeechRecognitionModel
2
3model = SpeechRecognitionModel("jonatasgrosman/wav2vec2-large-fr-voxpopuli-french")
4audio_paths = ["/path/to/file.mp3", "/path/to/another_file.wav"]
5
6transcriptions = model.transcribe(audio_paths)1import torch
2import librosa
3from datasets import load_dataset
4from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
5
6LANG_ID = "fr"
7MODEL_ID = "jonatasgrosman/wav2vec2-large-fr-voxpopuli-french"
8SAMPLES = 10
9
10test_dataset = load_dataset("common_voice", LANG_ID, split=f"test[:{SAMPLES}]")
11
12processor = Wav2Vec2Processor.from_pretrained(MODEL_ID)
13model = Wav2Vec2ForCTC.from_pretrained(MODEL_ID)
14
15# Preprocessing the datasets.
16# We need to read the audio files as arrays
17def speech_file_to_array_fn(batch):
18 speech_array, sampling_rate = librosa.load(batch["path"], sr=16_000)
19 batch["speech"] = speech_array
20 batch["sentence"] = batch["sentence"].upper()
21 return batch
22
23test_dataset = test_dataset.map(speech_file_to_array_fn)
24inputs = processor(test_dataset["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
25
26with torch.no_grad():
27 logits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
28
29predicted_ids = torch.argmax(logits, dim=-1)
30predicted_sentences = processor.batch_decode(predicted_ids)
31
32for i, predicted_sentence in enumerate(predicted_sentences):
33 print("-" * 100)
34 print("Reference:", test_dataset[i]["sentence"])
35 print("Prediction:", predicted_sentence)| Reference | Prediction |
|---|---|
| "CE DERNIER A ÉVOLUÉ TOUT AU LONG DE L'HISTOIRE ROMAINE." | CE DERNIER A ÉVOLÉ TOUT AU LONG DE L'HISTOIRE ROMAINE |
| CE SITE CONTIENT QUATRE TOMBEAUX DE LA DYNASTIE ACHÉMÉNIDE ET SEPT DES SASSANIDES. | CE SITE CONTIENT QUATRE TOMBEAUX DE LA DYNESTIE ACHÉMÉNIDE ET SEPT DES SACENNIDES |
| "J'AI DIT QUE LES ACTEURS DE BOIS AVAIENT, SELON MOI, BEAUCOUP D'AVANTAGES SUR LES AUTRES." | JAI DIT QUE LES ACTEURS DE BOIS AVAIENT SELON MOI BEAUCOUP DAVANTAGE SUR LES AUTRES |
| LES PAYS-BAS ONT REMPORTÉ TOUTES LES ÉDITIONS. | LE PAYS-BAS ON REMPORTÉ TOUTES LES ÉDITIONS |
| IL Y A MAINTENANT UNE GARE ROUTIÈRE. | IL A MAINTENANT GULA E RETIREN |
| HUIT | HUIT |
| DANS L’ATTENTE DU LENDEMAIN, ILS NE POUVAIENT SE DÉFENDRE D’UNE VIVE ÉMOTION | DANS LATTENTE DU LENDEMAIN IL NE POUVAIT SE DÉFENDRE DUNE VIVE ÉMOTION |
| LA PREMIÈRE SAISON EST COMPOSÉE DE DOUZE ÉPISODES. | LA PREMIÈRE SAISON EST COMPOSÉE DE DOUZ ÉPISODES |
| ELLE SE TROUVE ÉGALEMENT DANS LES ÎLES BRITANNIQUES. | ELLE SE TROUVE ÉGALEMENT DANS LES ÎLES BRITANNIQUES |
| ZÉRO | ZÉRO |
1import torch
2import re
3import librosa
4from datasets import load_dataset, load_metric
5from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
6
7LANG_ID = "fr"
8MODEL_ID = "jonatasgrosman/wav2vec2-large-fr-voxpopuli-french"
9DEVICE = "cuda"
10
11CHARS_TO_IGNORE = [",", "?", "¿", ".", "!", "¡", ";", ";", ":", '""', "%", '"', "�", "ʿ", "·", "჻", "~", "՞",
12 "؟", "،", "।", "॥", "«", "»", "„", "“", "”", "「", "」", "‘", "’", "《", "》", "(", ")", "[", "]",
13 "{", "}", "=", "`", "_", "+", "<", ">", "…", "–", "°", "´", "ʾ", "‹", "›", "©", "®", "—", "→", "。",
14 "、", "﹂", "﹁", "‧", "~", "﹏", ",", "{", "}", "(", ")", "[", "]", "【", "】", "‥", "〽",
15 "『", "』", "〝", "〟", "⟨", "⟩", "〜", ":", "!", "?", "♪", "؛", "/", "\\", "º", "−", "^", "ʻ", "ˆ"]
16
17test_dataset = load_dataset("common_voice", LANG_ID, split="test")
18
19wer = load_metric("wer.py") # https://github.com/jonatasgrosman/wav2vec2-sprint/blob/main/wer.py
20cer = load_metric("cer.py") # https://github.com/jonatasgrosman/wav2vec2-sprint/blob/main/cer.py
21
22chars_to_ignore_regex = f"[{re.escape(''.join(CHARS_TO_IGNORE))}]"
23
24processor = Wav2Vec2Processor.from_pretrained(MODEL_ID)
25model = Wav2Vec2ForCTC.from_pretrained(MODEL_ID)
26model.to(DEVICE)
27
28# Preprocessing the datasets.
29# We need to read the audio files as arrays
30def speech_file_to_array_fn(batch):
31 with warnings.catch_warnings():
32 warnings.simplefilter("ignore")
33 speech_array, sampling_rate = librosa.load(batch["path"], sr=16_000)
34 batch["speech"] = speech_array
35 batch["sentence"] = re.sub(chars_to_ignore_regex, "", batch["sentence"]).upper()
36 return batch
37
38test_dataset = test_dataset.map(speech_file_to_array_fn)
39
40# Preprocessing the datasets.
41# We need to read the audio files as arrays
42def evaluate(batch):
43 inputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
44
45 with torch.no_grad():
46 logits = model(inputs.input_values.to(DEVICE), attention_mask=inputs.attention_mask.to(DEVICE)).logits
47
48 pred_ids = torch.argmax(logits, dim=-1)
49 batch["pred_strings"] = processor.batch_decode(pred_ids)
50 return batch
51
52result = test_dataset.map(evaluate, batched=True, batch_size=8)
53
54predictions = [x.upper() for x in result["pred_strings"]]
55references = [x.upper() for x in result["sentence"]]
56
57print(f"WER: {wer.compute(predictions=predictions, references=references, chunk_size=1000) * 100}")
58print(f"CER: {cer.compute(predictions=predictions, references=references, chunk_size=1000) * 100}")| Model | WER | CER |
|---|---|---|
| jonatasgrosman/wav2vec2-large-xlsr-53-french | 15.90% | 5.29% |
| jonatasgrosman/wav2vec2-large-fr-voxpopuli-french | 17.62% | 6.04% |
| Ilyes/wav2vec2-large-xlsr-53-french | 19.67% | 6.70% |
| Nhut/wav2vec2-large-xlsr-french | 24.09% | 8.42% |
| facebook/wav2vec2-large-xlsr-53-french | 25.45% | 10.35% |
| MehdiHosseiniMoghadam/wav2vec2-large-xlsr-53-French | 28.22% | 9.70% |
| Ilyes/wav2vec2-large-xlsr-53-french_punctuation | 29.80% | 11.79% |
| facebook/wav2vec2-base-10k-voxpopuli-ft-fr | 61.06% | 33.31% |
1@misc{grosman2021voxpopuli-fr-wav2vec2-large-french,
2 title={Fine-tuned {F}rench {V}oxpopuli wav2vec2 large model for speech recognition in {F}rench},
3 author={Grosman, Jonatas},
4 howpublished={\url{https://huggingface.co/jonatasgrosman/wav2vec2-large-fr-voxpopuli-french}},
5 year={2021}
6}