theainerd commited on
Commit
00638e2
1 Parent(s): 76a3591

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +17 -20
README.md CHANGED
@@ -41,25 +41,23 @@ import torchaudio
41
  from datasets import load_dataset
42
  from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
43
 
44
- test_dataset = load_dataset("common_voice", "{lang_id}", split="test[:2%]") #TODO: replace {lang_id} in your language code here. Make sure the code is one of the *ISO codes* of [this](https://huggingface.co/languages) site.
45
-
46
- processor = Wav2Vec2Processor.from_pretrained("{model_id}") #TODO: replace {model_id} with your model id. The model id consists of {your_username}/{your_modelname}, *e.g.* `elgeish/wav2vec2-large-xlsr-53-arabic`
47
- model = Wav2Vec2ForCTC.from_pretrained("{model_id}") #TODO: replace {model_id} with your model id. The model id consists of {your_username}/{your_modelname}, *e.g.* `elgeish/wav2vec2-large-xlsr-53-arabic`
48
-
49
  resampler = torchaudio.transforms.Resample(48_000, 16_000)
50
 
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
 
@@ -87,30 +85,29 @@ processor = Wav2Vec2Processor.from_pretrained("theainerd/Wav2Vec2-large-xlsr-hin
87
  model = Wav2Vec2ForCTC.from_pretrained("theainerd/Wav2Vec2-large-xlsr-hindi")
88
  model.to("cuda")
89
 
90
- chars_to_ignore_regex = '[\\\\\\\\,\\\\\\\\?\\\\\\\\.\\\\\\\\!\\\\\\\\-\\\\\\\\;\\\\\\\\:\\\\\\\\"\\\\\\\\“]' # TODO: adapt this list to include all special characters you removed from the data
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
- \\\\tpred_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
 
 
41
  from datasets import load_dataset
42
  from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
43
 
44
+ test_dataset = load_dataset("common_voice", "hi", split="test[:2%]")
45
+ processor = Wav2Vec2Processor.from_pretrained("theainerd/Wav2Vec2-large-xlsr-hindi")
46
+ model = Wav2Vec2ForCTC.from_pretrained("theainerd/Wav2Vec2-large-xlsr-hindi")
 
 
47
  resampler = torchaudio.transforms.Resample(48_000, 16_000)
48
 
49
  # Preprocessing the datasets.
50
  # We need to read the aduio files as arrays
51
  def speech_file_to_array_fn(batch):
52
+ speech_array, sampling_rate = torchaudio.load(batch["path"])
53
+ batch["speech"] = resampler(speech_array).squeeze().numpy()
54
+ return batch
55
 
56
  test_dataset = test_dataset.map(speech_file_to_array_fn)
57
  inputs = processor(test_dataset["speech"][:2], sampling_rate=16_000, return_tensors="pt", padding=True)
58
 
59
  with torch.no_grad():
60
+ logits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
61
 
62
  predicted_ids = torch.argmax(logits, dim=-1)
63
 
 
85
  model = Wav2Vec2ForCTC.from_pretrained("theainerd/Wav2Vec2-large-xlsr-hindi")
86
  model.to("cuda")
87
 
 
88
  resampler = torchaudio.transforms.Resample(48_000, 16_000)
89
 
90
  # Preprocessing the datasets.
91
  # We need to read the aduio files as arrays
92
  def speech_file_to_array_fn(batch):
93
+ batch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower()
94
+ speech_array, sampling_rate = torchaudio.load(batch["path"])
95
+ batch["speech"] = resampler(speech_array).squeeze().numpy()
96
+ return batch
97
 
98
  test_dataset = test_dataset.map(speech_file_to_array_fn)
99
 
100
  # Preprocessing the datasets.
101
  # We need to read the aduio files as arrays
102
  def evaluate(batch):
103
+ inputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
104
 
105
+ with torch.no_grad():
106
+ logits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
107
 
108
+ pred_ids = torch.argmax(logits, dim=-1)
109
+ batch["pred_strings"] = processor.batch_decode(pred_ids)
110
+ return batch
111
 
112
  result = test_dataset.map(evaluate, batched=True, batch_size=8)
113