Ë
    T^(hv  ã                   ó  — d dl Z d dlZd dlZd dlmZ d dlmZmZmZm	Z	m
Z
mZ d dlZddlmZmZmZ ddlmZ ddlmZ ddlmZmZmZmZmZmZ d	d
lmZmZm Z   ejB                  e"«      Z#erddl$m%Z% ddl&m'Z'  e«       rd dl(Z( e«       rd dl)Z*ddl+m,Z, dZ- e«       rd dl.Z.d dl/m-Z- ddl0m1Z1 dejd                  dejd                  de3de3dejd                  de
fd„Z4	 	 	 	 dd„Z5 G d„ de«      Z6 e e d¬«      «       G d„ de«      «       Z7y)é    N)ÚIterable)ÚTYPE_CHECKINGÚDictÚListÚOptionalÚTupleÚUnioné   )ÚSquadExampleÚSquadFeaturesÚ"squad_convert_examples_to_features)Ú	ModelCard)ÚPreTrainedTokenizer)ÚPaddingStrategyÚadd_end_docstringsÚis_tf_availableÚis_tokenizers_availableÚis_torch_availableÚloggingé   )ÚArgumentHandlerÚChunkPipelineÚbuild_pipeline_init_args)ÚTFPreTrainedModel)ÚPreTrainedModel)Ú-TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES)ÚDataset)Ú*MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMESÚstartÚendÚtopkÚmax_answer_lenÚundesired_tokensÚreturnc                 ó@  — | j                   dk(  r| d   } |j                   dk(  r|d   }t        j                  t        j                  | d«      t        j                  |d«      «      }t        j                  t        j
                  |«      |dz
  «      }|j                  «       }|dk(  rt        j                  |«      g}n[t        |«      |k  rt        j                  | «      }n6t        j                  | |«      d| }	|	t        j                  ||	    «         }t        j                  ||j                  «      dd \  }
}t        j                  |
|j                  «       «      t        j                  ||j                  «       «      z  }|
|   }
||   }|d|
|f   }|
||fS )aG  
    Take the output of any `ModelForQuestionAnswering` and will generate probabilities for each span to be the actual
    answer.

    In addition, it filters out some unwanted/impossible cases like answer len being greater than max_answer_len or
    answer end position being before the starting position. The method supports output the k-best answer through the
    topk argument.

    Args:
        start (`np.ndarray`): Individual start probabilities for each token.
        end (`np.ndarray`): Individual end probabilities for each token.
        topk (`int`): Indicates how many possible answer span(s) to extract from the model output.
        max_answer_len (`int`): Maximum size of the answer to extract from the model's output.
        undesired_tokens (`np.ndarray`): Mask determining tokens that can be part of the answer
    r   Néÿÿÿÿr   )ÚndimÚnpÚmatmulÚexpand_dimsÚtrilÚtriuÚflattenÚargmaxÚlenÚargsortÚargpartitionÚunravel_indexÚshapeÚisinÚnonzero)r   r    r!   r"   r#   ÚouterÚ
candidatesÚscores_flatÚidx_sortÚidxÚstartsÚendsÚdesired_spansÚscoress                 úg/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/pipelines/question_answering.pyÚdecode_spansr@   .   st  € ð& ‡z�z�Q‚Ø�d‘ˆà
‡x�x�1‚}Ø�$‰iˆô �I‰I”b—n‘n U¨BÓ/´·±ÀÀQÓ1GÓH€Eô —‘œŸ™ ›¨¸!Ñ);Ó<€Jð ×$Ñ$Ó&€KØˆq‚yÜ—I‘I˜kÓ*Ð+‰Ü	ˆ[Ó	˜DÒ	 Ü—:‘:˜{˜lÓ+‰ä�o‰o˜{˜l¨DÓ1°!°DÐ9ˆØ”r—z‘z ;¨sÑ#3Ð"3Ó4Ñ5ˆä×#Ñ# H¨j×.>Ñ.>Ó?ÀÀÐC�L€FˆDÜ—G‘G˜FÐ$4×$<Ñ$<Ó$>Ó?Ä"Ç'Á'È$ÐP`×PhÑPhÓPjÓBkÑk€MØ�MÑ"€FØ�Ñ€DØ˜˜6 4˜Ñ(€Fà�4˜ÐÐó    c                 óL  — t        j                  t        j                  |«      dz
  «      }|�||z  }|dk(  }	t        j                  |	d| «      } t        j                  |	d|«      }t        j                  | | j                  dd¬«      z
  «      } | | j                  «       z  } t        j                  ||j                  dd¬«      z
  «      }||j                  «       z  }|r#t        || d   |d   z  j                  «       «      }dx| d<   |d<   t        | ||||«      \  }
}}|
|||fS )ai  
    Takes the raw output of any `ModelForQuestionAnswering` and first normalizes its outputs and then uses
    `decode_spans()` to generate probabilities for each span to be the actual answer.

    Args:
        start (`np.ndarray`): Individual start logits for each token.
        end (`np.ndarray`): Individual end logits for each token.
        p_mask (`np.ndarray`): A mask with 1 for values that cannot be in the answer
        attention_mask (`np.ndarray`): The attention mask generated by the tokenizer
        min_null_score(`float`): The minimum null (empty) answer score seen so far.
        topk (`int`): Indicates how many possible answer span(s) to extract from the model output.
        handle_impossible_answer(`bool`): Whether to allow null (empty) answers
        max_answer_len (`int`): Maximum size of the answer to extract from the model's output.
    r   g        g     ˆÃÀr&   T)ÚaxisÚkeepdims)r   r   )
