o
    iE                     @   sd   d Z ddlZddlmZ ddlmZ ddlmZ G dd dZdd	d
ddddddZ	dd Z
dS )zASR Interface module.    N
get_parser)dynamic_import)fill_missing_argsc                   @   s   e Zd ZdZedd ZededefddZdd	 Z	dddZ
dddZdd Zdd Zedd Zedd Zdd Zdd Zdd Zd
S )ASRInterfacez.ASR Interface for ESPnet model implementation.c                 C   s   | S )zAdd arguments to parser. parserr   r   M/home/ubuntu/.local/lib/python3.10/site-packages/espnet/nets/asr_interface.pyadd_arguments   s   zASRInterface.add_argumentsidimodimc                 K   s:   dd }t jdi |}t||}t|| j}| |||S )a  Initialize this class with python-level args.

        Args:
            idim (int): The number of an input feature dim.
            odim (int): The number of output vocab.

        Returns:
            ASRinterface: A new instance of ASRInterface.

        c                 S   s   t | ddS )NF)requiredr   r   r   r   r
   wrap   s   z ASRInterface.build.<locals>.wrapNr   )argparse	Namespacer   r   )clsr   r   kwargsr   argsr   r   r
   build   s
   
zASRInterface.buildc                 C      t d)ae  Compute loss for training.

        :param xs:
            For pytorch, batch of padded source sequences torch.Tensor (B, Tmax, idim)
            For chainer, list of source sequences chainer.Variable
        :param ilens: batch of lengths of source sequences (B)
            For pytorch, torch.Tensor
            For chainer, list of int
        :param ys:
            For pytorch, batch of padded source sequences torch.Tensor (B, Lmax)
            For chainer, list of source sequences chainer.Variable
        :return: loss value
        :rtype: torch.Tensor for pytorch, chainer.Variable for chainer
        z!forward method is not implementedNotImplementedErrorselfxsilensysr   r   r
   forward&   s   zASRInterface.forwardNc                 C   r   )aa  Recognize x for evaluation.

        :param ndarray x: input acouctic feature (B, T, D) or (T, D)
        :param namespace recog_args: argment namespace contraining options
        :param list char_list: list of characters
        :param torch.nn.Module rnnlm: language model module
        :return: N-best decoding results
        :rtype: list
        z#recognize method is not implementedr   r   x
recog_args	char_listrnnlmr   r   r
   	recognize7      
zASRInterface.recognizec                 C   r   )at  Beam search implementation for batch.

        :param torch.Tensor x: encoder hidden state sequences (B, Tmax, Henc)
        :param namespace recog_args: argument namespace containing options
        :param list char_list: list of characters
        :param torch.nn.Module rnnlm: language model module
        :return: N-best decoding results
        :rtype: list
        z$Batch decoding is not supported yet.r   r   r   r   r
   recognize_batchC   r%   zASRInterface.recognize_batchc                 C   r   )a_  Calculate attention.

        :param list xs: list of padded input sequences [(T1, idim), (T2, idim), ...]
        :param ndarray ilens: batch of lengths of input sequences (B)
        :param list ys: list of character id sequence tensor [(L1), (L2), (L3), ...]
        :return: attention weights (B, Lmax, Tmax)
        :rtype: float ndarray
        z2calculate_all_attentions method is not implementedr   r   r   r   r
   calculate_all_attentionsO      	z%ASRInterface.calculate_all_attentionsc                 C   r   )aj  Calculate CTC probability.

        :param list xs_pad: list of padded input sequences [(T1, idim), (T2, idim), ...]
        :param ndarray ilens: batch of lengths of input sequences (B)
        :param list ys: list of character id sequence tensor [(L1), (L2), (L3), ...]
        :return: CTC probabilities (B, Tmax, vocab)
        :rtype: float ndarray
        z1calculate_all_ctc_probs method is not implementedr   r   r   r   r
   calculate_all_ctc_probsZ   r(   z$ASRInterface.calculate_all_ctc_probsc                 C      ddl m} |S )zGet attention plot class.r   )PlotAttentionReport)espnet.asr.asr_utilsr+   )r   r+   r   r   r
   attention_plot_classe      z!ASRInterface.attention_plot_classc                 C   r*   )zGet CTC plot class.r   )PlotCTCReport)r,   r/   )r   r/   r   r   r
   ctc_plot_classl   r.   zASRInterface.ctc_plot_classc                 C   r   )zGet total subsampling factor.z6get_total_subsampling_factor method is not implementedr   r   r   r   r
   get_total_subsampling_factors   s   z)ASRInterface.get_total_subsampling_factorc                 C   r   )zEncode feature in `beam_search` (optional).

        Args:
            x (numpy.ndarray): input feature (T, D)
        Returns:
            torch.Tensor for pytorch, chainer.Variable for chainer:
                encoded feature (T, D)

        z encode method is not implementedr   )r   featr   r   r
   encodey   r%   zASRInterface.encodec                 C   r   )zGet scorers for `beam_search` (optional).

        Returns:
            dict[str, ScorerInterface]: dict of `ScorerInterface` objects

        z"decoders method is not implementedr   r1   r   r   r
   scorers   s   zASRInterface.scorers)NN)__name__
__module____qualname____doc__staticmethodr   classmethodintr   r   r$   r&   r'   r)   propertyr-   r0   r2   r4   r5   r   r   r   r
   r   	   s$    




r   z'espnet.nets.pytorch_backend.e2e_asr:E2Ez2espnet.nets.pytorch_backend.e2e_asr_transducer:E2Ez3espnet.nets.pytorch_backend.e2e_asr_transformer:E2Ez1espnet.nets.pytorch_backend.e2e_asr_conformer:E2E)rnn
transducertransformer	conformerz'espnet.nets.chainer_backend.e2e_asr:E2Ez3espnet.nets.chainer_backend.e2e_asr_transformer:E2E)r>   r@   )pytorchchainerc                 C   s0   t | t|t }t|tsJ |  d|S )zImport ASR models dynamically.

    Args:
        module (str): module_name:class_name or alias in `predefined_asr`
        backend (str): NN backend. e.g., pytorch, chainer

    Returns:
        type: ASR class

    z  does not implement ASRInterface)r   predefined_asrgetdict
issubclassr   )modulebackendmodel_classr   r   r
   dynamic_import_asr   s   rK   )r9   r   espnet.bin.asr_trainr   espnet.utils.dynamic_importr   espnet.utils.fill_missing_argsr   r   rD   rK   r   r   r   r
   <module>   s"     	