§
    [zni[  ã                   ó‚  — 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n# eef$ r
 eZd„ ZdZY nw xY w 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&dS )é    )ÚArgumentParserÚ	Namespace)ÚAnyÚListÚOptionalé   )ÚPipelineÚget_supported_tasksÚpipeline)Úloggingé   )ÚBaseTransformersCLICommand)ÚBodyÚFastAPIÚHTTPException)ÚAPIRoute)Ú	BaseModel)ÚJSONResponse)ÚrunTc                  ó   — d S )N© )ÚxÚys     ú}/var/www/html/cool-property-finder/image-detection-backend/venv/lib/python3.11/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Ø œJÐ0ˆdŒjˆj¨DØŒ{Ø”.ØŒ{ðñ ô €Cõ ˜˜TœY¨¬	°4´<Ñ@Ô@Ð@r   c                   ó   — e Zd ZU dZeed<   dS )ÚServeModelInfoResultz"
    Expose model information
    ÚinfosN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚdictÚ__annotations__r   r   r   r*   r*   ;   s%   € € € € € € ðð ð €K€K�K€K€Kr   r*   c                   óL   — e Zd ZU dZee         ed<   eee                  ed<   dS )ÚServeTokenizeResultz
    Tokenize result model
    ÚtokensÚ
tokens_idsN)	r,   r-   r.   r/   r   Ústrr1   r   Úintr   r   r   r3   r3   C   sC   € € € € € € ðð ð �ŒIÐÐÑØ˜˜cœÔ#Ð#Ð#Ñ#Ð#Ð#r   r3   c                   ó   — e Zd ZU dZeed<   dS )ÚServeDeTokenizeResultz!
    DeTokenize result model
    ÚtextN)r,   r-   r.   r/   r6   r1   r   r   r   r9   r9   L   s%   € € € € € € ðð ð €I€I�I€I€Ir   r9   c                   ó   — e Zd ZU dZeed<   dS )ÚServeForwardResultz
    Forward result model
    ÚoutputN)r,   r-   r.   r/   r   r1   r   r   r   r<   r<   T   s%   € € € € € € ðð ð €K€K�K€K€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d