r(   ÚabsÚarrayÚwhereÚexpÚmaxÚsumÚminÚitemr@   )r   r    Úp_maskÚattention_maskÚmin_null_scoreÚtop_kÚhandle_impossible_answerr"   r#   Úundesired_tokens_maskr;   r<   r>   s                r?   Úselect_starts_endsrS   `   s*  € ô2 —v‘vœbŸh™h vÓ.°Ñ2Ó3ÐàÐ!Ø+¨nÑ<Ðð -°Ñ3Ðô �H‰HÐ*¨H°eÓ<€EÜ
�(‰(Ð(¨(°CÓ
8€Cô �F‰F�5˜5Ÿ9™9¨"°t˜9Ó<Ñ<Ó=€EØ�E—I‘I“KÑ€Eä
�&‰&��s—w‘w B°�wÓ6Ñ6Ó
7€CØ
�—‘“	‰/€CáÜ˜^¨e°D©k¸CÀ¹IÑ.E×-KÑ-KÓ-MÓNˆð "Ð!€Eˆ$�K�#�d‘)ä'¨¨s°E¸>ÐK[Ó\Ñ€FˆD�&Ø�4˜ Ð/Ð/rA   c                   ó   — e Zd ZdZd„ Zd„ Zy)Ú QuestionAnsweringArgumentHandlera&  
    QuestionAnsweringPipeline requires the user to provide multiple arguments (i.e. question & context) to be mapped to
    internal [`SquadExample`].

    QuestionAnsweringArgumentHandler manages all the possible to create a [`SquadExample`] from the command-line
    supplied arguments.
    c                 óH  — t        |t        «      r|S t        |t        «      rsdD ]Y  }||vrt        d«      ‚||   €t	        d|› d�«      ‚t        ||   t
        «      sŒ:t        ||   «      dk(  sŒLt	        d|› d�«      ‚ t        j                  di |¤ŽS t	        |› d�«      ‚)	N©ÚquestionÚcontextzFYou need to provide a dictionary with keys {question:..., context:...}ú`z` cannot be Noner   z` cannot be emptyz2 argument needs to be of type (SquadExample, dict)© )	Ú
isinstancer   ÚdictÚKeyErrorÚ
ValueErrorÚstrr/   ÚQuestionAnsweringPipelineÚcreate_sample)ÚselfrL   Úks      r?   Ú	normalizez*QuestionAnsweringArgumentHandler.normalizeŸ   s¶   € Ü�dœLÔ)ØˆKÜ˜œdÔ#Ø,ò ?�Ø˜D‘=Ü"Ð#kÓlÐlØ˜!‘W�_Ü$ q¨¨Ð+;Ð%<Ó=Ð=Ü  Q¡¬Õ-´#°d¸1±g³,À!Ó2CÜ$ q¨¨Ð+<Ð%=Ó>Ð>ð?ô -×:Ñ:ÑB¸TÑBÐBÜ˜D˜6Ð!SÐTÓUÐUrA   c                 óê  — |�mt        |«      dkD  r_t        |«      dk(  r|d   }�n�t        |«      dk(  r/|D �ch c]  }t        |«      ’Œ c}t        hk(  r|d   |d   dœg}�n`t        |«      }�nSd|v r!t	        j
                  dt        «       |d   }�n.d|v r!t	        j
                  dt        «       |d   }�n	d	|v r÷d
