
    -cJj                         d dl mZ d dlmZmZ d dlmZmZmZm	Z	m
Z
mZmZmZmZmZmZmZmZmZmZmZmZ d dlmZ d dlmZmZmZmZmZmZmZm Z  de!fdZ" G d d	e#      Zy
)    )Path)ListOptional)FeatureExtractorConfigHomophoneReplacerConfigOfflineCanaryModelConfig"OfflineCohereTranscribeModelConfigOfflineFunASRNanoModelConfigOfflineQwen3ASRModelConfig#OfflineOmnilingualAsrCtcModelConfigOfflineMedAsrCtcModelConfigOfflineFireRedAsrCtcModelConfigOfflineCtcFstDecoderConfigOfflineDolphinModelConfigOfflineFireRedAsrModelConfigOfflineLMConfigOfflineModelConfigOfflineMoonshineModelConfigOfflineNemoEncDecCtcModelConfigOfflineParaformerModelConfig)OfflineRecognizer)OfflineRecognizerConfigOfflineSenseVoiceModelConfigOfflineStreamOfflineTdnnModelConfigOfflineTransducerModelConfigOfflineWenetCtcModelConfigOfflineWhisperModelConfigOfflineZipformerCtcModelConfigfc                 J    t        |       j                         s
J |  d       y )Nz does not exist)r   is_file)r    s    h/Users/ahmed/devFolder/claude-voice/.venv/lib/python3.12/site-packages/sherpa_onnx/offline_recognizer.py_assert_file_existsr$   &   s"    7??3?33    c            8          e Zd ZdZe	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dSdededededededed	ed
ededededededededededededededededededef6d       Z	e	 	 	 	 	 	 	 	 	 	 	 	 	 dTdededededed
edededed edededededefd!       Z
e	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dUd"ed#ed$ed%edededed
ededed&ed'ed(ed)ed*ed+eded,ed-ef&d.       Ze	 	 	 	 	 	 	 	 	 	 	 	 dVd/ededed%edededed
ededed0ed(ed)ed*ed+ed-ef d1       Ze	 	 	 	 	 	 	 	 	 	 	 dWd2ededededed
edededededededefd3       Ze	 	 	 	 	 	 	 	 	 	 	 dXdededededed
edededededededefd4       Ze	 	 	 	 	 	 	 	 	 	 	 dWdededededed
edededededededefd5       Ze	 	 	 	 dYdededed
ededefd6       Ze	 	 	 	 dYdededed
ededefd7       Ze	 	 	 	 dYdededed
ededefd8       Ze	 	 	 	 	 	 	 	 	 	 	 dWdededededed
edededededededefd9       Ze	 	 	 	 	 	 	 	 	 	 	 dWdededededed
edededededededefd:       Ze	 	 	 	 	 	 	 	 	 	 	 	 	 dZdededed;ed<edededed
edededededededef d=       Ze	 	 	 	 	 	 	 	 	 	 	 	 	 	 d[dedededed>eded
ededed?ed@edAedededededef"dB       Ze	 	 	 	 	 	 	 	 	 d\dedededed
edededededededefdC       Ze	 	 	 	 	 	 	 	 	 	 	 	 d]dedededededDed ed
edededededededefdE       Ze	 	 	 	 	 	 	 	 	 d\dFededGedHededed
edededededededefdI       Ze	 	 	 	 	 	 	 	 	 d\dedededed
edededededededefdJ       Ze	 	 	 	 	 	 	 	 	 	 	 d^dededededed
edededededededefdK       Ze	 	 	 	 	 	 	 	 	 	 	 dWdededededed
edededededededefdL       Zd_d-ee   fdNZdOefdPZ dQe!e   fdRZ"yM)`r   a  A class for offline (non-streaming) speech recognition.

    It supports multiple model families via factory methods:

    - :meth:`from_transducer` -- Zipformer, NeMo transducer, etc.
    - :meth:`from_paraformer` -- FunASR Paraformer
    - :meth:`from_sense_voice` -- SenseVoice multilingual model
    - :meth:`from_whisper` -- OpenAI Whisper
    - :meth:`from_moonshine` -- Moonshine
    - :meth:`from_dolphin` -- Dolphin CTC
    - :meth:`from_nemo_ctc` -- NeMo CTC
    - :meth:`from_zipformer2_ctc` -- Zipformer CTC

    Example using SenseVoice::

        import sherpa_onnx
        import soundfile as sf

        recognizer = sherpa_onnx.OfflineRecognizer.from_sense_voice(
            model="./sherpa-onnx-sense-voice-zh-en-ja-ko-yue-int8-2024-07-17/model.int8.onnx",
            tokens="./sherpa-onnx-sense-voice-zh-en-ja-ko-yue-int8-2024-07-17/tokens.txt",
            use_itn=True,
            debug=True,
        )

        audio, sample_rate = sf.read("test.wav", dtype="float32", always_2d=True)
        audio = audio[:, 0]  # mono

        stream = recognizer.create_stream()
        stream.accept_waveform(sample_rate, audio)
        recognizer.decode_stream(stream)
        print(stream.result)

    Example using Whisper::

        recognizer = sherpa_onnx.OfflineRecognizer.from_whisper(
            encoder="./sherpa-onnx-whisper-tiny/tiny-encoder.int8.onnx",
            decoder="./sherpa-onnx-whisper-tiny/tiny-decoder.int8.onnx",
            tokens="./sherpa-onnx-whisper-tiny/tiny-tokens.txt",
            language="en",
            task="transcribe",
        )

    Example using transducer::

        recognizer = sherpa_onnx.OfflineRecognizer.from_transducer(
            encoder="./model/encoder-epoch-99-avg-1.int8.onnx",
            decoder="./model/decoder-epoch-99-avg-1.onnx",
            joiner="./model/joiner-epoch-99-avg-1.int8.onnx",
            tokens="./model/tokens.txt",
            num_threads=2,
            decoding_method="greedy_search",
        )

    Please refer to the following files for more usages:

    - `<https://github.com/k2-fsa/sherpa-onnx/blob/master/python-api-examples/offline-decode-files.py>`_
    - `<https://github.com/k2-fsa/sherpa-onnx/blob/master/python-api-examples/offline-sense-voice-ctc-decode-files.py>`_
    encoderdecoderjoinertokensnum_threadssample_ratefeature_dimditherdecoding_methodmax_active_pathshotwords_filehotwords_scoreblank_penaltymodeling_unit	bpe_vocabdebugprovider
