o
    h                     @   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def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 )Npipez4Unable to determine file format from file extension z-. Please provide the format through --format )r   SUPPORTED_FORMATSendswith	Exception)r   ext r   m/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/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r-| jn|j| jd}t||S )N)taskmodelconfig	tokenizerdeviceinfer)formatoutput_path
input_pathcolumn	overwrite)r   r   r   r   r   r   r   r   inputr   from_stroutputr   default_input_namesr   
RunCommand)argsnlpr   readerr   r   r   run_command_factory'   s    
r'   c                   @   s8   e Zd ZdedefddZedef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)r0   defaultr/   r.   z--devicezSIndicate the device to run onto, -1 indicates CPU, >= 0 indicates GPU (default: -1))r0   r1   r.   z--overwrite
store_truez"Allow overwriting the output file.)actionr.   )func)	
add_parseradd_argumentr   strr   r   intset_defaultsr'   )r,   
run_parserr   r   r   register_subcommand?   s:   zRunCommand.register_subcommandc                 C   s   | j g }}| jD ] }| jjr|di |n||}t|tr%|| q	||7 }q	| j jr>| j|}t	d|  d S | j
| d S )NzCCurrent pipeline requires output to be in binary format, saving at r   )r(   r)   is_multi_columns
isinstancedictappendbinary_outputsave_binaryloggerwarningsave)r*   r%   outputsentryr!   binary_pathr   r   r   r-   _   s   


zRunCommand.runN)
__name__
__module____qualname__r   r   r+   staticmethodr   r<   r-   r   r   r   r   r#   :   s
    r#   N)argparser   	pipelinesr   r   r   r   utilsr    r
   
get_loggerrI   rC   r8   r   r'   r#   r   r   r   r   <module>   s   
