o
    h	                     @   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.text_classifiertextc                    sZ   t    | jj}d| _|j D ]\}}| dr!t	|| _q| jdkr+t
dd S )NentailzTCould not determine the entailment ID from the model config, please pass it at init.)supersetupmodelconfigentailment_idid2labelitemslower
startswithint
ValueError)selfr   idxlabel	__class__ \/var/www/html/ai/venv/lib/python3.10/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)return_tensorspadding)_labels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torchargmaxitemr$   )r   outputsr)   label_idr   r   r   decodeC   s   
zTextClassificationTool.decode)__name__
__module____qualname____doc__default_checkpointdescriptionnamer   pre_processor_classr   model_classinputsr-   r   r(   r/   __classcell__r   r   r   r   r      s    

	r   )r*   models.autor   r   baser   r   r   r   r   r   <module>   s   