






'''simple docstring'''





import argparse
import collections
import json
import os
import re
import string
import sys

import numpy as np


__UpperCAmelCase							:   Any							=  re.compile(R"\b(a|an|the)\b", re.UNICODE)

__UpperCAmelCase							:   Optional[Any]							=  None





def 					lowercase_ (    )     -> Any:





				'''simple docstring'''


				snake_case__      :int         =   argparse.ArgumentParser("Official evaluation script for SQuAD version 2.0."      )
				parser.add_argument("data_file"	,	metavar="data.json"	,	help="Input data JSON file."      )
				parser.add_argument("pred_file"	,	metavar="pred.json"	,	help="Model predictions."      )
				parser.add_argument(
				    "--out-file"	,	"-o"	,	metavar="eval.json"	,	help="Write accuracy metrics to file (default is stdout)."      )
				parser.add_argument(
				    "--na-prob-file"	,	"-n"	,	metavar="na_prob.json"	,	help="Model estimates of probability of no answer."      )
				parser.add_argument(
				    "--na-prob-thresh"	,	"-t"	,	type=__snake_case	,	default=1.0	,	help="Predict \"\" if no-answer probability exceeds this (default = 1.0)."	,	)
				parser.add_argument(
				    "--out-image-dir"	,	"-p"	,	metavar="out_images"	,	default=__snake_case	,	help="Save precision-recall curves to directory."      )
				parser.add_argument("--verbose"	,	"-v"	,	action="store_true"      )
				if len(sys.argv      ) == 1:
								parser.print_help()
								sys.exit(1      )
				return parser.parse_args()





def 					lowercase_ (    __snake_case     :							int      )     -> Union[str, Any]:





				'''simple docstring'''


				snake_case__      :Optional[int]         =   {}
				for article in dataset:
								for p in article["paragraphs"]:
												for qa in p["qas"]:
																snake_case__      :Optional[Any]         =   bool(qa["answers"]["text"]      )
				return qid_to_has_ans





def 					lowercase_ (    __snake_case     :							str      )     -> List[Any]:





				'''simple docstring'''


				def remove_articles(__snake_case     :							List[Any]      ):
								return ARTICLES_REGEX.sub(" "	,	__snake_case      )

				def white_space_fix(__snake_case     :							int      ):
								return " ".join(text.split()      )

				def remove_punc(__snake_case     :							List[str]      ):
								snake_case__      :Union[str, Any]         =   set(string.punctuation      )
								return "".join(ch for ch in text if ch not in exclude      )

				def lower(__snake_case     :							List[str]      ):
								return text.lower()

				return white_space_fix(remove_articles(remove_punc(lower(__snake_case      )      )      )      )





def 					lowercase_ (    __snake_case     :							Optional[int]      )     -> Optional[int]:





				'''simple docstring'''


				if not s:
								return []
				return normalize_answer(__snake_case      ).split()





def 					lowercase_ (    __snake_case     :							str	,	__snake_case     :							Optional[Any]      )     -> List[Any]:





				'''simple docstring'''


				return int(normalize_answer(__snake_case      ) == normalize_answer(__snake_case      )      )





def 					lowercase_ (    __snake_case     :							str	,	__snake_case     :							int      )     -> Dict:





				'''simple docstring'''


				snake_case__      :int         =   get_tokens(__snake_case      )
				snake_case__      :int         =   get_tokens(__snake_case      )
				snake_case__      :int         =   collections.Counter(__snake_case      ) & collections.Counter(__snake_case      )
				snake_case__      :Optional[int]         =   sum(common.values()      )
				if len(__snake_case      ) == 0 or len(__snake_case      ) == 0:
								# If either is no-answer, then F1 is 1 if they agree, 0 otherwise
								return int(gold_toks == pred_toks      )
				if num_same == 0:
								return 0
				snake_case__      :Union[str, Any]         =   1.0 * num_same / len(__snake_case      )
				snake_case__      :List[Any]         =   1.0 * num_same / len(__snake_case      )
				snake_case__      :List[Any]         =   (2 * precision * recall) / (precision + recall)
				return fa





