
    eThW                     h   S SK JrJr  S SKJrJrJr  SSKJrJ	r	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  S S
KJr  S SKJr  S SKJr  Sr\R>                  " S5      r S\4S jr! " S S\5      r" " S S\5      r# " S S\5      r$ " S S\5      r% " S S\5      r&g! \\4 a
    \rS rSr Nbf = f)    )ArgumentParser	Namespace)AnyListOptional   )Pipelineget_supported_taskspipeline)logging   )BaseTransformersCLICommand)BodyFastAPIHTTPException)APIRoute)	BaseModel)JSONResponse)runTc                      g )N )xys     U/var/www/auris/envauris/lib/python3.13/site-packages/transformers/commands/serving.pyr   r   "   s        Fztransformers/servingargsc                     [        U R                  U R                  (       a  U R                  OSU R                  U R                  U R
                  S9n[        XR                  U R                  U R                  5      $ )zr
Factory function used to instantiate serving server from provided command line arguments.

Returns: ServeCommand
N)taskmodelconfig	tokenizerdevice)
r   r   r   r    r!   r"   ServeCommandhostportworkers)r   nlps     r   serve_command_factoryr(   +   sV     YY JJdjjD{{..{{C YY		4<<@@r   c                   $    \ rS rSr% Sr\\S'   Srg)ServeModelInfoResult;   z
Expose model information
infosr   N)__name__
__module____qualname____firstlineno____doc__dict__annotations____static_attributes__r   r   r   r*   r*   ;   s     Kr   r*   c                   @    \ rS rSr% Sr\\   \S'   \\\	      \S'   Sr
g)ServeTokenizeResultC   z
Tokenize result model
tokens
tokens_idsr   N)r-   r.   r/   r0   r1   r   strr3   r   intr4   r   r   r   r6   r6   C   s"     Ic##r   r6   c                   $    \ rS rSr% Sr\\S'   Srg)ServeDeTokenizeResultL   z
DeTokenize result model
textr   N)r-   r.   r/   r0   r1   r:   r3   r4   r   r   r   r=   r=   L   s     Ir   r=   c                   $    \ rS rSr% Sr\\S'   Srg)ServeForwardResultT   z
Forward result model
outputr   N)r-   r.   r/   r0   r1   r   r3   r4   r   r   r   rA   rA   T   s     Kr   rA   c                       \ rS rSr\S\4S j5       rS\S\S\	S\	4S jr
S	 rS
 r\" SSS9\" SSS94S\S\4S jjr\" SSS9\" SSS9\" SSS94S\\	   S\S\4S jjr\" SSS94S jrSrg)r#   \   parserc                    U R                  SSS9nUR                  S[        [        5       SS9  UR                  S[        SS	S
