Ë
    HêñiÞo  ã                   óò   — d dl Z d dlZd dlmZmZ d dlZddlmZ ddl	m
Z
mZmZ ddlmZmZmZmZ  e«       r
d dlZddlmZ  G d	„ d
e«      Z G d„ de
«      Z e ed¬«      d«       G d„ de«      «       ZeZy)é    N)ÚAnyÚoverloadé   )ÚBasicTokenizer)ÚExplicitEnumÚadd_end_docstringsÚis_torch_availableé   )ÚArgumentHandlerÚChunkPipelineÚDatasetÚbuild_pipeline_init_args)Ú,MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMESc                   ó(   — e Zd ZdZdeee   z  fd„Zy)Ú"TokenClassificationArgumentHandlerz5
    Handles arguments for token classification.
    Úinputsc                 ó   — |j                  dd«      }|j                  d«      }|�;t        |t        t        f«      r%t	        |«      dkD  rt        |«      }t	        |«      }nWt        |t
        «      r|g}d}nAt        �t        |t        «      st        |t        j                  «      r||d |fS t        d«      ‚|j                  d«      }|r?t        |t        «      rt        |d   t        «      r|g}t	        |«      |k7  rt        d«      ‚||||fS )	NÚis_split_into_wordsFÚ	delimiterr   r
   zAt least one input is required.Úoffset_mappingz;offset_mapping should have the same batch size as the input)
ÚgetÚ
isinstanceÚlistÚtupleÚlenÚstrr   ÚtypesÚGeneratorTypeÚ
ValueError)Úselfr   Úkwargsr   r   Ú
batch_sizer   s          úm/var/www/pod-logistic/pod-ai/venv/lib/python3.12/site-packages/transformers/pipelines/token_classification.pyÚ__call__z+TokenClassificationArgumentHandler.__call__   sÿ   € Ø$Ÿj™jÐ)>ÀÓFÐØ—J‘J˜{Ó+ˆ	àÐ¤*¨V´d¼E°]Ô"CÌÈFËÐVWÊÜ˜&“\ˆFÜ˜V›‰JÜ˜¤Ô$Ø�XˆFØ‰JÜÐ ¤Z°¼Ô%@ÄJÈvÔW\×WjÑWjÔDkØÐ.°°iÐ?Ð?äÐ>Ó?Ð?àŸ™Ð$4Ó5ˆÙÜ˜.¬$Ô/´J¸~ÈaÑ?PÔRWÔ4XØ"0Ð!1�Ü�>Ó" jÒ0Ü Ð!^Ó_Ð_ØÐ*¨N¸IÐEÐEó    N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r$   © r%   r#   r   r      s   „ ñðF˜s T¨#¡Y™ô Fr%   r   c                   ó$   — e Zd ZdZdZdZdZdZdZy)ÚAggregationStrategyzDAll the valid aggregation strategies for TokenClassificationPipelineÚnoneÚsimpleÚfirstÚaverageÚmaxN)	r&   r'   r(   r)   ÚNONEÚSIMPLEÚFIRSTÚAVERAGEÚMAXr*   r%   r#   r,   r,   3   s   „ ÙNà€DØ€FØ€EØ€GØ
�Cr%   r,   T)Úhas_tokenizeraÙ	  
        ignore_labels (`list[str]`, defaults to `["O"]`):
            A list of labels to ignore.
        stride (`int`, *optional*):
            If stride is provided, the pipeline is applied on all the text. The text is split into chunks of size
            model_max_length. Works only with fast tokenizers and `aggregation_strategy` different from `NONE`. The
            value of this argument defines the number of overlapping tokens between chunks. In other words, the model
            will shift forward by `tokenizer.model_max_length - stride` tokens each step.
        aggregation_strategy (`str`, *optional*, defaults to `"none"`):
            The strategy to fuse (or not) tokens based on the model prediction.

                - "none" : Will simply not do any aggregation and simply return raw results from the model
                - "simple" : Will attempt to group entities following the default schema. (A, B-TAG), (B, I-TAG), (C,
                  I-TAG), (D, B-TAG2) (E, B-TAG2) will end up being [{"word": ABC, "entity": "TAG"}, {"word": "D",
                  "entity": "TAG2"}, {"word": "E", "entity": "TAG2"}] Notice that two consecutive B tags will end up as
                  different entities. On word based languages, we might end up splitting words undesirably : Imagine
                  Microsoft being tagged as [{"word": "Micro", "entity": "ENTERPRISE"}, {"word": "soft", "entity":
                  "NAME"}]. Look for FIRST, MAX, AVERAGE for ways to mitigate that and disambiguate words (on languages
                  that support that meaning, which is basically tokens separated by a space). These mitigations will
                  only work on real words, "New york" might still be tagged with two different entities.
                - "first" : (works only on word based models) Will use the `SIMPLE` strategy except that words, cannot
                  end up with different tags. Words will simply use the tag of the first token of the word when there
                  is ambiguity.
                - "average" : (works only on word based models) Will use the `SIMPLE` strategy except that words,
                  cannot end up with different tags. scores will be averaged first across tokens, and then the maximum
                  label is applied.
                - "max" : (works only on word based models) Will use the `SIMPLE` strategy except that words, cannot
                  end up with different tags. Word entity will simply be the token with the maximum score.c                   óÚ  ‡ — e Zd ZdZdZdZdZdZdZ e	«       fˆ fd„	Z
	 	 	 	 	 	 d'dedz  deeeef      dz  d	ed
