Update README.md
Browse files
README.md
CHANGED
@@ -44,7 +44,19 @@ import torchaudio
|
|
44 |
from datasets import load_dataset
|
45 |
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
46 |
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
processor = Wav2Vec2Processor.from_pretrained("gagan3012/wav2vec2-xlsr-nepali")
|
50 |
model = Wav2Vec2ForCTC.from_pretrained("gagan3012/wav2vec2-xlsr-nepali")
|
@@ -54,15 +66,15 @@ resampler = torchaudio.transforms.Resample(48_000, 16_000)
|
|
54 |
# Preprocessing the datasets.
|
55 |
# We need to read the aduio files as arrays
|
56 |
def speech_file_to_array_fn(batch):
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
|
61 |
test_dataset = test_dataset.map(speech_file_to_array_fn)
|
62 |
inputs = processor(test_dataset["speech"][:2], sampling_rate=16_000, return_tensors="pt", padding=True)
|
63 |
|
64 |
with torch.no_grad():
|
65 |
-
|
66 |
|
67 |
predicted_ids = torch.argmax(logits, dim=-1)
|
68 |
|
@@ -88,37 +100,49 @@ from datasets import load_dataset, load_metric
|
|
88 |
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
89 |
import re
|
90 |
|
91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
wer = load_metric("wer")
|
93 |
|
94 |
processor = Wav2Vec2Processor.from_pretrained("gagan3012/wav2vec2-xlsr-nepali")
|
95 |
model = Wav2Vec2ForCTC.from_pretrained("gagan3012/wav2vec2-xlsr-nepali")
|
96 |
model.to("cuda")
|
97 |
|
98 |
-
chars_to_ignore_regex = '[
|
99 |
resampler = torchaudio.transforms.Resample(48_000, 16_000)
|
100 |
|
101 |
# Preprocessing the datasets.
|
102 |
# We need to read the aduio files as arrays
|
103 |
def speech_file_to_array_fn(batch):
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
|
109 |
test_dataset = test_dataset.map(speech_file_to_array_fn)
|
110 |
|
111 |
# Preprocessing the datasets.
|
112 |
# We need to read the aduio files as arrays
|
113 |
def evaluate(batch):
|
114 |
-
|
115 |
|
116 |
-
|
117 |
-
|
118 |
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
|
123 |
result = test_dataset.map(evaluate, batched=True, batch_size=8)
|
124 |
|
|
|
44 |
from datasets import load_dataset
|
45 |
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
46 |
|
47 |
+
!wget https://www.openslr.org/resources/43/ne_np_female.zip
|
48 |
+
!unzip ne_np_female.zip
|
49 |
+
!ls ne_np_female
|
50 |
+
|
51 |
+
colnames=['path','sentence']
|
52 |
+
df = pd.read_csv('/content/ne_np_female/line_index.tsv',sep='\t',header=None,names = colnames)
|
53 |
+
df['path'] = '/content/ne_np_female/wavs/'+df['path'] +'.wav'
|
54 |
+
|
55 |
+
train, test = train_test_split(df, test_size=0.1)
|
56 |
+
|
57 |
+
test.to_csv('/content/ne_np_female/line_index_test.csv')
|
58 |
+
|
59 |
+
test_dataset = load_dataset('csv', data_files='/content/ne_np_female/line_index_test.csv',split = 'train')
|
60 |
|
61 |
processor = Wav2Vec2Processor.from_pretrained("gagan3012/wav2vec2-xlsr-nepali")
|
62 |
model = Wav2Vec2ForCTC.from_pretrained("gagan3012/wav2vec2-xlsr-nepali")
|
|
|
66 |
# Preprocessing the datasets.
|
67 |
# We need to read the aduio files as arrays
|
68 |
def speech_file_to_array_fn(batch):
|
69 |
+
speech_array, sampling_rate = torchaudio.load(batch["path"])
|
70 |
+
batch["speech"] = resampler(speech_array).squeeze().numpy()
|
71 |
+
return batch
|
72 |
|
73 |
test_dataset = test_dataset.map(speech_file_to_array_fn)
|
74 |
inputs = processor(test_dataset["speech"][:2], sampling_rate=16_000, return_tensors="pt", padding=True)
|
75 |
|
76 |
with torch.no_grad():
|
77 |
+
logits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
|
78 |
|
79 |
predicted_ids = torch.argmax(logits, dim=-1)
|
80 |
|
|
|
100 |
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
101 |
import re
|
102 |
|
103 |
+
!wget https://www.openslr.org/resources/43/ne_np_female.zip
|
104 |
+
!unzip ne_np_female.zip
|
105 |
+
!ls ne_np_female
|
106 |
+
|
107 |
+
colnames=['path','sentence']
|
108 |
+
df = pd.read_csv('/content/ne_np_female/line_index.tsv',sep='\t',header=None,names = colnames)
|
109 |
+
df['path'] = '/content/ne_np_female/wavs/'+df['path'] +'.wav'
|
110 |
+
|
111 |
+
train, test = train_test_split(df, test_size=0.1)
|
112 |
+
|
113 |
+
test.to_csv('/content/ne_np_female/line_index_test.csv')
|
114 |
+
|
115 |
+
test_dataset = load_dataset('csv', data_files='/content/ne_np_female/line_index_test.csv',split = 'train')
|
116 |
wer = load_metric("wer")
|
117 |
|
118 |
processor = Wav2Vec2Processor.from_pretrained("gagan3012/wav2vec2-xlsr-nepali")
|
119 |
model = Wav2Vec2ForCTC.from_pretrained("gagan3012/wav2vec2-xlsr-nepali")
|
120 |
model.to("cuda")
|
121 |
|
122 |
+
chars_to_ignore_regex = '[\,\?\.\!\-\;\:\"\“]'
|
123 |
resampler = torchaudio.transforms.Resample(48_000, 16_000)
|
124 |
|
125 |
# Preprocessing the datasets.
|
126 |
# We need to read the aduio files as arrays
|
127 |
def speech_file_to_array_fn(batch):
|
128 |
+
batch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower()
|
129 |
+
speech_array, sampling_rate = torchaudio.load(batch["path"])
|
130 |
+
batch["speech"] = resampler(speech_array).squeeze().numpy()
|
131 |
+
return batch
|
132 |
|
133 |
test_dataset = test_dataset.map(speech_file_to_array_fn)
|
134 |
|
135 |
# Preprocessing the datasets.
|
136 |
# We need to read the aduio files as arrays
|
137 |
def evaluate(batch):
|
138 |
+
inputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
|
139 |
|
140 |
+
with torch.no_grad():
|
141 |
+
logits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
|
142 |
|
143 |
+
pred_ids = torch.argmax(logits, dim=-1)
|
144 |
+
batch["pred_strings"] = processor.batch_decode(pred_ids)
|
145 |
+
return batch
|
146 |
|
147 |
result = test_dataset.map(evaluate, batched=True, batch_size=8)
|
148 |
|