def 					lowercase_ (    __snake_case     :							Dict	,	__snake_case     :							Tuple      )     -> Optional[int]:





				'''simple docstring'''


				snake_case__      :Optional[int]         =   {}
				snake_case__      :int         =   {}
				for article in dataset:
								for p in article["paragraphs"]:
												for qa in p["qas"]:
																snake_case__      :str         =   qa["id"]
																snake_case__      :Optional[int]         =   [t for t in qa["answers"]["text"] if normalize_answer(__snake_case      )]
																if not gold_answers:
																				# For unanswerable questions, only correct answer is empty string
																				snake_case__      :Optional[int]         =   [""]
																if qid not in preds:
																				print(F'Missing prediction for {qid}'      )
																				continue
																snake_case__      :Optional[Any]         =   preds[qid]
																# Take max over all gold answers
																snake_case__      :Dict         =   max(compute_exact(__snake_case	,	__snake_case      ) for a in gold_answers      )
																snake_case__      :int         =   max(compute_fa(__snake_case	,	__snake_case      ) for a in gold_answers      )
				return exact_scores, fa_scores





def 					lowercase_ (    __snake_case     :							Optional[int]	,	__snake_case     :							List[str]	,	__snake_case     :							Union[str, Any]	,	__snake_case     :							Dict      )     -> Optional[Any]:





				'''simple docstring'''


				snake_case__      :Optional[Any]         =   {}
				for qid, s in scores.items():
								snake_case__      :Any         =   na_probs[qid] > na_prob_thresh
								if pred_na:
												snake_case__      :Optional[Any]         =   float(not qid_to_has_ans[qid]      )
								else:
												snake_case__      :Any         =   s
				return new_scores





def 					lowercase_ (    __snake_case     :							Optional[int]	,	__snake_case     :							int	,	__snake_case     :							Union[str, Any]=None      )     -> Optional[Any]:





				'''simple docstring'''


				if not qid_list:
								snake_case__      :str         =   len(__snake_case      )
								return collections.OrderedDict(
								    [
								        ("exact", 1_00.0 * sum(exact_scores.values()      ) / total),
								        ("f1", 1_00.0 * sum(fa_scores.values()      ) / total),
								        ("total", total),
								    ]      )
				else:
								snake_case__      :Tuple         =   len(__snake_case      )
								return collections.OrderedDict(
								    [
								        ("exact", 1_00.0 * sum(exact_scores[k] for k in qid_list      ) / total),
								        ("f1", 1_00.0 * sum(fa_scores[k] for k in qid_list      ) / total),
								        ("total", total),
								    ]      )





def 					lowercase_ (    __snake_case     :							List[Any]	,	__snake_case     :							Dict	,	__snake_case     :							Dict      )     -> Optional[Any]:





				'''simple docstring'''


				for k in new_eval:
								snake_case__      :Tuple         =   new_eval[k]





def 					lowercase_ (    __snake_case     :							Union[str, Any]	,	__snake_case     :							List[str]	,	__snake_case     :							Union[str, Any]	,	__snake_case     :							str      )     -> int:





				'''simple docstring'''


				plt.step(__snake_case	,	__snake_case	,	color="b"	,	alpha=0.2	,	where="post"      )
				plt.fill_between(__snake_case	,	__snake_case	,	step="post"	,	alpha=0.2	,	color="b"      )
				plt.xlabel("Recall"      )
				plt.ylabel("Precision"      )
				plt.xlim([0.0, 1.0_5]      )
				plt.ylim([0.0, 1.0_5]      )
				plt.title(__snake_case      )
				plt.savefig(__snake_case      )
				plt.clf()