edz  dedz  f
d„Zedededeeeef      fd„«       Zedee   dedeeeeef         fd„«       Zdeee   z  dedeeeef      eeeeef         z  fˆ fd„Zd(d„Zd„ Zej0                  dfd„Zd„ Z	 	 d)dedej8                  dej8                  deeeef      dz  dej8                  dedeedz     dz  deeeef      dz  dee   fd„Zdee   dedee   fd„Zd ee   dedefd!„Zd ee   dedee   fd"„Z d ee   defd#„Z!d$edeeef   fd%„Z"d ee   dee   fd&„Z#ˆ xZ$S )*ÚTokenClassificationPipelineuv	  
    Named Entity Recognition pipeline using any `ModelForTokenClassification`. See the [named entity recognition
    examples](../task_summary#named-entity-recognition) for more information.

    Example:

    ```python
    >>> from transformers import pipeline

    >>> token_classifier = pipeline(model="Jean-Baptiste/camembert-ner", aggregation_strategy="simple")
    >>> sentence = "Je m'appelle jean-baptiste et je vis Ã  montrÃ©al"
    >>> tokens = token_classifier(sentence)
    >>> tokens
    [{'entity_group': 'PER', 'score': 0.9931, 'word': 'jean-baptiste', 'start': 12, 'end': 26}, {'entity_group': 'LOC', 'score': 0.998, 'word': 'montrÃ©al', 'start': 38, 'end': 47}]

    >>> token = tokens[0]
    >>> # Start and end provide an easy way to highlight words in the original text.
    >>> sentence[token["start"] : token["end"]]
    ' jean-baptiste'

    >>> # Some models use the same idea to do part of speech.
    >>> syntaxer = pipeline(model="vblagoje/bert-english-uncased-finetuned-pos", aggregation_strategy="simple")
    >>> syntaxer("My name is Sarah and I live in London")
    [{'entity_group': 'PRON', 'score': 0.999, 'word': 'my', 'start': 0, 'end': 2}, {'entity_group': 'NOUN', 'score': 0.997, 'word': 'name', 'start': 3, 'end': 7}, {'entity_group': 'AUX', 'score': 0.994, 'word': 'is', 'start': 8, 'end': 10}, {'entity_group': 'PROPN', 'score': 0.999, 'word': 'sarah', 'start': 11, 'end': 16}, {'entity_group': 'CCONJ', 'score': 0.999, 'word': 'and', 'start': 17, 'end': 20}, {'entity_group': 'PRON', 'score': 0.999, 'word': 'i', 'start': 21, 'end': 22}, {'entity_group': 'VERB', 'score': 0.998, 'word': 'live', 'start': 23, 'end': 27}, {'entity_group': 'ADP', 'score': 0.999, 'word': 'in', 'start': 28, 'end': 30}, {'entity_group': 'PROPN', 'score': 0.999, 'word': 'london', 'start': 31, 'end': 37}]
    ```

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

    This token recognition pipeline can currently be loaded from [`pipeline`] using the following task identifier:
    `"ner"` (for predicting the classes of tokens in a sequence: person, organisation, location or miscellaneous).

    The models that this pipeline can use are models that have been fine-tuned on a token classification task. See the
    up-to-date list of available models on
    [huggingface.co/models](https://huggingface.co/models?filter=token-classification).
    Ú	sequencesFTc                 ó~   •— t        ‰| �  di |¤Ž | j                  t        «       t	        d¬«      | _        || _        y )NF)Údo_lower_caser*   )ÚsuperÚ__init__Úcheck_model_typer   r   Ú_basic_tokenizerÚ_args_parser)r    Úargs_parserr!   Ú	__class__s      €r#   r>   z$TokenClassificationPipeline.__init__ˆ   s7   ø€ Ü‰ÑÑ"˜6Ò"à×ÑÔJÔKä .¸UÔ CˆÔØ'ˆÕr%   NÚaggregation_strategyr   r   Ústrider   c                 ó:  — i }||d<   |r	|€dn||d<   |�||d<   i }|�~t        |t        «      rt        |j                  «          }|t        j                  t        j
                  t        j                  hv r!| j                  j                  st        d«      ‚||d<   |�||d<   |�s|| j                  j                  k\  rt        d«      ‚|t        j                  k(  rt        d	|› d
�«      ‚| j                  j                  rdd|dœ}	|	|d<   nt        d«      ‚|i |fS )Nr   ú r   r   z{Slow tokenizers cannot handle subwords. Please set the `aggregation_strategy` option to `"simple"` or use a fast tokenizer.rD   Úignore_labelszl`stride` must be less than `tokenizer.model_max_length` (or even lower if the tokenizer adds special tokens)zI`stride` was provided to process all the text but `aggregation_strategy="z&"`, please select another one instead.T)Úreturn_overflowing_tokensÚpaddingrE   Útokenizer_paramszm`stride` was provided to process all the text but you're using a slow tokenizer. Please use a fast tokenizer.)r   r   r,   Úupperr4   r6   r5   Ú	tokenizerÚis_fastr   Úmodel_max_lengthr2   )
r    rH   rD   r   r   rE   r   Úpreprocess_paramsÚpostprocess_paramsrK   s
             r#   Ú_sanitize_parametersz0TokenClassificationPipeline._sanitize_parameters�   s}  € ð ÐØ3FÐÐ/Ñ0áØ4=Ð4E©SÈ9Ð˜kÑ*àÐ%Ø2@ÐÐ.Ñ/àÐØÐ+ÜÐ.´Ô4Ü':Ð;O×;UÑ;UÓ;WÑ'XÐ$à$Ü'×-Ñ-Ô/B×/FÑ/FÔH[×HcÑHcÐdñeàŸ™×.Ò.ä ð>óð ð :NÐÐ5Ñ6ØÐ$Ø2?Ð˜Ñ/ØÐØ˜Ÿ™×8Ñ8Ò8Ü ð Cóð ð $Ô':×'?Ñ'?Ò?Ü ðØ,Ð-Ð-SðUóð ð
 —>‘>×)Ò)à59Ø#'Ø"(ñ(Ð$ð
 =MÐ%Ð&8Ò9ä$ð8óð ð ! "Ð&8Ð8Ð8r%   r   r!   Úreturnc                  ó   — y ©Nr*   ©r    r   r!   s      r#   r$   z$TokenClassificationPipeline.__call__Ë   s   € ØLOr%   c                  ó   — y rU   r*   rV   s      r#   r$   z$TokenClassificationPipeline.__call__Î   s   € ØX[r%   c                 óÀ   •—  | j                   |fi |¤Ž\  }}}}||d<   ||d<   |r#t        d„ |D «       «      st        ‰| �  |gfi |¤ŽS |r||d<   t        ‰| �  |fi |¤ŽS )a  
        Classify each token of the text(s) given as inputs.

        Args:
            inputs (`str` or `List[str]`):
                One or several texts (or one list of texts) for token classification. Can be pre-tokenized when
                `is_split_into_words=True`.

        Return:
            A list or a list of list of `dict`: Each result comes as a list of dictionaries (one for each token in the
            corresponding input, or each entity if this pipeline was instantiated with an aggregation_strategy) with
            the following keys:

            - **word** (`str`) -- The token/word classified. This is obtained by decoding the selected tokens. If you
              want to have the exact string in the original sentence, use `start` and `end`.
            - **score** (`float`) -- The corresponding probability for `entity`.
            - **entity** (`str`) -- The entity predicted for that token/word (it is named *entity_group* when
              *aggregation_strategy* is not `"none"`.
            - **index** (`int`, only present when `aggregation_strategy="none"`) -- The index of the corresponding
              token in the sentence.
            - **start** (`int`, *optional*) -- The index of the start of the corresponding entity in the sentence. Only
              exists if the offsets are available within the tokenizer
            - **end** (`int`, *optional*) -- The index of the end of the corresponding entity in the sentence. Only
              exists if the offsets are available within the tokenizer
        r   r   c              3   ó<   K  — | ]  }t        |t        «      –— Œ y ­wrU   )r   r   )Ú.0Úinputs     r#   ú	<genexpr>z7TokenClassificationPipeline.__call__.<locals>.<genexpr>ï   s   è ø€ Ò*WÀu¬:°e¼T×+BÑ*Wùs   ‚r   )rA   Úallr=   r$   )r    r   r!   Ú_inputsr   r   r   rC   s          €r#   r$   z$TokenClassificationPipeline.__call__Ñ   sŠ   ø€ ð6 CTÀ$×BSÑBSÐTZÑBeÐ^dÑBeÑ?ˆÐ$ n°iØ(;ˆÐ$Ñ%Ø'ˆˆ{ÑÙ¤sÑ*WÐPVÔ*WÔ'WÜ‘7Ñ# V HÑ7°Ñ7Ð7ÙØ'5ˆFÐ#Ñ$ä‰wÑ Ñ1¨&Ñ1Ð1r%   c           	   +   óÎ  K  — |j                  di «      }| j                  j                  xr | j                  j                  dkD  }d }|d   }|r�|d   }t        |t        «      st        d«      ‚|}	|j                  |	«      }g }t        |«      }
