o
    *j?                     @   s   d dl Z d dlmZmZmZ d dlZd dlZd dlmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZmZ d dlmZmZ d dlm Z  e  Z!dgZ"ej#ej$e
j%dG dd deZ&dS )    N)AnyDictUnion)BertTokenizer)	Pipelines)Model)TableQuestionAnswering)
OutputKeys)Pipeline)	PIPELINES)"TableQuestionAnsweringPreprocessor)Database)ConstantSQLQuery)	ModelFileTasks)
get_loggerTableQuestionAnsweringPipeline)module_namec                       s   e Zd Z					ddeeef dedededef
 fd	d
Zdd Z	dd Z
dd Zdeeef deeef fddZdd Z  ZS )r   NgpuTmodelpreprocessordbconfig_filedevicec           	         s  t  j||||||dd|di d t| jts#J dtj |du r2t| jj	fi || _
ttj| jj	tj| _|du r[t| jtj| jj	dtj| jj	dd	| _n|| _t }|j| _|j| _|j| _|j| _|j| _|j| _|j| _|j| _|j| _dS )
a  use `model` and `preprocessor` to create a table question answering prediction pipeline

        Args:
            model (TableQuestionAnswering): a model instance
            preprocessor (TableQuestionAnsweringPreprocessor): a preprocessor instance
            db (Database): a database to store tables in the database
            kwargs (dict, `optional`):
                Extra kwargs passed into the preprocessor's constructor.
        compileFcompile_options)r   r   r   r   auto_collater   r   z,please check whether model config exists in Nz
table.jsonzsynonym.txt)	tokenizerZtable_file_pathZsyn_dict_file_path)super__init__pop
isinstancer   r   r   ZCONFIGURATIONr   Z	model_dirr   r   ospathjoinZ
VOCAB_FILEr   r   r   r   agg_opscond_opscond_conn_ops
action_opsZmax_select_numZmax_where_numZcol_type_dictZschema_link_dict
limit_dict)	selfr   r   r   r   r   r   kwargsZconstant	__class__ {/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/pipelines/nlp/table_question_answering_pipeline.pyr        sT   

	

z'TableQuestionAnsweringPipeline.__init__c                 C   s*   | j jdd | j| j | j   dS )zY Place model on certain device for pytorch models before first inference
                iX  )timeoutN)Z_model_prepare_lockacquirer   tor   release)r+   r/   r/   r0   prepare_model[   s   z,TableQuestionAnsweringPipeline.prepare_modelc                 C   s  | j |d  }|d }|d }|d g|d< |d u r|S |dkr"|S |dkr(|S |dkr.|S |d	kr4|S |d
kr|}g }g }	t|d D ]\}
}||d vr\|| |	|d |
  qDt|dk ro|t| |	d ||d< |	|d< g }|d D ]}|d t|k r|| q}t|dk r|t|ddg ||d< |S |dkr|}g }g }	t|d D ]:\}
}||d v r|| d}t|d D ]\}}|d | |kr|}q|	| q|| |	|d |
  q||d< |	|d< |S |dkrA|}|d }|d }	||d< |	|d< |d D ]+}|d t|k r=d}|d D ]}|d |d kr1d}q$|s=|d | q|S |dkr|}g }t|d D ]\}
}|d |d vrb|| qP||d< g }|d D ]}|d t|k r~|| qnt|dk r|t|ddg ||d< |S |dkr|}g }t|d D ]'\}
}d}|d D ]}|d |d krd}|| q|s|| q||d< g }|d D ]}|d t|k r|| qt|dk r|t|dddg ||d< |S |dkrZ|}|d }t|d D ]\}
}|d t|k r%|| q||d< g }|d D ]}|d t|k rA|| q1t|dk rT|t|ddg ||d< |S |S )Nactionheader_namesqltable_idfromZout_of_scriptsZswitch_tableZrestartZ	firstTurnZ	del_focusselagg   r   conds   ZNullZchange_agg_onlyZchange_focus_totalFTZdel_condZchange_condZadd_cond)r)   	enumerateappendlen)r+   history_sqlresulttabler6   headersZcurrent_sqlZpre_final_sqlZpre_selsZpre_aggsidxZseliZfinal_condsZcondiZchanged_aggiZ
idx_singleZaggiZ	pre_condiZin_flagZhistory_condiZin_single_flagZsingle_condir/   r/   r0   post_process_multi_turnb   s  















z6TableQuestionAnsweringPipeline.post_process_multi_turnc           "         s  ||d d d  d dg d dg  |d }g g }}g g }}|d D ]}|d	 d
v r8| | q*| | q*|D ]F}|d  }	|	dkrMq@dd  |d  f }
| j|d	  }|d }| d|	 d | d | d  | d|
 d | d | d  q@d| j|d   d }||}||}t|dkr=dfdd|D }d fdd|D }| j|d d	  }d||f }d||f }|d d }d\}}| jD ]}||v rd}| j| } nq|r|d| 7 }|d| 7 }t|d D ]9\}}|t d	 kr:d}td D ]\}}|dkr$|} nq||d vr5||d |< q|d |= qnd}g g }}t|d D ]{\}}| }	dd  | f }