model_type	rule_fsts	rule_farslmlm_scalehr_dict_dirhr_rule_fsts
hr_lexiconlodr_fst
lodr_scalec                    | j                  |       }t        t        |||      |||||||      }t        |||      }t	        |      dkD  r|	dk7  rt        d|	       |r|	dk7  rt        d|	       t        ||||||      }t        ||||	|
|||||t        |||	      
      } t        |       |_
        | |_        |S )a(  
        Please refer to
        `<https://k2-fsa.github.io/sherpa/onnx/pretrained_models/offline-transducer/index.html>`_
        to download pre-trained models for different languages, e.g., Chinese,
        English, etc.

        Args:
          tokens:
            Path to ``tokens.txt``. Each line in ``tokens.txt`` contains two
            columns::

                symbol integer_id

          encoder:
            Path to ``encoder.onnx``.
          decoder:
            Path to ``decoder.onnx``.
          joiner:
            Path to ``joiner.onnx``.
          num_threads:
            Number of threads for neural network computation.
          sample_rate:
            Sample rate of the training data used to train the model.
          feature_dim:
            Dimension of the feature used to train the model.
          dither:
            Dithering constant (0.0 means no dither).
            By default the audio samples are in range [-1,+1],
            so dithering constant 0.00003 is a good value,
            equivalent to the default 1.0 from kaldi
          decoding_method:
            Valid values: greedy_search, modified_beam_search.
          max_active_paths:
            Maximum number of active paths to keep. Used only when
            decoding_method is modified_beam_search.
          hotwords_file:
            The file containing hotwords, one words/phrases per line, and for each
            phrase the bpe/cjkchar are separated by a space.
          hotwords_score:
            The hotword score of each token for biasing word/phrase. Used only if
            hotwords_file is given with modified_beam_search as decoding method.
          blank_penalty:
            The penalty applied on blank symbol during decoding.
          modeling_unit:
            The modeling unit of the model, commonly used units are bpe, cjkchar,
            cjkchar+bpe, etc. Currently, it is needed only when hotwords are
            provided, we need it to encode the hotwords into token sequence.
            and the modeling unit is bpe or cjkchar+bpe.
          bpe_vocab:
            The vocabulary generated by google's sentencepiece program.
            It is a file has two columns, one is the token, the other is
            the log probability, you can get it from the directory where
            your bpe model is generated. Only used when hotwords provided
          debug:
            True to show debug messages.
          provider:
            onnxruntime execution providers. Valid values are: cpu, cuda, coreml.
          rule_fsts:
            If not empty, it specifies fsts for inverse text normalization.
            If there are multiple fsts, they are separated by a comma.
          rule_fars:
            If not empty, it specifies fst archives for inverse text normalization.
            If there are multiple archives, they are separated by a comma.
          lodr_fst:
            Path to the LODR FST file in binary format. If empty, LODR is disabled.
          lodr_scale:
            Scale factor for LODR rescoring. Only used when lodr_fst is provided.
        )encoder_filenamedecoder_filenamejoiner_filename)
transducerr*   r+   r6   r7   r4   r5   r8   )sampling_rater-   r.   r   modified_beam_searchz_Please use --decoding-method=modified_beam_search when using --hotwords-file. Currently given: zTPlease use --decoding-method=modified_beam_search when using --lm. Currently given: )modelscalelm_num_threadslm_providerr@   rA   dict_dirlexiconr9   )feat_configmodel_config	lm_configr/   r0   r1   r2   r3   r9   r:   hr)__new__r   r   r   len
ValueErrorr   r   r   _Recognizer
recognizerconfig)!clsr'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   selfrQ   rP   rR   recognizer_configs!                                    r#   from_transducerz!OfflineRecognizer.from_transducerg   s.   F {{3)3!(!( &
 #'!
 -%#
 }!o9O&O55D4EG 
 /%;;**9):< 
 $& !
	 4#%+-')'&$"&