def 					lowercase_ (    __snake_case     :							Tuple	,	__snake_case     :							Dict	,	__snake_case     :							Optional[Any]	,	__snake_case     :							int	,	__snake_case     :							Optional[Any]=None	,	__snake_case     :							Any=None      )     -> Union[str, Any]:





				'''simple docstring'''


				snake_case__      :Tuple         =   sorted(__snake_case	,	key=lambda __snake_case      : na_probs[k]      )
				snake_case__      :Tuple         =   0.0
				snake_case__      :List[Any]         =   1.0
				snake_case__      :Optional[Any]         =   0.0
				snake_case__      :Optional[Any]         =   [1.0]
				snake_case__      :str         =   [0.0]
				snake_case__      :int         =   0.0
				for i, qid in enumerate(__snake_case      ):
								if qid_to_has_ans[qid]:
												true_pos += scores[qid]
								snake_case__      :int         =   true_pos / float(i + 1      )
								snake_case__      :int         =   true_pos / float(__snake_case      )
								if i == len(__snake_case      ) - 1 or na_probs[qid] != na_probs[qid_list[i + 1]]:
												# i.e., if we can put a threshold after this point
												avg_prec += cur_p * (cur_r - recalls[-1])
												precisions.append(__snake_case      )
												recalls.append(__snake_case      )
				if out_image:
								plot_pr_curve(__snake_case	,	__snake_case	,	__snake_case	,	__snake_case      )
				return {"ap": 1_00.0 * avg_prec}





def 					lowercase_ (    __snake_case     :							Union[str, Any]	,	__snake_case     :							int	,	__snake_case     :							str	,	__snake_case     :							Union[str, Any]	,	__snake_case     :							int	,	__snake_case     :							Tuple      )     -> Optional[Any]:





				'''simple docstring'''


				if out_image_dir and not os.path.exists(__snake_case      ):
								os.makedirs(__snake_case      )
				snake_case__      :List[Any]         =   sum(1 for v in qid_to_has_ans.values() if v      )
				if num_true_pos == 0:
								return
				snake_case__      :Dict         =   make_precision_recall_eval(
				    __snake_case	,	__snake_case	,	__snake_case	,	__snake_case	,	out_image=os.path.join(__snake_case	,	"pr_exact.png"      )	,	title="Precision-Recall curve for Exact Match score"	,	)
				snake_case__      :int         =   make_precision_recall_eval(
				    __snake_case	,	__snake_case	,	__snake_case	,	__snake_case	,	out_image=os.path.join(__snake_case	,	"pr_f1.png"      )	,	title="Precision-Recall curve for F1 score"	,	)
				snake_case__      :Tuple         =   {k: float(__snake_case      ) for k, v in qid_to_has_ans.items()}
				snake_case__      :Optional[int]         =   make_precision_recall_eval(
				    __snake_case	,	__snake_case	,	__snake_case	,	__snake_case	,	out_image=os.path.join(__snake_case	,	"pr_oracle.png"      )	,	title="Oracle Precision-Recall curve (binary task of HasAns vs. NoAns)"	,	)
				merge_eval(__snake_case	,	__snake_case	,	"pr_exact"      )
				merge_eval(__snake_case	,	__snake_case	,	"pr_f1"      )
				merge_eval(__snake_case	,	__snake_case	,	"pr_oracle"      )





def 					lowercase_ (    __snake_case     :							Any	,	__snake_case     :							Any	,	__snake_case     :							Union[str, Any]	,	__snake_case     :							Any      )     -> Optional[int]:





				'''simple docstring'''


				if not qid_list:
								return
				snake_case__      :Optional[Any]         =   [na_probs[k] for k in qid_list]
				snake_case__      :Optional[Any]         =   np.ones_like(__snake_case      ) / float(len(__snake_case      )      )
				plt.hist(__snake_case	,	weights=__snake_case	,	bins=20	,	range=(0.0, 1.0)      )
				plt.xlabel("Model probability of no-answer"      )
				plt.ylabel("Proportion of dataset"      )
				plt.title(F'Histogram of no-answer probability: {name}'      )
				plt.savefig(os.path.join(__snake_case	,	F'na_prob_hist_{name}.png'      )      )
				plt.clf()





