Open-source speech recognition toolkit for training, inference, streaming ASR, VAD, punctuation, speaker diarization pipelines, and OpenAI-compatible/MCP serving.
Python asraudiochineseemotion-recognitionfunasrmcp-servermultilingual-asropenai-compatible-apiparaformerpunctuationpytorchreal-time-asrspeaker-diarizationspeech-recognitionspeech-to-textstreaming-asrtranscriptionvllmvoice-activity-detectionwhisper-alternative
Raise real exceptions instead of strings in the runtime Python bindings
Leonard Li4999dc7632f3
diff --git a/runtime/python/libtorch/funasr_torch/paraformer_bin.py b/runtime/python/libtorch/funasr_torch/paraformer_bin.pyindex 16c040678..8e404126a 100644--- a/runtime/python/libtorch/funasr_torch/paraformer_bin.py+++ b/runtime/python/libtorch/funasr_torch/paraformer_bin.py@@ -37,14 +37,18 @@ class Paraformer:if not Path(model_dir).exists():try:from modelscope.hub.snapshot_download import snapshot_download- except:- raise "You are exporting model from modelscope, please install modelscope and try it again. To install modelscope, you could:\n" "\npip3 install -U modelscope\n" "For the users in China, you could install with the command:\n" "\npip3 install -U modelscope -i https://mirror.sjtu.edu.cn/pypi/web/simple"+ except ImportError as e:+ raise ImportError(+ "You are exporting model from modelscope, please install modelscope and try it again. To install modelscope, you could:\n" "\npip3 install -U modelscope\n" "For the users in China, you could install with the command:\n" "\npip3 install -U modelscope -i https://mirror.sjtu.edu.cn/pypi/web/simple"+ ) from etry:model_dir = snapshot_download(model_dir, cache_dir=cache_dir)- except:- raise "model_dir must be model_name in modelscope or local path downloaded from modelscope, but is {}".format(- model_dir- )+ except Exception as e:+ raise RuntimeError(+ "model_dir must be model_name in modelscope or local path downloaded from modelscope, but is {}".format(+ model_dir+ )+ ) from emodel_file = os.path.join(model_dir, "model.torchscript")if quantize:@@ -53,8 +57,10 @@ class Paraformer:print(".torchscripts does not exist, begin to export torchscript")try:from funasr import AutoModel- except:- raise "You are exporting onnx, please install funasr and try it again. To install funasr, you could:\n" "\npip3 install -U funasr\n" "For the users in China, you could install with the command:\n" "\npip3 install -U funasr -i https://mirror.sjtu.edu.cn/pypi/web/simple"+ except ImportError as e:+ raise ImportError(+ "You are exporting onnx, please install funasr and try it again. To install funasr, you could:\n" "\npip3 install -U funasr\n" "For the users in China, you could install with the command:\n" "\npip3 install -U funasr -i https://mirror.sjtu.edu.cn/pypi/web/simple"+ ) from emodel = AutoModel(model=model_dir)model_dir = model.export(type="torchscript", quantize=quantize, **kwargs)@@ -258,14 +264,18 @@ class ContextualParaformer(Paraformer):if not Path(model_dir).exists():try:from modelscope.hub.snapshot_download import snapshot_download- except:- raise "You are exporting model from modelscope, please install modelscope and try it again. To install modelscope, you could:\n" "\npip3 install -U modelscope\n" "For the users in China, you could install with the command:\n" "\npip3 install -U modelscope -i https://mirror.sjtu.edu.cn/pypi/web/simple"+ except ImportError as e:+ raise ImportError(+ "You are exporting model from modelscope, please install modelscope and try it again. To install modelscope, you could:\n" "\npip3 install -U modelscope\n" "For the users in China, you could install with the command:\n" "\npip3 install -U modelscope -i https://mirror.sjtu.edu.cn/pypi/web/simple"+ ) from etry:model_dir = snapshot_download(model_dir, cache_dir=cache_dir)- except:- raise "model_dir must be model_name in modelscope or local path downloaded from modelscope, but is {}".format(- model_dir- )+ except Exception as e:+ raise RuntimeError(+ "model_dir must be model_name in modelscope or local path downloaded from modelscope, but is {}".format(+ model_dir+ )+ ) from eif quantize:model_bb_file = os.path.join(model_dir, "model_bb_quant.torchscript")@@ -278,8 +288,10 @@ class ContextualParaformer(Paraformer):print(".onnx does not exist, begin to export onnx")try:from funasr import AutoModel- except:- raise "You are exporting onnx, please install funasr and try it again. To install funasr, you could:\n" "\npip3 install -U funasr\n" "For the users in China, you could install with the command:\n" "\npip3 install -U funasr -i https://mirror.sjtu.edu.cn/pypi/web/simple"+ except ImportError as e:+ raise ImportError(+ "You are exporting onnx, please install funasr and try it again. To install funasr, you could:\n" "\npip3 install -U funasr\n" "For the users in China, you could install with the command:\n" "\npip3 install -U funasr -i https://mirror.sjtu.edu.cn/pypi/web/simple"+ ) from emodel = AutoModel(model=model_dir)model_dir = model.export(type="torchscript", quantize=quantize, **kwargs)diff --git a/runtime/python/libtorch/funasr_torch/sensevoice_bin.py b/runtime/python/libtorch/funasr_torch/sensevoice_bin.pyindex 608da41cd..c27bfdd83 100644--- a/runtime/python/libtorch/funasr_torch/sensevoice_bin.py+++ b/runtime/python/libtorch/funasr_torch/sensevoice_bin.py@@ -43,14 +43,18 @@ class SenseVoiceSmall:if not Path(model_dir).exists():try:from modelscope.hub.snapshot_download import snapshot_download- except:- raise "You are exporting model from modelscope, please install modelscope and try it again. To install modelscope, you could:\n" "\npip3 install -U modelscope\n" "For the users in China, you could install with the command:\n" "\npip3 install -U modelscope -i https://mirror.sjtu.edu.cn/pypi/web/simple"+ except ImportError as e:+ raise ImportError(+ "You are exporting model from modelscope, please install modelscope and try it again. To install modelscope, you could:\n" "\npip3 install -U modelscope\n" "For the users in China, you could install with the command:\n" "\npip3 install -U modelscope -i https://mirror.sjtu.edu.cn/pypi/web/simple"+ ) from etry:model_dir = snapshot_download(model_dir, cache_dir=cache_dir)- except:- raise "model_dir must be model_name in modelscope or local path downloaded from modelscope, but is {}".format(- model_dir- )+ except Exception as e:+ raise RuntimeError(+ "model_dir must be model_name in modelscope or local path downloaded from modelscope, but is {}".format(+ model_dir+ )+ ) from emodel_file = os.path.join(model_dir, "model.torchscript")if quantize:@@ -59,8 +63,10 @@ class SenseVoiceSmall:print(".torchscripts does not exist, begin to export torchscript")try:from funasr import AutoModel- except:- raise "You are exporting onnx, please install funasr and try it again. To install funasr, you could:\n" "\npip3 install -U funasr\n" "For the users in China, you could install with the command:\n" "\npip3 install -U funasr -i https://mirror.sjtu.edu.cn/pypi/web/simple"+ except ImportError as e:+ raise ImportError(+ "You are exporting onnx, please install funasr and try it again. To install funasr, you could:\n" "\npip3 install -U funasr\n" "For the users in China, you could install with the command:\n" "\npip3 install -U funasr -i https://mirror.sjtu.edu.cn/pypi/web/simple"+ ) from emodel = AutoModel(model=model_dir)model_dir = model.export(type="torchscript", quantize=quantize, **kwargs)diff --git a/runtime/python/onnxruntime/funasr_onnx/paraformer_bin.py b/runtime/python/onnxruntime/funasr_onnx/paraformer_bin.pyindex bde7f442e..94538033c 100644--- a/runtime/python/onnxruntime/funasr_onnx/paraformer_bin.py+++ b/runtime/python/onnxruntime/funasr_onnx/paraformer_bin.py@@ -49,14 +49,18 @@ class Paraformer:if not Path(model_dir).exists():try:from modelscope.hub.snapshot_download import snapshot_download- except:- raise "You are exporting model from modelscope, please install modelscope and try it again. To install modelscope, you could:\n" "\npip3 install -U modelscope\n" "For the users in China, you could install with the command:\n" "\npip3 install -U modelscope -i https://mirror.sjtu.edu.cn/pypi/web/simple"+ except ImportError as e:+ raise ImportError(+ "You are exporting model from modelscope, please install modelscope and try it again. To install modelscope, you could:\n" "\npip3 install -U modelscope\n" "For the users in China, you could install with the command:\n" "\npip3 install -U modelscope -i https://mirror.sjtu.edu.cn/pypi/web/simple"+ ) from etry:model_dir = snapshot_download(model_dir, cache_dir=cache_dir)- except:- raise "model_dir must be model_name in modelscope or local path downloaded from modelscope, but is {}".format(- model_dir- )+ except Exception as e:+ raise RuntimeError(+ "model_dir must be model_name in modelscope or local path downloaded from modelscope, but is {}".format(+ model_dir+ )+ ) from emodel_file = os.path.join(model_dir, "model.onnx")if quantize:@@ -65,8 +69,10 @@ class Paraformer:print(".onnx does not exist, begin to export onnx")try:from funasr import AutoModel- except:- raise "You are exporting onnx, please install funasr and try it again. To install funasr, you could:\n" "\npip3 install -U funasr\n" "For the users in China, you could install with the command:\n" "\npip3 install -U funasr -i https://mirror.sjtu.edu.cn/pypi/web/simple"+ except ImportError as e:+ raise ImportError(+ "You are exporting onnx, please install funasr and try it again. To install funasr, you could:\n" "\npip3 install -U funasr\n" "For the users in China, you could install with the command:\n" "\npip3 install -U funasr -i https://mirror.sjtu.edu.cn/pypi/web/simple"+ ) from emodel = AutoModel(model=model_dir)model_dir = model.export(type="onnx", quantize=quantize, **kwargs)@@ -284,14 +290,18 @@ class ContextualParaformer(Paraformer):if not Path(model_dir).exists():try:from modelscope.hub.snapshot_download import snapshot_download- except:- raise "You are exporting model from modelscope, please install modelscope and try it again. To install modelscope, you could:\n" "\npip3 install -U modelscope\n" "For the users in China, you could install with the command:\n" "\npip3 install -U modelscope -i https://mirror.sjtu.edu.cn/pypi/web/simple"+ except ImportError as e:+ raise ImportError(+ "You are exporting model from modelscope, please install modelscope and try it again. To install modelscope, you could:\n" "\npip3 install -U modelscope\n" "For the users in China, you could install with the command:\n" "\npip3 install -U modelscope -i https://mirror.sjtu.edu.cn/pypi/web/simple"+ ) from etry:model_dir = snapshot_download(model_dir, cache_dir=cache_dir)- except:- raise "model_dir must be model_name in modelscope or local path downloaded from modelscope, but is {}".format(- model_dir- )+ except Exception as e:+ raise RuntimeError(+ "model_dir must be model_name in modelscope or local path downloaded from modelscope, but is {}".format(+ model_dir+ )+ ) from eif quantize:model_bb_file = os.path.join(model_dir, "model_quant.onnx")@@ -304,8 +314,10 @@ class ContextualParaformer(Paraformer):print(".onnx does not exist, begin to export onnx")try:from funasr import AutoModel- except:- raise "You are exporting onnx, please install funasr and try it again. To install funasr, you could:\n" "\npip3 install -U funasr\n" "For the users in China, you could install with the command:\n" "\npip3 install -U funasr -i https://mirror.sjtu.edu.cn/pypi/web/simple"+ except ImportError as e:+ raise ImportError(+ "You are exporting onnx, please install funasr and try it again. To install funasr, you could:\n" "\npip3 install -U funasr\n" "For the users in China, you could install with the command:\n" "\npip3 install -U funasr -i https://mirror.sjtu.edu.cn/pypi/web/simple"+ ) from emodel = AutoModel(model=model_dir)model_dir = model.export(type="onnx", quantize=quantize, **kwargs)diff --git a/runtime/python/onnxruntime/funasr_onnx/paraformer_online_bin.py b/runtime/python/onnxruntime/funasr_onnx/paraformer_online_bin.pyindex 3f63ea020..81b991d4b 100644--- a/runtime/python/onnxruntime/funasr_onnx/paraformer_online_bin.py+++ b/runtime/python/onnxruntime/funasr_onnx/paraformer_online_bin.py@@ -39,14 +39,18 @@ class Paraformer:if not Path(model_dir).exists():try:from modelscope.hub.snapshot_download import snapshot_download- except:- raise "You are exporting model from modelscope, please install modelscope and try it again. To install modelscope, you could:\n" "\npip3 install -U modelscope\n" "For the users in China, you could install with the command:\n" "\npip3 install -U modelscope -i https://mirror.sjtu.edu.cn/pypi/web/simple"+ except ImportError as e:+ raise ImportError(+ "You are exporting model from modelscope, please install modelscope and try it again. To install modelscope, you could:\n" "\npip3 install -U modelscope\n" "For the users in China, you could install with the command:\n" "\npip3 install -U modelscope -i https://mirror.sjtu.edu.cn/pypi/web/simple"+ ) from etry:model_dir = snapshot_download(model_dir, cache_dir=cache_dir)- except:- raise "model_dir must be model_name in modelscope or local path downloaded from modelscope, but is {}".format(- model_dir- )+ except Exception as e:+ raise RuntimeError(+ "model_dir must be model_name in modelscope or local path downloaded from modelscope, but is {}".format(+ model_dir+ )+ ) from eencoder_model_file = os.path.join(model_dir, "model.onnx")decoder_model_file = os.path.join(model_dir, "decoder.onnx")@@ -57,8 +61,10 @@ class Paraformer:print(".onnx does not exist, begin to export onnx")try:from funasr import AutoModel- except:- raise "You are exporting onnx, please install funasr and try it again. To install funasr, you could:\n" "\npip3 install -U funasr\n" "For the users in China, you could install with the command:\n" "\npip3 install -U funasr -i https://mirror.sjtu.edu.cn/pypi/web/simple"+ except ImportError as e:+ raise ImportError(+ "You are exporting onnx, please install funasr and try it again. To install funasr, you could:\n" "\npip3 install -U funasr\n" "For the users in China, you could install with the command:\n" "\npip3 install -U funasr -i https://mirror.sjtu.edu.cn/pypi/web/simple"+ ) from emodel = AutoModel(model=model_dir)model_dir = model.export(type="onnx", quantize=quantize, **kwargs)diff --git a/runtime/python/onnxruntime/funasr_onnx/punc_bin.py b/runtime/python/onnxruntime/funasr_onnx/punc_bin.pyindex ba55186d0..7fa62d44d 100644--- a/runtime/python/onnxruntime/funasr_onnx/punc_bin.py+++ b/runtime/python/onnxruntime/funasr_onnx/punc_bin.py@@ -39,14 +39,18 @@ class CT_Transformer:if not Path(model_dir).exists():try:from modelscope.hub.snapshot_download import snapshot_download- except:- raise "You are exporting model from modelscope, please install modelscope and try it again. To install modelscope, you could:\n" "\npip3 install -U modelscope\n" "For the users in China, you could install with the command:\n" "\npip3 install -U modelscope -i https://mirror.sjtu.edu.cn/pypi/web/simple"+ except ImportError as e:+ raise ImportError(+ "You are exporting model from modelscope, please install modelscope and try it again. To install modelscope, you could:\n" "\npip3 install -U modelscope\n" "For the users in China, you could install with the command:\n" "\npip3 install -U modelscope -i https://mirror.sjtu.edu.cn/pypi/web/simple"+ ) from etry:model_dir = snapshot_download(model_dir, cache_dir=cache_dir)- except:- raise "model_dir must be model_name in modelscope or local path downloaded from modelscope, but is {}".format(- model_dir- )+ except Exception as e:+ raise RuntimeError(+ "model_dir must be model_name in modelscope or local path downloaded from modelscope, but is {}".format(+ model_dir+ )+ ) from emodel_file = os.path.join(model_dir, "model.onnx")if quantize:@@ -55,8 +59,10 @@ class CT_Transformer:print(".onnx does not exist, begin to export onnx")try:from funasr import AutoModel- except:- raise "You are exporting onnx, please install funasr and try it again. To install funasr, you could:\n" "\npip3 install -U funasr\n" "For the users in China, you could install with the command:\n" "\npip3 install -U funasr -i https://mirror.sjtu.edu.cn/pypi/web/simple"+ except ImportError as e:+ raise ImportError(+ "You are exporting onnx, please install funasr and try it again. To install funasr, you could:\n" "\npip3 install -U funasr\n" "For the users in China, you could install with the command:\n" "\npip3 install -U funasr -i https://mirror.sjtu.edu.cn/pypi/web/simple"+ ) from emodel = AutoModel(model=model_dir)model_dir = model.export(type="onnx", quantize=quantize, **kwargs)diff --git a/runtime/python/onnxruntime/funasr_onnx/sensevoice_bin.py b/runtime/python/onnxruntime/funasr_onnx/sensevoice_bin.pyindex 8f8ae57df..0bc265ebb 100644--- a/runtime/python/onnxruntime/funasr_onnx/sensevoice_bin.py+++ b/runtime/python/onnxruntime/funasr_onnx/sensevoice_bin.py@@ -46,14 +46,18 @@ class SenseVoiceSmall:if not Path(model_dir).exists():try:from modelscope.hub.snapshot_download import snapshot_download- except:- raise "You are exporting model from modelscope, please install modelscope and try it again. To install modelscope, you could:\n" "\npip3 install -U modelscope\n" "For the users in China, you could install with the command:\n" "\npip3 install -U modelscope -i https://mirror.sjtu.edu.cn/pypi/web/simple"+ except ImportError as e:+ raise ImportError(+ "You are exporting model from modelscope, please install modelscope and try it again. To install modelscope, you could:\n" "\npip3 install -U modelscope\n" "For the users in China, you could install with the command:\n" "\npip3 install -U modelscope -i https://mirror.sjtu.edu.cn/pypi/web/simple"+ ) from etry:model_dir = snapshot_download(model_dir, cache_dir=cache_dir)- except:- raise "model_dir must be model_name in modelscope or local path downloaded from modelscope, but is {}".format(- model_dir- )+ except Exception as e:+ raise RuntimeError(+ "model_dir must be model_name in modelscope or local path downloaded from modelscope, but is {}".format(+ model_dir+ )+ ) from emodel_file = os.path.join(model_dir, "model.onnx")if quantize:@@ -62,8 +66,10 @@ class SenseVoiceSmall:print(".onnx does not exist, begin to export onnx")try:from funasr import AutoModel- except:- raise "You are exporting onnx, please install funasr and try it again. To install funasr, you could:\n" "\npip3 install -U funasr\n" "For the users in China, you could install with the command:\n" "\npip3 install -U funasr -i https://mirror.sjtu.edu.cn/pypi/web/simple"+ except ImportError as e:+ raise ImportError(+ "You are exporting onnx, please install funasr and try it again. To install funasr, you could:\n" "\npip3 install -U funasr\n" "For the users in China, you could install with the command:\n" "\npip3 install -U funasr -i https://mirror.sjtu.edu.cn/pypi/web/simple"+ ) from emodel = AutoModel(model=model_dir)model_dir = model.export(type="onnx", quantize=quantize, **kwargs)diff --git a/runtime/python/onnxruntime/funasr_onnx/vad_bin.py b/runtime/python/onnxruntime/funasr_onnx/vad_bin.pyindex f784f2637..1c98b7f92 100644--- a/runtime/python/onnxruntime/funasr_onnx/vad_bin.py+++ b/runtime/python/onnxruntime/funasr_onnx/vad_bin.py@@ -39,14 +39,18 @@ class Fsmn_vad:if not Path(model_dir).exists():try:from modelscope.hub.snapshot_download import snapshot_download- except:- raise "You are exporting model from modelscope, please install modelscope and try it again. To install modelscope, you could:\n" "\npip3 install -U modelscope\n" "For the users in China, you could install with the command:\n" "\npip3 install -U modelscope -i https://mirror.sjtu.edu.cn/pypi/web/simple"+ except ImportError as e:+ raise ImportError(+ "You are exporting model from modelscope, please install modelscope and try it again. To install modelscope, you could:\n" "\npip3 install -U modelscope\n" "For the users in China, you could install with the command:\n" "\npip3 install -U modelscope -i https://mirror.sjtu.edu.cn/pypi/web/simple"+ ) from etry:model_dir = snapshot_download(model_dir, cache_dir=cache_dir)- except:- raise "model_dir must be model_name in modelscope or local path downloaded from modelscope, but is {}".format(- model_dir- )+ except Exception as e:+ raise RuntimeError(+ "model_dir must be model_name in modelscope or local path downloaded from modelscope, but is {}".format(+ model_dir+ )+ ) from emodel_file = os.path.join(model_dir, "model.onnx")if quantize:@@ -55,8 +59,10 @@ class Fsmn_vad:print(".onnx does not exist, begin to export onnx")try:from funasr import AutoModel- except:- raise "You are exporting onnx, please install funasr and try it again. To install funasr, you could:\n" "\npip3 install -U funasr\n" "For the users in China, you could install with the command:\n" "\npip3 install -U funasr -i https://mirror.sjtu.edu.cn/pypi/web/simple"+ except ImportError as e:+ raise ImportError(+ "You are exporting onnx, please install funasr and try it again. To install funasr, you could:\n" "\npip3 install -U funasr\n" "For the users in China, you could install with the command:\n" "\npip3 install -U funasr -i https://mirror.sjtu.edu.cn/pypi/web/simple"+ ) from emodel = AutoModel(model=model_dir)model_dir = model.export(type="onnx", quantize=quantize, **kwargs)@@ -224,14 +230,18 @@ class Fsmn_vad_online:if not Path(model_dir).exists():try:from modelscope.hub.snapshot_download import snapshot_download- except:- raise "You are exporting model from modelscope, please install modelscope and try it again. To install modelscope, you could:\n" "\npip3 install -U modelscope\n" "For the users in China, you could install with the command:\n" "\npip3 install -U modelscope -i https://mirror.sjtu.edu.cn/pypi/web/simple"+ except ImportError as e:+ raise ImportError(+ "You are exporting model from modelscope, please install modelscope and try it again. To install modelscope, you could:\n" "\npip3 install -U modelscope\n" "For the users in China, you could install with the command:\n" "\npip3 install -U modelscope -i https://mirror.sjtu.edu.cn/pypi/web/simple"+ ) from etry:model_dir = snapshot_download(model_dir, cache_dir=cache_dir)- except:- raise "model_dir must be model_name in modelscope or local path downloaded from modelscope, but is {}".format(- model_dir- )+ except Exception as e:+ raise RuntimeError(+ "model_dir must be model_name in modelscope or local path downloaded from modelscope, but is {}".format(+ model_dir+ )+ ) from emodel_file = os.path.join(model_dir, "model.onnx")if quantize:@@ -240,8 +250,10 @@ class Fsmn_vad_online:print(".onnx does not exist, begin to export onnx")try:from funasr import AutoModel- except:- raise "You are exporting onnx, please install funasr and try it again. To install funasr, you could:\n" "\npip3 install -U funasr\n" "For the users in China, you could install with the command:\n" "\npip3 install -U funasr -i https://mirror.sjtu.edu.cn/pypi/web/simple"+ except ImportError as e:+ raise ImportError(+ "You are exporting onnx, please install funasr and try it again. To install funasr, you could:\n" "\npip3 install -U funasr\n" "For the users in China, you could install with the command:\n" "\npip3 install -U funasr -i https://mirror.sjtu.edu.cn/pypi/web/simple"+ ) from emodel = AutoModel(model=model_dir)model_dir = model.export(type="onnx", quantize=quantize, **kwargs)