|v rót        |d	   t        «      r,t        |d
   t        «      r|d	   D �cg c]
  }||d
   dœ‘Œ }}nÂt        |d	   t        «      rct        |d
   t        «      rPt        |d	   «      t        |d
   «      k7  rt        d«      ‚t        |d	   |d
   «      D ��cg c]
  \  }}||dœ‘Œ }}}nLt        |d	   t        «      r t        |d
   t        «      r|d	   |d
   dœg}nt        d«      ‚t        d|› �«      ‚t        �t        j                  t        fnt        j                  f}t        ||«      r|S t        |t        «      r|g}n*t        |t        «      rt        |«      }nt        d|› �«      ‚t        |«      D ]  \  }}	| j!                  |	«      ||<   Œ |S c c}w c c}w c c}}w )Nr   r   r
   rW   ÚXz§Passing the `X` argument to the pipeline is deprecated and will be removed in v5. Inputs should be passed using the `question` and `context` keyword arguments instead.ÚdatazªPassing the `data` argument to the pipeline is deprecated and will be removed in v5. Inputs should be passed using the `question` and `context` keyword arguments instead.rX   rY   z2Questions and contexts don't have the same lengthszArguments can't be understoodzUnknown arguments zInvalid arguments )r/   Útyper`   ÚlistÚwarningsÚwarnÚFutureWarningr\   r_   Úzipr   ÚtypesÚGeneratorTyper]   r   Ú	enumeratere   )
rc   ÚargsÚkwargsÚinputsÚelÚQÚCÚgenerator_typesÚirL   s
             r?   Ú__call__z)QuestionAnsweringArgumentHandler.__call__®   s€  € àÐ¤ D£	¨A¢Ü�4‹y˜AŠ~Ø˜a™’Ü�T“˜a’¸Ö$=°"¤T¨"¥XÒ$=Ä#ÀÒ$FØ'+¨A¡w¸4À¹7ÑCÐD’ä˜d›’ð �F‰]Ü�M‰Mð zÜôð ˜C‘[ŠFØ�vÑÜ�M‰Mð }Üôð ˜F‘^ŠFØ˜6Ñ! i°6Ñ&9Ü˜& Ñ,¬dÔ3¼
À6È)ÑCTÔVYÔ8ZØQWÐXbÑQcÖdÈA q°V¸IÑ5FÓGÐd�ÑdÜ˜F :Ñ.´Ô5¼*ÀVÈIÑEVÔX\Ô:]Ü�v˜jÑ)Ó*¬c°&¸Ñ2CÓ.DÒDÜ$Ð%YÓZÐZäDGÈÈzÑHZÐ\bÐclÑ\mÓDn×o¹D¸A¸q q°QÓ7Ðo�ÒoÜ˜F :Ñ.´Ô4¼ÀFÈ9ÑDUÔWZÔ9[Ø'-¨jÑ'9ÀfÈYÑFWÑXÐY‘ä Ð!@ÓAÐAäÐ1°&°Ð:Ó;Ð;ô =DÐ<Oœ5×.Ñ.´Ñ8ÔV[×ViÑViÐUkˆÜ�f˜oÔ.ØˆMô �fœdÔ#Ø�X‰FÜ˜¤Ô)ä˜&“\‰FäÐ1°&°Ð:Ó;Ð;ä  Ó(ò 	-‰GˆAˆtØŸ™ tÓ,ˆF�1ŠIð	-ð ˆùòe %>ùò( eùó
 ps   ¸I%Ã/I*Å$I/N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__re   rz   r[   rA   r?   rU   rU   –   s   „ ñòVó7rA   rU   T)Úhas_tokenizerc                   ó@  ‡ — e Zd ZdZdZdZ	 	 	 dded   dedee	   dee
   d	e
f
ˆ fd
„Zedee
ee
   f   dee
ee
   f   deeee   f   fd„«       Z	 	 	 	 	 	 	 	 	 d d„Zˆ fd„Zd!d„Zd„ Z	 	 	 	 d"d„Zdddededededeeef   fd„Zde
dededee
ee
ef   f   fd„Zˆ xZS )#ra   aÏ  
    Question Answering pipeline using any `ModelForQuestionAnswering`. See the [question answering
    examples](../task_summary#question-answering) for more information.

    Example:

    ```python
    >>> from transformers import pipeline

    >>> oracle = pipeline(model="deepset/roberta-base-squad2")
    >>> oracle(question="Where do I live?", context="My name is Wolfgang and I live in Berlin")
    {'score': 0.9191, 'start': 34, 'end': 40, 'answer': 'Berlin'}
    ```

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

    This question answering pipeline can currently be loaded from [`pipeline`] using the following task identifier:
    `"question-answering"`.

    The models that this pipeline can use are models that have been fine-tuned on a question answering task. See the
    up-to-date list of available models on
    [huggingface.co/models](https://huggingface.co/models?filter=question-answering).
    zquestion,contextFÚmodel)r   r   Ú	tokenizerÚ	modelcardÚ	frameworkÚtaskc           	      ó¬   •— t        ‰| �  d|||||dœ|¤Ž t        «       | _        | j	                  | j
                  dk(  rt        «       y t        «       y )N)r�   r‚   rƒ   r„   r…   Útfr[   )ÚsuperÚ__init__rU   Ú_args_parserÚcheck_model_typer„   r   r   )rc   r�   r‚   rƒ   r„   r…   rs   Ú	__class__s          €r?   r‰   z"QuestionAnsweringPipeline.__init__  sf   ø€ ô 	‰Ñð 	
ØØØØØñ	
ð ò	
ô =Ó>ˆÔØ×Ñà�~‰~ Ò%ô :õ	
ô <õ	
rA   rX   rY   r$   c                 ó¨   — t        | t        «      r-t        | |«      D ��cg c]  \  }}t        d||ddd«      ‘Œ c}}S t        d| |ddd«      S c c}}w )aC  
        QuestionAnsweringPipeline leverages the [`SquadExample`] internally. This helper method encapsulate all the
        logic for converting question(s) and context(s) to [`SquadExample`].

        We currently support extractive question answering.

        Arguments:
            question (`str` or `List[str]`): The question(s) asked.
            context (`str` or `List[str]`): The context(s) in which we will look for the answer.

        Returns:
            One or a list of [`SquadExample`]: The corresponding [`SquadExample`] grouping question and context.
        N)r\   rj   rn   r   )rX   rY   ÚqÚcs       r?   rb   z'QuestionAnsweringPipeline.create_sample  sU   € ô" �h¤Ô%ÜKNÈxÐY`ÓKa×bÁ4À1Àa”L  q¨!¨T°4¸Õ>ÓbÐbä  h°¸¸tÀTÓJÐJùó cs    Ac
                 ó  — i }|�||d<   |�||d<   |�||d<   |�||d<   i }|�|€t        j                  dt        «       |}|�|dk  rt        d|› d�«      ‚||d	<   |�|dk  rt        d
