U
    9%e	                     @   s8   d dl Z ddlmZmZ ddlmZ G dd deZdS )    N   )"AutoModelForSequenceClassificationAutoTokenizer   )PipelineToolc                       sT   e Zd ZdZdZdZdZeZe	Z
ddggZdgZ fddZdd	 Zd
d Z  ZS )TextClassificationToolz
    Example:

    ```py
    from transformers.tools import TextClassificationTool

    classifier = TextClassificationTool()
    classifier("This is a super nice API!", labels=["positive", "negative"])
    ```
    zfacebook/bart-large-mnlia*  This is a tool that classifies an English text using provided labels. It takes two inputs: `text`, which should be the text to classify, and `labels`, which should be the list of labels to use for classification. It returns the most likely label in the list of provided `labels` for the input text.Ztext_classifiertextc                    sZ   t    | jj}d| _|j D ] \}}| dr"t	|| _q"| jdkrVt
dd S )NZentailzTCould not determine the entailment ID from the model config, please pass it at init.)supersetupmodelconfigZentailment_idZid2labelitemslower
startswithint
ValueError)selfr   idxlabel	__class__ e/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/transformers/tools/text_classification.pyr   0   s    

zTextClassificationTool.setupc                 C   s,   || _ | j|gt| dd |D dddS )Nc                 S   s   g | ]}d | qS )zThis example is r   ).0r   r   r   r   
<listcomp>>   s     z1TextClassificationTool.encode.<locals>.<listcomp>pt
max_length)Zreturn_tensorspadding)_labelsZpre_processorlen)r   r   labelsr   r   r   encode:   s    zTextClassificationTool.encodec                 C   s*   |j }t|d d df  }| j| S )Nr   )logitstorchZargmaxitemr   )r   outputsr#   Zlabel_idr   r   r   decodeC   s    zTextClassificationTool.decode)__name__
__module____qualname____doc__Zdefault_checkpointdescriptionnamer   Zpre_processor_classr   Zmodel_classinputsr&   r   r"   r'   __classcell__r   r   r   r   r      s   

	r   )r$   Zmodels.autor   r   baser   r   r   r   r   r   <module>   s   