o
    ॵi                     @   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   CONFIGURATIONr   from_pretrained	model_dirr   )selfr   r   r   r   r   r   kwargs	__class__ j/home/ubuntu/.local/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$   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   LOGITSsqueezedetachcpunumpytolistSCORES)r   r$   r,   r+   	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torchTensorsetattrtor   )r   gisinpsgis_input_idsgis_token_type_idsgis_rel_type_idsgis_absolute_position_idsgis_relative_position_idsgis_prov_idsgis_city_idsgis_dist_idschina_versiongeom_id	geom_typerel_typeabsolute_positionrelative_positionlxlyprov_idcity_iddist_id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 )Nsentence1_gis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_   r`   r(   sentence2_gisgis2r   qidlabelsrC   gis_listgis_tp)rX   ra   r:   r[   r\   r]   r^   r_   r`   rb   rc   r?   
LongTensorrB   r   
startswithr9   )r   batchmerged_batchrh   ri   krC   r"   r"   r#   _collate_fnn   s|   




zMGeoRankingPipeline._collate_fn)NNr   Tr   )__name__
__module____qualname__r   r   strr   r   r   r   r   r'   r6   rX   ro   __classcell__r"   r"   r    r#   r      s*    
"

"!)typingr   r   r   r   r2   r)   r?   modelscope.metainfor   modelscope.modelsr   modelscope.outputsr   modelscope.pipelines.baser	   modelscope.pipelines.builderr
   modelscope.preprocessorsr   modelscope.utils.constantr   r   __all__register_moduletext_rankingmgeo_rankingr   r"   r"   r"   r#   <module>   s   