def 					lowercase_ (    __snake_case     :							Optional[Any]	,	__snake_case     :							Optional[int]	,	__snake_case     :							List[Any]	,	__snake_case     :							List[Any]      )     -> List[str]:





				'''simple docstring'''


				snake_case__      :int         =   sum(1 for k in qid_to_has_ans if not qid_to_has_ans[k]      )
				snake_case__      :str         =   num_no_ans
				snake_case__      :Any         =   cur_score
				snake_case__      :int         =   0.0
				snake_case__      :Any         =   sorted(__snake_case	,	key=lambda __snake_case      : na_probs[k]      )
				for i, qid in enumerate(__snake_case      ):
								if qid not in scores:
												continue
								if qid_to_has_ans[qid]:
												snake_case__      :Any         =   scores[qid]
								else:
												if preds[qid]:
																snake_case__      :Optional[int]         =   -1
												else:
																snake_case__      :List[str]         =   0
								cur_score += diff
								if cur_score > best_score:
												snake_case__      :List[str]         =   cur_score
												snake_case__      :List[Any]         =   na_probs[qid]
				return 1_00.0 * best_score / len(__snake_case      ), best_thresh





def 					lowercase_ (    __snake_case     :							Optional[int]	,	__snake_case     :							Optional[Any]	,	__snake_case     :							List[str]	,	__snake_case     :							List[Any]	,	__snake_case     :							Union[str, Any]	,	__snake_case     :							List[str]      )     -> Optional[int]:





				'''simple docstring'''


				snake_case__      :Tuple         =   find_best_thresh(__snake_case	,	__snake_case	,	__snake_case	,	__snake_case      )
				snake_case__      :Dict         =   find_best_thresh(__snake_case	,	__snake_case	,	__snake_case	,	__snake_case      )
				snake_case__      :Any         =   best_exact
				snake_case__      :Tuple         =   exact_thresh
				snake_case__      :Any         =   best_fa
				snake_case__      :str         =   fa_thresh





def 					lowercase_ (    )     -> Tuple:





				'''simple docstring'''


				with open(OPTS.data_file      ) as f:
								snake_case__      :Any         =   json.load(__snake_case      )
								snake_case__      :Any         =   dataset_json["data"]
				with open(OPTS.pred_file      ) as f:
								snake_case__      :List[str]         =   json.load(__snake_case      )
				if OPTS.na_prob_file:
								with open(OPTS.na_prob_file      ) as f:
												snake_case__      :str         =   json.load(__snake_case      )
				else:
								snake_case__      :Optional[Any]         =   {k: 0.0 for k in preds}
				snake_case__      :List[str]         =   make_qid_to_has_ans(__snake_case      )  # maps qid to True/False
				snake_case__      :Tuple         =   [k for k, v in qid_to_has_ans.items() if v]
				snake_case__      :List[str]         =   [k for k, v in qid_to_has_ans.items() if not v]
				snake_case__      :Any         =   get_raw_scores(__snake_case	,	__snake_case      )
				snake_case__      :List[str]         =   apply_no_ans_threshold(__snake_case	,	__snake_case	,	__snake_case	,	OPTS.na_prob_thresh      )
				snake_case__      :List[str]         =   apply_no_ans_threshold(__snake_case	,	__snake_case	,	__snake_case	,	OPTS.na_prob_thresh      )
				snake_case__      :List[Any]         =   make_eval_dict(__snake_case	,	__snake_case      )
				if has_ans_qids:
								snake_case__      :Optional[Any]         =   make_eval_dict(__snake_case	,	__snake_case	,	qid_list=__snake_case      )
								merge_eval(__snake_case	,	__snake_case	,	"HasAns"      )
				if no_ans_qids:
								snake_case__      :Any         =   make_eval_dict(__snake_case	,	__snake_case	,	qid_list=__snake_case      )
								merge_eval(__snake_case	,	__snake_case	,	"NoAns"      )
				if OPTS.na_prob_file:
								find_all_best_thresh(__snake_case	,	__snake_case	,	__snake_case	,	__snake_case	,	__snake_case	,	__snake_case      )
				if OPTS.na_prob_file and OPTS.out_image_dir:
								run_precision_recall_analysis(__snake_case	,	__snake_case	,	__snake_case	,	__snake_case	,	__snake_case	,	OPTS.out_image_dir      )
								histogram_na_prob(__snake_case	,	__snake_case	,	OPTS.out_image_dir	,	"hasAns"      )
								histogram_na_prob(__snake_case	,	__snake_case	,	OPTS.out_image_dir	,	"noAns"      )
				if OPTS.out_file:
								with open(OPTS.out_file	,	"w"      ) as f:
												json.dump(__snake_case	,	__snake_case      )
				else:
								print(json.dumps(__snake_case	,	indent=2      )      )


