a
    d8+                     @   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y   dZY n0 eeZed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   l/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/transformers/commands/add_new_model.pyadd_new_model_command_factory%   s    r   c                   @   s8   e Zd ZeedddZd
eed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   Zadd_new_model_parserr   r   r   register_subcommand*   s    
z&AddNewModelCommand.register_subcommandN)r   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 rVt	t
jjjjnt	| jjj}|d d }| jstt| nXt| jd	}t|}W d    n1 s0    Y  tt| jd u r|n| jd
|d dd t D d }t|d d	}t|}W d    n1 s"0    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 s0    Y  t| d|
 d t| d| d|
 d| d dd }r| js6|| 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 r| jsr|| 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 tttt d+ fd,d-fd.d/		fd0d1}|| d2| d t| d S )3Na  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.Z	templatesZadding_a_new_modelrT)no_inputZextra_contextc                 S   s    g | ]}d |dd v r|qS r$   r   r&   r   r   r   r)   _   r*   z/configuration.jsonZlowercase_modelname$generate_tensorflow_pytorch_and_flaxZPyTorchZ
TensorFlowZFlaxz/src/transformers/models/)exist_okz/tests/models/z/__init__.pywz/configuration_z.pyc                 S   s|   t | d}| }W d    n1 s(0    Y  t | d,}|D ]}d|vrB|| qBW d    n1 sn0    Y  d S )Nr+   r/   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.mdxz/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l}t | D}|D ].}|| ||v r(d}|D ]}	||	 qFq(W d    n1 sl0    Y  W d    n1 s0    Y  |std| d | | |  ||  d S )NFr/   TzLine z was not found in file.)r0   r2   
ValueError)
r=   r>   r?   fhabs_pathZ
line_foundZnew_fileZold_filer5   Zline_to_copy)r:   r8   r<   r;   r9   r   r   replace   s    


J
z'AddNewModelCommand.run.<locals>.replacec                    s*   d| v r p(d| v r p(d| v o(  S )Nzgenerating PyTorchzgenerating TensorFlowzgenerating Flaxr   )r5   )output_flaxoutput_pytorchoutput_tensorflowr   r   
skip_units   s
    z*AddNewModelCommand.run.<locals>.skip_unitsc                    s   t | }g }d}d}|D ]}d|v rFd|vrF|dd }|}qd|v rnd|vrn|dd }|}qd|v rd|vr|s|s||| g }qd|v rd|vrg }qd|vr|| qW d    n1 s0    Y   |  d S )	NFz# To replace in: z##"r   z	# Below: z# End.z# Replace with)r0   splitappend)Zpath_to_datafileZdatafiler?   Z	skip_fileZskip_snippetr5   Zfile_to_replace_inr>   )r9   rC   rG   r   r   replace_in_files   s(    


*z0AddNewModelCommand.run.<locals>.replace_in_filesz/to_replace_)warningswarn_has_cookiecutterImportErroroslistdirlenr@   r!   r   __file__parentr   r
   r   r0   r    jsonloadr9   makedirsshutilr;   r8   r:   tempfiler<   r   rmdir)r"   directoriesZpath_to_transformer_rootZpath_to_cookiecutterZconfiguration_fileZtesting_configurationr(   configurationZlowercase_model_namer-   Z	model_dirr6   rK   r   )
r:   r8   r<   r;   rD   rE   rF   r9   rC   rG   r   run9   s    &(* "zAddNewModelCommand.run)N)
__name__
__module____qualname__staticmethodr   r   boolr   r#   r]   r   r   r   r   r   )   s   	r   )rU   rP   rX   rL   argparser   r   pathlibr   typingr   utilsr    r	   Zcookiecutter.mainr
   rN   rO   Z
get_loggerr^   loggerr   r   r   r   r   r   <module>   s    

