o
    wiQ                  	   @   s,  d dl mZmZ d dlmZmZ ddlmZmZm	Z	 ddl
mZ ddlmZ 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W n eefy_   eZdd ZdZY nw edZdef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Optional   )Pipelineget_supported_taskspipeline)logging   )BaseTransformersCLICommand)BodyFastAPIHTTPException)APIRoute)	BaseModel)JSONResponse)runTc                  O   s   d S )N )xyr   r   Z/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/transformers/commands/serving.pyr   "   s   r   Fztransformers/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   nlpr   r   r   serve_command_factory+   s   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%   ;      
 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*   liststrr,   r   intr   r   r   r   r.   C   s   
 r.   c                   @   r$   )ServeDeTokenizeResultz!
    DeTokenize result model
    textN)r'   r(   r)   r*   r2   r,   r   r   r   r   r4   L   r-   r4   c                   @   r$   )ServeForwardResultz
    Forward result model
    outputN)r'   r(   r)   r*   r   r,   r   r   r   r   r6   T   r-   r6   c                   @   s   e Zd ZedefddZdedededefdd	Z	d
d Z
dd ZedddedddfdedefddZedddedddedddfdee dedef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
        servezFCLI tool to run inference requests through REST and GraphQL endpoints.)helpz--taskzThe task to run the pipeline on)typechoicesr:   z--host	localhostz$Interface the server will listen on.)r;   defaultr:   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.)r;   r:   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_argumentr2   r   r3   set_defaultsr#   )r8   serve_parserr   r   r   register_subcommand]   s,   z ServeCommand.register_subcommandr	   r   r    r!   c                 C   s   || _ || _|| _|| _tstdt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)response_modelresponse_classmethodsz	/tokenizePOSTz/detokenizez/forwardiX  )routestimeout)	_pipeliner   r    r!   _serve_dependencies_installedRuntimeErrorloggerinfor   r   
model_infor%   r   tokenizer.   
detokenizer4   forwardr6   _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   rX   r   r    r!   rY   r   r   r   r      s   zServeCommand.runc                 C   s   t t| jjjdS )N)r&   )r%   varsrO   r   r   r[   r   r   r   rT      s   zServeCommand.model_infoNT)embedF
text_input
return_idsc              
   C   sj   z| j j|}|r| j j|}t||dW S t|dW S  ty4 } ztddt|ddd}~w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.
        )r/   r0   )r/      r   errorstatus_codedetailN)rO   r   rU   convert_tokens_to_idsr.   	Exceptionr   r2   )rY   r^   r_   
tokens_txtr0   er   r   r   rU      s   zServeCommand.tokenizer0   skip_special_tokenscleanup_tokenization_spacesc              
   C   sP   z| j j|||}td|dW S  ty' } ztddt|ddd}~w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.
        ra   )r   r5   r`   rb   rd   N)rO   r   decoder4   rh   r   r2   )rY   r0   rk   rl   decoded_strrj   r   r   r   rV      s   zServeCommand.detokenizec              
      s\   t |dkrtg g dS z| |}t|dW S  ty- } z	tddt|id}~ww )zF
        **inputs**: **attention_mask**: **tokens_type_ids**:
        r   )r7   	attention)r7   r`   rc   N)lenr6   rO   rh   r   r2   )rY   inputsr7   rj   r   r   r   rW      s   
zServeCommand.forward)r'   r(   r)   staticmethodr   rE   r   r2   r3   rZ   r   rT   r   boolrU   r1   rV   rW   r   r   r   r   r   \   s$    1(



r   N)&argparser   r   typingr   r   	pipelinesr   r   r	   utilsr
   ra   r   fastapir   r   r   fastapi.routingr   pydanticr   starlette.responsesr   uvicornr   rP   ImportErrorAttributeErrorobject
get_loggerrR   r#   r%   r.   r4   r6   r   r   r   r   r   <module>   s0   
		