|› �«      ‚||d<   |�||d<   |	�|	|d<   |i |fS )NÚpaddingÚ
doc_strideÚmax_question_lenÚmax_seq_lenz/topk parameter is deprecated, use top_k insteadr   z$top_k parameter should be >= 1 (got ú)rP   z-max_answer_len parameter should be >= 1 (got r"   rQ   Úalign_to_words)rk   rl   ÚUserWarningr_   )rc   r‘   r!   rP   r’   r"   r”   r“   rQ   r–   rs   Úpreprocess_paramsÚpostprocess_paramss                r?   Ú_sanitize_parametersz.QuestionAnsweringPipeline._sanitize_parameters4  s  € ð ÐØÐØ+2Ð˜iÑ(ØÐ!Ø.8Ð˜lÑ+ØÐ'Ø4DÐÐ0Ñ1ØÐ"Ø/:Ð˜mÑ,àÐØÐ  Ü�M‰MÐKÌ[ÔYØˆEØÐØ�qŠyÜ Ð#GÈÀwÈaÐ!PÓQÐQØ*/Ð˜wÑ'ØÐ%Ø Ò!Ü Ð#PÐQ_ÐP`Ð!aÓbÐbØ3AÐÐ/Ñ0Ø#Ð/Ø=UÐÐ9Ñ:ØÐ%Ø3AÐÐ/Ñ0Ø  "Ð&8Ð8Ð8rA   c                 óî   •— |rt        j                  dt        «        | j                  |i |¤Ž}t	        |t
        t        f«      r!t        |«      dk(  rt        ‰| �$  |d   fi |¤ŽS t        ‰| �$  |fi |¤ŽS )aÄ	  
        Answer the question(s) given as inputs by using the context(s).

        Args:
            question (`str` or `List[str]`):
                One or several question(s) (must be used in conjunction with the `context` argument).
            context (`str` or `List[str]`):
                One or several context(s) associated with the question(s) (must be used in conjunction with the
                `question` argument).
            top_k (`int`, *optional*, defaults to 1):
                The number of answers to return (will be chosen by order of likelihood). Note that we return less than
                top_k answers if there are not enough options available within the context.
            doc_stride (`int`, *optional*, defaults to 128):
                If the context is too long to fit with the question for the model, it will be split in several chunks
                with some overlap. This argument controls the size of that overlap.
            max_answer_len (`int`, *optional*, defaults to 15):
                The maximum length of predicted answers (e.g., only answers with a shorter length are considered).
            max_seq_len (`int`, *optional*, defaults to 384):
                The maximum length of the total sentence (context + question) in tokens of each chunk passed to the
                model. The context will be split in several chunks (using `doc_stride` as overlap) if needed.
            max_question_len (`int`, *optional*, defaults to 64):
                The maximum length of the question after tokenization. It will be truncated if needed.
            handle_impossible_answer (`bool`, *optional*, defaults to `False`):
                Whether or not we accept impossible as an answer.
            align_to_words (`bool`, *optional*, defaults to `True`):
                Attempts to align the answer to real words. Improves quality on space separated languages. Might hurt on
                non-space-separated languages (like Japanese or Chinese)

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

            - **score** (`float`) -- The probability associated to the answer.
            - **start** (`int`) -- The character start index of the answer (in the tokenized version of the input).
            - **end** (`int`) -- The character end index of the answer (in the tokenized version of the input).
            - **answer** (`str`) -- The answer to the question.
        z¯Passing a list of SQuAD examples to the pipeline is deprecated and will be removed in v5. Inputs should be passed using the `question` and `context` keyword arguments instead.r   r   )
rk   rl   rm   rŠ   r\   rj   Útupler/   rˆ   rz   )rc   rr   rs   ÚexamplesrŒ   s       €r?   rz   z"QuestionAnsweringPipeline.__call__^  s~   ø€ ñN Ü�M‰Mð BÜôð
 %�4×$Ñ$ dÐ5¨fÑ5ˆÜ�h¤¤u Ô.´3°x³=ÀAÒ3EÜ‘7Ñ# H¨Q¡KÑ:°6Ñ:Ð:Ü‰wÑ Ñ3¨FÑ3Ð3rA   c              #   óŽ  K  — t        |t        «      rt        d |d   |d   d d d «      }|€ t        | j                  j
                  d«      }|€t        |dz  d«      }||kD  rt        d|› d|› d�«      ‚| j                  j                  s.t        |g| j                  |||t        j                  d	d	¬
