U
    ,:%e                     @   s|   d dl mZ d dlmZ d dlmZmZmZmZ eG dd dZ	e	dddZ
d	e
_eG d
d dZedddZde_dS )    )	dataclass)load_state_dict_from_url)squim_objective_basesquim_subjective_baseSquimObjectiveSquimSubjectivec                   @   sJ   e Zd ZU dZeed< eed< dd Zddedd	d
Z	e
dd ZdS )SquimObjectiveBundleu,  Data class that bundles associated information to use pretrained
    :py:class:`~torchaudio.models.SquimObjective` model.

    This class provides interfaces for instantiating the pretrained model along with
    the information necessary to retrieve pretrained weights and additional data
    to be used with the model.

    Torchaudio library instantiates objects of this class, each of which represents
    a different pretrained model. Client code should access pretrained models via these
    instances.

    This bundle can estimate objective metric scores for speech enhancement, such as STOI, PESQ, Si-SDR.
    A typical use case would be a flow like `waveform -> list of scores`. Please see below for the code example.

    Example: Estimate the objective metric scores for the input waveform.
        >>> import torch
        >>> import torchaudio
        >>> from torchaudio.pipelines import SQUIM_OBJECTIVE as bundle
        >>>
        >>> # Load the SquimObjective bundle
        >>> model = bundle.get_model()
        Downloading: "https://download.pytorch.org/torchaudio/models/squim_objective_dns2020.pth"
        100%|████████████| 28.2M/28.2M [00:03<00:00, 9.24MB/s]
        >>>
        >>> # Resample audio to the expected sampling rate
        >>> waveform = torchaudio.functional.resample(waveform, sample_rate, bundle.sample_rate)
        >>>
        >>> # Estimate objective metric scores
        >>> scores = model(waveform)
        >>> print(f"STOI: {scores[0].item()},  PESQ: {scores[1].item()}, SI-SDR: {scores[2].item()}.")
    _path_sample_ratec                 C   s,   d| j  }|d kri n|}t|f|}|S Nz/https://download.pytorch.org/torchaudio/models/r	   r   self	dl_kwargsurlZ
state_dict r   c/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/torchaudio/pipelines/_squim_pipeline.py_get_state_dict-   s    z$SquimObjectiveBundle._get_state_dictNr   returnc                C   s"   t  }|| | |  |S )a  Construct the SquimObjective model, and load the pretrained weight.

        The weight file is downloaded from the internet and cached with
        :func:`torch.hub.load_state_dict_from_url`

        Args:
            dl_kwargs (dictionary of keyword arguments): Passed to :func:`torch.hub.load_state_dict_from_url`.

        Returns:
            Variation of :py:class:`~torchaudio.models.SquimObjective`.
        )r   load_state_dictr   evalr   r   modelr   r   r   	get_model3   s    zSquimObjectiveBundle.get_modelc                 C   s   | j S zUSample rate of the audio that the model is trained on.

        :type: float
        r
   r   r   r   r   sample_rateD   s    z SquimObjectiveBundle.sample_rate)__name__
__module____qualname____doc__str__annotations__floatr   r   r   propertyr   r   r   r   r   r      s   
 r   zsquim_objective_dns2020.pthi>  r   a  SquimObjective pipeline trained using approach described in
    :cite:`kumar2023torchaudio` on the *DNS 2020 Dataset* :cite:`reddy2020interspeech`.

    The underlying model is constructed by :py:func:`torchaudio.models.squim_objective_base`.
    The weights are under `Creative Commons Attribution 4.0 International License
    <https://github.com/microsoft/DNS-Challenge/blob/interspeech2020/master/LICENSE>`__.

    Please refer to :py:class:`SquimObjectiveBundle` for usage instructions.
    c                   @   sJ   e Zd ZU dZeed< eed< dd Zddedd	d
Z	e
dd ZdS )SquimSubjectiveBundleu  Data class that bundles associated information to use pretrained
    :py:class:`~torchaudio.models.SquimSubjective` model.

    This class provides interfaces for instantiating the pretrained model along with
    the information necessary to retrieve pretrained weights and additional data
    to be used with the model.

    Torchaudio library instantiates objects of this class, each of which represents
    a different pretrained model. Client code should access pretrained models via these
    instances.

    This bundle can estimate subjective metric scores for speech enhancement, such as MOS.
    A typical use case would be a flow like `waveform -> score`. Please see below for the code example.

    Example: Estimate the subjective metric scores for the input waveform.
        >>> import torch
        >>> import torchaudio
        >>> from torchaudio.pipelines import SQUIM_SUBJECTIVE as bundle
        >>>
        >>> # Load the SquimSubjective bundle
        >>> model = bundle.get_model()
        Downloading: "https://download.pytorch.org/torchaudio/models/squim_subjective_bvcc_daps.pth"
        100%|████████████| 360M/360M [00:09<00:00, 41.1MB/s]
        >>>
        >>> # Resample audio to the expected sampling rate
        >>> waveform = torchaudio.functional.resample(waveform, sample_rate, bundle.sample_rate)
        >>> # Use a clean reference (doesn't need to be the reference for the waveform) as the second input
        >>> reference = torchaudio.functional.resample(reference, sample_rate, bundle.sample_rate)
        >>>
        >>> # Estimate subjective metric scores
        >>> score = model(waveform, reference)
        >>> print(f"MOS: {score}.")
    r	   r
   c                 C   s,   d| j  }|d kri n|}t|f|}|S r   r   r   r   r   r   r      s    z%SquimSubjectiveBundle._get_state_dictNr   r   c                C   s"   t  }|| | |  |S )a  Construct the SquimSubjective model, and load the pretrained weight.

        The weight file is downloaded from the internet and cached with
        :func:`torch.hub.load_state_dict_from_url`

        Args:
            dl_kwargs (dictionary of keyword arguments): Passed to :func:`torch.hub.load_state_dict_from_url`.

        Returns:
            Variation of :py:class:`~torchaudio.models.SquimObjective`.
        )r   r   r   r   r   r   r   r   r      s    zSquimSubjectiveBundle.get_modelc                 C   s   | j S r   r   r   r   r   r   r      s    z!SquimSubjectiveBundle.sample_rate)r    r!   r"   r#   r$   r%   r&   r   r   r   r'   r   r   r   r   r   r(   \   s   
"r(   zsquim_subjective_bvcc_daps.ptha
  SquimSubjective pipeline trained
    as described in :cite:`manocha2022speech` and :cite:`kumar2023torchaudio`
    on the *BVCC* :cite:`cooper2021voices` and *DAPS* :cite:`mysore2014can` datasets.

    The underlying model is constructed by :py:func:`torchaudio.models.squim_subjective_base`.
    The weights are under `Creative Commons Attribution Non Commercial 4.0 International
    <https://zenodo.org/record/4660670#.ZBtWPOxuerN>`__.

    Please refer to :py:class:`SquimSubjectiveBundle` for usage instructions.
    N)dataclassesr   Ztorchaudio._internalr   Ztorchaudio.modelsr   r   r   r   r   ZSQUIM_OBJECTIVEr#   r(   ZSQUIM_SUBJECTIVEr   r   r   r   <module>   s    DF