
    fThtQ                        S SK r S SKrS SKrSSKJr  SSKJrJrJ	r	J
r
  SSKJrJrJrJrJr  \	" 5       (       a  S SKrSSKJrJr  \" 5       (       a  S SKrSSKJrJr   " S	 S
\5      r\" \" SS95       " S S\5      5       rg)    N   )GenerationConfig)add_end_docstringsis_tf_availableis_torch_availablerequires_backends   )ArgumentHandlerDatasetPipelinePipelineExceptionbuild_pipeline_init_args),MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES0MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING_NAMES)/TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES3TF_MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING_NAMESc                   "    \ rS rSrSrSS jrSrg)%TableQuestionAnsweringArgumentHandler!   z:
Handles arguments for the TableQuestionAnsweringPipeline
Nc                 T   [        U S5        SS KnUc  [        S5      eUGc/  [        U[        5      (       a(  UR                  S5      b  UR                  S5      b  U/nO[        U[        5      (       a  [        U5      S:  ay  [        S U 5       5      (       d  [        SS U 5        35      eUS   R                  S5      b  US   R                  S5      b  UnOz[        S	US   R                  5        S
35      e[        b  [        U[        5      (       d  [        U[        R                  5      (       a  U$ [        S[        U5       S35      eXS./nU HK  n[        US   UR                  5      (       a  M#  US   c  [        S5      eUR                  US   5      US'   MM     U$ )Npandasr   z(Keyword argument `table` cannot be None.querytablec              3   B   #    U  H  n[        U[        5      v   M     g 7fN)
isinstancedict.0ds     g/var/www/auris/envauris/lib/python3.13/site-packages/transformers/pipelines/table_question_answering.py	<genexpr>ATableQuestionAnsweringArgumentHandler.__call__.<locals>.<genexpr>6   s     >1:a..s   z:Keyword argument `table` should be a list of dict, but is c              3   8   #    U  H  n[        U5      v   M     g 7fr   )typer   s     r!   r"   r#   8   s     UmglbcVZ[\V]V]gls   zIf keyword argument `table` is a list of dictionaries, each dictionary should have a `table` and `query` key, but only dictionary has keys z `table` and `query` keys.zZInvalid input. Keyword argument `table` should be either of type `dict` or `list`, but is ))r   r   zTable cannot be None.)r   r   
ValueErrorr   r   getlistlenallkeysr   typesGeneratorTyper%   	DataFrame)selfr   r   kwargspdtqa_pipeline_inputstqa_pipeline_inputs          r!   __call__.TableQuestionAnsweringArgumentHandler.__call__&   s    	$)=GHH]%&&599W+=+IeiiX_N`Nl',g#E4((SZ!^>>>>$TUmglUmTno  8<<(4qg9N9Z*/'$JJOPQ(--/IZZtv  $E7)C)CzRWY^YlYlGmGm u+a) 
 .3#C"D"5092<<HH%g.6$%<==.0ll;Mg;V.W"7+ #6 #"     )NN)__name__
__module____qualname____firstlineno____doc__r5   __static_attributes__r8   r7   r!   r   r   !   s    -#r7   r   T)has_tokenizerc                      ^  \ rS rSrSrSrSr\" SS9r\	" 5       4U 4S jjr
S rS	 rU 4S
 jrSS jrSS jrSS jrS rSrU =r$ )TableQuestionAnsweringPipelineV   ah  
Table Question Answering pipeline using a `ModelForTableQuestionAnswering`. This pipeline is only available in
PyTorch.

Unless the model you're using explicitly sets these generation parameters in its configuration files
(`generation_config.json`), the following default values will be used:
- max_new_tokens: 256

Example:

```python
>>> from transformers import pipeline

>>> oracle = pipeline(model="google/tapas-base-finetuned-wtq")
>>> table = {
...     "Repository": ["Transformers", "Datasets", "Tokenizers"],
...     "Stars": ["36542", "4512", "3934"],
...     "Contributors": ["651", "77", "34"],
...     "Programming language": ["Python", "Python", "Rust, Python and NodeJS"],
... }
>>> oracle(query="How many stars does the transformers repository have?", table=table)
{'answer': 'AVERAGE > 36542', 'coordinates': [(0, 1)], 'cells': ['36542'], 'aggregator': 'AVERAGE'}
```

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

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

The models that this pipeline can use are models that have been fine-tuned on a tabular question answering task.
See the up-to-date list of available models on
[huggingface.co/models](https://huggingface.co/models?filter=table-question-answering).
ztable,queryT   )max_new_tokensc                 F  > [         TU ]  " U0 UD6  Xl        U R                  S:X  a+  [        R
                  " 5       nUR                  [        5        O*[        R
                  " 5       nUR                  [        5        U R                  U5        [        [        U R                  R                  SS 5      5      =(       a*    [        [        U R                  R                  SS 5      5      U l        [!        U R                  R                  S5      (       a  SU l        g S U l        g )Ntfaggregation_labelsnum_aggregation_labelstapas)super__init___args_parser	frameworkr   copyupdater   r   r   check_model_typeboolgetattrmodelconfig	aggregatehasattrr%   )r0   args_parserargsr1   mapping	__class__s        r!   rK   'TableQuestionAnsweringPipeline.__init__   s    $)&)'>>T!INNPGNNJKFKKMGNNGHg&gdjj&7&79MtTU 
Z^DJJ%%'?F[
  'tzz'8'8:NOOG	UY	r7   c                 &    U R                   " S0 UD6$ )Nr8   )rS   )r0   inputss     r!   batch_inference.TableQuestionAnsweringPipeline.batch_inference   s    zz#F##r7   c           
         U R                   S:X  Ga  / n/ nSnUS   R                  S   nUS   R                  U R                  5      nUS   R                  U R                  5      nUS   R                  U R                  5      nSn	[	        U5       GH=  n
UGb.  U	SS2S4   n[
        R                  " UR                  5       R                  5       5      nX   n	[	        UR                  S   5       H  nU	SS2S4   R                  5       U   nU	SS2S4   R                  5       U   S-
  nU	SS2S	4   R                  5       U   S-
  nUS:  d  M_  US:  d  Mg  US:X  d  Mo  [        XOU4   5      X'   M     [        R                  " U5      R                  [        R                  5      R                  U R                  5      U	SS2S4'   Xj   nXz   nX   n	U R                  UR!                  S5      UR!                  S5      U	R!                  S5      S
9nUR"                  nU R$                  (       a  UR'                  UR(                  5        UR'                  U5        [        R*                  R-                  US9nUR.                  UR                  [        R0                  5      R                  UR.                  R                  5      -  n[2        R4                  " [6        5      n[9        UR;                  5       R                  5       5       H  u  nnU	SS2S4   R                  5       U   nU	SS2S4   R                  5       U   S-
  nU	SS2S	4   R                  5       U   S-
  nUS:  d  Mb  US:  d  Mj  US:X  d  Mr  UUU4   R'                  U5        M     U Vs0 s H.  nU[
        R<                  " UU   5      R?                  5       S:  _M0     nnGM@     [        R@                  " [C        U5      S5      nU R$                  (       d  U4$ U[        R@                  " [C        U5      S5      4$ / n/ nSnUS   R                  S   nUS   nUS   nUS   R                  5       nSn	[	        U5       GH   n
Ub  U	SS2S4   n[
        R                  " U[
        RD                  S9nX   n	[	        UR                  S   5       H  nU	SS2S4   R                  5       U   nU	SS2S4   R                  5       U   S-
  nU	SS2S	4   R                  5       U   S-
  nUS:  d  M_  US:  d  Mg  US:X  d  Mo  [        XOU4   5      X'   M     XSS2S4'   Xj   nXz   nX   n	U R                  [
        RF                  " USS9[
        RF                  " USS9[
        RF                  " U	SS9S
9nUR"                  nU R$                  (       a  UR'                  UR(                  5        UR'                  U5        [H        RJ                  RM                  [H        RN                  " U[H        R0                  5      5      [H        RN                  " U[H        R0                  5      -  n[2        R4                  " [6        5      nU	n	[9        [H        R:                  " U5      R                  5       R                  5       5       H  u  nnU	SS2S4   R                  5       U   nU	SS2S4   R                  5       U   S-
  nU	SS2S	4   R                  5       U   S-
  nUS:  d  Mb  US:  d  Mj  US:X  d  Mr  UUU4   R'                  U5        M     U Vs0 s H.  nU[
        R<                  " UU   5      R?                  5       S:  _M0     nnGM     [H        RP                  " [C        U5      S5      nU R$                  (       d  U4$ U[H        RP                  " [C        U5      S5      4$ s  snf s  snf )z
Inference used for models that need to process sequences in a sequential fashion, like the SQA models which
handle conversational query related to a table.
ptN	input_idsr   attention_masktoken_type_ids   r	   r   )rb   rc   rd   )logitsg      ?)dtype)axis))rM   shapetodevicerangenp
zeros_likecpunumpytolistinttorch
from_numpyr%   longrS   	unsqueezerf   rU   appendlogits_aggregationdistributions	Bernoulliprobsfloat32collectionsdefaultdictr)   	enumeratesqueezearraymeancattupleint32expand_dimsrF   mathsigmoidcastconcat)r0   r]   
all_logitsall_aggregationsprev_answers
batch_sizerb   rc   rd   token_type_ids_exampleindexprev_labels_examplemodel_labelsi
segment_idcol_idrow_idinput_ids_exampleattention_mask_exampleoutputsrf   dist_per_tokenprobabilitiescoords_to_probspcolrowkeylogits_batchs                                r!   sequential_inference3TableQuestionAnsweringPipeline.sequential_inference   sL   
 >>T!J!L,2215J{+..t{{;I#$4588EN#$4588EN%)"z*  +*@A*F'#%==1D1H1H1J1P1P1R#SL-;-B*"<#5#5a#89%;AqD%A%H%H%J1%M
!71!=!D!D!Fq!IA!M!71!=!D!D!Fq!IA!M!Q;6Q;:?.1,?O2P.QLO : 493C3CL3Q3V3VW\WaWa3b3e3efjfqfq3r*1a40$-$4!)7)>&)7)>&**/99!<#9#C#CA#F#9#C#CA#F % 
 !>>$++G,F,FG!!&)!&!4!4!>!>f!>!M . 4 47M7R7RSXS`S`7a7d7d"((//8 ! #."9"9$"?%m&;&;&=&D&D&FGDAq!71!=!D!D!Fq!IJ0A6==?BQFC0A6==?BQFCaxC1Hq'c
3::1= H ]ll\kUXRXXoc.B%C%H%H%JS%P P\kl] +` !99U:%6:L*...L?s|UYYW\]mWnpqMr>ssJ!L,2215J{+I#$45N#$45;;=N%)"z*  +*@A*F'#%==1DBHH#UL-;-B*"<#5#5a#89%;AqD%A%H%H%J1%M
!71!=!D!D!Fq!IA!M!71!=!D!D!Fq!IA!M!Q;6Q;:?.1,?O2P.QLO : 4@1a40$-$4!)7)>&)7)>&** nn->QG#%>>2Hq#Q#%>>2Hq#Q % 
 !>>$++G,F,FG!!&) "

0K Lrww*BJJP ! #."9"9$"?)?&%bjj&?&E&E&G&N&N&PQDAq!71!=!D!D!Fq!IJ0A6==?BQFC0A6==?BQFCaxC1Hq'c
3::1= R ]ll\kUXRXXoc.B%C%H%H%JS%P P\kl] +` 99U:%6:L*...L?s|RYYW\]mWnpqMr>ssE  m|  ms   5_45_c                 v   > U R                   " U0 UD6n[        TU ]  " U40 UD6n[        U5      S:X  a  US   $ U$ )a2  
Answers queries according to a table. The pipeline accepts several types of inputs which are detailed below:

- `pipeline(table, query)`
- `pipeline(table, [query])`
- `pipeline(table=table, query=query)`
- `pipeline(table=table, query=[query])`
- `pipeline({"table": table, "query": query})`
- `pipeline({"table": table, "query": [query]})`
- `pipeline([{"table": table, "query": query}, {"table": table, "query": query}])`

The `table` argument should be a dict or a DataFrame built from that dict, containing the whole table:

Example:

```python
data = {
    "actors": ["brad pitt", "leonardo di caprio", "george clooney"],
    "age": ["56", "45", "59"],
    "number of movies": ["87", "53", "69"],
    "date of birth": ["7 february 1967", "10 june 1996", "28 november 1967"],
}
```

This dictionary can be passed in as such, or can be converted to a pandas DataFrame:

Example:

```python
import pandas as pd

table = pd.DataFrame.from_dict(data)
```

Args:
    table (`pd.DataFrame` or `Dict`):
        Pandas DataFrame or dictionary that will be converted to a DataFrame containing all the table values.
        See above for an example of dictionary.
    query (`str` or `List[str]`):
        Query or list of queries that will be sent to the model alongside the table.
    sequential (`bool`, *optional*, defaults to `False`):
        Whether to do inference sequentially or as a batch. Batching is faster, but models like SQA require the
        inference to be done sequentially to extract relations within sequences, given their conversational
        nature.
    padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `False`):
        Activates and controls padding. Accepts the following values:

        - `True` or `'longest'`: Pad to the longest sequence in the batch (or no padding if only a single
          sequence if provided).
        - `'max_length'`: Pad to a maximum length specified with the argument `max_length` or to the maximum
          acceptable input length for the model if that argument is not provided.
        - `False` or `'do_not_pad'` (default): No padding (i.e., can output a batch with sequences of different
          lengths).

    truncation (`bool`, `str` or [`TapasTruncationStrategy`], *optional*, defaults to `False`):
        Activates and controls truncation. Accepts the following values:

        - `True` or `'drop_rows_to_fit'`: Truncate to a maximum length specified with the argument `max_length`
          or to the maximum acceptable input length for the model if that argument is not provided. This will
          truncate row by row, removing rows from the table.
        - `False` or `'do_not_truncate'` (default): No truncation (i.e., can output batch with sequence lengths
          greater than the model maximum admissible input size).


Return:
    A dictionary or a list of dictionaries containing results: Each result is a dictionary with the following
    keys:

    - **answer** (`str`) -- The answer of the query given the table. If there is an aggregator, the answer will
      be preceded by `AGGREGATOR >`.
    - **coordinates** (`List[Tuple[int, int]]`) -- Coordinates of the cells of the answers.
    - **cells** (`List[str]`) -- List of strings made up of the answer cell values.
    - **aggregator** (`str`) -- If the model has an aggregator, this returns the aggregator.
r	   r   )rL   rJ   r5   r*   )r0   rX   r1   pipeline_inputsresultsrZ   s        r!   r5   'TableQuestionAnsweringPipeline.__call__  sJ    V ++T<V<'"?=f=w<11:r7   c                     0 nUb  X%S'   Ub  X5S'   0 nUb  XS'   [        U SS 5      b  U R                  US'   [        U SS 5      b  U R                  US'   U R                  US'   XV0 4$ )Npadding
truncation
sequentialassistant_modelassistant_tokenizer	tokenizer)rR   r   r   r   )r0   r   r   r   r1   preprocess_paramsforward_paramss          r!   _sanitize_parameters3TableQuestionAnsweringPipeline._sanitize_parametersj  s    +2i(!.8l+!+5<(4*D1=040D0DN,-4.5A*...N;'484L4LN01 "44r7   c                     Uc  U R                   S:X  a  SnOSnUS   US   peUR                  (       a  [        S5      eUb  US:X  a  [        S5      eU R                  XVU R                  XCS	9nXWS'   U$ )
NrI   drop_rows_to_fitdo_not_truncater   r   ztable is empty zquery is empty)return_tensorsr   r   )r%   emptyr'   r   rM   )r0   pipeline_inputr   r   r   r   r   r]   s           r!   
preprocess)TableQuestionAnsweringPipeline.preprocess}  s~    yyG#/
.
%g.w0Gu;;-..=ERK-..T^^Xbtwr7   c                    UR                  S5      nU R                  S:X  a-  U(       a  U R                  " S0 UD6nOFU R                  " S0 UD6nO3SU;  a  U R                  US'   U R
                  R                  " S0 UDUD6nXUS.nU$ )Nr   rI   generation_config)model_inputsr   r   r8   )popr%   r   r^   r   rS   generate)r0   r   r   generate_kwargsr   r   model_outputss          r!   _forward'TableQuestionAnsweringPipeline._forward  s      )9933ClC..>> #/97;7M7M 34jj))LLLOLG)5RYZr7   c                 h   US   nUS   nUS   nU R                   S:X  Ga  U R                  (       a  US S u  pVU R                  R                  X%U5      nUu  p[	        U	5       V
Vs0 s H(  u  pXR
                  R                  R                  U   _M*     nn
nU R
                  R                  R                  n[	        U	5       V
Vs0 s H  u  pX:w  d  M  XU
   S-   _M     nn
nO)US   nU R                  R                  X%5      nUS   n0 n0 n/ n[	        U5       H  u  nnU Vs/ s H  nUR                  U   PM     nnUR                  US5      nUR                  US5      nUS	R                  U5      -   UU Vs/ s H  nUR                  U   PM     snS
.nU(       a  UUS'   UR                  U5        M     [        W5      S:X  a  [        S5      eO,U R                  R                  USS9 Vs/ s H  nSU0PM	     nn[        U5      S:  a  U$ US   $ s  snn
f s  snn
f s  snf s  snf s  snf )Nr   r   r   rI   r   z > r   r   z, )answercoordinatescells
aggregatorzEmpty answerT)skip_special_tokensr   r	   )r%   rU   r   convert_logits_to_predictionsr   rS   rT   rG   no_aggregation_label_indexiatr(   joinrw   r*   r   batch_decode)r0   r   r]   r   r   rf   
logits_aggpredictionsanswer_coordinates_batchagg_predictionsr   predaggregatorsno_agg_label_indexaggregators_prefixanswersr   r   
coordinater   r   aggregator_prefixr   s                          r!   postprocess*TableQuestionAnsweringPipeline.postprocess  sS   ~.g&	*99~~%,Ra[""nnJJ6[ef<G9(\efu\vw\vQXQRq**"3"3"F"Ft"LL\vw%)ZZ%6%6%Q%Q"=F=W&=W'![_[u-A1~--=W # &" !"nnJJ6Z+6q>( %'"G&/0H&I"{ALM::.M(__UB7
$6$:$:5"$E!/$))E2BB#.FQRk
eii
3kR
 +5F<(v& 'J 6{a'77   9=8S8ST[qu8S8vw8vf&)8vGwg,*w:
:? x& N S xs$   '/HHH&H%=H*
3H/)rL   rU   r%   )NNN)NTN)F)r9   r:   r;   r<   r=   default_input_names_pipeline_calls_generater   _default_generation_configr   rK   r^   r   r5   r   r   r   r   r>   __classcell__)rZ   s   @r!   rA   rA   V   sa     D (#!1" $I#J Z"$@tDPd5& "(; (;r7   rA   )r}   r-   rp   rm   
generationr   utilsr   r   r   r   baser
   r   r   r   r   rs   models.auto.modeling_autor   r   
tensorflowrF   models.auto.modeling_tf_autor   r   r   rA   r8   r7   r!   <module>r      s       )  b a 
 2#O 2#j ,4@Ao;X o; Bo;r7   