d}|	D ]2  }|j                  ||t        |«      z   f«       |t        |«      |
z   z  }Œ4 |	}d|d<   nt        |t        «      st        d«      ‚|} | j                  |fd|d| j                  j                  d	œ|¤Ž}|r!| j                  j                  st        d
«      ‚|j                  dd «       t        |d   «      }t        |«      D ]t  }|j                  «       D ��ci c]  \  }}|||   j                  d«      “Œ }}}|�||d<   |dk(  r|nd |d<   ||dz
  k(  |d<   |�|j                  |«      |d<   ||d<   |–— Œv y c c}}w ­w)NrK   r   r   r   zEWhen `is_split_into_words=True`, `sentence` must be a list of tokens.TzKWhen `is_split_into_words=False`, `sentence` must be an untokenized string.Úpt)Úreturn_tensorsÚ
truncationÚreturn_special_tokens_maskÚreturn_offsets_mappingz@is_split_into_words=True is only supported with fast tokenizers.Úoverflow_to_sample_mappingÚ	input_idsr   Úsentencer
   Úis_lastÚword_idsÚword_to_chars_map)ÚpoprM   rO   r   r   r   Újoinr   Úappendr   rN   ÚrangeÚitemsÚ	unsqueezeri   )r    rg   r   rP   rK   rb   rj   r   r   ÚwordsÚdelimiter_lenÚchar_offsetÚwordÚtext_to_tokenizer   Ú
