gagan3012 commited on
Commit
8f7c665
1 Parent(s): cd11bf1

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +15 -14
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", "{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.
46
 
47
- 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`
48
- 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`
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 = '[\,\?\.\!\-\;\:\"\“]' # TODO: adapt this list to include all special characters you removed from the data
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
- batch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower()
103
- speech_array, sampling_rate = torchaudio.load(batch["path"])
104
- batch["speech"] = resampler(speech_array).squeeze().numpy()
105
- return batch
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
- inputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
113
 
114
- with torch.no_grad():
115
- logits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
116
 
117
- pred_ids = torch.argmax(logits, dim=-1)
118
- batch["pred_strings"] = processor.batch_decode(pred_ids)
119
- return batch
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