kingabzpro commited on
Commit
d7ce62b
1 Parent(s): 8f446a3
alphabet.json ADDED
@@ -0,0 +1 @@
 
1
+ {"labels": [" ", "\u0621", "\u0622", "\u0624", "\u0626", "\u0627", "\u0628", "\u062a", "\u062b", "\u062c", "\u062d", "\u062e", "\u062f", "\u0630", "\u0631", "\u0632", "\u0633", "\u0634", "\u0635", "\u0636", "\u0637", "\u0638", "\u0639", "\u063a", "\u0641", "\u0642", "\u0644", "\u0645", "\u0646", "\u0648", "\u0649", "\u064a", "\u064b", "\u064e", "\u064f", "\u0650", "\u0651", "\u0654", "\u0670", "\u0679", "\u067e", "\u0686", "\u0688", "\u0691", "\u0698", "\u06a9", "\u06af", "\u06ba", "\u06be", "\u06c1", "\u06c2", "\u06cc", "\u06d2", "\u2047", "", "<s>", "</s>"], "is_bpe": false}
eval.py ADDED
@@ -0,0 +1,153 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ import argparse
3
+ import re
4
+ from typing import Dict
5
+
6
+ import torch
7
+ from datasets import Audio, Dataset, load_dataset, load_metric
8
+
9
+ from transformers import AutoFeatureExtractor, pipeline
10
+
11
+
12
+ def log_results(result: Dataset, args: Dict[str, str]):
13
+ """DO NOT CHANGE. This function computes and logs the result metrics."""
14
+
15
+ log_outputs = args.log_outputs
16
+ dataset_id = "_".join(args.dataset.split("/") + [args.config, args.split])
17
+
18
+ # load metric
19
+ wer = load_metric("wer")
20
+ cer = load_metric("cer")
21
+
22
+ # compute metrics
23
+ wer_result = wer.compute(references=result["target"], predictions=result["prediction"])
24
+ cer_result = cer.compute(references=result["target"], predictions=result["prediction"])
25
+
26
+ # print & log results
27
+ result_str = f"WER: {wer_result}\n" f"CER: {cer_result}"
28
+ print(result_str)
29
+
30
+ with open(f"{dataset_id}_eval_results.txt", "w") as f:
31
+ f.write(result_str)
32
+
33
+ # log all results in text file. Possibly interesting for analysis
34
+ if log_outputs is not None:
35
+ pred_file = f"log_{dataset_id}_predictions.txt"
36
+ target_file = f"log_{dataset_id}_targets.txt"
37
+
38
+ with open(pred_file, "w") as p, open(target_file, "w") as t:
39
+
40
+ # mapping function to write output
41
+ def write_to_file(batch, i):
42
+ p.write(f"{i}" + "\n")
43
+ p.write(batch["prediction"] + "\n")
44
+ t.write(f"{i}" + "\n")
45
+ t.write(batch["target"] + "\n")
46
+
47
+ result.map(write_to_file, with_indices=True)
48
+
49
+
50
+ def normalize_text(text: str) -> str:
51
+ """DO ADAPT FOR YOUR USE CASE. this function normalizes the target text."""
52
+
53
+ chars_to_ignore_regex = """[\!\؛\،\٫\؟\۔\٪\"\'\:\-\‘\’]""" # noqa: W605 IMPORTANT: this should correspond to the chars that were ignored during training
54
+
55
+ text = re.sub(chars_to_ignore_regex, "", text.lower())
56
+ text = re.sub("[،]", '', text)
57
+ text = re.sub("[؟]", '', text)
58
+ text = re.sub("['َ]", '', text)
59
+ text = re.sub("['ُ]", '', text)
60
+ text = re.sub("['ِ]", '', text)
61
+ text = re.sub("['ّ]", '', text)
62
+ text = re.sub("['ٔ]", '', text)
63
+ text = re.sub("['ٰ]", '', text)
64
+ # batch["sentence"] = re.sub("[ء]", '', batch["sentence"])
65
+ # batch["sentence"] = re.sub("[آ]", 'ا', batch["sentence"])
66
+ text = re.sub("[ۂ]", 'ہ', text)
67
+ text = re.sub("[ي]", "ی",text)
68
+ text = re.sub("[ؤ]", "و", text)
69
+ # batch["sentence"] = re.sub("[ئ]", 'ى', batch["sentence"])
70
+ text = re.sub("[ى]", 'ی', text)
71
+ text = re.sub("[۔]", '', text)
72
+
73
+ # In addition, we can normalize the target text, e.g. removing new lines characters etc...
74
+ # note that order is important here!
75
+ token_sequences_to_ignore = ["\n\n", "\n", " ", " "]
76
+
77
+ for t in token_sequences_to_ignore:
78
+ text = " ".join(text.split(t))
79
+
80
+ return text
81
+
82
+
83
+ def main(args):
84
+ # load dataset
85
+ dataset = load_dataset(args.dataset, args.config, split=args.split, use_auth_token=True)
86
+
87
+ # for testing: only process the first two examples as a test
88
+ # dataset = dataset.select(range(10))
89
+
90
+ # load processor
91
+ feature_extractor = AutoFeatureExtractor.from_pretrained(args.model_id)
92
+ sampling_rate = feature_extractor.sampling_rate
93
+
94
+ # resample audio
95
+ dataset = dataset.cast_column("audio", Audio(sampling_rate=sampling_rate))
96
+
97
+ # load eval pipeline
98
+ if args.device is None:
99
+ args.device = 0 if torch.cuda.is_available() else -1
100
+ asr = pipeline("automatic-speech-recognition", model=args.model_id, device=args.device)
101
+
102
+ # map function to decode audio
103
+ def map_to_pred(batch):
104
+ prediction = asr(
105
+ batch["audio"]["array"], chunk_length_s=args.chunk_length_s, stride_length_s=args.stride_length_s
106
+ )
107
+
108
+ batch["prediction"] = prediction["text"]
109
+ batch["target"] = normalize_text(batch["sentence"])
110
+ return batch
111
+
112
+ # run inference on all examples
113
+ result = dataset.map(map_to_pred, remove_columns=dataset.column_names)
114
+
115
+ # compute and log_results
116
+ # do not change function below
117
+ log_results(result, args)
118
+
119
+
120
+ if __name__ == "__main__":
121
+ parser = argparse.ArgumentParser()
122
+
123
+ parser.add_argument(
124
+ "--model_id", type=str, required=True, help="Model identifier. Should be loadable with 🤗 Transformers"
125
+ )
126
+ parser.add_argument(
127
+ "--dataset",
128
+ type=str,
129
+ required=True,
130
+ help="Dataset name to evaluate the `model_id`. Should be loadable with 🤗 Datasets",
131
+ )
132
+ parser.add_argument(
133
+ "--config", type=str, required=True, help="Config of the dataset. *E.g.* `'en'` for Common Voice"
134
+ )
135
+ parser.add_argument("--split", type=str, required=True, help="Split of the dataset. *E.g.* `'test'`")
136
+ parser.add_argument(
137
+ "--chunk_length_s", type=float, default=None, help="Chunk length in seconds. Defaults to 5 seconds."
138
+ )
139
+ parser.add_argument(
140
+ "--stride_length_s", type=float, default=None, help="Stride of the audio chunks. Defaults to 1 second."
141
+ )
142
+ parser.add_argument(
143
+ "--log_outputs", action="store_true", help="If defined, write outputs to log file for analysis."
144
+ )
145
+ parser.add_argument(
146
+ "--device",
147
+ type=int,
148
+ default=None,
149
+ help="The device to run the pipeline on. -1 for CPU (default), 0 for the first GPU and so on.",
150
+ )
151
+ args = parser.parse_args()
152
+
153
+ main(args)
language_model/5gram.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a51741105f0d3b54ac8ede33500c8f80fcd6b03900080a45a5661ec8e58b776f
3
+ size 165145481
language_model/attrs.json ADDED
@@ -0,0 +1 @@
 
1
+ {"alpha": 0.5, "beta": 1.5, "unk_score_offset": -10.0, "score_boundary": true}
language_model/unigrams.txt ADDED
The diff for this file is too large to render. See raw diff