" &&78'r%   rI   languageuse_itnc                     | j                  |       }t        t        ||	|
      ||||      }t        ||      }t	        |||||t        |||            }t        |      |_        ||_        |S )ay  
        Please refer to
        `<https://github.com/k2-fsa/sherpa-onnx/releases/tag/asr-models>`_
        to download pre-trained models.

        Args:
          tokens:
            Path to ``tokens.txt``. Each line in ``tokens.txt`` contains two
            columns::

                symbol integer_id

          model:
            Path to ``model.onnx``.
          num_threads:
            Number of threads for neural network computation.
          sample_rate:
            Sample rate of the training data used to train the model.
          feature_dim:
            Dimension of the feature used to train the model.
          decoding_method:
            Valid values are greedy_search.
          debug:
            True to show debug messages.
          provider:
            onnxruntime execution providers. Valid values are: cpu, cuda, coreml.
          language:
            If not empty, then valid values are: auto, zh, en, ja, ko, yue
          use_itn:
            True to enable inverse text normalization; False to disable it.
          rule_fsts:
            If not empty, it specifies fsts for inverse text normalization.
            If there are multiple fsts, they are separated by a comma.
          rule_fars:
            If not empty, it specifies fst archives for inverse text normalization.
            If there are multiple archives, they are separated by a comma.
        )rI   r^   r_   )sense_voicer*   r+   r6   r7   rG   r-   rM   rP   rQ   r/   r9   r:   rS   )	rT   r   r   r   r   r   rW   rX   rY   )rZ   rI   r*   r+   r,   r-   r/   r6   r7   r^   r_   r9   r:   r=   r>   r?   r[   rQ   rP   r\   s                       r#   from_sense_voicez"OfflineRecognizer.from_sense_voice
  s    p {{3)4!
 #

 -%#

 4#%+&$"&
 &&78'r%   encoder_adaptorllm	embedding	tokenizersystem_promptuser_promptmax_new_tokenstemperaturetop_pseeditnhotwordsc                 t   | j                  |       }t               }||_        ||_        ||_        ||_        ||_        ||_        ||_        ||_	        ||_
        ||_        ||_        ||_        ||_        t        |||	|
      }t!        ||      }t#        |||      }t%        |      |_        ||_        |S )a  
        Create an offline recognizer for FunASR-nano models.

        Args:
          encoder_adaptor:
            Path to ``encoder_adaptor.onnx``.
          llm:
            Path to ``llm.onnx`` (KV cache model).
          embedding:
            Path to ``embedding.onnx``.
          tokenizer:
            Path to tokenizer directory (e.g., Qwen3-0.6B).
          num_threads:
            Number of threads for neural network computation.
          sample_rate:
            Sample rate of the training data used to train the model.
          feature_dim:
            Dimension of the feature used to train the model.
          decoding_method:
            Valid values are greedy_search.
          debug:
            True to show debug messages.
          provider:
            onnxruntime execution providers. Valid values are: cpu, cuda.
          system_prompt:
            System prompt for FunASR-nano.
          user_prompt:
            User prompt template for FunASR-nano.
          max_new_tokens:
            Maximum number of new tokens to generate.
          temperature:
            Sampling temperature.
          top_p:
            Top-p (nucleus) sampling threshold.
          seed:
            Random seed.
          language:
            Language for transcription (empty string means None).
          itn:
            Whether to apply inverse text normalization (default: True).
          hotwords:
            Hotwords (comma-separated, e.g., "Sherpa,FunASR").
        )funasr_nanor+   r6   r7   rb   rP   rQ   r/   )rT   r
   re   rf   rg   rh   ri   rj   rk   rl   rm   rn   r^   ro   rp   r   r   r   rW   rX   rY   )rZ   re   rf   rg   rh   r+   r,   r-   r/   r6   r7   ri   rj   rk   rl   rm   rn   r^   ro   rp   r[   funasr_nano_configrQ   rP   r\   s                            r#   from_funasr_nanoz"OfflineRecognizer.from_funasr_nanod  s    D {{39;-<*!$'0$'0$+8()4&,:))4&#( "&&.#!$&.#)*#	
 -%#

 4#%+

 &&78'r%   conv_frontendmax_total_lenc                     | j                  |       }t        ||||||||||
      }t        |||	|
      }t        ||      }t	        |||      }t        |      |_        ||_        |S )a  
        Create an offline recognizer for Qwen3-ASR (conv_frontend + encoder +
        decoder with KV cache; tokenizer directory with vocab.json / merges.txt).

        Args:
          conv_frontend:
            Path to ``conv_frontend.onnx``.
          encoder:
            Path to ``encoder.onnx``.
          decoder:
            Path to ``decoder.onnx`` (KV cache LLM).
          tokenizer:
            Path to tokenizer directory (e.g. Qwen3-ASR model folder with
            ``vocab.json``, ``merges.txt``, ``tokenizer_config.json``).
          num_threads:
            Number of threads for neural network computation.
          sample_rate:
            Sample rate of input audio (used by the feature extractor).
          feature_dim:
            Mel feature dimension (Qwen3-ASR offline path uses 128 by default).
          decoding_method:
            Valid values: greedy_search.
          debug:
            True to show debug messages.
          provider:
            onnxruntime execution providers. Valid values are: cpu, cuda.
          max_total_len:
            Maximum KV cache sequence length (see model / ``--qwen3-asr-max-total-len``).
          max_new_tokens:
            Maximum new tokens to generate per utterance.
          temperature:
            Sampling temperature for decoding.
          top_p:
            Top-p (nucleus) sampling threshold.
          seed:
            Random seed for sampling.
          hotwords:
            Optional comma-separated hotwords (UTF-8, ASCII ','), e.g. ``"foo,bar,baz"``.
        )
