o
    *j                     @   s   d dl mZ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mZ d
gZejeje	jdG dd
 d
eZdS )    )AnyDictOptionalUnionN)	Pipelines)Model)
OutputKeys)Pipeline)	PIPELINES)Preprocessor)	ModelFileTasksMGeoRankingPipeline)module_namec                	       s   e Zd Z					ddeeef dee dedef fd	d
Zde	ee
f de	ee
f fddZde	ee
f de	ee
f fddZdd Zdd Z  ZS )r   NgpuT   modelpreprocessorconfig_filedevicec                    s^   t  j|||||d t| jtsJ dtj |du r-tj| jj	fd|i|| _
dS dS )a)  Use `model` and `preprocessor` to create a nlp word segment pipeline
           for prediction.

        Args:
            model (str or Model): Supply either a local model dir which
            supported the WS task, or a model id from the model hub, or a torch
            model instance. preprocessor (Preprocessor): An optional
            preprocessor instance, please make sure the preprocessor fits for
            the model if supplied. kwargs (dict, `optional`):
                Extra kwargs passed into the preprocessor's constructor.
        )r   r   r   r   auto_collatez,please check whether model config exists in Nsequence_length)super__init__
isinstancer   r   r   ZCONFIGURATIONr   Zfrom_pretrainedZ	model_dirr   )selfr   r   r   r   r   r   kwargs	__class__ w/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/pipelines/multi_modal/mgeo_ranking_pipeline.pyr      s&   
zMGeoRankingPipeline.__init__inputsreturnc                 K   s   | j di ||S )Nr   )r   )r   r!   Zforward_paramsr   r   r    forward9   s   zMGeoRankingPipeline.forwardc                 C   s:   dd }|t j d   }|| }t j|iS )zprocess the prediction results
        Args:
            inputs (Dict[str, Any]): _description_

        Returns:
            Dict[str, Any]: the predicted text representation
        c                 S   s   t | dt |   S )N   )npexp)logitsr   r   r    sigmoidF   s   z0MGeoRankingPipeline.postprocess.<locals>.sigmoid)r   ZLOGITSZsqueezedetachcpunumpytolistZSCORES)r   r!   r(   r'   Z	pred_listr   r   r    postprocess=   s   	
zMGeoRankingPipeline.postprocessc                 C   sd  g g g }}}g g }}g g g }}	}
d}t |d dkrD|D ]!\}}}}}}|| || || || || q!n@t |d dkrd}|D ]3\	}}}}}}}}}|| || || || || || |	| |
| qP||||||||	|
|	 t| D ]}tt||tjrt	||t||
| j q|S )NFr      	   T)lenappendupdatevarskeysr   getattrtorchZTensorsetattrtor   )r   gisZinpsZgis_input_idsZgis_token_type_idsZgis_rel_type_idsZgis_absolute_position_idsZgis_relative_position_idsZgis_prov_idsZgis_city_idsZgis_dist_idsZchina_versionZgeom_idZ	geom_typeZrel_typeZabsolute_positionZrelative_positionZlxlyZprov_idZcity_idZdist_idZattr   r   r    get_gisM   sD   












zMGeoRankingPipeline.get_gisc                 C   s  i }g }g }|D ]}d|krX|d }|  ||d }|jd ur8||j|j|j|j|j|j|j|j	|j
d	 n||j|j|j|j|j|jd |tdg| j qd|kr|d }|  ||d }|jd ur||j|j|j|j|j|j|j|j	|j
d	 n||j|j|j|j|j|jd |tdg| j qd	|v sd
|v r|| | j||< q|ds|| | j||< qt|dkr||d< t|dkr||d< |S )NZsentence1_gisZgis1)		input_idsattention_masktoken_type_idsrel_type_idsabsolute_position_idsrelative_position_idsprov_idscity_idsdist_ids)r<   r=   r>   r?   r@   rA   r$   Zsentence2_gisZgis2r   Zqidlabelsr:   gis_listgis_tp)r;   rB   r2   r<   r=   r>   r?   r@   rA   rC   rD   r7   Z
LongTensorr9   r   
startswithr1   )r   batchZmerged_batchrF   rG   kr:   r   r   r    _collate_fnn   s|   




zMGeoRankingPipeline._collate_fn)NNr   Tr   )__name__
__module____qualname__r   r   strr   r   r   r   r   r#   r.   r;   rK   __classcell__r   r   r   r    r      s*    
"

"!)typingr   r   r   r   r,   r%   r7   Zmodelscope.metainfor   Zmodelscope.modelsr   Zmodelscope.outputsr   Zmodelscope.pipelines.baser	   Zmodelscope.pipelines.builderr
   Zmodelscope.preprocessorsr   Zmodelscope.utils.constantr   r   __all__Zregister_moduleZtext_rankingZmgeo_rankingr   r   r   r   r    <module>   s   