S )r#   Úparserc                 ó\  — |                       dd¬¦  «        }|                     dt          t          ¦   «         d¬¦  «         |                     dt          dd	¬
¦  «         |                     dt          dd¬
¦  «         |                     dt          dd¬
¦  «         |                     dt          d¬¦  «         |                     dt          d¬¦  «         |                     dt          d¬¦  «         |                     dt          dd¬
¦  «         |                     t          ¬¦  «         dS )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]   sN  € ð ×(Ò(ØÐbð )ñ 
ô 
ˆð 	×!Ò!ØÝÝ'Ñ)Ô)Ø2ð	 	"ñ 	
ô 	
ð 	
ð 	×!Ò! (µ¸kÐPvÐ!ÑwÔwÐwØ×!Ò! (µ¸dÐIkÐ!ÑlÔlÐlØ×!Ò! +µCÀÐIaÐ!ÑbÔbÐbØ×!Ò! )µ#Ð<cÐ!ÑdÔdÐdØ×!Ò! *µ3Ð=kÐ!ÑlÔlÐlØ×!Ò! -µcÐ@XÐ!ÑYÔYÐYØ×!Ò!ØÝØØfð	 	"ñ 	
ô 	
ð 	
ð 	×!Ò!Õ'<Ð!Ñ=Ô=Ð=Ð=Ð=r   r   r$   r%   r&   c                 óî  — || _         || _        || _        || _        t          st          d¦  «        ‚t                               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¬¦  «        | _        d S )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Ð;Ð;°TÐ;Ð;Ñ<Ô<Ð<ÝåØØœÝ';Ý'3Ø!& ðñ ô õ Ø#ØœÝ':Ý'3Ø!' ðñ ô õ Ø%ØœÝ'<Ý'3Ø!' ðñ ô õ Ø"ØœÝ'9Ý'3Ø!' ðñ ô ð-ð< ð? ñ  ô  ˆDŒIˆIˆIr   c                 óT   — t          | j        | j        | j        | j        ¬¦  «         d S )N)r$   r%   r&   )r   ra   r$   r%   r&   ©rb   s    r   r   zServeCommand.run­   s'   € ÝˆDŒI˜DœI¨D¬I¸t¼|ÐLÑLÔLÐLÐLÐLr   c                 óZ   — t          t          | j        j        j        ¦  «        ¬¦  «        S )N)r+   )r*   ÚvarsrX   r   r    re   s    r   r]   zServeCommand.model_info°   s#   € Ý#­$¨t¬~Ô/CÔ/JÑ*KÔ*KÐLÑLÔLÐLr   NT)ÚembedFÚ
text_inputÚ
return_idsc                 ó,  — 	 | j         j                             |¦  «        }|r0| 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×:Ò:¸:ÑFÔFˆJàð >Ø!œ^Ô5×KÒKÈJÑWÔW�
Ý*°*ÈÐTÑTÔTÐTå*°*Ð=Ñ=Ô=Ð=øåð 	Xð 	Xð 	XÝ¨CÀ"ÍsÐSTÉvÌvÐ8VÐ8VÐWÑWÔWÐWøøøøð	Xøøøs   ‚AA# ÁA# Á#
BÁ-!BÂBr5   Úskip_special_tokensÚcleanup_tokenization_spacesc                 óÎ   — 	 | 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×9Ò9¸*ÐFYÐ[vÑwÔwˆKÝ(¨r¸ÐDÑDÔDÐDøÝð 	Xð 	Xð 	XÝ¨CÀ"ÍsÐSTÉvÌvÐ8VÐ8VÐWÑWÔWÐWøøøøð	Xøøøs   ‚14 ´
A$¾!AÁA$c              ƒ   óü   K  — t          |¦  «        dk    rt          g g ¬¦  «        S 	 |                      |¦  «        }t          |¬¦  «        S # t          $ r$}t	          ddt          |¦  «        i¦  «        ‚d}~ww xY 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Ö   s�   è è € õ ˆv‰;Œ;˜!ÒÐÝ%¨R¸2Ð>Ñ>Ô>Ð>ð	8à—^’^ FÑ+Ô+ˆFÝ%¨VÐ4Ñ4Ô4Ð4øÝð 	8ð 	8ð 	8Ý  g­s°1©v¬vÐ%6Ñ7Ô7Ð7øøøøð	8øøøs   ¨$A Á
A;ÁA6Á6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ð MðMð Mð Mð *.¨¨d¸$Ð)?Ñ)?Ô)?ÐTXÐTXÐY^ÐfjÐTkÑTkÔTkð Xð X 3ð XÈTð Xð Xð Xð Xð( !%  T°Ð 6Ñ 6Ô 6Ø$( D¨°dÐ$;Ñ$;Ô$;Ø,0¨D°¸TÐ,BÑ,BÔ,Bð	Xð Xà˜”IðXð "ðXð &*ð	Xð Xð Xð Xð" $( 4¨°DÐ#9Ñ#9Ô#9ð 8ð 8ð 8ð 8ð 8ð 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�      sQ  ðð /Ð .Ð .Ð .Ð .Ð .Ð .Ð .Ø &Ð &Ð &Ð &Ð &Ð &Ð &Ð &Ð &Ð &à ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ø Ð Ð Ð Ð Ð Ø (Ð (Ð (Ð (Ð (Ð (ð*Ø4Ð4Ð4Ð4Ð4Ð4Ð4Ð4Ð4Ð4Ø(Ð(Ð(Ð(Ð(Ð(Ø"Ð"Ð"Ð"Ð"Ð"Ø0Ð0Ð0Ð0Ð0Ð0ØÐÐÐÐÐà$(Ð!Ð!øØ�^Ð$ð *ð *ð *Ø€Iðð ð ð %*Ð!Ð!Ð!ð*øøøð 
ˆÔ	Ð6Ñ	7Ô	7€ðA 	ð Að Að Að Að ð ð ð ð ˜9ñ ô ð ð$ð $ð $ð $ð $˜)ñ $ô $ð $ðð ð ð ð ˜Iñ ô ð ðð ð ð ð ˜ñ ô ð ðH8ð H8ð H8ð H8ð H8Ð-ñ H8ô H8ð H8ð H8ð H8s   ª$A ÁA ÁA 