num_chunksÚiÚkÚvÚmodel_inputss                       r#   Ú
preprocessz&TokenClassificationPipeline.preprocessö   s!  è ø€ Ø,×0Ñ0Ð1CÀRÓHÐØ—^‘^×4Ñ4Ò\¸¿¹×9XÑ9XÐ[\Ñ9\ˆ
à ÐØ/Ð0EÑFÐÙØ)¨+Ñ6ˆIÜ˜h¬Ô-Ü Ð!hÓiÐiØˆEØ —~‘~ eÓ,ˆHà "ÐÜ 	›NˆMØˆKØò 9�Ø!×(Ñ(¨+°{ÄSÈÃYÑ7NÐ)OÔPØœs 4›y¨=Ñ8Ñ8‘ð9ð
  %ÐØ6:ÐÐ2Ò3ä˜h¬Ô,Ü Ð!nÓoÐoØ'Ðà�—‘Øð
àØ!Ø'+Ø#'§>¡>×#9Ñ#9ñ
ð ñ
ˆñ  t§~¡~×'=Ò'=ÜÐ_Ó`Ð`à�
‰
Ð/°Ô6Ü˜ Ñ,Ó-ˆ
ä�zÓ"ò 	ˆAØ=C¿\¹\»^×L±T°Q¸˜A˜q ™tŸ~™~¨aÓ0Ñ0ÐLˆLÑLØÐ)Ø1?�Ð-Ñ.à34¸²6¡x¸tˆL˜Ñ$Ø&'¨:¸©>Ñ&9ˆL˜Ñ#Ø Ð,Ø+1¯?©?¸1Ó+=�˜ZÑ(Ø4E�Ð0Ñ1àÓñ	ùÛLùs   ‚E;G%Å=GÆA	G%c                 óD  — |j                  d«      }|j                  dd «      }|j                  d«      }|j                  d«      }|j                  dd «      }|j                  dd «      } | j                  d
i |¤Ž}t        |t        «      r|d   n|d   }	|	||||||d	œ|¥S )NÚspecial_tokens_maskr   rg   rh   ri   rj   Úlogitsr   )r~   r}   r   rg   rh   ri   rj   r*   )rk   Úmodelr   Údict)
r    rz   r}   r   rg   rh   ri   rj   Úoutputr~   s
             r#   Ú_forwardz$TokenClassificationPipeline._forward.  sÄ   € à*×.Ñ.Ð/DÓEÐØ%×)Ñ)Ð*:¸DÓAˆØ×#Ñ# JÓ/ˆØ×"Ñ" 9Ó-ˆØ×#Ñ# J°Ó5ˆØ(×,Ñ,Ð-@À$ÓGÐà�—‘Ñ+˜lÑ+ˆÜ%/°¼Ô%=�˜Ò!À6È!Á9ˆð Ø#6Ø,Ø ØØ Ø!2ñ	