rv   r'   r(   rh   rp   rw   rk   rl   rm   rn   )	qwen3_asrr+   r6   r7   rb   rs   )rT   r   r   r   r   rW   rX   rY   )rZ   rv   r'   r(   rh   r+   r,   r-   r/   r6   r7   rw   rk   rl   rm   rn   rp   r[   qwen3rQ   rP   r\   s                         r#   from_qwen3_asrz OfflineRecognizer.from_qwen3_asr  s    v {{3*'')#
 *#	
 -%#

 4#%+

 &&78'r%   
paraformerc                     | j                  |       }t        t        |      ||||d      }t        ||      }t	        ||||	|
t        |||            }t        |      |_        ||_        |S )a  
        Please refer to
        `<https://k2-fsa.github.io/sherpa/onnx/pretrained_models/offline-paraformer/index.html>`_
        to download pre-trained models.

        Args:
          tokens:
            Path to ``tokens.txt``. Each line in ``tokens.txt`` contains two
            columns::

                symbol integer_id

          paraformer:
            Path to ``model.onnx``.
          num_threads:
            Number of threads for neural network computation.
          sample_rate:
            Sample rate of the training data used to train the model.
          feature_dim:
            Dimension of the feature used to train the model.
          decoding_method:
            Valid values are greedy_search.
          debug:
            True to show debug messages.
          provider:
            onnxruntime execution providers. Valid values are: cpu, cuda, coreml.
          rule_fsts:
            If not empty, it specifies fsts for inverse text normalization.
            If there are multiple fsts, they are separated by a comma.
          rule_fars:
            If not empty, it specifies fst archives for inverse text normalization.
            If there are multiple archives, they are separated by a comma.
        rI   r|   )r|   r*   r+   r6   r7   r8   rb   rM   rc   )	rT   r   r   r   r   r   rW   rX   rY   )rZ   r|   r*   r+   r,   r-   r/   r6   r7   r9   r:   r=   r>   r?   r[   rQ   rP   r\   s                     r#   from_paraformerz!OfflineRecognizer.from_paraformer*  s    d {{3)3*E##
 -%#

 4#%+&$"&
 &&78'r%   c                     | j                  |       }t        |||||      }t        ||      }t        ||||	|
t	        |||            }t        |      |_        ||_        |S )a/  
        Please refer to
        `<https://github.com/k2-fsa/sherpa-onnx/releases/tag/asr-models>`_
        to download pre-trained models.

        Args:
          model:
            Path to ``model.onnx``.
          tokens:
            Path to ``tokens.txt``. Each line in ``tokens.txt`` contains two
            columns::

                symbol integer_id

          num_threads:
            Number of threads for neural network computation.
          sample_rate:
            Sample rate of the training data used to train the model. It is
            ignored and is hard-coded in C++ to 40.
          feature_dim:
            Dimension of the feature used to train the model. It is ignored
            and is hard-coded in C++ to 40.
          decoding_method:
            Valid values are greedy_search.
          debug:
            True to show debug messages.
          provider:
            onnxruntime execution providers. Valid values are: cpu, cuda, coreml.
          rule_fsts:
            If not empty, it specifies fsts for inverse text normalization.
            If there are multiple fsts, they are separated by a comma.
          rule_fars:
            If not empty, it specifies fst archives for inverse text normalization.
            If there are multiple archives, they are separated by a comma.
        )telespeech_ctcr*   r+   r6   r7   rb   rM   rc   )rT   r   r   r   r   rW   rX   rY   rZ   rI   r*   r+   r,   r-   r/   r6   r7   r9   r:   r=   r>   r?   r[   rQ   rP   r\   s                     r#   from_telespeech_ctcz%OfflineRecognizer.from_telespeech_ctc{  s    h {{3) #
 -%#

 4#%+&$jL	
 &&78'r%   c                     | j                  |       }t        t        |      ||||      }t        ||      }t	        ||||	|
t        |||            }t        |      |_        ||_        |S )a  
        Please refer to
        `<https://k2-fsa.github.io/sherpa/onnx/dolphin/index.html>`_
        to download pre-trained models.

        Args:
          model:
            Path to ``model.onnx`` or ``model.int8.onnx``.
          tokens:
            Path to ``tokens.txt``. Each line in ``tokens.txt`` contains two
            columns::

                symbol integer_id

          num_threads:
            Number of threads for neural network computation.
          sample_rate:
            Sample rate of the training data used to train the model.
          feature_dim:
            Dimension of the feature used to train the model.
          decoding_method:
            Valid values are greedy_search.
          debug:
            True to show debug messages.
          provider:
            onnxruntime execution providers. Valid values are: cpu, cuda, coreml.
          rule_fsts:
            If not empty, it specifies fsts for inverse text normalization.
            If there are multiple fsts, they are separated by a comma.
          rule_fars:
            If not empty, it specifies fst archives for inverse text normalization.
            If there are multiple archives, they are separated by a comma.
        r~   )dolphinr*   r+   r6   r7   rb   rM   rc   )	rT   r   r   r   r   r   rW   rX   rY   r   s                     r#   from_dolphin_ctcz"OfflineRecognizer.from_dolphin_ctc  s    d {{3)-E:#
 -%#

 4#%+&$"&
 &&78'r%   c                     | j                  |       }t        t        |      ||||      }t        ||      }	t	        |	      |_        |	|_        |S )a  
        Please refer to
        `<https://k2-fsa.github.io/sherpa/onnx/FireRedAsr/index.html>`_
        to download pre-trained models.

        Args:
          model:
            Path to ``model.onnx``.
          tokens:
            Path to ``tokens.txt``. Each line in ``tokens.txt`` contains two
            columns::

                symbol integer_id

          num_threads:
            Number of threads for neural network computation.
          decoding_method:
            The only supported decoding method is greedy_search.
          debug:
            True to show debug messages.
          provider:
            onnxruntime execution providers. Valid values are: cpu, cuda, coreml.
        r~   )fire_red_asr_ctcr*   r+   r6   r7   rQ   r/   )rT   r   r   r   rW   rX   rY   
rZ   rI   r*   r+   r/   r6   r7   r[   rQ   r\   s
             r#   from_fire_red_asr_ctcz'OfflineRecognizer.from_fire_red_asr_ctc  s_    B {{3)<5I#
 4%+
 &&78'r%   c                     | j                  |       }t        t        |      ||||      }t        ||      }	t	        |	      |_        |	|_        |S )a  
        Please refer to
        `<https://k2-fsa.github.io/sherpa/onnx/medasr/index.html>`_
        to download pre-trained models.

        Args:
          model:
            Path to ``model.onnx``.
          tokens:
            Path to ``tokens.txt``. Each line in ``tokens.txt`` contains two
            columns::

                symbol integer_id

          num_threads:
            Number of threads for neural network computation.
          decoding_method:
            The only supported decoding method is greedy_search.
          debug:
            True to show debug messages.
          provider:
            onnxruntime execution providers. Valid values are: cpu, cuda, coreml.
        r~   )medasrr*   r+   r6   r7   r   )rT   r   r   r   rW   rX   rY   r   s
             r#   from_medasr_ctcz!OfflineRecognizer.from_medasr_ctcM  s_    B {{3).U;#
 4%+
 &&78'r%   c                     | j                  |       }t        t        |      ||||      }t        ||      }	t	        |	      |_        |	|_        |S )a  
        Please refer to
        `<https://k2-fsa.github.io/sherpa/onnx/omnilingual-asr/index.html>`_
        to download pre-trained models.

        Args:
          model:
            Path to ``model.onnx``.
          tokens:
            Path to ``tokens.txt``. Each line in ``tokens.txt`` contains two
            columns::

                symbol integer_id

          num_threads:
            Number of threads for neural network computation.
          decoding_method:
            The only supported decoding method is greedy_search.
          debug:
            True to show debug messages.
          provider:
            onnxruntime execution providers. Valid values are: cpu, cuda, coreml.
        r~   )omnilingualr*   r+   r6   r7   r   )rT   r   r   r   rW   rX   rY   r   s
             r#   from_omnilingual_asr_ctcz*OfflineRecognizer.from_omnilingual_asr_ctc  s_    B {{3);%H#
 4%+
 &&78'r%   c                     | j                  |       }t        t        |      ||||      }t        ||      }t	        ||||	|