9  UR                  S[        SSS
9  UR                  S[        SSS
9  UR                  S[        SS9  UR                  S[        SS9  UR                  S[        SS9  UR                  S[        SSS
9  UR                  [        S9  g)z
Register this command to argparse so it's available for the transformer-cli

Args:
    parser: Root parser to register command-specific arguments
servezFCLI tool to run inference requests through REST and GraphQL endpoints.)helpz--taskzThe task to run the pipeline on)typechoicesrI   z--host	localhostz$Interface the server will listen on.)rJ   defaultrI   z--porti"  z Port the serving will listen to.z	--workersr   zNumber of http workersz--modelz%Model's name or path to stored model.)rJ   rI   z--configz,Model's config name or path to stored model.z--tokenizerzTokenizer name to use.z--devicezSIndicate the device to run onto, -1 indicates CPU, >= 0 indicates GPU (default: -1))funcN)
add_parseradd_argumentr:   r
   r;   set_defaultsr(   )rF   serve_parsers     r   register_subcommand ServeCommand.register_subcommand]   s    ((b ) 
 	!!')2	 	" 	
 	!!(kPv!w!!(dIk!l!!+CIa!b!!)#<c!d!!*3=k!l!!-c@X!Y!!f	 	" 	
 	!!'<!=r   r   r$   r%   r&   c                    Xl         X l        X0l        X@l        [        (       d  [        S5      e[        R                  SU SU 35        [        [        SU R                  [        [        S/S9[        SU R                  [        [        S/S9[        S	U R                  [         [        S/S9[        S
U R"                  [$        [        S/S9/SS9U l        g )NzUsing serve command requires FastAPI and uvicorn. Please install transformers with [serving]: pip install "transformers[serving]". Or install FastAPI and uvicorn separately.zServing model over :/GET)response_modelresponse_classmethodsz	/tokenizePOSTz/detokenizez/forwardiX  )routestimeout)	_pipeliner$   r%   r&   _serve_dependencies_installedRuntimeErrorloggerinfor   r   
model_infor*   r   tokenizer6   
detokenizer=   forwardrA   _app)selfr   r$   r%   r&   s        r   __init__ServeCommand.__init__|   s    !		,,=  KK-dV1TF;<';'3!& #':'3!' %'<'3!' "'9'3!'-< ? DIr   c                 l    [        U R                  U R                  U R                  U R                  S9  g )N)r$   r%   r&   )r   ri   r$   r%   r&   rj   s    r   r   ServeCommand.run   s     DIIDIIDIIt||Lr   c                 b    [        [        U R                  R                  R                  5      S9$ )N)r,   )r*   varsr`   r   r    rn   s    r   re   ServeCommand.model_info   s"    #$t~~/C/C/J/J*KLLr   NT)embedF
text_input
return_idsc                     U R                   R                  R                  U5      nU(       a.  U R                   R                  R                  U5      n[	        X4S9$ [	        US9$ ! [
         a  n[        SS[        U5      S.S9eSnAff = f)z
Tokenize the provided input and eventually returns corresponding tokens id: - **text_input**: String to
tokenize - **return_ids**: Boolean flags indicating if the tokens have to be converted to their integer
mapping.
)r8   r9   )r8      r   errorstatus_codedetailN)r`   r!   rf   convert_tokens_to_idsr6   	Exceptionr   r:   )rj   rt   ru   
tokens_txtr9   es         r   rf   ServeCommand.tokenize   s~    
	X11:::FJ!^^55KKJW
**TT**== 	XC"sSTv8VWW	Xs   AA% A% %
B
/BB
r9   skip_special_tokenscleanup_tokenization_spacesc                      U R                   R                  R                  XU5      n[        SUS9$ ! [         a  n[        SS[        U5      S.S9eSnAff = f)a  
Detokenize the provided tokens ids to readable text: - **tokens_ids**: List of tokens ids -
**skip_special_tokens**: Flag indicating to not try to decode special tokens - **cleanup_tokenization_spaces**:
Flag indicating to remove all leading/trailing spaces and intermediate ones.
rx   )r   r?   rw   ry   r{   N)r`   r!   decoder=   r   r   r:   )rj   r9   r   r   decoded_strr   s         r   rg   ServeCommand.detokenize   s[    	X..2299*[vwK(rDD 	XC"sSTv8VWW	Xs   /2 
AAAc                    #    [        U5      S:X  a
  [        / / S9$  U R                  U5      n[        US9$ ! [         a  n[	        SS[        U5      05      eSnAff = f7f)z6
**inputs**: **attention_mask**: **tokens_type_ids**:
r   )rC   	attention)rC   rw   rz   N)lenrA   r`   r   r   r:   )rj   inputsrC   r   s       r   rh   ServeCommand.forward   sc      v;!%R2>>	8^^F+F%V44 	8gs1v%677	8s%   A 7 A 
AAAA )ri   r`   r$   r%   r&   )r-   r.   r/   r0   staticmethodr   rT   r	   r:   r;   rk   r   re   r   boolrf   r   rg   rh   r4   r   r   r   r#   r#   \   s    >N > ></ / /C /# /bMM *.d$)?TXY^fjTk X3 XT X( !%T 6$(d$;,0T,B	XIX "X &*	X" $(D#9 8r   r#   N)'argparser   r   typingr   r   r   	pipelinesr	   r
   r   utilsr   rx   r   fastapir   r   r   fastapi.routingr   pydanticr   starlette.responsesr   uvicornr   ra   ImportErrorAttributeErrorobject
get_loggerrc   r(   r*   r6   r=   rA   r#   r   r   r   <module>r      s    / & & ? ?  (*44("0$(! 
		2	3A	 A 9 $) $I  H8- H8{ 	^$ *I %*!*s   $B B10B1