ð ð	
ð 		
r%   c                 ó�  — |€dg}g }|d   j                  d«      }|D �]~  }|d   d   j                  t        j                  t        j                  fv r4|d   d   j                  t        j                  «      j                  «       }n|d   d   j                  «       }|d   d   }|d   d   }	|d   �|d   d   nd }
|d   d   j                  «       }|j                  d	«      }t        j                  |d
d¬«      }t        j                  ||z
  «      }||j                  d
d¬«      z  }| j                  ||	||
||||¬«      }| j                  ||«      }|D �cg c],  }|j                  dd «      |vr|j                  dd «      |vr|‘Œ. }}|j                  |«       �Œ� t        |«      }|dkD  r| j!                  |«      }|S c c}w )NÚOr   rj   r~   rg   rf   r   r}   ri   éÿÿÿÿT)ÚaxisÚkeepdims)ri   rj   ÚentityÚentity_groupr
   )r   ÚdtypeÚtorchÚbfloat16Úfloat16ÚtoÚfloat32ÚnumpyÚnpr1   ÚexpÚsumÚgather_pre_entitiesÚ	aggregateÚextendr   Úaggregate_overlapping_entities)r    Úall_outputsrD   rH   Úall_entitiesrj   Úmodel_outputsr~   rg   rf   r   r}   ri   ÚmaxesÚshifted_expÚscoresÚpre_entitiesÚgrouped_entitiesrˆ   Úentitiesrv   s                        r#   Úpostprocessz'TokenClassificationPipeline.postprocessE  s
  € ØÐ Ø ˜EˆMØˆð (¨™N×.Ñ.Ð/BÓCÐà(ó $	*ˆMØ˜XÑ& qÑ)×/Ñ/´E·N±NÄEÇMÁMÐ3RÑRØ& xÑ0°Ñ3×6Ñ6´u·}±}ÓE×KÑKÓM‘à& xÑ0°Ñ3×9Ñ9Ó;�à" 1‘~ jÑ1ˆHØ% kÑ2°1Ñ5ˆIà6CÐDTÑ6UÐ6a�Ð.Ñ/°Ò2Ðgkð ð #0Ð0EÑ"FÀqÑ"I×"OÑ"OÓ"QÐØ$×(Ñ(¨Ó4ˆHä—F‘F˜6¨°TÔ:ˆEÜŸ&™& ¨%¡Ó0ˆKØ  ;§?¡?¸ÀT ?Ó#JÑJˆFà×3Ñ3ØØØØØ#Ø$Ø!Ø"3ð 4ó 	ˆLð  $Ÿ~™~¨lÐ<PÓQÐð /öàØ—:‘:˜h¨Ó-°]ÑBØ—J‘J˜~¨tÓ4¸MÑIò ðˆHð ð ×Ñ Ö)ðI$	*ôJ ˜Ó%ˆ
Ø˜Š>Ø×>Ñ>¸|ÓLˆLØÐùòs   Å1Gc                 ó4  — t        |«      dk(  r|S t        |d„ ¬«      }g }|d   }|D ]\  }|d   |d   cxk  r|d   k  r3n n0|d   |d   z
  }|d   |d   z
  }||kD  s||k(  sŒ;|d   |d   kD  sŒG|}ŒJ|j                  |«       |}Œ^ |j                  |«       |S )Nr   c                 ó   — | d   S )NÚstartr*   )Úxs    r#   ú<lambda>zLTokenClassificationPipeline.aggregate_overlapping_entities.<locals>.<lambda>z  s
   € °!°G±*€ r%   ©Úkeyr¤   ÚendÚscore)r   Úsortedrm   )r    r    Úaggregated_entitiesÚprevious_entityrˆ   Úcurrent_lengthÚprevious_lengths          r#   r—   z:TokenClassificationPipeline.aggregate_overlapping_entitiesw  sÎ   € Üˆx‹=˜AÒØˆOÜ˜(Ñ(<Ô=ˆØ ÐØ" 1™+ˆØò 	)ˆFØ˜wÑ'¨6°'©?ÔS¸_ÈUÑ=SÕSØ!'¨¡°¸±Ñ!@�Ø"1°%Ñ"8¸?È7Ñ;SÑ"S�à" _Ò4Ø%¨Ó8Ø˜w™¨/¸'Ñ*BÓBà&,‘Oà#×*Ñ*¨?Ô;Ø"(‘ð	)ð 	×"Ñ" ?Ô3Ø"Ð"r%   rg   rf   r�   r}   ri   rj   c	                 óN  — g }	t        |«      D �]“  \  }
}||
   rŒ| j                  j                  t        ||
   «      «      }|��=||
   \  }}|�|�||
   }|�||   \  }}||z  }||z  }t	        |t        «      s |j                  «       }|j                  «       }||| }t        | j                  dd«      rCt        | j                  j                  j                  dd«      rt        |«      t        |«      k7  }n_|t        j                  t        j                  t        j                  hv rt        j                  dt         «       |dkD  xr d||dz
  |dz    v}t        ||
   «      | j                  j"                  k(  r|}d}nd}d}d}|||||