t        |||            }t        |      |_        ||_        |S )a  
        Please refer to
        `<https://k2-fsa.github.io/sherpa/onnx/pretrained_models/offline-ctc/icefall/index.html>`_
        to download pre-trained models for different languages, e.g., Chinese,
        English, etc.

        Args:
          model:
            Path to ``model.onnx``.
          tokens:
            Path to ``tokens.txt``. Each line in ``tokens.txt`` contains two
            columns::

                symbol integer_id

          num_threads:
            Number of threads for neural network computation.
          sample_rate:
            Sample rate of the training data used to train the model.
          feature_dim:
            Dimension of the feature used to train the model.
          decoding_method:
            Valid values are greedy_search.
          debug:
            True to show debug messages.
          provider:
            onnxruntime execution providers. Valid values are: cpu, cuda, coreml.
          rule_fsts:
            If not empty, it specifies fsts for inverse text normalization.
            If there are multiple fsts, they are separated by a comma.
          rule_fars:
            If not empty, it specifies fst archives for inverse text normalization.
            If there are multiple archives, they are separated by a comma.
        r~   )zipformer_ctcr*   r+   r6   r7   rb   rM   rc   )	rT   r   r   r   r   r   rW   rX   rY   r   s                     r#   from_zipformer_ctcz$OfflineRecognizer.from_zipformer_ctc  s    f {{3)8uE#
 -%#

 4#%+&$"&
 &&78'r%   c                     | j                  |       }t        t        |      ||||d      }t        ||      }t	        ||||	|
t        |||            }t        |      |_        ||_        |S )a  
        Please refer to
        `<https://k2-fsa.github.io/sherpa/onnx/pretrained_models/offline-ctc/nemo/index.html>`_
        to download pre-trained models for different languages, e.g., Chinese,
        English, etc.

        Args:
          model:
            Path to ``model.onnx``.
          tokens:
            Path to ``tokens.txt``. Each line in ``tokens.txt`` contains two
            columns::

                symbol integer_id

          num_threads:
            Number of threads for neural network computation.
          sample_rate:
            Sample rate of the training data used to train the model.
          feature_dim:
            Dimension of the feature used to train the model.
          decoding_method:
            Valid values are greedy_search.
          debug:
            True to show debug messages.
          provider:
            onnxruntime execution providers. Valid values are: cpu, cuda, coreml.
          rule_fsts:
            If not empty, it specifies fsts for inverse text normalization.
            If there are multiple fsts, they are separated by a comma.
          rule_fars:
            If not empty, it specifies fst archives for inverse text normalization.
            If there are multiple archives, they are separated by a comma.
        r~   nemo_ctc)r   r*   r+   r6   r7   r8   rb   rM   rc   )	rT   r   r   r   r   r   rW   rX   rY   r   s                     r#   from_nemo_ctczOfflineRecognizer.from_nemo_ctc  s    f {{3)45A#!
 -%#

 4#%+&$"&
 &&78'r%   src_langtgt_langc                     | j                  |       }t        t        ||||      |||
