U
    &cY                     @   sl   d dl Z d dlmZ d dlmZ d dlmZmZmZm	Z	 e 
eZedddZdd	 ZG d
d deZdS )    N)ArgumentParser)BaseTransformersCLICommand)SUPPORTED_TASKSPipelinePipelineDataFormatpipeline)pathc                 C   s<   | sdS t jD ]}| |r|  S qtd| t jd S )NpipezeUnable to determine file format from file extension {}. Please provide the format through --format {})r   SUPPORTED_FORMATSendswith	Exceptionformat)r   ext r   =/tmp/pip-unpacked-wheel-ymerj3tt/transformers/commands/run.pytry_infer_format_from_ext   s    


 r   c                 C   sr   t | j| jr| jnd | j| j| jd}| jdkr:t| jn| j}t	j
|| j| j| jrZ| jn|j| jd}t||S )N)taskmodelconfig	tokenizerdeviceinfer)r   Zoutput_pathZ
input_pathcolumn	overwrite)r   r   r   r   r   r   r   r   inputr   Zfrom_stroutputr   Zdefault_input_namesr   
RunCommand)argsnlpr   readerr   r   r   run_command_factory   s     r    c                   @   s6   e Zd ZeedddZeedddZdd Z	d	S )
r   )r   r   c                 C   s   || _ || _d S )N)_nlp_reader)selfr   r   r   r   r   __init__-   s    zRunCommand.__init__)parserc                 C   s   | j ddd}|jdt dd |jdtdd	 |jd
tdd	 |jdtdd	 |jdtdd	 |jdtdd	 |jdtdd	 |jdtdtjdd |jdtddd |jdddd |jt	d  d S )!NrunzRun a pipeline through the CLI)helpz--taskzTask to run)choicesr'   z--inputz%Path to the file to use for inference)typer'   z--outputz9Path to the file that will be used post to write results.z--modelz)Name or path to the model to instantiate.z--configz2Name or path to the model's config to instantiate.z--tokenizerz?Name of the tokenizer to use. (default: same as the model name)z--columnzXName of the column to use as input. (For multi columns input as QA use column1,columns2)z--formatr   zInput format to read from)r)   defaultr(   r'   z--devicezSIndicate the device to run onto, -1 indicates CPU, >= 0 indicates GPU (default: -1))r)   r*   r'   z--overwrite
store_truez"Allow overwriting the output file.)actionr'   )func)

add_parseradd_argumentr   keysstrr   r
   intset_defaultsr    )r%   Z
run_parserr   r   r   register_subcommand1   s>      zRunCommand.register_subcommandc                 C   s   | j g  }}| jD ]<}| jjr(|f |n||}t|trF|| q||7 }q| j jrv| j|}t	d
| n| j| d S )NzECurrent pipeline requires output to be in binary format, saving at {})r!   r"   Zis_multi_columns
isinstancedictappendZbinary_outputZsave_binaryloggerwarningr   save)r#   r   outputsentryr   Zbinary_pathr   r   r   r&   Q   s    


zRunCommand.runN)
__name__
__module____qualname__r   r   r$   staticmethodr   r5   r&   r   r   r   r   r   ,   s   r   )loggingargparser   Ztransformers.commandsr   Ztransformers.pipelinesr   r   r   r   	getLoggerr>   r9   r2   r   r    r   r   r   r   r   <module>   s   
