voidful commited on
Commit
ecf47c2
1 Parent(s): 3f42bd2

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +49 -0
README.md CHANGED
@@ -9,6 +9,55 @@ tags:
9
  license: apache-2.0
10
  ---
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  ## Evaluation on Common Voice HK Test
13
  ```python
14
  import torchaudio
 
9
  license: apache-2.0
10
  ---
11
 
12
+ ## Colab trial with recording or voice file
13
+ [Colab trial](https://colab.research.google.com/drive/1e_z5jQHYbO2YKEaUgzb1ww1WwiAyydAj?usp=sharing)
14
+
15
+ ```
16
+ import torchaudio
17
+ from datasets import load_dataset, load_metric
18
+ from transformers import (
19
+ Wav2Vec2ForCTC,
20
+ Wav2Vec2Processor,
21
+ )
22
+ import torch
23
+ import re
24
+ import sys
25
+
26
+ model_name = "voidful/wav2vec2-large-xlsr-53-hk"
27
+ device = "cuda"
28
+ processor_name = "voidful/wav2vec2-large-xlsr-53-hk"
29
+
30
+ chars_to_ignore_regex = r"[¥•"#$%&'()*+,-/:;<=>@[\]^_`{|}~⦅⦆「」、 、〃〈〉《》「」『』【】〔〕〖〗〘〙〚〛〜〝〞〟〰〾〿–—‘’‛“”„‟…‧﹏﹑﹔·'℃°•·.﹑︰〈〉─《﹖﹣﹂﹁﹔!?。。"#$%&'()*+,﹐-/:;<=>@[\]^_`{|}~⦅⦆「」、、〃》「」『』【】〔〕〖〗〘〙〚〛〜〝〞〟〰〾〿–—‘’‛“”„‟…‧﹏..!\"#$%&()*+,\-.\:;<=>?@\[\]\\\/^_`{|}~]"
31
+
32
+ model = Wav2Vec2ForCTC.from_pretrained(model_name).to(device)
33
+ processor = Wav2Vec2Processor.from_pretrained(processor_name)
34
+
35
+ resampler = torchaudio.transforms.Resample(orig_freq=48_000, new_freq=16_000)
36
+
37
+ def load_file_to_data(file):
38
+ batch = {}
39
+ speech, _ = torchaudio.load(file)
40
+ batch["speech"] = resampler.forward(speech.squeeze(0)).numpy()
41
+ batch["sampling_rate"] = resampler.new_freq
42
+ return batch
43
+
44
+
45
+ def predict(data):
46
+ features = processor(data["speech"], sampling_rate=data["sampling_rate"], padding=True, return_tensors="pt")
47
+ input_values = features.input_values.to(device)
48
+ attention_mask = features.attention_mask.to(device)
49
+ with torch.no_grad():
50
+ logits = model(input_values, attention_mask=attention_mask).logits
51
+ pred_ids = torch.argmax(logits, dim=-1)
52
+ return processor.batch_decode(pred_ids)
53
+
54
+ ```
55
+
56
+ Predict
57
+ ```python
58
+ predict(load_file_to_data('voice file path'))
59
+ ```
60
+
61
  ## Evaluation on Common Voice HK Test
62
  ```python
63
  import torchaudio