|d	œ}|	j%                  |«       �Œ– |	S )
zTFuse various numpy arrays into dicts with all the information needed for aggregationNÚ
_tokenizerÚcontinuing_subword_prefixz?Tokenizer does not support real words, using fallback heuristicr   rG   r
   F)rt   r�   r¤   r©   ÚindexÚ
is_subword)Ú	enumeraterM   Úconvert_ids_to_tokensÚintr   ÚitemÚgetattrr±   r   r   r,   r4   r5   r6   ÚwarningsÚwarnÚUserWarningÚunk_token_idrm   )r    rg   rf   r�   r   r}   rD   ri   rj   rž   ÚidxÚtoken_scoresrt   Ú	start_indÚend_indÚ
word_indexÚ
start_charÚ_Úword_refr´   Ú
pre_entitys                        r#   r”   z/TokenClassificationPipeline.gather_pre_entities�  sß  € ð ˆÜ!*¨6Ó!2ó 8	,ÑˆC�à" 3Ò'Øà—>‘>×7Ñ7¼¸IÀc¹NÓ8KÓLˆDØÑ)Ø%3°CÑ%8Ñ"�	˜7ð Ð'Ð,=Ð,IØ!)¨#¡�JØ!Ð-Ø(9¸*Ñ(E™˜
 AØ! ZÑ/˜	Ø :Ñ-˜ä! )¬SÔ1Ø )§¡Ó 0�IØ%Ÿl™l›n�GØ# I¨gÐ6�Ü˜4Ÿ>™>¨<¸Ô>Ä7Ø—N‘N×-Ñ-×3Ñ3Ð5PÐRVôDô
 "% T£¬c°(«mÑ!;‘Jð ,Ü+×1Ñ1Ü+×3Ñ3Ü+×/Ñ/ð0ñ ô
 !Ÿ™Ø]Ü'ôð "+¨Q¡Ò!e°3¸hÀyÐSTÁ}ÐW`ÐcdÑWdÐ>eÐ3e�Jä�y ‘~Ó&¨$¯.©.×*EÑ*EÒEØ#�DØ!&‘Jà �	Ø�Ø"�
ð Ø&Ø"ØØØ(ñˆJð ×Ñ 
Ö+ðq8	,ðr Ðr%   rž   c                 óŽ  — |t         j                  t         j                  hv rlg }|D ]d  }|d   j                  «       }|d   |   }| j                  j
                  j                  |   ||d   |d   |d   |d   dœ}|j                  |«       Œf n| j                  ||«      }|t         j                  k(  r|S | j                  |«      S )Nr�   r³   rt   r¤   r©   )rˆ   rª   r³   rt   r¤   r©   )
r,   r2   r3   Úargmaxr   ÚconfigÚid2labelrm   Úaggregate_wordsÚgroup_entities)r    rž   rD   r    rÆ   Ú
entity_idxrª   rˆ   s           r#   r•   z%TokenClassificationPipeline.aggregateÕ  sÝ   € ØÔ$7×$<Ñ$<Ô>Q×>XÑ>XÐ#YÑYØˆHØ*ò (�
Ø'¨Ñ1×8Ñ8Ó:�
Ø" 8Ñ,¨ZÑ8�à"Ÿj™j×/Ñ/×8Ñ8¸ÑDØ"Ø'¨Ñ0Ø& vÑ.Ø'¨Ñ0Ø% eÑ,ñ�ð —‘ Õ'ñ(ð ×+Ñ+¨LÐ:NÓOˆHàÔ#6×#;Ñ#;Ò;ØˆOØ×"Ñ" 8Ó,Ð,r%   r    c                 ó(  — | j                   j                  |D �cg c]  }|d   ‘Œ	 c}«      }|t        j                  k(  rA|d   d   }|j	                  «       }||   }| j
                  j                  j                  |   }nó|t        j                  k(  rLt        |d„ ¬«      }|d   }|j	                  «       }||   }| j
                  j                  j                  |   }n”|t        j                  k(  rvt        j                  |D �cg c]  }|d   ‘Œ	 c}«      }t        j                  |d¬«      }	|	j	                  «       }