«      }�n¡| j                  j                  dk(  }| j	                  |r|j                  n|j                  |r|j                  n|j                  ||rdnd||dddd¬«
      }t        |d   «      }	t        |	«      D �
�cg c](  }
|j!                  |
«      D �cg c]  }|r|dk7  nd‘Œ c}‘Œ* }}
}g }t        |	«      D ]Ý  }|d   |   }d|v r|d   |   nd }d|v r|d   |   nd }| j                  j"                  �Qt%        j&                  t%        j(                  |«      | j                  j"                  k(  «      d   }|D ]
  }d||   |<   Œ ||   }|j+                  t-        d%i d|“d|“d|“d|“d||   “dd “di “dd“dd“dd“dd“dg “dd“dd“d d	“d!d “Ž«       Œß t/        |«      D �]R  \  }}i }i }| j                  j0                  ddgz   }|j2                  j5                  «       D ]ô  \  }}||v ræ| j6                  d"k(  rpt9        j:                  |«      }|j<                  t8        j>                  k(  r$t9        j@                  |t8        jB                  «      }t9        jD                  |d«      ||<   Œ‰| j6                  d#k(  sŒ™tG        jH                  |«      }|j<                  tF        jB                  k(  r|jK                  «       }|jM                  d«      ||<   Œð|||<   Œö |t        |«      dz
  k(  }||d$œ|¥|¥–— �ŒU y c c}w c c}}
w ­w)&NrX   rY   i€  r
   é€   z`doc_stride` (z ) is larger than `max_seq_len` (r•   F)r�   r‚   Úmax_seq_lengthr’   Úmax_query_lengthÚpadding_strategyÚis_trainingÚtqdm_enabledÚrightÚonly_secondÚ
