simonsr commited on
Commit
9c650d4
β€’
1 Parent(s): e698b23

Update README.md

Browse files

Fixed incorrect tabbign in code

Files changed (1) hide show
  1. README.md +17 -16
README.md CHANGED
@@ -52,15 +52,15 @@ resampler = torchaudio.transforms.Resample(48_000, 16_000)
52
  # Preprocessing the datasets.
53
  # We need to read the audio files as arrays
54
  def speech_file_to_array_fn(batch):
55
- \\tspeech_array, sampling_rate = torchaudio.load(batch["path"])
56
- \\tbatch["speech"] = resampler(speech_array).squeeze().numpy()
57
- \\treturn batch
58
 
59
  test_dataset = test_dataset.map(speech_file_to_array_fn)
60
  inputs = processor(test_dataset["speech"][:2], sampling_rate=16_000, return_tensors="pt", padding=True)
61
 
62
  with torch.no_grad():
63
- \\tlogits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
64
 
65
  predicted_ids = torch.argmax(logits, dim=-1)
66
 
@@ -88,31 +88,32 @@ processor = Wav2Vec2Processor.from_pretrained("{model_id}") #TODO: replace {mode
88
  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`
89
  model.to("cuda")
90
 
91
- chars_to_ignore_regex = '[\\\\,\\\\?\\\\.\\\\!\\\\-\\\\;\\\\:\\\\"\\\\β€œ\\\\%\\\\β€˜\\\\”\\\\οΏ½\\\\(\\\\)\\\\=\\\\Β΄\\\\–\\\\&\\\\…\\\\β€”\\\\’]'
92
  resampler = torchaudio.transforms.Resample(48_000, 16_000)
93
 
94
  # Preprocessing the datasets.
95
  # We need to read the aduio files as arrays
96
  def speech_file_to_array_fn(batch):
97
- batch["sentence"] = unidecode.unidecode(batch["sentence"])
98
- \\tbatch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower()
99
- \\tspeech_array, sampling_rate = torchaudio.load(batch["path"])
100
- \\tbatch["speech"] = resampler(speech_array).squeeze().numpy()
101
- \\treturn batch
102
 
103
  test_dataset = test_dataset.map(speech_file_to_array_fn)
104
 
105
  # Preprocessing the datasets.
106
  # We need to read the aduio files as arrays
107
  def evaluate(batch):
108
- \\tinputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
109
 
110
- \\twith torch.no_grad():
111
- \\t\\tlogits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
112
 
113
- \\tpred_ids = torch.argmax(logits, dim=-1)
114
- \\tbatch["pred_strings"] = processor.batch_decode(pred_ids)
115
- \\treturn batch
 
116
 
117
  result = test_dataset.map(evaluate, batched=True, batch_size=8)
118
 
52
  # Preprocessing the datasets.
53
  # We need to read the audio files as arrays
54
  def speech_file_to_array_fn(batch):
55
+ speech_array, sampling_rate = torchaudio.load(batch["path"])
56
+ batch["speech"] = resampler(speech_array).squeeze().numpy()
57
+ return batch
58
 
59
  test_dataset = test_dataset.map(speech_file_to_array_fn)
60
  inputs = processor(test_dataset["speech"][:2], sampling_rate=16_000, return_tensors="pt", padding=True)
61
 
62
  with torch.no_grad():
63
+ logits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
64
 
65
  predicted_ids = torch.argmax(logits, dim=-1)
66
 
88
  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`
89
  model.to("cuda")
90
 
91
+ chars_to_ignore_regex = '[\,\?\.\!\-\;\:\"\β€œ\%\β€˜\”\οΏ½\(\)\=\Β΄\–\&\…\β€”\’]'
92
  resampler = torchaudio.transforms.Resample(48_000, 16_000)
93
 
94
  # Preprocessing the datasets.
95
  # We need to read the aduio files as arrays
96
  def speech_file_to_array_fn(batch):
97
+ batch["sentence"] = unidecode.unidecode(batch["sentence"])
98
+ batch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower()
99
+ speech_array, sampling_rate = torchaudio.load(batch["path"])
100
+ batch["speech"] = resampler(speech_array).squeeze().numpy()
101
+ return batch
102
 
103
  test_dataset = test_dataset.map(speech_file_to_array_fn)
104
 
105
  # Preprocessing the datasets.
106
  # We need to read the aduio files as arrays
107
  def evaluate(batch):
108
+ inputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
109
 
110
+ with torch.no_grad():
111
+ logits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
112
 
113
+ pred_ids = torch.argmax(logits, dim=-1)
114
+
115
+ batch["pred_strings"] = processor.batch_decode(pred_ids)
116
+ return batch
117
 
118
  result = test_dataset.map(evaluate, batched=True, batch_size=8)
119