AlhitawiMohammed22 commited on
Commit
f959a75
1 Parent(s): d9636b6

create gradio app CER,WER&Acc

Browse files
Files changed (8) hide show
  1. app.py +5 -0
  2. eval_accuracy.py +0 -0
  3. eval_cer.py +143 -0
  4. eval_wer.py +0 -0
  5. requirements.txt +2 -0
  6. test_accuracy.py +0 -0
  7. test_eval_cer.py +96 -0
  8. test_eval_wer.py +0 -0
app.py ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ import evaluate
2
+ from evaluate.utils import launch_gradio_widget
3
+
4
+ module = evaluate.load("cer")
5
+ launch_gradio_widget(module)
eval_accuracy.py ADDED
File without changes
eval_cer.py ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """ Character Error Ratio (CER) metric. """
2
+ from typing import List
3
+ import datasets, evaluate , jiwer
4
+ import jiwer.transforms as tr
5
+ from datasets.config import PY_VERSION
6
+ from packaging import version
7
+
8
+
9
+ if PY_VERSION < version.parse("3.8"):
10
+ import importlib_metadata
11
+ else:
12
+ import importlib.metadata as importlib_metadata
13
+
14
+ SENTENCE_DELIMITER = ""
15
+
16
+ if version.parse(importlib_metadata.version("jiwer")) < version.parse("2.3.0"):
17
+
18
+ class SentencesToListOfCharacters(tr.AbstractTransform):
19
+ def __init__(self, sentence_delimiter: str = " "):
20
+ self.sentence_delimiter = sentence_delimiter
21
+
22
+ def process_string(self, s: str):
23
+ return list(s)
24
+
25
+ def process_list(self, inp: List[str]):
26
+ chars = []
27
+ for sent_idx, sentence in enumerate(inp):
28
+ chars.extend(self.process_string(sentence))
29
+ if self.sentence_delimiter is not None and self.sentence_delimiter != "" and sent_idx < len(inp) - 1:
30
+ chars.append(self.sentence_delimiter)
31
+ return chars
32
+
33
+ cer_transform = tr.Compose(
34
+ [tr.RemoveMultipleSpaces(), tr.Strip(), SentencesToListOfCharacters(SENTENCE_DELIMITER)]
35
+ )
36
+ else:
37
+ cer_transform = tr.Compose(
38
+ [
39
+ tr.RemoveMultipleSpaces(),
40
+ tr.Strip(),
41
+ tr.ReduceToSingleSentence(SENTENCE_DELIMITER),
42
+ tr.ReduceToListOfListOfChars(),
43
+ ]
44
+ )
45
+
46
+
47
+ _CITATION = """\
48
+ @inproceedings{inproceedings,
49
+ author = {Morris, Andrew and Maier, Viktoria and Green, Phil},
50
+ year = {2004},
51
+ month = {01},
52
+ pages = {},
53
+ title = {From WER and RIL to MER and WIL: improved evaluation measures for connected speech recognition.}
54
+ }
55
+ """
56
+
57
+ _DESCRIPTION = """\
58
+ Character error rate (CER) is a common metric of the performance of an automatic speech recognition system.
59
+
60
+ CER is similar to Word Error Rate (WER), but operates on character instead of word. Please refer to docs of WER for further information.
61
+
62
+ Character error rate can be computed as:
63
+
64
+ CER = (S + D + I) / N = (S + D + I) / (S + D + C)
65
+
66
+ where
67
+
68
+ S is the number of substitutions,
69
+ D is the number of deletions,
70
+ I is the number of insertions,
71
+ C is the number of correct characters,
72
+ N is the number of characters in the reference (N=S+D+C).
73
+
74
+ CER's output is not always a number between 0 and 1, in particular when there is a high number of insertions. This value is often associated to the percentage of characters that were incorrectly predicted. The lower the value, the better the
75
+ performance of the ASR system with a CER of 0 being a perfect score.
76
+ """
77
+
78
+ _KWARGS_DESCRIPTION = """
79
+ Computes CER score of transcribed segments against references.
80
+ Args:
81
+ references: list of references for each speech input.
82
+ predictions: list of transcribtions to score.
83
+ concatenate_texts: Whether or not to concatenate sentences before evaluation, set to True for more accurate result.
84
+ Returns:
85
+ (float): the character error rate
86
+
87
+ Examples for Hungarain Languge:
88
+ >>> # Colab usage
89
+ >>> !pip install evaluate jiwer
90
+ >>> import evaluate
91
+ >>> from evaluate import load
92
+
93
+ >>> predictions = ["ez a jóslat", "van egy másik minta is"]
94
+ >>> references = ["ez a hivatkozás", "van még egy"]
95
+ >>> cer = evaluate.load("cer")
96
+ >>> cer_score = cer.compute(predictions=predictions, references=references)
97
+ >>> print(cer_score)
98
+ >>> 0.9615384615384616
99
+ """
100
+
101
+
102
+ @evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
103
+ class CER(evaluate.Metric):
104
+ def _info(self):
105
+ return evaluate.MetricInfo(
106
+ description=_DESCRIPTION,
107
+ citation=_CITATION,
108
+ inputs_description=_KWARGS_DESCRIPTION,
109
+ features=datasets.Features(
110
+ {
111
+ "predictions": datasets.Value("string", id="sequence"),
112
+ "references": datasets.Value("string", id="sequence"),
113
+ }
114
+ ),
115
+ codebase_urls=["https://github.com/jitsi/jiwer/"],
116
+ reference_urls=[
117
+ "https://en.wikipedia.org/wiki/Word_error_rate",
118
+ "https://sites.google.com/site/textdigitisation/qualitymeasures/computingerrorrates",
119
+ ],
120
+ )
121
+
122
+ def _compute(self, predictions, references, concatenate_texts=False):
123
+ if concatenate_texts:
124
+ return jiwer.compute_measures(
125
+ references,
126
+ predictions,
127
+ truth_transform=cer_transform,
128
+ hypothesis_transform=cer_transform,
129
+ )["wer"]
130
+
131
+ incorrect = 0
132
+ total = 0
133
+ for prediction, reference in zip(predictions, references):
134
+ measures = jiwer.compute_measures(
135
+ reference,
136
+ prediction,
137
+ truth_transform=cer_transform,
138
+ hypothesis_transform=cer_transform,
139
+ )
140
+ incorrect += measures["substitutions"] + measures["deletions"] + measures["insertions"]
141
+ total += measures["substitutions"] + measures["deletions"] + measures["hits"]
142
+
143
+ return incorrect / total
eval_wer.py ADDED
File without changes
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ git+https://github.com/huggingface/evaluate@8b9373dc8693ffe0244a52551ac5573cffa503aa
2
+ jiwer
test_accuracy.py ADDED
File without changes
test_eval_cer.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+ from cer import CER
3
+
4
+ cer = CER()
5
+ class TestCER(unittest.TestCase):
6
+ def test_cer_case_sensitive(self):
7
+ refs = ["Magyar Országgyűlés"]
8
+ preds = ["Magyar Országgyűlés"]
9
+ # S = 2, D = 0, I = 0, N = 11, CER = 2 / 11
10
+ char_error_rate = cer.compute(predictions=preds, references=refs)
11
+ self.assertTrue(abs(char_error_rate - 0.1818181818) < 1e-6)
12
+
13
+ def test_cer_whitespace(self):
14
+ refs = ["Farkasok voltak"]
15
+ preds = ["Farkasokvoltak"]
16
+ # S = , D = , I = 1, N = , CER = I / N
17
+ char_error_rate = cer.compute(predictions=preds, references=refs)
18
+ self.assertTrue(abs(char_error_rate - 0.) < 1e-6)
19
+
20
+ refs = ["Farkasokvoltak"]
21
+ preds = ["Ferkasok voltak"]
22
+ # S = , D = 1, I = 0, N = 14, CER =
23
+ char_error_rate = cer.compute(predictions=preds, references=refs)
24
+ self.assertTrue(abs(char_error_rate - 0.) < 1e-6)
25
+
26
+ # consecutive whitespaces case 1
27
+ refs = ["Farkasok voltak"]
28
+ preds = ["Farkasok voltak"]
29
+ # S = 0, D = 0, I = 0, N = , CER = 0
30
+ char_error_rate = cer.compute(predictions=preds, references=refs)
31
+ self.assertTrue(abs(char_error_rate - 0.0) < 1e-6)
32
+
33
+ # consecutive whitespaces case 2
34
+ refs = ["Farkasok voltak"]
35
+ preds = ["Farkasok voltak"]
36
+ # S = 0, D = 0, I = 0, N = ?, CER = 0
37
+ char_error_rate = cer.compute(predictions=preds, references=refs)
38
+ self.assertTrue(abs(char_error_rate - 0.0) < 1e-6)
39
+
40
+ def test_cer_sub(self):
41
+ refs = ["Magyar"]
42
+ preds = ["Megyar"]
43
+ # S = 1, D = 0, I = 0, N = 6, CER = 0.125
44
+ char_error_rate = cer.compute(predictions=preds, references=refs)
45
+ self.assertTrue(abs(char_error_rate - 0.125) < 1e-6)
46
+
47
+ def test_cer_del(self):
48
+ refs = ["Farkasokvoltak"]
49
+ preds = ["Farkasokavoltak"]
50
+ # S = 0, D = 1, I = 0, N = 14, CER = 0.
51
+ char_error_rate = cer.compute(predictions=preds, references=refs)
52
+ self.assertTrue(abs(char_error_rate - 0.) < 1e-6)
53
+
54
+ def test_cer_insert(self):
55
+ refs = ["Farkasokvoltak"]
56
+ preds = ["Farkasokoltak"]
57
+ # S = 0, D = 0, I = 1, N = 14, CER = 0.
58
+ char_error_rate = cer.compute(predictions=preds, references=refs)
59
+ self.assertTrue(abs(char_error_rate - 0.) < 1e-6)
60
+
61
+ def test_cer_equal(self):
62
+ refs = ["Magyar"]
63
+ char_error_rate = cer.compute(predictions=refs, references=refs)
64
+ self.assertEqual(char_error_rate, 0.0)
65
+
66
+ def test_cer_list_of_seqs(self):
67
+ # ['Eötvös Loránd University','I love my daughter']
68
+ refs = ["Eötvös Loránd Tudományegyetem", "szeretem a lányom"]
69
+ char_error_rate = cer.compute(predictions=refs, references=refs)
70
+ self.assertEqual(char_error_rate, 0.0)
71
+
72
+ refs = ["diák", "Az arab nyelvet könnyű megtanulni!", "autó"]
73
+ preds = ["dxák", "Az arab nyelvet könnyű megtanulni!", "autó"]
74
+ # S = 1, D = 0, I = 0, N = 28, CER = 1 / 42
75
+ char_error_rate = cer.compute(predictions=preds, references=refs)
76
+ self.assertTrue(abs(char_error_rate - 0.0238095238) < 1e-6)
77
+
78
+ def test_correlated_sentences(self):
79
+ # Learn artificial intelligence to secure your future
80
+ # Tanuljon mesterséges intelligenciát, hogy biztosítsa jövőjét
81
+ refs = ["Tanuljon mesterséges intelligenciát,", " hogy biztosítsa jövőjét"]
82
+ preds = ["Tanuljon mesterséges intelligenciát, hogy", " biztosítsa jövőjét"]
83
+ # S = 0, D = 0, I = 1, N = 28, CER = 2 / 60
84
+ # whitespace at the front of " biztosítsa jövőjét" will be strip during preporcessing
85
+ # so need to insert 2 whitespaces
86
+ char_error_rate = cer.compute(predictions=preds, references=refs, concatenate_texts=True)
87
+ self.assertTrue(abs(char_error_rate - 0.03333333333) < 1e-6)
88
+
89
+ def test_cer_empty(self):
90
+ refs = [""]
91
+ preds = ["tök mindegy"]
92
+ with self.assertRaises(ValueError):
93
+ cer.compute(predictions=preds, references=refs)
94
+
95
+ if __name__ == "__main__":
96
+ unittest.main()
test_eval_wer.py ADDED
File without changes