export.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/usr/bin/env python3
  2. # -*- encoding: utf-8 -*-
  3. # Copyright FunASR (https://github.com/FunAudioLLM/SenseVoice). All Rights Reserved.
  4. # MIT License (https://opensource.org/licenses/MIT)
  5. import os
  6. import torch
  7. from model import SenseVoiceSmall
  8. from utils import export_utils
  9. from utils.model_bin import SenseVoiceSmallONNX
  10. from funasr.utils.postprocess_utils import rich_transcription_postprocess
  11. quantize = False
  12. model_dir = "iic/SenseVoiceSmall"
  13. model, kwargs = SenseVoiceSmall.from_pretrained(model=model_dir, device="cuda:0")
  14. rebuilt_model = model.export(type="onnx", quantize=False)
  15. model_path = kwargs.get("output_dir", os.path.dirname(kwargs.get("init_param")))
  16. model_file = os.path.join(model_path, "model.onnx")
  17. if quantize:
  18. model_file = os.path.join(model_path, "model_quant.onnx")
  19. # export model
  20. if not os.path.exists(model_file):
  21. with torch.no_grad():
  22. del kwargs['model']
  23. export_dir = export_utils.export(model=rebuilt_model, **kwargs)
  24. print("Export model onnx to {}".format(model_file))
  25. # export model init
  26. model_bin = SenseVoiceSmallONNX(model_path)
  27. # build tokenizer
  28. try:
  29. from funasr.tokenizer.sentencepiece_tokenizer import SentencepiecesTokenizer
  30. tokenizer = SentencepiecesTokenizer(bpemodel=os.path.join(model_path, "chn_jpn_yue_eng_ko_spectok.bpe.model"))
  31. except:
  32. tokenizer = None
  33. # inference
  34. wav_or_scp = "/Users/shixian/Downloads/asr_example_hotword.wav"
  35. language_list = [0]
  36. textnorm_list = [15]
  37. res = model_bin(wav_or_scp, language_list, textnorm_list, tokenizer=tokenizer)
  38. print([rich_transcription_postprocess(i) for i in res])