File size: 7,412 Bytes
ade4f6a
 
 
e4406a3
ade4f6a
 
8a34d65
e4406a3
 
 
 
 
 
 
8a3bc32
e4406a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a3bc32
 
 
 
778443c
8a3bc32
 
 
e4406a3
 
8a3bc32
e4406a3
 
 
 
8a3bc32
 
42742c6
e4406a3
8a3bc32
 
 
 
3d1d87d
8a3bc32
 
 
 
 
 
 
 
 
 
 
79f9224
 
42742c6
79f9224
42742c6
778443c
79f9224
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a34d65
 
42742c6
8a34d65
778443c
8a34d65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42742c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4406a3
42742c6
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
from pywhispercpp.model import Model
from pathlib import Path
import time
import csv

from silero_vad.utils_vad import languages
from scripts.asr_utils import get_origin_text_dict, get_text_distance

def save_csv(file_path, rows):
    with open(file_path, "w", encoding="utf-8") as f:
        writer = csv.writer(f)
        writer.writerows(rows)
        print(f"write csv to {file_path}")

def load_model():
    models_dir = Path("/Users/jeqin/work/code/Translator/python_server/moyoyo_asr_models")
    whisper_model = 'large-v3-turbo-q5_0'
    t0 = time.time()
    model = Model(
                model=whisper_model,
                models_dir=models_dir,
                print_realtime=False,
                print_progress=False,
                print_timestamps=False,
                translate=False,
                # beam_search=1,
                temperature=0.,
                no_context=True
            )
    print("load model time: ", time.time()-t0)
    return model

def run_recordings():
    model = load_model()
    audios = Path("../test_data/recordings/")
    rows = [["file_name", "time", "inference_result"]]
    original = get_origin_text_dict()
    for audio in sorted(audios.glob("*.wav"), key=lambda x: int(x.stem)):
        print(audio)
        t1 = time.time()
        output = model.transcribe(str(audio), language="zh", initial_prompt="以下是普通话句子,这是一段会议内容。")# initial_prompt="这是一段中文的会议内容。"
        t = time.time() - t1
        print("inference time:", t)
        text = " ".join([a.text for a in output])
        print(text)
        d, nd, diff = get_text_distance(original[audio.stem], text)
        rows.append([audio.name, round(t, 3), text, d, round(nd,3), diff])
    save_csv("csv/pywhisper.csv", rows)


def run_test_audios():
    model = load_model()
    lang = "zh"
    audios = Path("../test_data/audio_clips/")
    rows = [["file_name", "time", "inference_result"]]
    for audio in sorted(audios.glob(f"*{lang}*/*.wav")):
        print(audio)
        t1 = time.time()
        output = model.transcribe(str(audio), language=lang, initial_prompt="以下是普通话句子,这是一段会议内容。")# initial_prompt="这是一段中文的会议内容。"
        t = time.time() - t1
        print("inference time:", t)
        text = " ".join([a.text for a in output])
        print(text)
        rows.append([f"{audio.parent.name}/{audio.name}", round(t, 3), text])
    save_csv("csv/whisper.csv", rows)

def run_test_dataset():
    from test_data.audios import read_dataset
    model = load_model()
    test_data = Path("../test_data/dataset/dataset.txt")
    audio_parent = Path("../test_data/")
    rows = [["file_name", "time", "inference_result"]]
    result_list = []
    count = 0
    try:
        for audio_path, sentence, duration in read_dataset(test_data):
            count += 1
            print(f"processing {count}: {audio_path}")

            t1 = time.time()
            output = model.transcribe(str(audio_parent/audio_path), language="zh")# , initial_prompt="以下是普通话句子,这是一段会议内容。"
            t = time.time() - t1
            print("inference time:", t)
            text = " ".join([a.text for a in output])
            print(text)
            result_list.append({
                "index": count,
                "audio_path": audio_path,
                "reference": sentence,
                "duration": duration,
                "inference_time": round(t, 3),
                "inference_result": text
            })
    except Exception as e:
        print(e)
    except KeyboardInterrupt as e:
        print(e)
    import json
    with open("csv/whisper_dataset_results.json", "w", encoding="utf-8") as f:
        json.dump(result_list, f, ensure_ascii=False, indent=2)

def run_test_emilia():
    from test_data.audios import read_emilia
    model = load_model()
    parent = Path("../test_data/ZH-B000000")
    result_list = []
    count = 0
    try:
        for audio_path, sentence, duration in read_emilia(parent, count_limit=5000):
            count += 1
            print(f"processing {count}: {audio_path.name}")

            t1 = time.time()
            output = model.transcribe(str(audio_path), language="zh")# , initial_prompt="以下是普通话句子,这是一段会议内容。"
            t = time.time() - t1
            print("inference time:", t)
            text = " ".join([a.text for a in output])
            print(text)
            result_list.append({
                "index": count,
                "audio_path": audio_path.name,
                "reference": sentence,
                "duration": duration,
                "inference_time": round(t, 3),
                "inference_result": text
            })
    except Exception as e:
        print(e)
    except KeyboardInterrupt as e:
        print(e)
    import json
    with open("csv/whisper_emilia_results.json", "w", encoding="utf-8") as f:
        json.dump(result_list, f, ensure_ascii=False, indent=2)

def run_test_st():
    from test_data.audios import read_st
    model = load_model()
    # parent = Path("../test_data/ST-CMDS-20170001_1-OS")
    result_list = []
    count = 0
    try:
        for audio_path, sentence in read_st(count_limit=5000):
            count += 1
            print(f"processing {count}: {audio_path}")

            t1 = time.time()
            output = model.transcribe(
                str(audio_path), language="zh"
            )
            t = time.time() - t1
            print("inference time:", t)
            text = " ".join([a.text for a in output])
            print(text)
            result_list.append({
                "index": count,
                "audio_path": audio_path.name,
                "reference": sentence,
                # "duration": duration,
                "inference_time": round(t, 3),
                "inference_result": text
            })
    except Exception as e:
        print(e)
    except KeyboardInterrupt as e:
        print(e)
    import json
    with open("csv/whisper_st_results.json", "w", encoding="utf-8") as f:
        json.dump(result_list, f, ensure_ascii=False, indent=2)


def run_test_wenet():
    from test_data.audios import read_wenet
    model = load_model()
    result_list = []
    count = 0
    try:
        for audio_path, sentence in read_wenet(count_limit=5000):
            count += 1
            print(f"processing {count}: {audio_path}")

            t1 = time.time()
            output = model.transcribe(
                str(audio_path), language="zh"
            )
            t = time.time() - t1
            print("inference time:", t)
            text = " ".join([a.text for a in output])
            print(text)
            result_list.append({
                "index": count,
                "audio_path": audio_path.name,
                "reference": sentence,
                # "duration": duration,
                "inference_time": round(t, 3),
                "inference_result": text
            })
    except Exception as e:
        print(e)
    except KeyboardInterrupt as e:
        print(e)
    import json
    with open("csv/whisper_wenet_results.json", "w", encoding="utf-8") as f:
        json.dump(result_list, f, ensure_ascii=False, indent=2)


if __name__ == '__main__':
    # run_test_emilia()
    # run_recordings()
    run_test_wenet()