o
    h$+                     @   s   d dl Z d dlZd dlmZ d dlZddlmZmZm	Z	m
Z
 ddlmZmZmZ e	 r1ddlmZ e
 r:ddlmZ d	d
 Zdd ZG dd deZeedddG dd deZdS )    N)Dict   )ExplicitEnumadd_end_docstringsis_tf_availableis_torch_available   )GenericTensorPipelinebuild_pipeline_init_args)2TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES)/MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMESc                 C   s   ddt |    S )Ng      ?)npexp)_outputs r   ~/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/transformers/pipelines/text_classification.pysigmoid   s   r   c                 C   s0   t j| ddd}t | | }||jddd S )NT)axiskeepdims)r   maxr   sum)r   maxesshifted_expr   r   r   softmax   s   r   c                   @   s   e Zd ZdZdZdZdS )ClassificationFunctionr   r   noneN)__name__
__module____qualname__SIGMOIDSOFTMAXNONEr   r   r   r   r      s    r   T)has_tokenizeraQ  
        return_all_scores (`bool`, *optional*, defaults to `False`):
            Whether to return all prediction scores or just the one of the predicted class.
        function_to_apply (`str`, *optional*, defaults to `"default"`):
            The function to apply to the model outputs in order to retrieve the scores. Accepts four different values:

            - `"default"`: if the model has a single label, will apply the sigmoid function on the output. If the model
              has several labels, will apply the softmax function on the output. In case of regression tasks, will not
              apply any function on the output.
            - `"sigmoid"`: Applies the sigmoid function on the output.
            - `"softmax"`: Applies the softmax function on the output.
            - `"none"`: Does not apply any function on the output.c                       sh   e Zd ZdZdZejZ fddZdddZ	 fd	d
Z
deeef fddZdd ZdddZ  ZS )TextClassificationPipelinea  
    Text classification pipeline using any `ModelForSequenceClassification`. See the [sequence classification
    examples](../task_summary#sequence-classification) for more information.

    Example:

    ```python
    >>> from transformers import pipeline

    >>> classifier = pipeline(model="distilbert/distilbert-base-uncased-finetuned-sst-2-english")
    >>> classifier("This movie is disgustingly good !")
    [{'label': 'POSITIVE', 'score': 1.0}]

    >>> classifier("Director tried too much.")
    [{'label': 'NEGATIVE', 'score': 0.996}]
    ```

    Learn more about the basics of using a pipeline in the [pipeline tutorial](../pipeline_tutorial)

    This text classification pipeline can currently be loaded from [`pipeline`] using the following task identifier:
    `"sentiment-analysis"` (for classifying sequences according to positive or negative sentiments).

    If multiple classification labels are available (`model.config.num_labels >= 2`), the pipeline will run a softmax
    over the results. If there is a single label, the pipeline will run a sigmoid over the result. In case of regression
    tasks (`model.config.problem_type == "regression"`), will not apply any function on the output.

    The models that this pipeline can use are models that have been fine-tuned on a sequence classification task. See
    the up-to-date list of available models on
    [huggingface.co/models](https://huggingface.co/models?filter=text-classification).
    Fc                    s4   t  jdi | | | jdkrt d S t d S )Ntfr   )super__init__check_model_type	frameworkr   r   )selfkwargs	__class__r   r   r(   T   s   
z#TextClassificationPipeline.__init__N c                 K   s   |}i }t | jjdr|d u r| jjj}t|ts|d u r&||d< d|d< n|d ur;tdt |r7d |d< nd|d< t|t	rFt
|  }|d urN||d< |i |fS )Nreturn_all_scorestop_kF_legacyz`return_all_scores` is now deprecated,  if want a similar functionality use `top_k=None` instead of `return_all_scores=True` or `top_k=1` instead of `return_all_scores=False`.r   function_to_apply)hasattrmodelconfigr0   
isinstanceintwarningswarnUserWarningstrr   upper)r+   r0   r3   r1   tokenizer_kwargspreprocess_paramspostprocess_paramsr   r   r   _sanitize_parameters]   s(   




z/TextClassificationPipeline._sanitize_parametersc                    s<   |f}t  j|i |}d|v}t|d tr|r|gS |S )a  
        Classify the text(s) given as inputs.

        Args:
            inputs (`str` or `List[str]` or `Dict[str]`, or `List[Dict[str]]`):
                One or several texts to classify. In order to use text pairs for your classification, you can send a
                dictionary containing `{"text", "text_pair"}` keys, or a list of those.
            top_k (`int`, *optional*, defaults to `1`):
                How many results to return.
            function_to_apply (`str`, *optional*, defaults to `"default"`):
                The function to apply to the model outputs in order to retrieve the scores. Accepts four different
                values:

                If this argument is not specified, then it will apply the following functions according to the number
                of labels:

                - If problem type is regression, will not apply any function on the output.
                - If the model has a single label, will apply the sigmoid function on the output.
                - If the model has several labels, will apply the softmax function on the output.

                Possible values are:

                - `"sigmoid"`: Applies the sigmoid function on the output.
                - `"softmax"`: Applies the softmax function on the output.
                - `"none"`: Does not apply any function on the output.

        Return:
            A list or a list of list of `dict`: Each result comes as list of dictionaries with the following keys:

            - **label** (`str`) -- The label predicted.
            - **score** (`float`) -- The corresponding probability.

            If `top_k` is used, one such dictionary is returned per label.
        r1   r   )r'   __call__r7   r<   )r+   inputsr,   resultr2   r-   r   r   rB   {   s   #z#TextClassificationPipeline.__call__returnc                 K   s   | j }t|tr| jdi |d|i|S t|trDt|dkrDt|d trDt|d dkrD| jd|d d |d d |d|S t|trMtd| j|fd|i|S )Nreturn_tensorsr   r   r   )text	text_pairrF   zThe pipeline received invalid inputs, if you are trying to send text pairs, you can try to send a dictionary `{"text": "My text", "text_pair": "My pair"}` in order to send a text pair.r   )r*   r7   dict	tokenizerlistlen