| j
                  j                  j                  |
   }|	|
   }nt        d«      ‚||||d   d   |d	   d
   dœ}|S c c}w c c}w )Nrt   r   r�   c                 ó(   — | d   j                  «       S )Nr�   )r1   )rˆ   s    r#   r¦   z<TokenClassificationPipeline.aggregate_word.<locals>.<lambda>ó  s   € ¸&ÀÑ:J×:NÑ:NÓ:P€ r%   r§   )r†   zInvalid aggregation_strategyr¤   r…   r©   )rˆ   rª   rt   r¤   r©   )rM   Úconvert_tokens_to_stringr,   r4   rÈ   r   rÉ   rÊ   r6   r1   r5   r‘   ÚstackÚnanmeanr   )r    r    rD   rˆ   rt   r�   r¾   rª   Ú
max_entityÚaverage_scoresrÍ   Ú
new_entitys               r#   Úaggregate_wordz*TokenClassificationPipeline.aggregate_wordë  s|  € Ø�~‰~×6Ñ6ÐU]Ö7^È6¸¸v»Ò7^Ó_ˆØÔ#6×#<Ñ#<Ò<Ø˜a‘[ Ñ*ˆFØ—-‘-“/ˆCØ˜3‘KˆEØ—Z‘Z×&Ñ&×/Ñ/°Ñ4‰FØ!Ô%8×%<Ñ%<Ò<Ü˜XÑ+PÔQˆJØ Ñ)ˆFØ—-‘-“/ˆCØ˜3‘KˆEØ—Z‘Z×&Ñ&×/Ñ/°Ñ4‰FØ!Ô%8×%@Ñ%@Ò@Ü—X‘X¸hÖG°F˜v hÓ/ÒGÓHˆFÜŸZ™Z¨°QÔ7ˆNØ'×.Ñ.Ó0ˆJØ—Z‘Z×&Ñ&×/Ñ/°
Ñ;ˆFØ" :Ñ.‰EäÐ;Ó<Ð<àØØØ˜a‘[ Ñ)Ø˜B‘< Ñ&ñ
ˆ
ð Ðùò7 8_ùò Hs   šF
ÄFc                 ó>  — |t         j                  t         j                  hv rt        d«      ‚g }d}|D ]C  }|€|g}Œ	|d   r|j	                  |«       Œ |j	                  | j                  ||«      «       |g}ŒE |�!|j	                  | j                  ||«      «       |S )zú
        Override tokens from a given word that disagree to force agreement on word boundaries.

        Example: micro|soft| com|pany| B-ENT I-NAME I-ENT I-ENT will be rewritten with first strategy as microsoft|
        company| B-ENT I-ENT
        z;NONE and SIMPLE strategies are invalid for word aggregationNr´   )r,   r2   r3   r   rm   rÖ   )r    r    rD   Úword_entitiesÚ
word_grouprˆ   s         r#   rË   z+TokenClassificationPipeline.aggregate_words	  sº   € ð  Ü×$Ñ$Ü×&Ñ&ð$
ñ 
ô ÐZÓ[Ð[àˆØˆ
Øò 	&ˆFØÐ!Ø$˜X‘
Ø˜Ò%Ø×!Ñ! &Õ)à×$Ñ$ T×%8Ñ%8¸ÐEYÓ%ZÔ[Ø$˜X‘
ð	&ð Ð!Ø× Ñ  ×!4Ñ!4°ZÐAUÓ!VÔWØÐr%   c                 ó@  — |d   d   j                  dd«      d   }t        j                  |D �cg c]  }|d   ‘Œ	 c}«      }|D �cg c]  }|d   ‘Œ	 }}t        j                  |«      | j                  j                  |«      |d   d   |d   d	   d
œ}|S c c}w c c}w )zª
        Group together the adjacent tokens with the same entity predicted.

        Args:
            entities (`dict`): The entities predicted by the pipeline.
        r   rˆ   ú-r
   r…   rª   rt   r¤   r©   )r‰   rª   rt   r¤   r©   )Úsplitr‘   rÒ   ÚmeanrM   rÐ   )r    r    rˆ   r�   Útokensr‰   s         r#   Úgroup_sub_entitiesz.TokenClassificationPipeline.group_sub_entities%  sª   € ð ˜!‘˜XÑ&×,Ñ,¨S°!Ó4°RÑ8ˆÜ—‘¸8ÖD°˜V G›_ÒDÓEˆØ/7Ö8 V�&˜“.Ð8ˆÐ8ð #Ü—W‘W˜V“_Ø—N‘N×;Ñ;¸FÓCØ˜a‘[ Ñ)Ø˜B‘< Ñ&ñ
