U
    4Af                     @   sp   d dl mZ ddlmZmZmZmZ ddlmZ ddl	m
Z
 eeZeddd	Zd
d ZG dd de
ZdS )    )ArgumentParser   )PipelinePipelineDataFormatget_supported_taskspipeline)logging   )BaseTransformersCLICommand)pathc                 C   s@   | sdS t jD ]}| |r|  S qtd|  dt j d S )Npipez4Unable to determine file format from file extension z-. Please provide the format through --format )r   SUPPORTED_FORMATSendswith	Exception)r   ext r   =/tmp/pip-unpacked-wheel-zw5xktn0/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)formatZ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   strr   r   intset_defaultsr#   )r(   Z
run_parserr   r   r   register_subcommand?   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 )NzCCurrent pipeline requires output to be in binary format, saving at )r$   r%   Zis_multi_columns
isinstancedictappendZbinary_outputZsave_binaryloggerwarningsave)r&   r!   outputsentryr   Zbinary_pathr   r   r   r)   _   s    


zRunCommand.runN)
__name__
__module____qualname__r   r   r'   staticmethodr   r7   r)   r   r   r   r   r   :   s   r   N)argparser   Z	pipelinesr   r   r   r   utilsr    r
   Z
get_loggerr@   r;   r4   r   r#   r   r   r   r   r   <module>   s   