ValueError)r+   rC   r>   rF   r   r   r   
preprocess   s   
4
z%TextClassificationPipeline.preprocessc                 C   sF   | j dkr	| jjn| jj}dt|j v rd|d< | jdi |S )Npt	use_cacheFr   )r*   r5   forwardcallinspect	signature
parameterskeys)r+   model_inputsmodel_forwardr   r   r   _forward   s   z#TextClassificationPipeline._forwardr   Tc                    st  |d u rG j jjdkrtj}n8 j jjdks j jjdkr!tj}n& j jjdks/ j jjdkr3tj}nt j jdrD|d u rD j jj	}ntj}|d d } j
dkrY|  }n| }|tjkrgt|}n|tjkrqt|}n|tjkry|}ntd	| |dkr|r j jj|   |  d
S  fddt|D }|s|jdd dd |d ur|d | }|S )N
regressionmulti_label_classificationr   single_label_classificationr3   logitsr   rO   z+Unrecognized `function_to_apply` argument: labelscorec                    s(   g | ]\}} j jj| | d qS )r^   )r5   r6   id2labelitem).0ir`   r+   r   r   
<listcomp>   s    z:TextClassificationPipeline.postprocess.<locals>.<listcomp>c                 S   s   | d S )Nr`   r   )xr   r   r   <lambda>   s    z8TextClassificationPipeline.postprocess.<locals>.<lambda>T)keyreverse)r5   r6   problem_typer   r#   
num_labelsr!   r"   r4   r3   r*   floatnumpyr   r   rM   ra   argmaxrb   r   	enumeratesort)r+   model_outputsr3   r1   r2   outputsscoresdict_scoresr   re   r   postprocess   s>   





$
z&TextClassificationPipeline.postprocess)NNr/   )Nr   T)r   r   r    __doc__r0   r   r#   r3   r(   rA   rB   r   r<   r	   rN   rY   rv   __classcell__r   r   r-   r   r%   "   s    
	-r%   )rS   r9   typingr   rn   r   utilsr   r   r   r   baser	   r
   r   models.auto.modeling_tf_autor   models.auto.modeling_autor   r   r   r   r%   r   r   r   r   <module>   s$    