ˆð Ðùò EùÚ8s   ¯BÁBÚentity_namec                 ó‚   — |j                  d«      rd}|dd  }||fS |j                  d«      rd}|dd  }||fS d}|}||fS )NzB-ÚBr   zI-ÚI)Ú
startswith)r    rà   ÚbiÚtags       r#   Úget_tagz#TokenClassificationPipeline.get_tag:  sk   € Ø×!Ñ! $Ô'ØˆBØ˜a˜b�/ˆCð �3ˆwˆð ×#Ñ# DÔ)ØˆBØ˜a˜b�/ˆCð �3ˆwˆð ˆBØˆCØ�3ˆwˆr%   c                 óh  — g }g }|D ]†  }|s|j                  |«       Œ| j                  |d   «      \  }}| j                  |d   d   «      \  }}||k(  r|dk7  r|j                  |«       Œd|j                  | j                  |«      «       |g}Œˆ |r |j                  | j                  |«      «       |S )z³
        Find and group together the adjacent tokens with the same entity predicted.

        Args:
            entities (`dict`): The entities predicted by the pipeline.
        rˆ   r…   râ   )rm   rç   rß   )	r    r    Úentity_groupsÚentity_group_disaggrˆ   rå   ræ   Úlast_biÚlast_tags	            r#   rÌ   z*TokenClassificationPipeline.group_entitiesH  sÍ   € ð ˆØ Ðàò 	/ˆFÙ&Ø#×*Ñ*¨6Ô2Øð —l‘l 6¨(Ñ#3Ó4‰GˆB�Ø $§¡Ð-@ÀÑ-DÀXÑ-NÓ OÑˆG�Xà�hŠ 2¨¢9à#×*Ñ*¨6Õ2ð ×$Ñ$ T×%<Ñ%<Ð=PÓ%QÔRØ'- hÑ#ð'	/ñ( à× Ñ  ×!8Ñ!8Ð9LÓ!MÔNàÐr%   )NNNFNNrU   )NN)%r&   r'   r(   r)   Údefault_input_namesÚ_load_processorÚ_load_image_processorÚ_load_feature_extractorÚ_load_tokenizerr   r>   r,   r   r   r·   Úboolr   rR   r   r   r€   r$   r{   r‚   r2   r¡   r—   r‘   Úndarrayr”   r•   rÖ   rË   rß   rç   rÌ   Ú__classcell__)rC   s   @r#   r9   r9   =   sÝ  ø„ ñ@"ðH &Ðà€OØ!ÐØ#ÐØ€Oá#EÓ#Gõ (ð Ø;?Ø7;Ø$)Ø!Ø $ñ99ð 2°DÑ8ð99ð ˜U 3¨ 8™_Ñ-°Ñ4ð	99ð
 "ð99ð �d‘
ð99ð ˜‘:ó99ðv ØO˜sÐO¨cÐO°d¸4ÀÀSÀ¹>Ñ6JÒOó ØOàØ[˜t C™yÐ[°CÐ[¸DÀÀdÈ3ÐPSÈ8ÁnÑAUÑ<VÒ[ó Ø[ð#2˜s T¨#¡Y™ð #2¸#ð #2À$ÀtÈCÐQTÈHÁ~ÑBVÐY]Ð^bÐcgÐhkÐmpÐhpÑcqÑ^rÑYsÑBsõ #2óJ6òp
ð. =P×<TÑ<TÐdhó 0òd#ð< -1Ø:>ñFàðFð —:‘:ðFð —
‘
ð	Fð
 ˜U 3¨ 8™_Ñ-°Ñ4ðFð  ŸZ™ZðFð 2ðFð �s˜T‘zÑ" TÑ)ðFð    c¨3 h¡Ñ0°4Ñ7ðFð 
ˆd‰óFðP- d¨4¡jð -ÐH[ð -Ð`dÐeiÑ`jó -ð, t¨D¡zð ÐI\ð Ðaeó ð<¨¨T©
ð ÐJ]ð ÐbfÐgkÑbló ð8¨4°©:ð ¸$ó ð* 3ð ¨5°°c°©?ó ð# t¨D¡zð #°d¸4±j÷ #r%   r9   )r   rº   Útypingr   r   r�   r‘   Ú$models.bert.tokenization_bert_legacyr   Úutilsr   r   r	   Úbaser   r   r   r   r‹   Úmodels.auto.modeling_autor   r   r,   r9   ÚNerPipeliner*   r%   r#   ú<module>rû      sˆ   ðÛ Û ß  ã å A÷ñ ÷
 TÓ Sñ ÔÛåXôF¨ô Fô:˜,ô ñ Ù¨4Ô0ðnóô>O -ó Oó?ð>Oðd *�r%   