
    9i                        S SK JrJrJr  S SKrS SKrS SKJr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  S S	KJr  S S
KJrJrJr  S SKJr  \" 5       r\R<                  " \R>                  \R@                  S9\R<                  " \RB                  \RB                  S9\R<                  " \RD                  \RD                  S9\R<                  " \R>                  \R>                  S9\R<                  " \R>                  \RF                  S9\R<                  " \R>                  \RD                  S9\R<                  " \RF                  \RF                  S9 " S S\5      5       5       5       5       5       5       5       r$g)    )AnyDictUnionN)	PipelinesPreprocessors)Model)
OutputKeysTextClassificationModelOutput)Pipeline)	PIPELINES)batch_process)Preprocessor)Fields	ModelFileTasks)
get_logger)module_namec            	          ^  \ rS rSr    SS\\\4   S\S\S\4U 4S jjjrU 4S jr	S\
\\4   S	\
\\4   4U 4S
 jjr SS\\
\\4   \4   S\S	\
\\4   4S jjrSrU =r$ )TextClassificationPipeline   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c  U R                  R                  R                  S:X  aP  [        R                  " SU R                  R                  [        R                  [        R                   S.UD6U l        OlUR                  S	S
5      nUR                  SS5      nUR                  SS5      n	[        R                  " U R                  R                  40 UUU	S.UED6U l        [%        U R"                  S5      (       a  U R"                  R&                  U l        gg)u  The inference pipeline for all the text classification sub-tasks.

Args:
    model (`str` or `Model` or module instance): A model instance or a model local dir
        or a model id in the model hub.
    preprocessor (`Preprocessor`, `optional`): A Preprocessor instance.
    kwargs (dict, `optional`):
        Extra kwargs passed into the preprocessor's constructor.

Examples:
    >>> from modelscope.pipelines import pipeline
    >>> pipeline_ins = pipeline('text-classification',
        model='damo/nlp_structbert_sentence-similarity_chinese-base')
    >>> input = ('这是个测试', '这也是个测试')
    >>> print(pipeline_ins(input))
compileFcompile_options)r   r   r   r   auto_collater   r   z,please check whether model config exists in NOfaForAllTasks)model_name_or_pathtypefieldfirst_sequencetextsecond_sequencesequence_lengthi   )r#   r%   r&   id2label )super__init__pop
isinstancer   r   r   CONFIGURATION	__class____name__r   from_pretrained	model_dirr   ofa_tasks_preprocessorr   multi_modalr   hasattrr'   )selfr   r   r   r   r   kwargsr#   r%   r&   r.   s             u/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/modelscope/pipelines/nlp/text_classification_pipeline.pyr*   #TextClassificationPipeline.__init__$   sw   . 	%#%JJy%0"JJ'8"= 	 	? $**e,, 	U:9;R;R:ST	U, zz##,,0@@$0$@$@ %'+zz';';&== ,,% 	%! "(,<f!E"(**->"E"(**->"D$0$@$@JJ((%*8+:+:- !	-%! 4$$j11 --66DM 2    c                    > U R                   R                  R                  S:X  a  [        U R                   U5      $ [        [
        U ]  U5      $ )Nr   )r   r.   r/   r   r)   r   _batch)r5   datar.   s     r7   r;   !TextClassificationPipeline._batch]   s@    ::((,<< T223TA$GGr9   inputsreturnc                    > U R                   R                  R                  S:X  a0  [        R                  " 5          [
        TU ]  " U40 UD6sS S S 5        $ U R                   " S0 UDUD6$ ! , (       d  f       N"= f)Nr   r(   )r   r.   r/   torchno_gradr)   forward)r5   r>   forward_paramsr.   s      r7   rC   "TextClassificationPipeline.forwardc   s\    ::((,<<wv@@ !zz5F5n55 !s   A**
A8topkc                   ^  T R                   R                  R                  S:X  a  U$ [        T SS5      c  [        R                  S5        U[        R                     R                  5       R                  5       nUR                  S   S:X  a  US   nS nU" U5      nUR                  S   nUb  [        X&5      OUn[        R                  " XR* 5      U* S n[        R                  " XWSS	9R                  5       nU 4S
 jn[        R                   " U5      n	U	" U5      R                  5       n[#        [%        U5      5      n[#        [%        U5      5      n[        R&                  U[        R(                  U0$ )a  Process the prediction results

Args:
    inputs (`Dict[str, Any]` or `TextClassificationModelOutput`): The model output, please check
        the `TextClassificationModelOutput` class for details.
    topk (int): The topk probs to take
Returns:
    Dict[str, Any]: the prediction results.
        scores: The probabilities of each label.
        labels: The real labels.
    Label at index 0 is the smallest probability.
r   r'   Nz7The id2label mapping is None, will return original ids.r      c           	          [         R                  " U [         R                  " U SSS9-
  5      nXR                  SSS9-  $ )NT)axiskeepdims)npexpmaxsum)logitsrN   s     r7   softmax7TextClassificationPipeline.postprocess.<locals>.softmax   s8    ffVbffV"t&LLMWW"tW<<<r9   rJ   )rK   c                    > [        TSS 5      bk  U TR                  ;   a  TR                  U    $ [        U 5      TR                  ;   a  TR                  [        U 5         $ [        SU  STR                   35      eU $ )Nr'   zid z not found in id2label: )getattrr'   str	Exception)idr5   s    r7   map_to_label<TextClassificationPipeline.postprocess.<locals>.map_to_label   s{    4T2>T]]*#}}R00RDMM1#}}SW55'!"%=dmm_MO O Ir9   )r   r.   r/   rU   loggerwarningr	   LOGITScpunumpyshapeminrM   argpartitiontake_along_axistolist	vectorizelistreversedSCORESLABELS)
r5   r>   rF   rQ   rR   probsnum_classestop_indicesrY   v_funcs
   `         r7   postprocess&TextClassificationPipeline.postprocessj   s?     ::((,<<MtZ.6MOJ--.224::<F||A!#= FOE++b/K-1-=3t);D//%7?K&&uCJJLE
 \\,/F -446K%)Ex45K%%uj.?.?MMr9   )r'   r   )NNgpuT)N)r/   
__module____qualname____firstlineno__r   r   rV   r   r*   r;   r   r   rC   r
   intrn   __static_attributes____classcell__)r.   s   @r7   r   r      s    $ /3$($"77eSj)77+77 "77 	77 77rH6d38n 6%)#s(^6 !%4N!$sCx."?#@ A4N 4N *.c3h4N 4Nr9   r   )%typingr   r   r   r_   rM   rA   modelscope.metainfor   r   modelscope.models.baser   modelscope.outputsr	   r
   modelscope.pipelines.baser   modelscope.pipelines.builderr   modelscope.pipelines.utilr   modelscope.preprocessorsr   modelscope.utils.constantr   r   r   modelscope.utils.loggerr   r[   register_moduletext_classificationsentiment_analysisnlisentence_similaritysentiment_classificationr   r(   r9   r7   <module>r      sP   # #   8 ( H . 2 3 1 > > .	 	9+G+GI
599)--@
	9+H+HJ
	9+H+HJ
	9+M+MO
	9+H+HJ
	""224|N |N4JOJJ AI|Nr9   