|      }t        ||      }t	        |||	||t        |||            }t        |      |_        ||_        |S )a  
        Please refer to
        `<https://k2-fsa.github.io/sherpa/onnx/nemo/index.html>`_
        to download pre-trained models for different languages.

        Args:
          encoder:
            Path to ``encoder.onnx`` or ``encoder.int8.onnx``.
          decoder:
            Path to ``decoder.onnx`` or ``decoder.int8.onnx``.
          tokens:
            Path to ``tokens.txt``. Each line in ``tokens.txt`` contains two
            columns::

                symbol integer_id

          src_lang:
            The language of the input audio. Valid values are: en, es, de, fr.
            If you leave it empty, it uses en internally.
          tgt_lang:
            The language of the output text. Valid values are: en, es, de, fr.
            If you leave it empty, it uses en internally.
          num_threads:
            Number of threads for neural network computation.
          sample_rate:
            Sample rate of the training data used to train the model. Not used
          feature_dim:
            Dimension of the feature used to train the model. Not used
          decoding_method:
            Valid values are greedy_search. Not used
          debug:
            True to show debug messages.
          provider:
            onnxruntime execution providers. Valid values are: cpu, cuda, coreml.
          rule_fsts:
            If not empty, it specifies fsts for inverse text normalization.
            If there are multiple fsts, they are separated by a comma.
          rule_fars:
            If not empty, it specifies fst archives for inverse text normalization.
            If there are multiple archives, they are separated by a comma.
        )r'   r(   r   r   )canaryr*   r+   r6   r7   rb   rM   rc   )	rT   r   r   r   r   r   rW   rX   rY   )rZ   r'   r(   r*   r   r   r+   r,   r-   r/   r6   r7   r9   r:   r=   r>   r?   r[   rQ   rP   r\   s                        r#   from_nemo_canaryz"OfflineRecognizer.from_nemo_canaryT  s    z {{3)+!!	 #
 -%#

 4#%+&$"&
 &&78'r%   tasktail_paddingsenable_token_timestampsenable_segment_timestampsc                     | j                  |       }t        t        |||||
||      ||||	d      }t        dd      }t	        |||||t        |||            }t        |      |_        ||_        |S )	a	  
        Please refer to
        `<https://k2-fsa.github.io/sherpa/onnx/pretrained_models/whisper/index.html>`_
        to download pre-trained models for different kinds of whisper models,
        e.g., tiny, tiny.en, base, base.en, etc.

        Args:
          encoder:
            Path to the encoder model, e.g., tiny-encoder.onnx,
            tiny-encoder.int8.onnx, tiny-encoder.ort, etc.
          decoder:
            Path to the decoder model, e.g., tiny-decoder.onnx,
            tiny-decoder.int8.onnx, tiny-decoder.ort, etc.
          tokens:
            Path to ``tokens.txt``. Each line in ``tokens.txt`` contains two
            columns::

                symbol integer_id

          language:
            The spoken language in the audio file. Example values: en, de, zh,
            jp, fr. See https://github.com/openai/whisper/blob/main/whisper/tokenizer.py#L10
            for all possible values. Note that for non-multilingual models, the
            only valid value is 'en'.
          task:
            Valid values are: transcribe, translate. Note that for
            non-multilingual models, the only valid value is 'transcribe'.
          num_threads:
            Number of threads for neural network computation.
          decoding_method:
            Valid values: greedy_search.
          debug:
            True to show debug messages.
          provider:
            onnxruntime execution providers. Valid values are: cpu, cuda, coreml.
          enable_token_timestamps:
            True to enable token-level timestamps using cross-attention alignment
            and DTW. Requires ONNX models exported with attention outputs.
            When enabled, result.timestamps will contain token-level start times.
            Defaults to False.
          enable_segment_timestamps:
            True to enable segment-level timestamps using Whisper's native
            timestamp token mode. The decoder outputs timestamp tokens like
            <|0.00|> to mark segment boundaries. Does not require attention
            outputs. Can be combined with enable_token_timestamps for both
            segment and token-level timestamps. Defaults to False.
          rule_fsts:
            If not empty, it specifies fsts for inverse text normalization.
            If there are multiple fsts, they are separated by a comma.
          rule_fars:
            If not empty, it specifies fst archives for inverse text normalization.
            If there are multiple archives, they are separated by a comma.
        )r'   r(   r^   r   r   r   r   whisper)r   r*   r+   r6   r7   r8   >  P   rb   rM   rc   )	rT   r   r   r   r   r   rW   rX   rY   )rZ   r'   r(   r*   r^   r   r+   r/   r6   r7   r   r   r   r9   r:   r=   r>   r?   r[   rQ   rP   r\   s                         r#   from_whisperzOfflineRecognizer.from_whisper  s    T {{3)-!+(?*C # 
" -

 4#%+&$"&
 &&78'r%   c                     | j                  |       }t        t        ||      ||||      }t        dd      }t	        |||||	t        |
||            }t        |      |_        ||_        |S )a  
        Please refer to
        `<https://k2-fsa.github.io/sherpa/onnx/fire_red_asr/index.html>`_
        to download pre-trained models for different kinds of FireRedAsr models,
        e.g., xs, large, etc.

        Args:
          encoder:
            Path to the encoder model.
          decoder:
            Path to the decoder model.
          tokens:
            Path to ``tokens.txt``. Each line in ``tokens.txt`` contains two
            columns::

                symbol integer_id
          num_threads:
            Number of threads for neural network computation.
          decoding_method:
            Valid values: greedy_search.
          debug:
            True to show debug messages.
          provider:
            onnxruntime execution providers. Valid values are: cpu, cuda, coreml.
          rule_fsts:
            If not empty, it specifies fsts for inverse text normalization.
            If there are multiple fsts, they are separated by a comma.
          rule_fars:
            If not empty, it specifies fst archives for inverse text normalization.
            If there are multiple archives, they are separated by a comma.
        )r'   r(   )fire_red_asrr*   r+   r6   r7   r   r   rb   rM   rc   )	rT   r   r   r   r   r   rW   rX   rY   )rZ   r'   r(   r*   r+   r/   r6   r7   r9   r:   r=   r>   r?   r[   rQ   rP   r\   s                    r#   from_fire_red_asrz#OfflineRecognizer.from_fire_red_asr%  s    ^ {{3)5 #	
 -

 4#%+&$"&
 &&78'r%   	use_punctc                     | j                  |       }t        t        |||||      |||	|
      }t        dd      }t	        |||||t        |||            }t        |      |_        ||_        |S )a}  
        Please refer to
        `<https://k2-fsa.github.io/sherpa/onnx/cohere_transcribe/index.html>`_
        to download pre-trained models

        Args:
          encoder:
            Path to the encoder model, e.g., encoder.int8.onnx
          decoder:
            Path to the merged decoder model, e.g., decoder.int8.onnx
          tokens:
            Path to ``tokens.txt``. Each line in ``tokens.txt`` contains two
            columns::

                symbol integer_id

          num_threads:
            Number of threads for neural network computation.
          language:
            The default language to use. Valid values are:
            ar, de, el, en, es, fr, it, ja, ko, nl, pl, pt, vi, zh.
            Note that you can set language per stream. For example,
            stream.set_option("language", "de").
          use_punct:
            True to enable punctuations and cases.
          use_itn:
            True to enable inverse text normalization.
          decoding_method:
            Valid values: greedy_search.
          debug:
            True to show debug messages.
          provider:
            onnxruntime execution providers. Valid values are: cpu, cuda.
            To use NVIDIA GPUs, you have to first install a CUDA-enabled version
            of sherpa-onnx
          rule_fsts:
            If not empty, it specifies fsts for inverse text normalization.
            If there are multiple fsts, they are separated by a comma.
          rule_fars:
            If not empty, it specifies fst archives for inverse text normalization.
            If there are multiple archives, they are separated by a comma.
        )r'   r(   r^   r_   r   )cohere_transcriber*   r+   r6   r7   r      rb   rM   rQ   rP   r/   r9   r:   rS   )	rT   r   r	   r   r   r   rW   rX   rY   )rZ   r'   r(   r*   r+   r^   r   r_   r/   r6   r7   r9   r:   r=   r>   r?   r[   rQ   unused_feat_configr\   s                       r#   from_cohere_transcribez(OfflineRecognizer.from_cohere_transcribeu  s    z {{3)@!# #
 4

 4%*+&$"&
 &&78'r%   preprocessoruncached_decodercached_decoderc                     | j                  |       }t        t        ||||      ||||	      }t        dd      }t	        ||||
|t        |||            }t        |      |_        ||_        |S )a  
        Please refer to
        `<https://k2-fsa.github.io/sherpa/onnx/moonshine/index.html>`_
        to download pre-trained models for different kinds of moonshine models,
        e.g., tiny, base, etc.

        Args:
          preprocessor:
            Path to the preprocessor model, e.g., preprocess.onnx
          encoder:
            Path to the encoder model, e.g., encode.int8.onnx
          uncached_decoder:
            Path to the uncached decoder model, e.g., uncached_decode.int8.onnx,
          cached_decoder:
            Path to the cached decoder model, e.g., cached_decode.int8.onnx,
          tokens:
            Path to ``tokens.txt``. Each line in ``tokens.txt`` contains two
            columns::

                symbol integer_id

          num_threads:
            Number of threads for neural network computation.
          decoding_method:
            Valid values: greedy_search.
          debug:
            True to show debug messages.
          provider:
            onnxruntime execution providers. Valid values are: cpu, cuda, coreml.
          rule_fsts:
            If not empty, it specifies fsts for inverse text normalization.
            If there are multiple fsts, they are separated by a comma.
          rule_fars:
            If not empty, it specifies fst archives for inverse text normalization.
            If there are multiple archives, they are separated by a comma.
        )r   r'   r   r   	moonshiner*   r+   r6   r7   r   r   rb   rM   r   	rT   r   r   r   r   r   rW   rX   rY   )rZ   r   r'   r   r   r*   r+   r/   r6   r7   r9   r:   r=   r>   r?   r[   rQ   r   r\   s                      r#   from_moonshinez OfflineRecognizer.from_moonshine  s    l {{3)1)!1-	 #
 4

 4%*+&$"&
 &&78'r%   c                     | j                  |       }t        t        ||      ||||      }t        dd      }t	        |||||	t        |
||            }t        |      |_        ||_        |S )a  
        Please refer to
        `<https://k2-fsa.github.io/sherpa/onnx/moonshine/index.html>`_
        to download pre-trained models for different kinds of moonshine v2 models,
        e.g., tiny-en, base-zh, etc.

        Args:
          encoder:
            Path to the encoder model, e.g., encoder_model.ort
          decoder:
            Path to the merged decoder model, e.g., decoder_model_merged.ort,
          tokens:
            Path to ``tokens.txt``. Each line in ``tokens.txt`` contains two
            columns::

                symbol integer_id

          num_threads:
            Number of threads for neural network computation.
          decoding_method:
            Valid values: greedy_search.
          debug:
            True to show debug messages.
          provider:
            onnxruntime execution providers. Valid values are: cpu, cuda, coreml.
          rule_fsts:
            If not empty, it specifies fsts for inverse text normalization.
            If there are multiple fsts, they are separated by a comma.
          rule_fars:
            If not empty, it specifies fst archives for inverse text normalization.
            If there are multiple archives, they are separated by a comma.
        )r'   merged_decoderr   r   r   rb   rM   r   r   )rZ   r'   r(   r*   r+   r/   r6   r7   r9   r:   r=   r>   r?   r[   rQ   r   r\   s                    r#   from_moonshine_v2z#OfflineRecognizer.from_moonshine_v2/  s    ` {{3)1& #	
 4

 4%*+&$"&
 &&78'r%   c                     | j                  |       }t        t        |      ||||d      }t        ||      }t	        ||||	|
t        |||            }t        |      |_        ||_        |S )a  
        Please refer to
        `<https://k2-fsa.github.io/sherpa/onnx/pretrained_models/offline-ctc/yesno/index.html>`_
        to download pre-trained models.

        Args:
          model:
            Path to ``model.onnx``.
          tokens:
            Path to ``tokens.txt``. Each line in ``tokens.txt`` contains two
            columns::

                symbol integer_id

          num_threads:
            Number of threads for neural network computation.
          sample_rate:
            Sample rate of the training data used to train the model.
          feature_dim:
            Dimension of the feature used to train the model.
          decoding_method:
            Valid values are greedy_search.
          debug:
            True to show debug messages.
          provider:
            onnxruntime execution providers. Valid values are: cpu, cuda, coreml.
          rule_fsts:
            If not empty, it specifies fsts for inverse text normalization.
            If there are multiple fsts, they are separated by a comma.
          rule_fars:
            If not empty, it specifies fst archives for inverse text normalization.
            If there are multiple archives, they are separated by a comma.
        r~   tdnn)r   r*   r+   r6   r7   r8   rb   rM   rc   )	rT   r   r   r   r   r   rW   rX   rY   r   s                     r#   from_tdnn_ctczOfflineRecognizer.from_tdnn_ctc  s    d {{3)'e4#
 -%#

 4#%+&$"&
 &&78'r%   c                     | j                  |       }t        t        |      ||||d      }t        ||      }t	        ||||	|
