
    9i&              
       T   S SK r S SKJrJrJrJrJrJr  S SKr	S SK
r
S SKJr  S SKJr  S SKJr  S SKJrJr  S SKJr  S SKJr  S S	KJrJr  S S
KJrJr  S/r\R>                  " \R@                  \R@                  S9\R>                  " \R@                  \RB                  S9\R>                  " \R@                  \RD                  S9\R>                  " \R@                  \RF                  S9\R>                  " \RB                  \RB                  S9 " S S\5      5       5       5       5       5       r$g)    N)AnyDictListOptionalTupleUnion)	Pipelines)Model)
OutputKeys)InputPipeline)	PIPELINES)Preprocessor)	ModelFileTasks)torch_nested_detachtorch_nested_numpifyTokenClassificationPipeline)module_namec            	       x  ^  \ rS rSr     SS\\\4   S\\   S\S\4U 4S jjjr	S\
\\4   S\
\\4   4S	 jrS\
\\4   S\
\\4   4S
 jrS\
\\4   S\4S jrS\S\
\\4   4U 4S jjrS\\   S\S\\
\\4      4U 4S jjrS\\   S\4S jrS\\
\\4      S\
\\\\4   4   S\\
\\4      4S jrSrU =r$ )r      modelpreprocessorconfig_filedevicec                   > [         TU ]  UUUUUUR                  SS5      UR                  S0 5      S9  [        U R                  [
        5      (       d   S[        R                   35       eUc2  [        R                  " U R                  R                  4SU0UD6U l        U R                  R                  5         X`l        [        U R                  S5      (       d   eU R                  R                  U l        g)	ak  use `model` and `preprocessor` to create a token classification pipeline for prediction

Args:
    model (str or Model): A model instance or a model local dir or a model id in the model hub.
    preprocessor (Preprocessor): a preprocessor instance, must not be None.
    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 Nsequence_lengthid2label)super__init__pop
isinstancer   r
   r   CONFIGURATIONr   from_pretrained	model_dirr   evalr    hasattrr!   )	selfr   r   r   r   r   r    kwargs	__class__s	           v/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/modelscope/pipelines/nlp/token_classification_pipeline.pyr#   $TokenClassificationPipeline.__init__"   s      	%#%JJy%0"JJ'8"= 	 	? $**e,, 	U:9;R;R:ST	U,  , < <

$$! /! !D 	

.t((*5555))22    inputsreturnc                     UR                  [        R                  5      n[        R                  " 5          0 U R
                  " S0 UDUD6E[        R                  U0EsS S S 5        $ ! , (       d  f       g = f)N )r$   r   TEXTtorchno_gradr   )r+   r1   forward_paramstexts       r.   forward#TokenClassificationPipeline.forwardI   sS    zz*//*]]_**8v88:D//4 __s   (A''
A5c                 L    U R                   " U40 UD6n[        R                  U0$ )zProcess the prediction results

Args:
    inputs (Dict[str, Any]): should be tensors from model

Returns:
    Dict[str, Any]: the prediction results
)_chunk_processr   OUTPUT)r+   r1   postprocess_paramschunkss       r.   postprocess'TokenClassificationPipeline.postprocessQ   s+     $$VB/AB!!6**r0   c                    US   n[         R                  U;  aG  U[         R                     n[        UR                  5      S:X  a  US   n[
        R                  " USS9nO1U[         R                     n[        UR                  5      S:X  a  US   nUS   n[        UR                  5      S:X  a  US   nUR                  S5      nUb  UR                  S5      R                  5       R                  5       R                  5       n[
        R                  " USSU5      n[        UR                  5      S:X  a  US   nUR                  U5      n[        [        U5      5      n[        [        U5      5      nU V	s/ s H  oR                   U	   PM     n
n	UR#                  S
S5      nU(       ak  [         R                  U;   aU  U[         R                     n[        UR                  5      S:X  a  US   n[        [        UR%                  S5      5      5      nOSn/ n0 n['        [)        X5      5       GH  u  nu  nnUS   S;   aH  U(       a  X>S   US    US'   UR+                  U5        USS	 US   US   S.nU(       a  WU   X_      US'   US   S;   a*  U(       d#  USS	 US   US   S.nU(       a  WU   X_      US'   US   S;   a*  U(       d#  USS	 US   US   S.nU(       a  WU   X_      US'   US   S;   a  U(       a  US   US'   US   S;   d  M  U(       d  M  X>S   US    US'   UR+                  U5        0 nGM     U(       a  X>S   US    US'   UR+                  U5        U$ s  sn	f )zprocess the prediction results and output as chunks

Args:
    inputs (Dict[str, Any]): should be tensors from model

Returns:
    List: The output chunks
r9      r   )dim   offset_mapping
label_maskNreturn_probTFBSstartendspan   )typerL   rM   probIEIESES)r   PREDICTIONSLOGITSlenshaper6   argmaxgetsumlongcpuitemnarrowmasked_selectr   r   r!   r$   softmax	enumeratezipappend)r+   r1   r?   r9   logitspredictionsrH   rI   masked_lengthsxlabelsrJ   probsr@   chunkilabeloffsetss                     r.   r=   *TokenClassificationPipeline._chunk_process^   s{    f~!!/J--.F6<< A%,,v26K !7!78K;$$%*)!n 01~##$)+A.NZZ-
!'^^B/446::<AACN"\\1 N :##$)']
%33J?K-/1*+>{+KL,78Kq--"K8(,,]DA  F*
 1 12v||$)#AYF,'r(:;= $#,S-H#IAwQx4$(we$EE&MMM%(!!"I$QZ"1:
 $)!H[^$<E&MQx3 %ab	!(&qzE
 #(-a(@fQx3 %ab	!(&qzE
 #(-a(@fQx5 #*1:E%LQx45$(we$EE&MMM%(EM $JP  we=E&MMM% w 9s   8M'inputc                 
  > UR                  SS5      nUS::  a  [        T	U ]  " U/UQ70 UD6$ U R                  U/U5      u  pV/ nU H&  nUR	                  [        T	U ]  " U/UQ70 UD65        M(     U R                  Xv5      S   $ )Nsplit_max_lengthr   )r$   r"   _process_single_auto_splitre   
_auto_join)
r+   rq   argsr,   rs   split_textsindex_mappingoutputsr9   r-   s
            r.   rt   +TokenClassificationPipeline._process_single   s    !::&8&')q 7*5B4B6BB)-)9)95':J*L&KG#uw6tMdMfMN $??7:1==r0   
batch_sizec                    > UR                  SS5      nUS::  a  [        T	U ]  " U/UQ7SU0UD6$ U R                  X5      u  pg[        T	U ]  " U/UQ7SU0UD6nU R	                  X5      $ )Nrs   r   r|   )r$   r"   _process_batchru   rv   )
r+   rq   r|   rw   r,   rs   rx   ry   rz   r-   s
            r.   r~   *TokenClassificationPipeline._process_batch   s    !::&8&')q 7)?/3?",?7=? ? *.)9)9*)&Kg,E59E(2E=CEG??7::r0   input_textsrs   c                 D   / n0 nSn[        U5       H  u  pg[        U5      U:  a  UR                  U5        US4XE'   US-  nM2  [        R                  " [        U5      U-  5      n[        U5       H&  n	X-  n
UR                  XzX-    5        Xj4XE'   US-  nM(     M     X44$ )Nr   rO   )rc   rX   re   mathceilrange)r+   r   rs   rx   ry   new_idxraw_idxr9   n_splitrm   offsets              r.   ru   'TokenClassificationPipeline._auto_split   s    &{3MG4y++""4(*11&1))CI0@$@AwA1F&&t63L'MN.5->M*qLG	 ( 4 ))r0   rz   ry   c                 2   / n[        U5       H  u  pEX$   u  pgU[        U5      :  a  UR                  U5        M-  U[        R                      HA  nUS==   U-  ss'   US==   U-  ss'   X6   [        R                     R                  U5        MC     M     U$ )NrL   rM   )rc   rX   re   r   r>   )	r+   rz   ry   joined_outputsidxoutputr   r   rl   s	            r.   rv   &TokenClassificationPipeline._auto_join   s     $W-KC+0OG#n--%%f-#J$5$56E'Nf,N%LF*L"+J,=,=>EEeL 7 . r0   )r!   r   r    )NNgpuTi   )__name__
__module____qualname____firstlineno__r   r
   strr   r   r#   r   r   r:   rA   r   r=   r   rt   intr~   ru   r   rv   __static_attributes____classcell__)r-   s   @r.   r   r      se    9=$($"!$%3eSj)%3'5%3 "%3 	%3 %3Nd38n %)#s(^+$sCx. +-1#s(^+cT#s(^ c04cJ>U >S#X >;DK ;S ;$(c3h$8;*tCy *C *$S#X/U38_ 45:>tCH~:N r0   )%r   typingr   r   r   r   r   r   numpynpr6   modelscope.metainfor	   modelscope.modelsr
   modelscope.outputsr   modelscope.pipelines.baser   r   modelscope.pipelines.builderr   modelscope.preprocessorsr   modelscope.utils.constantr   r   modelscope.utils.tensor_utilsr   r   __all__register_moduletoken_classificationpart_of_speechword_segmentationnamed_entity_recognitionr   r4   r0   r.   <module>r      s    : :   ) # ) 5 2 1 6A )
) 	I,J,JL
	I,D,DF
	I,G,GI
	I,N,NP
	i&>&>@]( ]@PIFL]r0   