only_firstT)
ÚtextÚ	text_pairr‘   Ú
truncationÚ
max_lengthÚstrideÚreturn_token_type_idsÚreturn_overflowing_tokensÚreturn_offsets_mappingÚreturn_special_tokens_maskÚ	input_idsr   r   rN   Útoken_type_idsrM   ÚencodingÚ	cls_indexÚtoken_to_orig_mapÚexample_indexÚ	unique_idÚparagraph_lenÚtoken_is_max_contextÚtokensÚstart_positionÚend_positionÚis_impossibleÚqas_idr‡   Úpt)ÚexampleÚis_lastr[   )'r\   r]   r   rK   r‚   Úmodel_max_lengthr_   Úis_fastr   r   Ú
MAX_LENGTHÚpadding_sideÚquestion_textÚcontext_textr/   ÚrangeÚsequence_idsÚcls_token_idr(   r5   rF   Úappendr   rq   Úmodel_input_namesÚ__dict__Úitemsr„   r‡   ÚconstantÚdtypeÚint64ÚcastÚint32r*   ÚtorchÚtensorÚlongÚ	unsqueeze)rc   rÀ   r‘   r’   r“   r”   ÚfeaturesÚquestion_firstÚencoded_inputsÚ	num_spansÚspan_idÚtokrM   Úspan_idxÚinput_ids_span_idxÚattention_mask_span_idxÚtoken_type_ids_span_idxÚcls_indicesr´   Úsubmaskry   ÚfeatureÚfw_argsÚothersrÌ   rd   ÚvrÕ   rÁ   s                                r?   Ú
preprocessz$QuestionAnsweringPipeline.preprocess�  sV  è ø€ ô �gœtÔ$Ü" 4¨°Ñ)<¸gÀiÑ>PÐRVÐX\Ð^bÓcˆGàÐÜ˜dŸn™n×=Ñ=¸sÓCˆKØÐÜ˜[¨AÑ-¨sÓ3ˆJà˜Ò#Ü˜~¨j¨\Ð9YÐZeÐYfÐfgÐhÓiÐià�~‰~×%Ò%Ü9Ø!˜ØŸ.™.Ø*Ø%Ø!1Ü!0×!;Ñ!;Ø!Ø"ô	ŠHð "Ÿ^™^×8Ñ8¸GÑCˆNà!Ÿ^™^Ù.<�W×*Ò*À'×BVÑBVÙ2@˜'×.Ò.Àg×F[ÑF[ØÙ,:™=ÀØ&Ø!Ø&*Ø*.Ø'+Ø+/ð ,ó ˆNô" ˜N¨;Ñ7Ó8ˆIô  % YÓ/÷àð ?M×>YÑ>YÐZaÓ>bÖc°s™^�˜’°Ñ2ÔcðˆFñ ð
 ˆHÜ! )Ó,ò #�Ø%3°KÑ%@ÀÑ%JÐ"àBRÐVdÑBd�NÐ#3Ñ4°XÒ>Ðjnð (ð CSÐVdÑBd�NÐ#3Ñ4°XÒ>Ðjnð (ð —>‘>×.Ñ.Ð:Ü"$§*¡*¬R¯X©XÐ6HÓ-IÈTÏ^É^×MhÑMhÑ-hÓ"iÐjkÑ"l�KØ%0ò 8˜	Ø67˜˜xÑ(¨Ò3ð8à  Ñ*�Ø—‘Ü!ò Ù"4ðá'>ðñ (?ðñ  'ð	ð
 "0°Ò!9ðñ #'ðñ +-ðñ '(ðñ #$ðñ '(ðñ ./ðñ  "ðñ ()ðñ  &'ð!ñ" ',ð#ñ$  $ð%õð#ôJ $ HÓ-ó 	P‰JˆAˆwØˆGØˆFØ $§¡× @Ñ @ÀHÐN^ÐC_Ñ _Ðà×(Ñ(×.Ñ.Ó0ò "‘��1ØÐ)Ñ)Ø—~‘~¨Ò-Ü!#§¡¨Q£˜Ø!Ÿ<™<¬2¯8©8Ò3Ü%'§W¡W¨V´R·X±XÓ%>˜FÜ%'§^¡^°F¸AÓ%>˜ š
ØŸ™¨4Ó/Ü!&§¡¨a£˜Ø!Ÿ<™<¬5¯;©;Ò6Ø%+§[¡[£]˜FØ%+×%5Ñ%5°aÓ%8˜ š
à !�F˜1’Ið"ð œ3˜x›=¨1Ñ,Ñ,ˆGØ%°'ÑO¸WÐOÈÐOÔOñ+	PùòU dùóùs2   ‚D9OÄ;N?ÅN:Å#N?Å(GOÌ<A>OÎ:N?Î?Oc                 óÂ  — |d   }| j                   j                  D �ci c]  }|||   “Œ
 }}| j                  dk(  r| j                  j                  n| j                  j
                  }dt        j                  |«      j                  j                  «       v rd|d<    | j                  d	i |¤Ž}t        |t        «      r|d   |d   |dœ|¥S |d d \  }}|||dœ|¥S c c}w )
NrÀ   r¿   Ú	use_cacheFÚstart_logitsÚ
end_logits)r   r    rÀ   r
   r[   )r‚   rÌ   r„   r�   ÚforwardÚcallÚinspectÚ	signatureÚ
parametersÚkeysr\   r]   )	rc   rt   rÀ   rd   Úmodel_inputsÚmodel_forwardÚoutputr   r    s	            r?   Ú_forwardz"QuestionAnsweringPipeline._forward  sã   € Ø˜Ñ#ˆØ.2¯n©n×.NÑ.NÖO¨˜˜6 !™9™ÐOˆÐOà.2¯n©nÀÒ.D˜Ÿ
™
×*Ò*È$Ï*É*Ï/É/ˆØœ'×+Ñ+¨MÓ:×EÑE×JÑJÓLÑLØ(-ˆL˜Ñ%Ø�—‘Ñ+˜lÑ+ˆÜ�fœdÔ#Ø# NÑ3¸FÀ<Ñ<PÐ]dÑoÐhnÐoÐoà  ˜‰JˆE�3Ø"¨3¸7ÑMÀfÐMÐMùò Ps   žCc                 óä  — d}g }|D �]¨  }| j                   dk(  re|d   j                  t        j                  k(  rE|d   j	                  t        j
                  «      }	|d   j	                  t        j
                  «      }