|d | dkrq| |	 | |
 qJ|d | d kr| | j|d |  d! |	 d"  | | j|d |  d! |
 d"  qJ| | j|d |  d# |	 d"  | | j|d |  d# |
 d"  qJt|dkrt|dkrd$d|d% ||f } d&d|d ||f }!nVt|dkrd'd|d% |f } d(d|d |f }!n6t|dkr/d)d|d% |f } d*d|d |f }!nd+d|d% f } d,d|d f }!t| |!|d d-}|S ).z.
        convert sql struct to string
        r8   r:   r   r7   u   空列	header_idnullr>   r=   )      	`%s`.`%s`r9   r?   z(  z "z" )Zcond_conn_opz, c                    s   g | ]} |d   qS )r   r/   .0cond)header_namesr/   r0   
<listcomp>  s    zBTableQuestionAnsweringPipeline.sql_dict_to_str.<locals>.<listcomp>c                    s$   g | ]}d d  |d  f qS )rN   r9   r   r/   rP   )
header_idsrF   r/   r0   rT     s    z%s %s)Fr@   Tz	 LIMIT %dr;   Zheader_attributeZPRIMARY r<   rL   z
(DISTINCT )(z&SELECT %s FROM %s WHERE %s ORDER BY %sZ
table_namez(SELECT %s FROM `%s` WHERE %s ORDER BY %szSELECT %s FROM %s WHERE %szSELECT %s FROM `%s` WHERE %szSELECT %s FROM %s ORDER BY %szSELECT %s FROM `%s` ORDER BY %szSELECT %s FROM %szSELECT %s FROM `%s`)stringqueryZ
sql_result)	rB   r'   r(   r%   rC   r*   rA   r&   r   )"r+   rE   tablesr8   Zstr_cond_listZsql_cond_listZwhere_condsZorderby_condsrR   r7   rJ   opvalueZcond_strZstr_where_condsZsql_where_condsZstr_orderby_columnZsql_orderby_columnZstr_orderby_opZstr_orderbyZsql_orderbyZ	limit_keyZis_inZ	limit_numkeyrH   r;   Zprimary_selindexZattribZstr_sel_listZsql_sel_listZ	final_strZ	final_sqlr/   )rU   rS   rF   r0   sql_dict_to_str   s   














z.TableQuestionAnsweringPipeline.sql_dict_to_strinputsreturnc              
   C   sr  |d }|d }z| j ||| jj|d  d|d< W n ty'   ||d< Y nw | j|| jjd}| jjrzI| jj |j	}g g }}|j
D ]}|| jj|d  d |d |d  ||d  qHg }	| D ]	}
|	t|
 qm|||	d	}W n# ty } zt| g g g d	}W Y d
}~nd
}~ww g g g d	}tj|jtj|j	tj|d tj|i}tj|iS )zprocess the prediction results

        Args:
            inputs (Dict[str, Any]): _description_

        Returns:
            Dict[str, str]: the prediction results
        rE   rD   r9   )rD   rE   rF   r8   )rE   r[   Zheaderid2namer   )rJ   r7   rowsN)rI   r   r[   	Exceptionr`   Zis_use_sqliteZconnection_objcursorexecuterZ   descriptionrB   getZfetchalllistloggererrorr	   Z
SQL_STRINGrY   Z	SQL_QUERYZHISTORYZQUERY_RESULTZOUTPUT)r+   ra   rE   rD   r8   re   rU   rS   rg   rc   resZ	tabledataeoutputr/   r/   r0   postprocessf  sX   	





z*TableQuestionAnsweringPipeline.postprocessc                 C   s   |S )Nr/   )r+   datar/   r/   r0   _collate_fn  s   z*TableQuestionAnsweringPipeline._collate_fn)NNNr   T)__name__
__module____qualname__r   r   strr   r   r    r5   rI   r`   r   r   ro   rq   __classcell__r/   r/   r-   r0   r      s.    
; "o5)'r#   typingr   r   r   jsonZtorchZtransformersr   Zmodelscope.metainfor   Zmodelscope.modelsr   Zmodelscope.models.nlpr   Zmodelscope.outputsr	   Zmodelscope.pipelines.baser
   Zmodelscope.pipelines.builderr   Zmodelscope.preprocessorsr   Z7modelscope.preprocessors.nlp.space_T_cn.fields.databaser   Z5modelscope.preprocessors.nlp.space_T_cn.fields.structr   r   Zmodelscope.utils.constantr   r   Zmodelscope.utils.loggerr   rj   __all__Zregister_moduleZtable_question_answeringZ!table_question_answering_pipeliner   r/   r/   r/   r0   <module>   s.   