if __name__ == "__main__":
	__UpperCAmelCase							:   List[Any]							=  parse_args()
	if OPTS.out_image_dir:
		import matplotlib

		matplotlib.use("Agg")
		import matplotlib.pyplot as plt
	main()







import json
import os
import shutil
import warnings
from argparse import ArgumentParser, Namespace
from pathlib import Path
from typing import List

from ..utils import logging
from . import BaseTransformersCLICommand


try:
	from cookiecutter.main import cookiecutter

	__UpperCAmelCase							:   Dict							=  True
except ImportError:
	__UpperCAmelCase							:   List[Any]							=  False

__UpperCAmelCase							:   Dict							=  logging.get_logger(__name__)  # pylint: disable=invalid-name





def 					lowercase_ (    __snake_case     :							Namespace      )     -> Dict:





				'''simple docstring'''


				return AddNewModelCommand(args.testing	,	args.testing_file	,	path=args.path      )


class 					_snake_case						(			_A ):






	@staticmethod
	def    lowerCAmelCase_							(		UpperCamelCase       )						->    Any:
					snake_case__      :Dict         =   parser.add_parser("add-new-model"       )
					add_new_model_parser.add_argument("--testing"					,action="store_true"					,help="If in testing mode."       )
					add_new_model_parser.add_argument("--testing_file"					,type=UpperCamelCase					,help="Configuration file on which to run."       )
					add_new_model_parser.add_argument(
					    "--path"					,type=UpperCamelCase					,help="Path to cookiecutter. Should only be used for testing purposes."       )
					add_new_model_parser.set_defaults(func=UpperCamelCase       )






	def __init__(		self					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase=None					,*UpperCamelCase       )						->    Any:
					snake_case__      :Union[str, Any]         =   testing
					snake_case__      :Union[str, Any]         =   testing_file
					snake_case__      :List[str]         =   path






	def    lowerCAmelCase_							(		self       )						->    List[Any]:
					warnings.warn(
					    "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."       )
					if not _has_cookiecutter:
									raise ImportError(
									    "Model creation dependencies are required to use the `add_new_model` command. Install them by running "
									    "the following at the root of your `transformers` clone:\n\n\t$ pip install -e .[modelcreation]\n"       )
					# Ensure that there is no other `cookiecutter-template-xxx` directory in the current working directory
					snake_case__      :Tuple         =   [directory for directory in os.listdir() if "cookiecutter-template-" == directory[:22]]
					if len(UpperCamelCase       ) > 0:
									raise ValueError(
									    "Several 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."       )

					snake_case__      :str         =   (
					    Path(UpperCamelCase       ).parent.parent.parent.parent if self._path is None else Path(self._path       ).parent.parent
					)
					snake_case__      :Tuple         =   path_to_transformer_root / "templates" / "adding_a_new_model"

					# Execute cookiecutter
					if not self._testing:
									cookiecutter(str(UpperCamelCase       )       )
					else:
									with open(self._testing_file					,"r"       ) as configuration_file:
													snake_case__      :str         =   json.load(UpperCamelCase       )

									cookiecutter(
									    str(path_to_cookiecutter if self._path is None else self._path       )					,no_input=UpperCamelCase					,extra_context=UpperCamelCase					,)

					snake_case__      :List[Any]         =   [directory for directory in os.listdir() if "cookiecutter-template-" in directory[:22]][0]

					# Retrieve configuration
					with open(directory + "/configuration.json"					,"r"       ) as configuration_file:
									snake_case__      :Dict         =   json.load(UpperCamelCase       )

					snake_case__      :Optional[Any]         =   configuration["lowercase_modelname"]
					snake_case__      :List[Any]         =   configuration["generate_tensorflow_pytorch_and_flax"]
					os.remove(f'{directory}/configuration.json'       )

					snake_case__      :Any         =   "PyTorch" in generate_tensorflow_pytorch_and_flax
					snake_case__      :Any         =   "TensorFlow" in generate_tensorflow_pytorch_and_flax
					snake_case__      :Any         =   "Flax" in generate_tensorflow_pytorch_and_flax

					snake_case__      :Dict         =   f'{path_to_transformer_root}/src/transformers/models/{lowercase_model_name}'
					os.makedirs(UpperCamelCase					,exist_ok=UpperCamelCase       )
					os.makedirs(f'{path_to_transformer_root}/tests/models/{lowercase_model_name}'					,exist_ok=UpperCamelCase       )

					# Tests require submodules as they have parent imports
					with open(f'{path_to_transformer_root}/tests/models/{lowercase_model_name}/__init__.py'					,"w"       ):
									pass

					shutil.move(
					    f'{directory}/__init__.py'					,f'{model_dir}/__init__.py'					,)
					shutil.move(
					    f'{directory}/configuration_{lowercase_model_name}.py'					,f'{model_dir}/configuration_{lowercase_model_name}.py'					,)

					def remove_copy_lines(UpperCamelCase       ):
									with open(UpperCamelCase					,"r"       ) as f:
													snake_case__      :List[str]         =   f.readlines()
									with open(UpperCamelCase					,"w"       ) as f:
													for line in lines:
																	if "# Copied from transformers." not in line:
																					f.write(UpperCamelCase       )

					if output_pytorch:
									if not self._testing:
													remove_copy_lines(f'{directory}/modeling_{lowercase_model_name}.py'       )

									shutil.move(
									    f'{directory}/modeling_{lowercase_model_name}.py'					,f'{model_dir}/modeling_{lowercase_model_name}.py'					,)

									shutil.move(
									    f'{directory}/test_modeling_{lowercase_model_name}.py'					,f'{path_to_transformer_root}/tests/models/{lowercase_model_name}/test_modeling_{lowercase_model_name}.py'					,)
					else:
									os.remove(f'{directory}/modeling_{lowercase_model_name}.py'       )
									os.remove(f'{directory}/test_modeling_{lowercase_model_name}.py'       )

					if output_tensorflow:
									if not self._testing:
													remove_copy_lines(f'{directory}/modeling_tf_{lowercase_model_name}.py'       )

									shutil.move(
									    f'{directory}/modeling_tf_{lowercase_model_name}.py'					,f'{model_dir}/modeling_tf_{lowercase_model_name}.py'					,)

									shutil.move(
									    f'{directory}/test_modeling_tf_{lowercase_model_name}.py'					,f'{path_to_transformer_root}/tests/models/{lowercase_model_name}/test_modeling_tf_{lowercase_model_name}.py'					,)
					else:
									os.remove(f'{directory}/modeling_tf_{lowercase_model_name}.py'       )
									os.remove(f'{directory}/test_modeling_tf_{lowercase_model_name}.py'       )

					if output_flax:
									if not self._testing:
													remove_copy_lines(f'{directory}/modeling_flax_{lowercase_model_name}.py'       )

									shutil.move(
									    f'{directory}/modeling_flax_{lowercase_model_name}.py'					,f'{model_dir}/modeling_flax_{lowercase_model_name}.py'					,)

									shutil.move(
									    f'{directory}/test_modeling_flax_{lowercase_model_name}.py'					,f'{path_to_transformer_root}/tests/models/{lowercase_model_name}/test_modeling_flax_{lowercase_model_name}.py'					,)
					else:
									os.remove(f'{directory}/modeling_flax_{lowercase_model_name}.py'       )
									os.remove(f'{directory}/test_modeling_flax_{lowercase_model_name}.py'       )

					shutil.move(
					    f'{directory}/{lowercase_model_name}.md'					,f'{path_to_transformer_root}/docs/source/en/model_doc/{lowercase_model_name}.md'					,)

					shutil.move(
					    f'{directory}/tokenization_{lowercase_model_name}.py'					,f'{model_dir}/tokenization_{lowercase_model_name}.py'					,)

					shutil.move(
					    f'{directory}/tokenization_fast_{lowercase_model_name}.py'					,f'{model_dir}/tokenization_{lowercase_model_name}_fast.py'					,)

					from os import fdopen, remove
					from shutil import copymode, move
					from tempfile import mkstemp

					def replace(UpperCamelCase					,UpperCamelCase					,UpperCamelCase       ):
									# Create temp file
									snake_case__    ,						snake_case__      :Optional[Any]         =   mkstemp()
									snake_case__      :Optional[Any]         =   False
									with fdopen(UpperCamelCase					,"w"       ) as new_file:
													with open(UpperCamelCase       ) as old_file:
																	for line in old_file:
																					new_file.write(UpperCamelCase       )
																					if line_to_copy_below in line:
																									snake_case__      :Optional[Any]         =   True
																									for line_to_copy in lines_to_copy:
																													new_file.write(UpperCamelCase       )

									if not line_found:
													raise ValueError(f'Line {line_to_copy_below} was not found in file.'       )

									# Copy the file permissions from the old file to the new file
									copymode(UpperCamelCase					,UpperCamelCase       )
									# Remove original file
									remove(UpperCamelCase       )
									# Move new file
									move(UpperCamelCase					,UpperCamelCase       )

					def skip_units(UpperCamelCase       ):
									return (
									    ("generating PyTorch" in line and not output_pytorch)
									    or ("generating TensorFlow" in line and not output_tensorflow)
									    or ("generating Flax" in line and not output_flax)
									)

					def replace_in_files(UpperCamelCase       ):
									with open(UpperCamelCase       ) as datafile:
													snake_case__      :int         =   []
													snake_case__      :Optional[int]         =   False
													snake_case__      :List[str]         =   False
													for line in datafile:
																	if "# To replace in: " in line and "##" not in line:
																					snake_case__      :Optional[Any]         =   line.split("\""       )[1]
																					snake_case__      :Tuple         =   skip_units(UpperCamelCase       )
																	elif "# Below: " in line and "##" not in line:
																					snake_case__      :Optional[Any]         =   line.split("\""       )[1]
																					snake_case__      :List[str]         =   skip_units(UpperCamelCase       )
																	elif "# End." in line and "##" not in line:
																					if not skip_file and not skip_snippet:
																									replace(UpperCamelCase					,UpperCamelCase					,UpperCamelCase       )

																					snake_case__      :Tuple         =   []
																	elif "# Replace with" in line and "##" not in line:
																					snake_case__      :Optional[Any]         =   []
																	elif "##" not in line:
																					lines_to_copy.append(UpperCamelCase       )

									remove(UpperCamelCase       )

					replace_in_files(f'{directory}/to_replace_{lowercase_model_name}.py'       )
					os.rmdir(UpperCamelCase       )