n
|d   }	|d   }
|d   }|d   }|j                  dd «      �|d   j                  «       nd }t        |	|
||||||«      \  }}}}| j                  j                  sßt        j                  |j                  «      }t        |||«      D ]®  \  }}}|d   }|j                  |j!                  «       t        j"                  |||   k(  «      d	   d	   j!                  «       t        j"                  |||   k(  «      d	   d
   j!                  «       dj%                  |j&                  ||   ||   dz    «      dœ«       Œ° �Œ¿t)        | j                  j*                  dk(  «      }|d   }| j                  j*                  dk(  r9|d   | j                  j,                  k(  j                  «       j/                  «       }nd	}|rdnd	}t        |||«      D ]Y  \  }}}||z
  }||z
  }| j1                  |||||«      \  }}|j                  |j!                  «       |||j2                  || dœ«       Œ[ �Œ« |r|j                  |d	d	ddœ«       t5        |d„ d¬«      d | }t7        |«      dk(  r|d	   S |S )Né@B r¿   r   r    rÀ   rM   rN   rµ   r   r&   ú r   )Úscorer   r    Úanswerr¥   r³   Úleftr±   Ú c                 ó   — | d   S )Nrú   r[   )Úxs    r?   ú<lambda>z7QuestionAnsweringPipeline.postprocess.<locals>.<lambda>g  s
   € °°'±
€ rA   T)ÚkeyÚreverse)r„   rÐ   rÔ   Úbfloat16ÚtoÚfloat32ÚgetÚnumpyrS   r‚   rÃ   r(   rF   Úchar_to_word_offsetrn   rË   rL   rG   ÚjoinÚ
doc_tokensÚboolrÅ   Úpad_token_idrJ   Úget_indicesrÇ   Úsortedr/   )rc   Úmodel_outputsrP   rQ   r"   r–   rO   Úanswersrõ   Ústart_Úend_rÀ   rM   rN   r;   r<   r>   Úchar_to_wordÚsÚerú   rµ   rÙ   ÚencÚoffsetÚsequence_indexÚstart_indexÚ	end_indexs                               r?   Úpostprocessz%QuestionAnsweringPipeline.postprocess  s  € ð !ˆØˆØ#ó F	ˆFØ�~‰~ Ò%¨&°©/×*?Ñ*?Ä5Ç>Á>Ò*QØ ™×+Ñ+¬E¯M©MÓ:�Ø˜e‘}×'Ñ'¬¯©Ó6‘à ™�Ø˜e‘}�Ø˜YÑ'ˆGØ˜HÑ%ˆFà4:·J±JÐ?OÐQUÓ4VÐ4b�Ð'Ñ(×.Ñ.Ô0Ðhlð ô 4FØ˜˜f n°nÀeÐMeÐguó4Ñ0ˆF�D˜& .ð —>‘>×)Ò)Ü!Ÿx™x¨×(CÑ(CÓD�ô $' v¨t°VÓ#<ò 	‘K�A�q˜%Ø(.Ð/BÑ(CÐ%Ø—N‘Nà%*§Z¡Z£\Ü%'§X¡X¨lÐ>OÐPQÑ>RÑ.RÓ%SÐTUÑ%VÐWXÑ%Y×%^Ñ%^Ó%`Ü#%§8¡8¨LÐ<MÈaÑ<PÑ,PÓ#QÐRSÑ#TÐUWÑ#X×#]Ñ#]Ó#_Ø&)§h¡h¨w×/AÑ/AÐBSÐTUÑBVÐYjÐklÑYmÐpqÑYqÐ/rÓ&sñ	õò	ô  "& d§n¡n×&AÑ&AÀWÑ&LÓ!M�Ø˜ZÑ(�ð —>‘>×.Ñ.°&Ò8Ø$ [Ñ1°T·^±^×5PÑ5PÑP×WÑWÓY×]Ñ]Ó_‘Fà�Fñ
 '5¡¸!�Ü#& v¨t°VÓ#<ò ‘K�A�q˜%Ø˜F™
�AØ˜F™
�Aà-1×-=Ñ-=¸cÀ1ÀaÈÐYgÓ-hÑ*�K à—N‘Nà%*§Z¡Z£\Ø%0Ø#,Ø&-×&:Ñ&:¸;ÀyÐ&Qñ	õòðsF	ñP $Ø�N‰N ^¸aÈÐUWÑXÔYÜ˜Ñ&:ÀDÔIÈ&È5ÐQˆÜˆw‹<˜1ÒØ˜1‘:ÐØˆrA   r  ztokenizers.Encodingr  r  r  r–   c                 óh  — |rS	 |j                  |«      }|j                  |«      }|j                  ||¬«      d   }|j                  ||¬«      d   }	||	fS |j                  |   d   }|j                  |   d   }	||	fS # t        $ r* |j                  |   d   }|j                  |   d   }	Y ||	fS w xY w)N)r  r   r   )Útoken_to_wordÚword_to_charsÚ	ExceptionÚoffsets)
rc   r  r  r  r  r–   Ú
start_wordÚend_wordr  r  s
             r?   r  z%QuestionAnsweringPipeline.get_indicesl  sâ   € ñ ð.Ø ×.Ñ.¨qÓ1�
Ø×,Ñ,¨QÓ/�Ø!×/Ñ/°
È>Ð/ÓZÐ[\Ñ]�Ø×-Ñ-¨hÀ~Ð-ÓVÐWXÑY�	ð ˜IÐ%Ð%ð Ÿ+™+ a™.¨Ñ+ˆKØŸ™ A™ qÑ)ˆIØ˜IÐ%Ð%øô ò .à!Ÿk™k¨!™n¨QÑ/�ØŸK™K¨™N¨1Ñ-‘	ð ˜IÐ%Ð%ð.ús   „AA> Á>-B1Â0B1r¨   r   r    c                 óœ  — g }dx}x}x}}t        |j                  d«      «      D ]t  \  }	}
| j                  j                  |
«      }||cxk  r|k  r#n n ||k(  r|}||k(  r|t	        |
«      z   }||
gz  }||kD  r n!|t	        |«      z  }|t	        |
«      dz   z  }Œv dj                  |«      t        d|«      t        t	        |«      |«      dœS )a¢  
        When decoding from token probabilities, this method maps token indexes to actual word in the initial context.

        Args:
            text (`str`): The actual context to extract the answer from.
            start (`int`): The answer starting token index.
            end (`int`): The answer end token index.

        Returns:
            Dictionary like `{'answer': str, 'start': int, 'end': int}`
        r   rù   r   )rû   r   r    )rq   Úsplitr‚   Útokenizer/   r	  rI   rK   )rc   r¨   r   r    ÚwordsÚ	token_idxÚchar_start_idxÚchar_end_idxÚ	chars_idxry   ÚwordÚtokens               r?   Úspan_to_answerz(QuestionAnsweringPipeline.span_to_answer~  së   € ð ˆØ@AÐAˆ	ÐA�NÐA \°Iä  §¡¨C£Ó1ò 	'‰GˆAˆtØ—N‘N×+Ñ+¨DÓ1ˆEð ˜	Ô( SÕ(Ø Ò%Ø%.�Nà Ò#Ø#,¬s°4«yÑ#8�Là˜$˜‘�ð ˜3ŠÙð œ˜U›Ñ#ˆIØœ˜T› Q™Ñ&‰Ið'	'ð. —h‘h˜u“oÜ˜˜NÓ+Ü”s˜4“y ,Ó/ñ
ð 	
rA   )NNrý   )	NNNNNNNNN)Ú
do_not_padNé@   N)r   Fé   T)r{   r|   r}   r~   Údefault_input_namesrQ   r	   r   r   r   r`   r‰   Ústaticmethodr   r   rb   rš   rz   rè   rö   r  Úintr  r   r  r   r-  Ú__classcell__)rŒ   s   @r?   ra   ra   è   s~  ø„ ñð0 -ÐØ$Ðð *.Ø#'Øñ
àÐ;Ñ<ð
ð 'ð
ð ˜IÑ&ð	
ð
 ˜C‘=ð
ð õ
ð2 ðKØ˜˜T #™Y˜Ñ'ðKØ27¸¸TÀ#¹Y¸Ñ2GðKà	ˆ|˜T ,Ñ/Ð/Ñ	0òKó ðKð. ØØØØØØØ!%Øó(9ôT04ódsPòjNð" Ø!&ØØóWðr&Ø(ð&Ø-0ð&Ø58ð&ØJMð&Ø_cð&à	ˆs�Cˆx‰ó&ð$)
 3ð )
¨sð )
¸ð )
ÀÀcÈ5ÐQTÐVYÐQYÉ?ÐFZÑA[÷ )
rA   ra   )rø   r   Fr0  )8rï   ro   rk   Úcollections.abcr   Útypingr   r   r   r   r   r	   r  r(   rh   r   r   r   rƒ   r   Útokenization_utilsr   Úutilsr   r   r   r   r   r   Úbaser   r   r   Ú
get_loggerr{   ÚloggerÚmodeling_tf_utilsr   Úmodeling_utilsr   Ú
tokenizersÚ
tensorflowr‡   Úmodels.auto.modeling_tf_autor   r   rÔ   Útorch.utils.dataÚmodels.auto.modeling_autor   Úndarrayr3  r@   rS   rU   ra   r[   rA   r?   ú<module>rD     s  ðÛ Û Û Ý $ß D× Dã ç RÑ RÝ !Ý 4÷÷ ÷ KÑ Jð 
ˆ×	Ñ	˜HÓ	%€áÝ5Ý0áÔ ÛáÔÛå\à€GáÔÛÝ(åVð/ Ø�:‰:ð/ ØŸJ™Jð/ Ø.1ð/ ØCFð/ ØZ\×ZdÑZdð/ à
ó/ ðn Ø
Ø"Øó30ôlO ô Oñd Ñ,¸4Ô@ÓAô~
 ó ~
ó Bñ~
rA   