()
| 5 | import os |
| 6 | |
| 7 | def main(): |
| 8 | import soundfile |
| 9 | from funasr import AutoModel |
| 10 | |
| 11 | print("[Paraformer-Streaming] Loading model...") |
| 12 | t0 = time.time() |
| 13 | model = AutoModel( |
| 14 | model="iic/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-online", |
| 15 | device="cpu", |
| 16 | disable_update=True, |
| 17 | disable_pbar=True, |
| 18 | ) |
| 19 | print("[Paraformer-Streaming] Model loaded in %.1fs" % (time.time() - t0)) |
| 20 | |
| 21 | wav_file = os.path.join(model.model_path, "example/asr_example.wav") |
| 22 | speech, sample_rate = soundfile.read(wav_file) |
| 23 | |
| 24 | chunk_size = [0, 10, 5] |
| 25 | encoder_chunk_look_back = 4 |
| 26 | decoder_chunk_look_back = 1 |
| 27 | chunk_stride = chunk_size[1] * 960 # 600ms |
| 28 | |
| 29 | print("[Paraformer-Streaming] Running streaming inference (%.2fs audio, %d chunks)..." % ( |
| 30 | len(speech) / sample_rate, int((len(speech) - 1) / chunk_stride + 1))) |
| 31 | t0 = time.time() |
| 32 | |
| 33 | cache = {} |
| 34 | total_chunk_num = int((len(speech) - 1) / chunk_stride + 1) |
| 35 | all_text = "" |
| 36 | |
| 37 | for i in range(total_chunk_num): |
| 38 | speech_chunk = speech[i * chunk_stride:(i + 1) * chunk_stride] |
| 39 | is_final = i == total_chunk_num - 1 |
| 40 | res = model.generate( |
| 41 | input=speech_chunk, |
| 42 | cache=cache, |
| 43 | is_final=is_final, |
| 44 | chunk_size=chunk_size, |
| 45 | encoder_chunk_look_back=encoder_chunk_look_back, |
| 46 | decoder_chunk_look_back=decoder_chunk_look_back, |
| 47 | ) |
| 48 | txt = res[0].get("text", "") if res else "" |
| 49 | all_text += txt |
| 50 | |
| 51 | print("[Paraformer-Streaming] Inference done in %.1fs" % (time.time() - t0)) |
| 52 | print("[Paraformer-Streaming] Result: '%s'" % all_text) |
| 53 | |
| 54 | expected = "欢迎大家来体验达摩院推出的语音识别模型" |
| 55 | if expected in all_text: |
| 56 | print("[Paraformer-Streaming] PASSED") |
| 57 | return 0 |
| 58 | else: |
| 59 | print("[Paraformer-Streaming] FAILED - expected text not found") |
| 60 | return 1 |
| 61 | |
| 62 | if __name__ == "__main__": |
| 63 | sys.exit(main()) |
no test coverage detected
searching dependent graphs…