o
    *j                      @   sL  d dl 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 d dlmZ d dlmZ d	d
gZe	jejejde	jejejde	jejejdG dd	 d	eZe	jejejde	jejejde	jejejde	jejejde	jejejde	jejejde	jejejdG dd
 d
eZdS )    )AnyDictN)HeadsModels
TaskModels)MODELS)EncoderModel)'AttentionTokenClassificationModelOutput
OutputKeys)Tasks)parse_label_mappingModelForTokenClassification"ModelForTokenClassificationWithCRF)module_namec                       sh   e Zd ZejZejZdZdZ	de
f fddZ fddZ												d fd	d
	Z  ZS )r   encoderT	model_dirc                    sv   i | _ |d}|du r(t|}|durt|dkrt|}dd | D | _ ||d< t j|g|R i | dS )zinitialize the sequence classification model from the `model_dir` path.

        Args:
            model_dir (str): the model path.
        
num_labelsNr   c                 S   s   i | ]\}}||qS  r   ).0labelidr   r   w/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/models/nlp/task_models/token_classification.py
<dictcomp>1   s    z8ModelForTokenClassification.__init__.<locals>.<dictcomp>)Zid2labelgetr   lenitemssuper__init__)selfr   argskwargsr   Zlabel2id	__class__r   r   r   "   s   
z$ModelForTokenClassification.__init__c                    sL   t   }t|dr|d d ur|jn|j|d< n|j|d< | jj|d< |S )Nclassifier_dropoutr   )r   parse_head_cfghasattrr#   Zhidden_dropout_probconfigr   )r   Zhead_cfgr!   r   r   r$   5   s   



z*ModelForTokenClassification.parse_head_cfgNc              
      sD   ||d< ||d< t  j|||||||||	f	i |}||_||_|S )Noffset_mapping
label_mask)r   forwardr'   r(   )r   Z	input_idsZattention_maskZtoken_type_idsZposition_idsZ	head_maskZinputs_embedslabelsZoutput_attentionsZoutput_hidden_statesZreturn_dictr'   r(   r    Zoutputsr!   r   r   r)   @   s   z#ModelForTokenClassification.forward)NNNNNNNNNNNN)__name__
__module____qualname__r   token_classificationtaskr   	head_typebase_model_prefixZoverride_base_model_prefixstrr   r$   r)   __classcell__r   r   r!   r   r      s(    c                   @   s   e Zd ZejZdZdd ZdS )r   r   c              	   K   s   |d }| j |d |d }|d }|d }t|}tt|D ]%}|| d  	 }	t
|| dd|	}
||  }|
|| |< q"|}td |d d |||dS )Nlogitsr(   r'   r   )Zlossr4   Zhidden_statesZ
attentionsr(   r'   Zpredictions)headdecodetorchZ
zeros_likeranger   sumlongcpuitemZnarrowboolr	   )r   Zinputsr    r4   Zpredictsr'   maskZmasked_predictiZmasked_lengthsZselected_predictsZmask_positionr   r   r   postprocessh   s.   

z.ModelForTokenClassificationWithCRF.postprocessN)r+   r,   r-   r   transformer_crfr0   r1   rA   r   r   r   r   r   [   s    
)typingr   r   r8   Zmodelscope.metainfor   r   r   Zmodelscope.models.builderr   Z,modelscope.models.nlp.task_models.task_modelr   Zmodelscope.outputsr	   r
   Zmodelscope.utils.constantr   Zmodelscope.utils.hubr   __all__Zregister_moduler.   Zpart_of_speechZnamed_entity_recognitionZtoken_classification_for_nerr   rB   ZtcrfZ	tcrf_wsegZword_segmentationr   r   r   r   r   <module>   s@   C"