o
    %ݫi                     @   sd   d Z ddlZddlZddlm  mZ ddlmZ ddl	m
Z
 e
eZdejd< G dd deZdS )	zThis lobe enables the integration of huggingface pretrained LaBSE models.
Reference: https://arxiv.org/abs/2007.01852

Transformer from HuggingFace needs to be installed:
https://huggingface.co/transformers/installation.html

Authors
 * Ha Nguyen 2023
    N)HFTransformersInterface)
get_loggerfalseTOKENIZERS_PARALLELISMc                       s.   e Zd ZdZ		d fdd	Zdd Z  ZS )LaBSEa,  This lobe enables the integration of HuggingFace and SpeechBrain
    pretrained LaBSE models.

    Source paper LaBSE: https://arxiv.org/abs/2007.01852
    Transformer from HuggingFace needs to be installed:
    https://huggingface.co/transformers/installation.html

    The model can be used as a fixed text-based sentence-level embeddings generator or can be finetuned.
    It will download automatically the model from HuggingFace or use a local path.

    Arguments
    ---------
    source : str
        HuggingFace hub name: e.g "setu4993/LaBSE"
    save_path : str
        Path (dir) of the downloaded model.
    freeze : bool (default: True)
        If True, the model is frozen. If False, the model will be trained
        alongside with the rest of the pipeline.
    output_norm : bool (default: True)
        If True, normalize the output.
    Example
    -------
    >>> inputs = ["La vie est belle"]
    >>> model_hub = "setu4993/smaller-LaBSE"
    >>> save_path = "savedir"
    >>> model = LaBSE(model_hub, save_path)
    >>> outputs = model(inputs)
    Tc                    s(   t  j|||d | j|d || _d S )N)source	save_pathfreeze)r   )super__init__load_tokenizeroutput_norm)selfr   r   r	   r   	__class__ k/home/ubuntu/.local/lib/python3.10/site-packages/speechbrain/lobes/models/huggingface_transformers/labse.pyr   9   s   
zLaBSE.__init__c                 C   s   | j rLt = | j|ddd}| D ]}|| j| jjd||< d|| _q| jd	i |j	}| j
r;tj|dd}|W  d   S 1 sGw   Y  | j|ddd}| D ]}|| j| jjd||< qX| jd	i |j	}| j
rztj|dd}|S )
zThis method implements a forward of the labse model,
        which generates sentence-level embeddings from input text.

        Arguments
        ----------
        input_texts (translation): list
            The list of texts (required).
        ptT)return_tensorspadding)deviceF   )pNr   )r	   torchno_grad	tokenizerkeystomodelr   requires_gradpooler_outputr   F	normalize)r   input_textskey
embeddingsr   r   r   forwardF   s0   

 zLaBSE.forward)TT)__name__
__module____qualname____doc__r   r&   __classcell__r   r   r   r   r      s    "r   )r*   osr   torch.nn.functionalnn
functionalr!   =speechbrain.lobes.models.huggingface_transformers.huggingfacer   speechbrain.utils.loggerr   r'   loggerenvironr   r   r   r   r   <module>   s    

