From 4999dc7632f31a353674af4c120ce36a3e1a25f2 Mon Sep 17 00:00:00 2001 From: Leonard Li Date: Sat, 22 Aug 2026 12:35:50 -0400 Subject: [PATCH] Raise real exceptions instead of strings in the runtime Python bindings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `raise "some message"` is invalid in Python 3 — a str is not an exception — so the interpreter discards the message and raises TypeError: exceptions must derive from BaseException instead. Paired with a bare `except:`, the original cause is lost too, and the user is told to install something they may already have. Concretely: funasr was installed, but importing it failed on a missing torchaudio. The message said 'please install funasr', the traceback said TypeError, and the actual ModuleNotFoundError was nowhere to be seen. This converts all 30 occurrences under runtime/python to real exceptions: - guarded imports -> except ImportError as e: raise ImportError(...) from e - everything else -> except Exception as e: raise RuntimeError(...) from e `from e` keeps the original traceback, so a missing transitive dependency stays visible. Narrowing the bare `except:` also stops unrelated failures from being reported as a missing package — the model_dir branch in particular fired for any failure, so a mistyped path also surfaced as TypeError. Messages are unchanged. No behaviour changes beyond the exception type. --- .../libtorch/funasr_torch/paraformer_bin.py | 44 ++++++++++++------- .../libtorch/funasr_torch/sensevoice_bin.py | 22 ++++++---- .../onnxruntime/funasr_onnx/paraformer_bin.py | 44 ++++++++++++------- .../funasr_onnx/paraformer_online_bin.py | 22 ++++++---- .../onnxruntime/funasr_onnx/punc_bin.py | 22 ++++++---- .../onnxruntime/funasr_onnx/sensevoice_bin.py | 22 ++++++---- .../python/onnxruntime/funasr_onnx/vad_bin.py | 44 ++++++++++++------- 7 files changed, 140 insertions(+), 80 deletions(-) diff --git a/runtime/python/libtorch/funasr_torch/paraformer_bin.py b/runtime/python/libtorch/funasr_torch/paraformer_bin.py index 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 @@ def __init__( 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 e try: 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 e model_file = os.path.join(model_dir, "model.torchscript") if quantize: @@ -53,8 +57,10 @@ def __init__( 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 e model = AutoModel(model=model_dir) model_dir = model.export(type="torchscript", quantize=quantize, **kwargs) @@ -258,14 +264,18 @@ def __init__( 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 e try: 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 e if quantize: model_bb_file = os.path.join(model_dir, "model_bb_quant.torchscript") @@ -278,8 +288,10 @@ def __init__( 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 e model = 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.py index 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 @@ def __init__( 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 e try: 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 e model_file = os.path.join(model_dir, "model.torchscript") if quantize: @@ -59,8 +63,10 @@ def __init__( 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 e model = 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.py index 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 @@ def __init__( 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 e try: 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 e model_file = os.path.join(model_dir, "model.onnx") if quantize: @@ -65,8 +69,10 @@ def __init__( 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 e model = AutoModel(model=model_dir) model_dir = model.export(type="onnx", quantize=quantize, **kwargs) @@ -284,14 +290,18 @@ def __init__( 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 e try: 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 e if quantize: model_bb_file = os.path.join(model_dir, "model_quant.onnx") @@ -304,8 +314,10 @@ def __init__( 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 e model = 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.py index 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 @@ def __init__( 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 e try: 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 e encoder_model_file = os.path.join(model_dir, "model.onnx") decoder_model_file = os.path.join(model_dir, "decoder.onnx") @@ -57,8 +61,10 @@ def __init__( 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 e model = 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.py index 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 @@ def __init__( 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 e try: 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 e model_file = os.path.join(model_dir, "model.onnx") if quantize: @@ -55,8 +59,10 @@ def __init__( 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 e model = 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.py index 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 @@ def __init__( 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 e try: 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 e model_file = os.path.join(model_dir, "model.onnx") if quantize: @@ -62,8 +66,10 @@ def __init__( 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 e model = 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.py index 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 @@ def __init__( 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 e try: 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 e model_file = os.path.join(model_dir, "model.onnx") if quantize: @@ -55,8 +59,10 @@ def __init__( 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 e model = AutoModel(model=model_dir) model_dir = model.export(type="onnx", quantize=quantize, **kwargs) @@ -224,14 +230,18 @@ def __init__( 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 e try: 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 e model_file = os.path.join(model_dir, "model.onnx") if quantize: @@ -240,8 +250,10 @@ def __init__( 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 e model = AutoModel(model=model_dir) model_dir = model.export(type="onnx", quantize=quantize, **kwargs)