Ë
    S^(h[  ã                   ód  — d dl mZmZ d dlmZmZm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mZ d d	lmZ d d
lmZ d dlmZ d dlmZ dZ ej>                  d«      Z defd„Z! G d„ de«      Z" G d„ de«      Z# G d„ de«      Z$ G d„ de«      Z% G d„ de«      Z&y# eef$ r
 eZd„ ZdZY Œ`w xY w)é    )ÚArgumentParserÚ	Namespace)ÚAnyÚListÚOptionalé   )ÚPipelineÚget_supported_tasksÚpipeline)Úloggingé   )ÚBaseTransformersCLICommand)ÚBodyÚFastAPIÚHTTPException)ÚAPIRoute)Ú	BaseModel)ÚJSONResponse)ÚrunTc                   ó   — y )N© )ÚxÚys     ú[/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/commands/serving.pyr   r   "   s   € Øó    Fztransformers-cli/servingÚargsc                 óú   — t        | j                  | j                  r| j                  nd| j                  | j                  | j
                  ¬«      }t        || j                  | j                  | j                  «      S )z~
    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(   +   sX   € ô Ø�Y‰YØ ŸJšJˆd�jŠj¨DØ�{‰{Ø—.‘.Ø�{‰{ô€Cô ˜˜TŸY™Y¨¯	©	°4·<±<Ó@Ð@r   c                   ó   — e Zd ZU dZeed<   y)ÚServeModelInfoResultz"
    Expose model information
    ÚinfosN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚdictÚ__annotations__r   r   r   r*   r*   ;   s   … ñð „Kr   r*   c                   ó8   — e Zd ZU dZee   ed<   eee      ed<   y)ÚServeTokenizeResultz
    Tokenize result model
    ÚtokensÚ
tokens_idsN)	r,   r-   r.   r/   r   Ústrr1   r   Úintr   r   r   r3   r3   C   s"   … ñð �‰IÓØ˜˜c™Ñ#Ô#r   r3   c                   ó   — e Zd ZU dZeed<   y)ÚServeDeTokenizeResultz!
    DeTokenize result model
    ÚtextN)r,   r-   r.   r/   r6   r1   r   r   r   r9   r9   L   s   … ñð „Ir   r9   c                   ó   — e Zd ZU dZeed<   y)ÚServeForwardResultz
    Forward result model
    ÚoutputN)r,   r-   r.   r/   r   r1   r   r   r   r<   r<   T   s   … ñð „Kr   r<   c                   óè   — e Zd Zedefd„«       Zdedededefd„Z	d„ Z
d	„ Z ed
d¬«       edd¬«      fdedefd„Z ed
d¬«       edd¬«       edd¬«      fdee   dedefd„Z ed
d¬«      fd„Zy
)r#   Úparserc                 óð  — | j                  dd¬«      }|j                  dt        t        «       d¬«       |j                  dt        dd	¬
«       |j                  dt        dd¬
«       |j                  dt        dd¬
«       |j                  dt        d¬«       |j                  dt        d¬«       |j                  dt        d¬«       |j                  dt        dd¬
«       |j                  t        ¬«       y)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ÚchoicesrB   z--hostÚ	localhostz$Interface the server will listen on.)rC   ÚdefaultrB   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.)rC   rB   z--configz,Model's config name or path to stored model.z--tokenizerzTokenizer name to use.z--deviceéÿÿÿÿzSIndicate the device to run onto, -1 indicates CPU, >= 0 indicates GPU (default: -1))ÚfuncN)Ú
add_parserÚadd_argumentr6   r
   r7   Úset_defaultsr(   )r?   Úserve_parsers     r   Úregister_subcommandz ServeCommand.register_subcommand]   s  € ð ×(Ñ(ØÐbð )ó 
ˆð 	×!Ñ!ØÜÜ'Ó)Ø2ð	 	"ô 	
ð 	×!Ñ! (´¸kÐPvÐ!ÔwØ×!Ñ! (´¸dÐIkÐ!ÔlØ×!Ñ! +´CÀÐIaÐ!ÔbØ×!Ñ! )´#Ð<cÐ!ÔdØ×!Ñ! *´3Ð=kÐ!ÔlØ×!Ñ! -´cÐ@XÐ!ÔYØ×!Ñ!ØÜØØfð	 	"ô 	
ð 	×!Ñ!Ô'<Ð!Õ=r   r   r$   r%   r&   c                 óÈ  — || _         || _        || _        || _        t        st        d«      ‚t        j                  d|› d|› �«       t        t        d| j                  t        t        dg¬«      t        d| j                  t        t        dg¬«      t        d	| j                  t         t        dg¬«      t        d
| j"                  t$        t        dg¬«      gd¬«      | _        y )Nz­Using 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   Útokenizer3   Ú
detokenizer9   Úforwardr<   Ú_app)Úselfr   r$   r%   r&   s        r   Ú__init__zServeCommand.__init__|   sß   € Ø!ˆŒàˆŒ	ØˆŒ	ØˆŒå,Üð=óð ô �K‰KÐ-¨d¨V°1°T°FÐ;Ô<ÜäØØŸ™Ü';Ü'3Ø!& ôô Ø#ØŸ™Ü':Ü'3Ø!' ôô Ø%ØŸ™Ü'<Ü'3Ø!' ôô Ø"ØŸ™Ü'9Ü'3Ø!' ôð-ð< ô? ˆD�Ir   c                 ór   — t        | j                  | j                  | j                  | j                  ¬«       y )N)r$   r%   r&   )r   ra   r$   r%   r&   ©rb   s    r   r   zServeCommand.run­   s    € ÜˆD�I‰I˜DŸI™I¨D¯I©I¸t¿|¹|ÖLr   c                 óh   — t        t        | j                  j                  j                  «      ¬«      S )N)r+   )r*   ÚvarsrX   r   r    re   s    r   r]   zServeCommand.model_info°   s"   € Ü#¬$¨t¯~©~×/CÑ/C×/JÑ/JÓ*KÔLÐLr   NT)ÚembedFÚ
text_inputÚ
return_idsc                 ó"  — 	 | j                   j                  j                  |«      }|r2| j                   j                  j                  |«      }t	        ||¬«      S t	        |¬«      S # t
        $ r}t        ddt        |«      dœ¬«      ‚d}~ww xY w)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.
        )r4   r5   )r4   éô  Ú ©r   Úerror©Ústatus_codeÚdetailN)rX   r!   r^   Úconvert_tokens_to_idsr3   Ú	Exceptionr   r6   )rb   ri   rj   Ú
tokens_txtr5   Úes         r   r^   zServeCommand.tokenize³   s€   € ð
	XØŸ™×1Ñ1×:Ñ:¸:ÓFˆJáØ!Ÿ^™^×5Ñ5×KÑKÈJÓW�
Ü*°*ÈÔTÐTä*°*Ô=Ð=øäò 	XÜ¨CÀ"ÌsÐSTËvÑ8VÔWÐWûð	Xús   ‚AA' ÁA' Á'	BÁ0B	Â	Br5   Úskip_special_tokensÚcleanup_tokenization_spacesc                 óÀ   — 	 | j                   j                  j                  |||«      }t        d|¬«      S # t        $ r}t        ddt        |«      dœ¬«      ‚d}~ww xY w)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.
        rm   )r   r:   rl   rn   rp   N)rX   r!   Údecoder9   rt   r   r6   )rb   r5   rw   rx   Údecoded_strrv   s         r   r_   zServeCommand.detokenizeÅ   s^   € ð	XØŸ.™.×2Ñ2×9Ñ9¸*ÐFYÐ[vÓwˆKÜ(¨r¸ÔDÐDøÜò 	XÜ¨CÀ"ÌsÐSTËvÑ8VÔWÐWûð	Xús   ‚36 ¶	A¿AÁAc              ƒ   óÌ   K  — t        |«      dk(  rt        g g ¬«      S 	 | j                  |«      }t        |¬«      S # t        $ r}t	        ddt        |«      i«      ‚d}~ww xY w­w)zF
        **inputs**: **attention_mask**: **tokens_type_ids**:
        r   )r=   Ú	attention)r=   rl   ro   N)Úlenr<   rX   rt   r   r6   )rb   Úinputsr=   rv   s       r   r`   zServeCommand.forwardÖ   sd   è ø€ ô ˆv‹;˜!ÒÜ%¨R¸2Ô>Ð>ð	8à—^‘^ FÓ+ˆFÜ%¨VÔ4Ð4øÜò 	8Ü  g¬s°1«vÐ%6Ó7Ð7ûð	8üs%   ‚A$Ÿ< »A$¼	A!ÁAÁA!Á!A$)r,   r-   r.   Ústaticmethodr   rM   r	   r6   r7   rc   r   r]   r   Úboolr^   r   r_   r`   r   r   r   r#   r#   \   sÑ   „ Øð> Nò >ó ð>ð</ ð /°ð /¸Cð /È#ó /òbMòMñ *.¨d¸$Ô)?ÑTXÐY^ÐfjÔTkñ 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   rm   r   Úfastapir   r   r   Úfastapi.routingr   Úpydanticr   Ústarlette.responsesr   Úuvicornr   rY   ÚImportErrorÚAttributeErrorÚobjectÚ
get_loggerr[   r(   r*   r3   r9   r<   r#   r   r   r   ú<module>r�      sÄ   ð÷ /ß &Ñ &ç ?Ñ ?Ý Ý (ð*ß4Ñ4Ý(Ý"Ý0Ýà$(Ð!ð 
ˆ×	Ñ	Ð6Ó	7€ðA 	ó Aô ˜9ô ô$˜)ô $ô˜Iô ô˜ô ôH8Ð-õ H8øð{ 	�^Ð$ò *Ø€Iòð %*Ò!ð*ús   ª$B ÂB/Â.B/