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
fix: replace mutable default argument cache: dict = {} with None
游雁5dfdce60091d
diff --git a/funasr/frontends/wav_frontend.py b/funasr/frontends/wav_frontend.pyindex da23f9c7c..c733d6663 100644--- a/funasr/frontends/wav_frontend.py+++ b/funasr/frontends/wav_frontend.py@@ -318,9 +318,11 @@ class WavFrontendOnline(nn.Module):self,input: torch.Tensor,input_lengths: torch.Tensor,- cache: dict = {},+ cache: dict = None,**kwargs,) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:+ if cache is None:+ cache = {}batch_size = input.size(0)input = torch.cat((cache["input_cache"], input), dim=1)@@ -378,9 +380,11 @@ class WavFrontendOnline(nn.Module):input: torch.Tensor,input_lengths: torch.Tensor,is_final: bool = False,- cache: dict = {},+ cache: dict = None,**kwargs,):+ if cache is None:+ cache = {}batch_size = input.size(0)feats = []feats_lens = []@@ -487,7 +491,9 @@ class WavFrontendOnline(nn.Module):# self.init_cache(cache)return feats, feats_lengths- def init_cache(self, cache: dict = {}):+ def init_cache(self, cache: dict = None):+ if cache is None:+ cache = {}cache["reserve_waveforms"] = torch.empty(0)cache["input_cache"] = torch.empty(0)cache["lfr_splice_cache"] = []diff --git a/funasr/models/ct_transformer_streaming/model.py b/funasr/models/ct_transformer_streaming/model.pyindex a1afc7aea..4394821d5 100644--- a/funasr/models/ct_transformer_streaming/model.py+++ b/funasr/models/ct_transformer_streaming/model.py@@ -65,9 +65,11 @@ class CTTransformerStreaming(CTTransformer):key: list = None,tokenizer=None,frontend=None,- cache: dict = {},+ cache: dict = None,**kwargs,):+ if cache is None:+ cache = {}assert len(data_in) == 1if len(cache) == 0:diff --git a/funasr/models/fsmn_vad_streaming/model.py b/funasr/models/fsmn_vad_streaming/model.pyindex 1657631ab..8e43b559d 100644--- a/funasr/models/fsmn_vad_streaming/model.py+++ b/funasr/models/fsmn_vad_streaming/model.py@@ -203,8 +203,10 @@ class WindowDetector(object):return int(self.win_size_frame)def DetectOneFrame(- self, frameState: FrameState, frame_count: int, cache: dict = {}+ self, frameState: FrameState, frame_count: int, cache: dict = None) -> AudioChangeState:+ if cache is None:+ cache = {}cur_frame_state = FrameState.kFrameStateSilif frameState == FrameState.kFrameStateSpeech:cur_frame_state = 1@@ -300,7 +302,9 @@ class FsmnVADStreaming(nn.Module):self.encoder = encoderself.encoder_conf = encoder_conf- def ResetDetection(self, cache: dict = {}):+ def ResetDetection(self, cache: dict = None):+ if cache is None:+ cache = {}cache["stats"].continous_silence_frame_count = 0cache["stats"].latest_confirmed_speech_frame = 0cache["stats"].lastest_confirmed_silence_frame = -1@@ -323,7 +327,9 @@ class FsmnVADStreaming(nn.Module):cache["stats"].decibel = cache["stats"].decibel[real_drop_frames:]cache["stats"].scores = cache["stats"].scores[:, real_drop_frames:, :]- def ComputeDecibel(self, cache: dict = {}) -> None:+ def ComputeDecibel(self, cache: dict = None) -> None:+ if cache is None:+ cache = {}frame_sample_length = int(self.vad_opts.frame_length_ms * self.vad_opts.sample_rate / 1000)frame_shift_length = int(self.vad_opts.frame_in_ms * self.vad_opts.sample_rate / 1000)if cache["stats"].data_buf_all is None:@@ -347,7 +353,9 @@ class FsmnVADStreaming(nn.Module):cache["stats"].decibel.extend(decibel_numpy)- def ComputeScores(self, feats: torch.Tensor, cache: dict = {}) -> None:+ def ComputeScores(self, feats: torch.Tensor, cache: dict = None) -> None:+ if cache is None:+ cache = {}scores = self.encoder(feats, cache=cache["encoder"]) # return B * T * Dassert (scores.shape[1] == feats.shape[1]@@ -359,11 +367,13 @@ class FsmnVADStreaming(nn.Module):else:cache["stats"].scores = torch.cat((cache["stats"].scores, scores), dim=1)- def PopDataBufTillFrame(self, frame_idx: int, cache: dict = {}) -> None: # need check again+ def PopDataBufTillFrame(self, frame_idx: int, cache: dict = None) -> None: # need check againwhile cache["stats"].data_buf_start_frame < frame_idx:if len(cache["stats"].data_buf) >= int(self.vad_opts.frame_in_ms * self.vad_opts.sample_rate / 1000):+ if cache is None:+ cache = {}cache["stats"].data_buf_start_frame += 1cache["stats"].data_buf = cache["stats"].data_buf_all[(cache["stats"].data_buf_start_frame - cache["stats"].last_drop_frames)@@ -377,8 +387,10 @@ class FsmnVADStreaming(nn.Module):first_frm_is_start_point: bool,last_frm_is_end_point: bool,end_point_is_sent_end: bool,- cache: dict = {},+ cache: dict = None,) -> None:+ if cache is None:+ cache = {}self.PopDataBufTillFrame(start_frm, cache=cache)expected_sample_number = int(frm_cnt * self.vad_opts.sample_rate * self.vad_opts.frame_in_ms / 1000@@ -428,7 +440,9 @@ class FsmnVADStreaming(nn.Module):if last_frm_is_end_point:cur_seg.contain_seg_end_point = True- def OnSilenceDetected(self, valid_frame: int, cache: dict = {}):+ def OnSilenceDetected(self, valid_frame: int, cache: dict = None):+ if cache is None:+ cache = {}cache["stats"].lastest_confirmed_silence_frame = valid_frameif cache["stats"].vad_state_machine == VadStateMachine.kVadInStateStartPointNotDetected:self.PopDataBufTillFrame(valid_frame, cache=cache)@@ -436,11 +450,15 @@ class FsmnVADStreaming(nn.Module):# silence_detected_callback_# pass- def OnVoiceDetected(self, valid_frame: int, cache: dict = {}) -> None:+ def OnVoiceDetected(self, valid_frame: int, cache: dict = None) -> None:+ if cache is None:+ cache = {}cache["stats"].latest_confirmed_speech_frame = valid_frameself.PopDataToOutputBuf(valid_frame, 1, False, False, False, cache=cache)- def OnVoiceStart(self, start_frame: int, fake_result: bool = False, cache: dict = {}) -> None:+ def OnVoiceStart(self, start_frame: int, fake_result: bool = False, cache: dict = None) -> None:+ if cache is None:+ cache = {}if self.vad_opts.do_start_point_detection:passif cache["stats"].confirmed_start_frame != -1:@@ -457,8 +475,10 @@ class FsmnVADStreaming(nn.Module):)def OnVoiceEnd(- self, end_frame: int, fake_result: bool, is_last_frame: bool, cache: dict = {}+ self, end_frame: int, fake_result: bool, is_last_frame: bool, cache: dict = None) -> None:+ if cache is None:+ cache = {}for t in range(cache["stats"].latest_confirmed_speech_frame + 1, end_frame):self.OnVoiceDetected(t, cache=cache)if self.vad_opts.do_end_point_detection:@@ -475,22 +495,30 @@ class FsmnVADStreaming(nn.Module):cache["stats"].number_end_time_detected += 1def MaybeOnVoiceEndIfLastFrame(- self, is_final_frame: bool, cur_frm_idx: int, cache: dict = {}+ self, is_final_frame: bool, cur_frm_idx: int, cache: dict = None) -> None:+ if cache is None:+ cache = {}if is_final_frame:self.OnVoiceEnd(cur_frm_idx, False, True, cache=cache)cache["stats"].vad_state_machine = VadStateMachine.kVadInStateEndPointDetected- def GetLatency(self, cache: dict = {}) -> int:+ def GetLatency(self, cache: dict = None) -> int:+ if cache is None:+ cache = {}return int(self.LatencyFrmNumAtStartPoint(cache=cache) * self.vad_opts.frame_in_ms)- def LatencyFrmNumAtStartPoint(self, cache: dict = {}) -> int:+ def LatencyFrmNumAtStartPoint(self, cache: dict = None) -> int:+ if cache is None:+ cache = {}vad_latency = cache["windows_detector"].GetWinSize()if self.vad_opts.do_extend:vad_latency += int(self.vad_opts.lookback_time_start_point / self.vad_opts.frame_in_ms)return vad_latency- def GetFrameState(self, t: int, cache: dict = {}):+ def GetFrameState(self, t: int, cache: dict = None):+ if cache is None:+ cache = {}frame_state = FrameState.kFrameStateInvalidif t >= len(cache["stats"].decibel):return FrameState.kFrameStateSil@@ -551,10 +579,12 @@ class FsmnVADStreaming(nn.Module):self,feats: torch.Tensor,waveform: torch.tensor,- cache: dict = {},+ cache: dict = None,is_final: bool = False,**kwargs,):+ if cache is None:+ cache = {}# if len(cache) == 0:# self.AllResetDetection()# self.waveform = waveform # compute decibel for each frame@@ -619,7 +649,9 @@ class FsmnVADStreaming(nn.Module):# self.AllResetDetection()return segments- def init_cache(self, cache: dict = {}, **kwargs):+ def init_cache(self, cache: dict = None, **kwargs):+ if cache is None:+ cache = {}cache["frontend"] = {}cache["prev_samples"] = torch.empty(0)@@ -757,7 +789,9 @@ class FsmnVADStreaming(nn.Module):models = export_rebuild_model(model=self, **kwargs)return models- def DetectCommonFrames(self, cache: dict = {}) -> int:+ def DetectCommonFrames(self, cache: dict = None) -> int:+ if cache is None:+ cache = {}if cache["stats"].vad_state_machine == VadStateMachine.kVadInStateEndPointDetected:return 0for i in range(self.vad_opts.nn_eval_block_size - 1, -1, -1):@@ -769,7 +803,9 @@ class FsmnVADStreaming(nn.Module):return 0- def DetectLastFrames(self, cache: dict = {}) -> int:+ def DetectLastFrames(self, cache: dict = None) -> int:+ if cache is None:+ cache = {}if cache["stats"].vad_state_machine == VadStateMachine.kVadInStateEndPointDetected:return 0for i in range(self.vad_opts.nn_eval_block_size - 1, -1, -1):@@ -785,8 +821,10 @@ class FsmnVADStreaming(nn.Module):return 0def DetectOneFrame(- self, cur_frm_state: FrameState, cur_frm_idx: int, is_final_frame: bool, cache: dict = {}+ self, cur_frm_state: FrameState, cur_frm_idx: int, is_final_frame: bool, cache: dict = None) -> None:+ if cache is None:+ cache = {}tmp_cur_frm_state = FrameState.kFrameStateInvalidif cur_frm_state == FrameState.kFrameStateSpeech:if math.fabs(1.0) > self.vad_opts.fe_prior_thres:diff --git a/funasr/models/paraformer_streaming/model.py b/funasr/models/paraformer_streaming/model.pyindex cbf666992..9b9e609ab 100644--- a/funasr/models/paraformer_streaming/model.py+++ b/funasr/models/paraformer_streaming/model.py@@ -155,6 +155,8 @@ class ParaformerStreaming(Paraformer):cache: dict = None,**kwargs,) -> Tuple[torch.Tensor, torch.Tensor]:+ if cache is None:+ cache = {}"""Frontend + Encoder. Note that this method is used by asr_inference.pyArgs:speech: (Batch, Length, ...)@@ -432,7 +434,9 @@ class ParaformerStreaming(Paraformer):decoder_out = torch.log_softmax(decoder_out, dim=-1)return decoder_out, ys_pad_lens- def init_cache(self, cache: dict = {}, **kwargs):+ def init_cache(self, cache: dict = None, **kwargs):+ if cache is None:+ cache = {}chunk_size = kwargs.get("chunk_size", [0, 10, 5])encoder_chunk_look_back = kwargs.get("encoder_chunk_look_back", 0)decoder_chunk_look_back = kwargs.get("decoder_chunk_look_back", 0)@@ -560,9 +564,11 @@ class ParaformerStreaming(Paraformer):key: list = None,tokenizer=None,frontend=None,- cache: dict = {},+ cache: dict = None,**kwargs,):+ if cache is None:+ cache = {}# init beamsearchis_use_ctc = kwargs.get("decoding_ctc_weight", 0.0) > 0.00001 and self.ctc != Nonediff --git a/funasr/models/sanm/encoder.py b/funasr/models/sanm/encoder.pyindex 0d39ca742..735595971 100644--- a/funasr/models/sanm/encoder.py+++ b/funasr/models/sanm/encoder.py@@ -429,7 +429,9 @@ class SANMEncoder(nn.Module):return (xs_pad, intermediate_outs), olens, Nonereturn xs_pad, olens, None- def _add_overlap_chunk(self, feats: np.ndarray, cache: dict = {}):+ def _add_overlap_chunk(self, feats: np.ndarray, cache: dict = None):+ if cache is None:+ cache = {}if len(cache) == 0:return featscache["feats"] = to_device(cache["feats"], device=feats.device)@@ -444,6 +446,8 @@ class SANMEncoder(nn.Module):cache: dict = None,ctc: CTC = None,):+ if cache is None:+ cache = {}xs_pad *= self.output_size() ** 0.5if self.embed is None:xs_pad = xs_paddiff --git a/funasr/models/sanm_kws_streaming/model.py b/funasr/models/sanm_kws_streaming/model.pyindex c459e7c42..184965955 100644--- a/funasr/models/sanm_kws_streaming/model.py+++ b/funasr/models/sanm_kws_streaming/model.py@@ -112,6 +112,8 @@ class SanmKWSStreaming(SanmKWS):cache: dict = None,**kwargs,) -> Tuple[torch.Tensor, torch.Tensor]:+ if cache is None:+ cache = {}"""Frontend + Encoder. Note that this method is used by asr_inference.pyArgs:speech: (Batch, Length, ...)@@ -137,7 +139,9 @@ class SanmKWSStreaming(SanmKWS):return encoder_out, torch.tensor([encoder_out.size(1)])- def init_cache(self, cache: dict = {}, **kwargs):+ def init_cache(self, cache: dict = None, **kwargs):+ if cache is None:+ cache = {}chunk_size = kwargs.get("chunk_size", [0, 10, 5])encoder_chunk_look_back = kwargs.get("encoder_chunk_look_back", 0)decoder_chunk_look_back = kwargs.get("decoder_chunk_look_back", 0)@@ -252,9 +256,11 @@ class SanmKWSStreaming(SanmKWS):key: list = None,tokenizer=None,frontend=None,- cache: dict = {},+ cache: dict = None,**kwargs,):+ if cache is None:+ cache = {}keywords = kwargs.get("keywords")from funasr.utils.kws_utils import KwsCtcPrefixDecoderself.kws_decoder = KwsCtcPrefixDecoder(diff --git a/funasr/models/scama/beam_search.py b/funasr/models/scama/beam_search.pyindex 259f483b2..bf0bb5cf7 100644--- a/funasr/models/scama/beam_search.py+++ b/funasr/models/scama/beam_search.py@@ -621,8 +621,10 @@ class BeamSearchScamaStreaming(torch.nn.Module):x: torch.Tensor,x_mask: torch.Tensor = None,pre_acoustic_embeds: torch.Tensor = None,- cache: dict = {},+ cache: dict = None,) -> Tuple[Dict[str, torch.Tensor], Dict[str, Any]]:+ if cache is None:+ cache = {}"""Score new hypothesis by `self.full_scorers`.Args:@@ -762,8 +764,10 @@ class BeamSearchScamaStreaming(torch.nn.Module):x: torch.Tensor,x_mask: torch.Tensor = None,pre_acoustic_embeds: torch.Tensor = None,- cache: dict = {},+ cache: dict = None,) -> List[Hypothesis]:+ if cache is None:+ cache = {}"""Search new tokens for running hypotheses and encoded speech x.Args:@@ -825,8 +829,10 @@ class BeamSearchScamaStreaming(torch.nn.Module):minlenratio: float = 0.0,maxlen: int = None,minlen: int = 0,- cache: dict = {},+ cache: dict = None,) -> List[Hypothesis]:+ if cache is None:+ cache = {}"""Perform beam search.Args:diff --git a/funasr/models/scama/encoder.py b/funasr/models/scama/encoder.pyindex e1fe9242e..0cf48f526 100644--- a/funasr/models/scama/encoder.py+++ b/funasr/models/scama/encoder.py@@ -445,7 +445,9 @@ class SANMEncoderChunkOpt(nn.Module):return (xs_pad, intermediate_outs), olens, Nonereturn xs_pad, olens, None- def _add_overlap_chunk(self, feats: np.ndarray, cache: dict = {}):+ def _add_overlap_chunk(self, feats: np.ndarray, cache: dict = None):+ if cache is None:+ cache = {}if len(cache) == 0:return featscache["feats"] = to_device(cache["feats"], device=feats.device)@@ -460,6 +462,8 @@ class SANMEncoderChunkOpt(nn.Module):cache: dict = None,**kwargs,):+ if cache is None:+ cache = {}is_final = kwargs.get("is_final", False)xs_pad *= self.output_size() ** 0.5if self.embed is None:diff --git a/funasr/models/scama/model.py b/funasr/models/scama/model.pyindex 22a48bccc..bb207c88e 100644--- a/funasr/models/scama/model.py+++ b/funasr/models/scama/model.py@@ -270,6 +270,8 @@ class SCAMA(nn.Module):cache: dict = None,**kwargs,) -> Tuple[torch.Tensor, torch.Tensor]:+ if cache is None:+ cache = {}"""Frontend + Encoder. Note that this method is used by asr_inference.pyArgs:speech: (Batch, Length, ...)@@ -602,7 +604,9 @@ class SCAMA(nn.Module):return results- def init_cache(self, cache: dict = {}, **kwargs):+ def init_cache(self, cache: dict = None, **kwargs):+ if cache is None:+ cache = {}device = kwargs.get("device", "cuda")chunk_size = kwargs.get("chunk_size", [0, 10, 5])@@ -648,9 +652,11 @@ class SCAMA(nn.Module):key: list = None,tokenizer=None,frontend=None,- cache: dict = {},+ cache: dict = None,**kwargs,):+ if cache is None:+ cache = {}# init beamsearchis_use_ctc = kwargs.get("decoding_ctc_weight", 0.0) > 0.00001 and self.ctc != None