U
    9%e6+                     @   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k
r   dZY nX 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   b/var/www/html/Darija-Ai-API/env/lib/python3.8/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 krVt	t
jjjjnt	| jjj}|d d }| jstt| nDt| jd	}t|}W 5 Q R X tt| jd kr|n| jd
|d dd t D d }t|d d	}t|}W 5 Q R X |d }|d }	t| d d|	kd|	kd|	k| d| }
tj|
d
d tj| d| d
d t| d| dd W 5 Q R X t| d|
 d t| d| d|
 d| d dd }rF| js|| 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s2|| 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   s      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 kr|qS r$   r   r&   r   r   r   r)   _   s      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   sT   t | d}| }W 5 Q R X t | d"}|D ]}d|kr.|| q.W 5 Q R X 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.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N}t | :}|D ].}|| ||kr(d}|D ]}	||	 qFq(W 5 Q R X W 5 Q R X |std| d | | |  ||  d S )NFr.   TzLine z was not found in file.)r/   r1   
ValueError)
r<   r=   r>   fhabs_pathZ
line_foundnew_fileZold_filer4   Zline_to_copy)r9   r7   r;   r:   r8   r   r   replace   s    


"
z'AddNewModelCommand.run.<locals>.replacec                    s*   d| kr p(d| kr p(d| ko(  S )Nzgenerating PyTorchzgenerating TensorFlowzgenerating Flaxr   )r4   )output_flaxoutput_pytorchoutput_tensorflowr   r   
skip_units   s
    z*AddNewModelCommand.run.<locals>.skip_unitsc              	      s   t | }g }d}d}|D ]}d|krFd|krF|dd }|}qd|krnd|krn|dd }|}qd|krd|kr|s|s||| g }qd|krd|krg }qd|kr|| qW 5 Q R X  |  d S )	NFz# To replace in: z##"r   z	# Below: z# End.z# Replace with)r/   splitappend)Zpath_to_datafileZdatafiler>   Z	skip_fileZskip_snippetr4   Zfile_to_replace_inr=   )r8   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   r/   r    jsonloadr8   makedirsshutilr:   r7   r9   tempfiler;   r   rmdir)r"   directoriesZpath_to_transformer_rootZpath_to_cookiecutterZconfiguration_fileZtesting_configurationr(   configurationZlowercase_model_namer,   Z	model_dirr5   rK   r   )
r9   r7   r;   r:   rD   rE   rF   r8   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    

