o
    h6+                     @   s   d dl Z d dlZd dlZd dlZd dlmZmZ d dlmZ d dl	m
Z
 ddlmZ ddlmZ z
d d	lmZ d
ZW n eyE   dZY nw eeZdefddZG dd deZdS )    N)ArgumentParser	Namespace)Path)List   )logging   )BaseTransformersCLICommand)cookiecutterTFargsc                 C   s   t | j| j| jdS )N)path)AddNewModelCommandtestingtesting_filer   )r    r   Y/var/www/html/ai/venv/lib/python3.10/site-packages/transformers/commands/add_new_model.pyadd_new_model_command_factory%   s   r   c                   @   s:   e Zd ZedefddZddedefddZd	d
 Z	dS )r   parserc                 C   sJ   |  d}|jdddd |jdtdd |jd	td
d |jtd d S )Nzadd-new-modelz	--testing
store_truezIf in testing mode.)actionhelpz--testing_filez#Configuration file on which to run.)typer   z--pathz?Path to cookiecutter. Should only be used for testing purposes.)func)
add_parseradd_argumentstrset_defaultsr   )r   add_new_model_parserr   r   r   register_subcommand*   s   
z&AddNewModelCommand.register_subcommandNr   r   c                 G   s   || _ || _|| _d S N)_testing_testing_file_path)selfr   r   r   r   r   r   r   __init__4   s   
zAddNewModelCommand.__init__c              	      s  t d tstddd t D }t|dkrtd| jd u r+t	t
jjjjnt	| jjj}|d d }| jsBtt| n,t| jd	}t|}W d    n1 sXw   Y  tt| jd u rf|n| jd
|d dd t D d }t|d d	}t|}W d    n1 sw   Y  |d }|d }	t| d d|	v d|	v d|	v | d| }
tj|
d
d tj| d| d
d t| d| dd W d    n1 sw   Y  t| d|
 d t| d| d|
 d| d dd }rA| js|| d| d t| d| d|
 d| d t| d| d| d| d| d nt| d| d t| d| d r| jsh|| d| d t| d| d|
 d| d t| d| d| d| d| d nt| d| d t| d| d r| js|| d | d t| d | d|
 d | d t| d!| d| d| d!| d nt| d | d t| d!| d t| d"| d#| d$| d# t| d%| d|
 d%| d t| d&| d|
 d%| d' dd(lmm dd)lm m dd*lm d+td,td-tt f fd.d/fd0d1		fd2d3}|| d4| d t| d S )5Na  The command `transformers-cli add-new-model` is deprecated and will be removed in v5 of Transformers. It is not actively maintained anymore, so might give a result that won't pass all tests and quality checks, you should use `transformers-cli add-new-model-like` instead.zModel creation dependencies are required to use the `add_new_model` command. Install them by running the following at the root of your `transformers` clone:

	$ pip install -e .[modelcreation]
c                 S   s    g | ]}d |dd kr|qS zcookiecutter-template-N   r   .0	directoryr   r   r   
<listcomp>E        z*AddNewModelCommand.run.<locals>.<listcomp>r   zSeveral directories starting with `cookiecutter-template-` in current working directory. Please clean your directory by removing all folders starting with `cookiecutter-template-` or change your working directory.	templatesadding_a_new_modelrT)no_inputextra_contextc                 S   s    g | ]}d |dd v r|qS r%   r   r'   r   r   r   r*   _   r+   z/configuration.jsonlowercase_modelname$generate_tensorflow_pytorch_and_flaxPyTorch
TensorFlowFlaxz/src/transformers/models/)exist_okz/tests/models/z/__init__.pywz/configuration_z.pyc                 S   s~   t | d}| }W d    n1 sw   Y  t | d}|D ]}d|vr,|| q!W d    d S 1 s8w   Y  d S )Nr.   r7   z# Copied from transformers.)open	readlineswrite)r   flinesliner   r   r   remove_copy_lines~   s   

"z1AddNewModelCommand.run.<locals>.remove_copy_linesz
/modeling_z/test_modeling_z/modeling_tf_z/test_modeling_tf_z/modeling_flax_z/test_modeling_flax_/z.mdz/docs/source/en/model_doc/z/tokenization_z/tokenization_fast_z_fast.py)fdopenremove)copymodemove)mkstemporiginal_fileline_to_copy_belowlines_to_copyc           
   	      s    \}}d}|d6}t | "}|D ]}|| ||v r+d}|D ]}	||	 q#qW d    n1 s6w   Y  W d    n1 sEw   Y  |sTtd| d | | |  ||  d S )NFr7   TzLine z was not found in file.)r8   r:   
ValueError)
rE   rF   rG   fhabs_path
line_foundnew_fileold_filer=   line_to_copy)rB   r@   rD   rC   rA   r   r   replace   s(   


	
z'AddNewModelCommand.run.<locals>.replacec                    s*   d| v r pd| v o pd| v o  S )Nzgenerating PyTorchzgenerating TensorFlowzgenerating Flaxr   )r=   )output_flaxoutput_pytorchoutput_tensorflowr   r   
skip_units   s
   z*AddNewModelCommand.run.<locals>.skip_unitsc                    s   t | d}g }d}d}|D ]S}d|v r#d|vr#|dd }|}qd|v r7d|vr7|dd }|}qd|v rLd|vrL|sI|sI||| g }qd|v rWd|vrWg }qd|vr`|| qW d    n1 skw   Y   |  d S )	NFz# To replace in: z##"r   z	# Below: z# End.z# Replace with)r8   splitappend)path_to_datafiledatafilerG   	skip_fileskip_snippetr=   file_to_replace_inrF   )rA   rO   rS   r   r   replace_in_files   s.   



z0AddNewModelCommand.run.<locals>.replace_in_filesz/to_replace_)warningswarn_has_cookiecutterImportErroroslistdirlenrH   r"   r   __file__parentr    r
   r   r8   r!   jsonloadrA   makedirsshutilrC   r@   rB   tempfilerD   r   rmdir)r#   directoriespath_to_transformer_rootpath_to_cookiecutterconfiguration_filetesting_configurationr)   configurationlowercase_model_namer2   	model_dirr>   r\   r   )
rB   r@   rD   rC   rP   rQ   rR   rA   rO   rS   r   run9   s   &&zAddNewModelCommand.runr   )
__name__
__module____qualname__staticmethodr   r   boolr   r$   rt   r   r   r   r   r   )   s
    	r   )rf   ra   ri   r]   argparser   r   pathlibr   typingr   utilsr    r	   cookiecutter.mainr
   r_   r`   
get_loggerru   loggerr   r   r   r   r   r   <module>   s$   