t        |||            }t        |      |_        ||_        |S )a  
        Please refer to
        `<https://k2-fsa.github.io/sherpa/onnx/pretrained_models/whisper/index.html>`_
        to download pre-trained models for different languages, e.g., Chinese,
        English, etc.

        Args:
          model:
            Path to ``model.onnx``.
          tokens:
            Path to ``tokens.txt``. Each line in ``tokens.txt`` contains two
            columns::

                symbol integer_id

          num_threads:
            Number of threads for neural network computation.
          sample_rate:
            Sample rate of the training data used to train the model.
          feature_dim:
            Dimension of the feature used to train the model.
          decoding_method:
            Valid values are greedy_search.
          debug:
            True to show debug messages.
          provider:
            onnxruntime execution providers. Valid values are: cpu, cuda, coreml.
          rule_fsts:
            If not empty, it specifies fsts for inverse text normalization.
            If there are multiple fsts, they are separated by a comma.
          rule_fars:
            If not empty, it specifies fst archives for inverse text normalization.
            If there are multiple archives, they are separated by a comma.
        r~   	wenet_ctc)r   r*   r+   r6   r7   r8   rb   rM   rc   )	rT   r   r   r   r   r   rW   rX   rY   r   s                     r#   from_wenet_ctcz OfflineRecognizer.from_wenet_ctc  s    f {{3)0u=#"
 -%#

 4#%+&$"&
 &&78'r%   Nc                 p    || j                   j                         S | j                   j                  |      S )a  Create a new offline stream for feeding audio data.

        The returned stream should be passed to :meth:`decode_stream` or
        :meth:`decode_streams` after audio has been accepted.

        Args:
          hotwords:
            Optional hotwords string for biasing recognition. Each hotword
            is separated by ``/``, and tokens within a hotword are separated
            by a space. For instance, ``"HELLO WORLD/GOODBYE"``.
            If ``None``, no hotwords biasing is applied.

        Returns:
          An :class:`OfflineStream` instance.

        Example::

            stream = recognizer.create_stream()
            stream.accept_waveform(sample_rate, audio)
            recognizer.decode_stream(stream)
            print(stream.result)

        Example with hotwords::

            stream = recognizer.create_stream(hotwords="HELLO WORLD/GOODBYE")
            stream.accept_waveform(sample_rate, audio)
            recognizer.decode_stream(stream)
            print(stream.result)
        )rX   create_stream)r[   rp   s     r#   r   zOfflineRecognizer.create_stream#  s2    < ??0022??00::r%   sc                 :    | j                   j                  |       y)a  Run speech recognition on a single stream.

        After calling this method, the recognition result is available via
        ``stream.result.text``.

        Args:
          s:
            An :class:`OfflineStream` that has already been fed audio with
            :meth:`~OfflineStream.accept_waveform`.

        Example::

            stream = recognizer.create_stream()
            stream.accept_waveform(sample_rate, audio)
            recognizer.decode_stream(stream)
            print(stream.result.text)
        N)rX   decode_stream)r[   r   s     r#   r   zOfflineRecognizer.decode_streamF  s    $ 	%%a(r%   ssc                 :    | j                   j                  |       y)am  Run speech recognition on multiple streams in parallel.

        This is more efficient than calling :meth:`decode_stream` in a loop,
        as the internal decoder processes all streams concurrently.

        Args:
          ss:
            A list of :class:`OfflineStream` instances, each of which has
            already been fed audio with
            :meth:`~OfflineStream.accept_waveform`.

        Example::

            streams = []
            for audio_file in audio_files:
                audio, sample_rate = sf.read(audio_file, dtype="float32", always_2d=True)
                audio = audio[:, 0]  # mono
                s = recognizer.create_stream()
                s.accept_waveform(sample_rate, audio)
                streams.append(s)

            recognizer.decode_streams(streams)
            for s in streams:
                print(s.result.text)
        N)rX   decode_streams)r[   r   s     r#   r   z OfflineRecognizer.decode_streamsZ  s    4 	&&r*r%   )   r   r           greedy_search    g      ?r   cjkcharr   FcpurF   r   r   r   g?r   r   r   r   r   )r   r   r   r   Fr   r   Fr   r   r   r   r   )r   r   r   r   Fr   zYou are a helpful assistant.u   语音转写:   ư>皙?*   r   Tr   )r   r   r   r   Fr   r   r   r   r   r   r   )r   r   r   r   Fr   r   r   r   r   r   )r   r   (   r   Fr   r   r   r   r   r   )r   r   Fr   )enr   r   r   r   r   Fr   r   r   r   r   r   )r   
transcriber   r   Fr   FFr   r   r   r   r   )	r   r   Fr   r   r   r   r   r   )r   r   TTr   Fr   r   r   r   r   r   )r   i@     r   Fr   r   r   r   r   r   )N)#__name__
__module____qualname____doc__classmethodstrintfloatboolr]   rd   ru   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r%   r#   r   r   *   s(   :x   . ! #"&&9`` ` 	`
 ` ` ` ` ` ` ` ` ` ` `  !`" #`$ %`& '`( )`* +`, -`. /`0 1`2 3`4 5`6 7`8 9` `D 
  .!WW W 	W
 W W W W W W W W W W W  !W Wr   .;*!!)ee e 	e
 e e e e e e e e e e e  !e" #e$ %e& 'e( )e eN   . !!#[[ [ 	[
 [ [ [ [ [ [ [ [ [ [ [  ![" #[ [z 
  .NN N 	N
 N N N N N N N N N N N` 
  .MM M 	M
 M M M M M M M M M M M^ 
  .MM M 	M
 M M M M M M M M M M M^ 
 .// / 	/
 / / / /b 
 .// / 	/
 / / / /b 
 .// / 	/
 / / / /b 
  .NN N 	N
 N N N N N N N N N N N` 
  .OO O 	O
 O O O O O O O O O O Ob   .#]] ] 	]
 ] ] ] ] ] ] ] ] ] ] ]  !]" #] ]~   .(-*/%nn n 	n
 n n n n n n n "&n $(n n n  !n" #n$ %n n`  .MM M 	M
 M M M M M M M M M M^  .!^^ ^ 	^
 ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^  !^ ^@  .VV V 	V
 V V V V V V V V V V V Vp  .NN N 	N
 N N N N N N N N N N` 
 .NN N 	N
 N N N N N N N N N N N` 
  .OO O 	O
 O O O O O O O O O O Ob!;hsm !;F)} )(+m!4 +r%   r   N)$pathlibr   typingr   r   sherpa_onnx.lib._sherpa_onnxr   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   rW   r   r   r   r   r   r   r   r   r   r$   objectr   r%   r#   <module>r      sU     !    & J	 	 	43 4J+ J+r%   