U
    4Af[                  	   @   s2  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 zLd 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W n( eefk
r   eZdd ZdZY nX edZ edddZ!G dd deZ"G dd deZ#G dd deZ$G dd deZ%G d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                  O   s   d S )N )xyr   r   A/tmp/pip-unpacked-wheel-zw5xktn0/transformers/commands/serving.pyr   "   s    r   Fztransformers-cli/serving)argsc                 C   s:   t | j| jr| jnd| j| j| jd}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   Znlpr   r   r   serve_command_factory+   s    r#   c                   @   s   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   
r$   c                   @   s.   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   strr+   r   intr   r   r   r   r,   C   s   
r,   c                   @   s   e Zd ZU dZeed< dS )ServeDeTokenizeResultz!
    DeTokenize result model
    textN)r&   r'   r(   r)   r/   r+   r   r   r   r   r1   L   s   
r1   c                   @   s   e Zd ZU dZeed< dS )ServeForwardResultz
    Forward result model
    outputN)r&   r'   r(   r)   r   r+   r   r   r   r   r3   T   s   
r3   c                   @   s   e Zd ZeedddZeeeedddZ	dd Z
d	d
 ZedddedddfeedddZedddedddedddfee eedddZedddfddZdS )r   )parserc                 C   s   | j ddd}|jdtt dd |jdtdd	d
 |jdtddd
 |jdtddd
 |jdtdd |jdtdd |jdtdd |jdtddd
 |jtd 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
        ZservezFCLI tool to run inference requests through REST and GraphQL endpoints.)helpz--taskzThe task to run the pipeline on)typechoicesr6   z--host	localhostz$Interface the server will listen on.)r7   defaultr6   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.)r7   r6   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	   r0   set_defaultsr#   )r5   Zserve_parserr   r   r   register_subcommand]   s.     z ServeCommand.register_subcommand)r
   r    r!   r"   c                 C   s   || _ || _|| _|| _ts&tdnttd| d|  tt	d| j
ttdgdt	d| jttdgdt	d	| jttdgdt	d
| jttdgdgdd| _d S )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)Zresponse_modelresponse_classmethodsz	/tokenizePOSTz/detokenizez/forwardiX  )Zroutestimeout)	_pipeliner    r!   r"   _serve_dependencies_installedRuntimeErrorloggerinfor   r   
model_infor$   r   tokenizer,   
detokenizer1   forwardr3   _app)selfr
   r    r!   r"   r   r   r   __init__|   sR    zServeCommand.__init__c                 C   s   t | j| j| j| jd d S )N)r    r!   r"   )r   rQ   r    r!   r"   rR   r   r   r   r      s    zServeCommand.runc                 C   s   t t| jjjdS )N)r%   )r$   varsrH   r   r   rT   r   r   r   rM      s    zServeCommand.model_infoNT)embedF)
text_input
return_idsc              
   C   s|   z>| j j|}|r0| j j|}t||dW S t|dW S W n8 tk
rv } ztddt|ddW 5 d}~X Y nX dS )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.
        )r-   r.   )r-      r   errorstatus_codeZdetailN)rH   r   rN   Zconvert_tokens_to_idsr,   	Exceptionr   r/   )rR   rW   rX   Z
tokens_txtr.   er   r   r   rN      s    zServeCommand.tokenize)r.   skip_special_tokenscleanup_tokenization_spacesc              
   C   s^   z | j j|||}td|dW S  tk
rX } ztddt|ddW 5 d}~X Y nX dS )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.
        rZ   )r   r2   rY   r[   r]   N)rH   r   decoder1   r_   r   r/   )rR   r.   ra   rb   Zdecoded_strr`   r   r   r   rO      s
    zServeCommand.detokenizec              
      sh   t |dkrtg g dS z| |}t|dW S  tk
rb } ztddt|iW 5 d}~X Y nX dS )zF
        **inputs**: **attention_mask**: **tokens_type_ids**:
        r   )r4   Z	attention)r4   rY   r\   N)lenr3   rH   r_   r   r/   )rR   inputsr4   r`   r   r   r   rP      s    
zServeCommand.forward)r&   r'   r(   staticmethodr   r@   r   r/   r0   rS   r   rM   r   boolrN   r   rO   rP   r   r   r   r   r   \   s   1&


r   N)'argparser   r   typingr   r   r   Z	pipelinesr   r	   r
   utilsr   rZ   r   Zfastapir   r   r   Zfastapi.routingr   Zpydanticr   Zstarlette.responsesr   Zuvicornr   rI   ImportErrorAttributeErrorobjectZ
get_loggerrK   r#   r$   r,   r1   r3   r   r   r   r   r   <module>   s,   

	