


'''simple docstring'''



import argparse
import re
from typing import Dict

import torch
from datasets import Audio, Dataset, load_dataset, load_metric

from transformers import AutoFeatureExtractor, pipeline







def      _UpperCamelCase   (		UpperCamelCase__			,   UpperCamelCase__       ):
			UpperCAmelCase__	:       Any   =  args.log_outputs
			UpperCAmelCase__	:       int   =  """_""".join(args.dataset.split("""/"""       ) + [args.config, args.split]       )

			# load metric
			UpperCAmelCase__	:       Tuple   =  load_metric("""wer"""       )
			UpperCAmelCase__	:       Optional[Any]   =  load_metric("""cer"""       )

			# compute metrics
			UpperCAmelCase__	:       Union[str, Any]   =  wer.compute(references=result["""target"""]			,   predictions=result["""prediction"""]       )
			UpperCAmelCase__	:       List[str]   =  cer.compute(references=result["""target"""]			,   predictions=result["""prediction"""]       )

			# print & log results
			UpperCAmelCase__	:       Optional[Any]   =  f'''WER: {wer_result}\nCER: {cer_result}'''
			print(UpperCamelCase__       )

			with open(f'''{dataset_id}_eval_results.txt'''			,   """w"""       ) as f:
						f.write(UpperCamelCase__       )

			# log all results in text file. Possibly interesting for analysis
			if log_outputs is not None:
						UpperCAmelCase__	:       str   =  f'''log_{dataset_id}_predictions.txt'''
						UpperCAmelCase__	:       Union[str, Any]   =  f'''log_{dataset_id}_targets.txt'''

						with open(UpperCamelCase__			,   """w"""       ) as p, open(UpperCamelCase__			,   """w"""       ) as t:
									# mapping function to write output
									def write_to_file(UpperCamelCase__			,   UpperCamelCase__       ):
												p.write(f'''{i}''' + """\n"""       )
												p.write(batch["""prediction"""] + """\n"""       )
												t.write(f'''{i}''' + """\n"""       )
												t.write(batch["""target"""] + """\n"""       )

									result.map(UpperCamelCase__			,   with_indices=UpperCamelCase__       )







def      _UpperCamelCase   (		UpperCamelCase__       ):
			UpperCAmelCase__	:       Dict   =  """[,?.!\-\;\:\"“%‘”�—’…–]"""  # noqa: W605 IMPORTANT: this should correspond to the chars that were ignored during training

			UpperCAmelCase__	:       Any   =  re.sub(UpperCamelCase__			,   """"""			,   text.lower()       )

			# In addition, we can normalize the target text, e.g. removing new lines characters etc...
			# note that order is important here!
			UpperCAmelCase__	:       str   =  ["""\n\n""", """\n""", """   """, """  """]

			for t in token_sequences_to_ignore:
						UpperCAmelCase__	:       List[Any]   =  """ """.join(text.split(UpperCamelCase__       )       )

			return text







def      _UpperCamelCase   (		UpperCamelCase__       ):
			# load dataset
			UpperCAmelCase__	:       Optional[int]   =  load_dataset(args.dataset			,   args.config			,   split=args.split			,   use_auth_token=UpperCamelCase__       )

			# for testing: only process the first two examples as a test
			# dataset = dataset.select(range(10))

			# load processor
			UpperCAmelCase__	:       Any   =  AutoFeatureExtractor.from_pretrained(args.model_id       )
			UpperCAmelCase__	:       int   =  feature_extractor.sampling_rate

			# resample audio
			UpperCAmelCase__	:       Union[str, Any]   =  dataset.cast_column("""audio"""			,   Audio(sampling_rate=UpperCamelCase__       )       )

			# load eval pipeline
			if args.device is None:
						UpperCAmelCase__	:       Optional[Any]   =  0 if torch.cuda.is_available() else -1
			UpperCAmelCase__	:       List[Any]   =  pipeline("""automatic-speech-recognition"""			,   model=args.model_id			,   device=args.device       )

			# map function to decode audio
			def map_to_pred(UpperCamelCase__       ):
						UpperCAmelCase__	:       int   =  asr(
						    batch["""audio"""]["""array"""]			,   chunk_length_s=args.chunk_length_s			,   stride_length_s=args.stride_length_s       )

						UpperCAmelCase__	:       int   =  prediction["""text"""]
						UpperCAmelCase__	:       Tuple   =  normalize_text(batch["""sentence"""]       )
						return batch

			# run inference on all examples
			UpperCAmelCase__	:       List[Any]   =  dataset.map(UpperCamelCase__			,   remove_columns=dataset.column_names       )

			# compute and log_results
			# do not change function below
			log_results(UpperCamelCase__			,   UpperCamelCase__       )


if __name__ == "__main__":
		__A						=argparse.ArgumentParser()

		parser.add_argument(
		    '--model_id', type=str, required=True, help='Model identifier. Should be loadable with 🤗 Transformers'
		)
		parser.add_argument(
		    '--dataset',
		    type=str,
		    required=True,
		    help='Dataset name to evaluate the `model_id`. Should be loadable with 🤗 Datasets',
		)
		parser.add_argument(
		    '--config', type=str, required=True, help='Config of the dataset. *E.g.* `\'en\'`  for Common Voice'
		)
		parser.add_argument('--split', type=str, required=True, help='Split of the dataset. *E.g.* `\'test\'`')
		parser.add_argument(
		    '--chunk_length_s', type=float, default=None, help='Chunk length in seconds. Defaults to 5 seconds.'
		)
		parser.add_argument(
		    '--stride_length_s', type=float, default=None, help='Stride of the audio chunks. Defaults to 1 second.'
		)
		parser.add_argument(
		    '--log_outputs', action='store_true', help='If defined, write outputs to log file for analysis.'
		)
		parser.add_argument(
		    '--device',
		    type=int,
		    default=None,
		    help='The device to run the pipeline on. -1 for CPU (default), 0 for the first GPU and so on.',
		)
		__A						=parser.parse_args()

		main(args)





import importlib
import json
import os
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
import transformers.models.auto
from transformers.models.auto.configuration_auto import CONFIG_MAPPING, AutoConfig
from transformers.models.bert.configuration_bert import BertConfig
from transformers.models.roberta.configuration_roberta import RobertaConfig
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, get_tests_dir


sys.path.append(str(Path(__file__).parent.parent.parent.parent / '''utils'''))

from test_module.custom_configuration import CustomConfig  # noqa E402


a__		     =     get_tests_dir('''fixtures/dummy-config.json''')


class    SCREAMING_SNAKE_CASE_				(       unittest.TestCase   ):



	"""simple docstring"""


	def 					lowerCamelCase__     (					self		:						str	)	->       Dict:







						"""simple docstring"""







						__UpperCamelCase					:	Optional[Any]             =      0






	def 					lowerCamelCase__     (					self		:						Union[str, Any]	)	->       List[str]:







						"""simple docstring"""







						self.assertIsNotNone(transformers.models.auto.__spec__	)
						self.assertIsNotNone(importlib.util.find_spec("""transformers.models.auto"""	)	)






	def 					lowerCamelCase__     (					self		:						Optional[Any]	)	->       Dict:







						"""simple docstring"""







						__UpperCamelCase					:	int             =      AutoConfig.from_pretrained("""bert-base-uncased"""	)
						self.assertIsInstance(lowerCAmelCase					,     lowerCAmelCase	)






	def 					lowerCamelCase__     (					self		:						List[Any]	)	->       Optional[Any]:







						"""simple docstring"""







						__UpperCamelCase					:	Dict             =      AutoConfig.from_pretrained(lowerCAmelCase	)
						self.assertIsInstance(lowerCAmelCase					,     lowerCAmelCase	)






	def 					lowerCamelCase__     (					self		:						Any	)	->       Optional[int]:







						"""simple docstring"""







						__UpperCamelCase					:	Optional[int]             =      AutoConfig.from_pretrained(lowerCAmelCase	)
						self.assertIsInstance(lowerCAmelCase					,     lowerCAmelCase	)






	def 					lowerCamelCase__     (					self		:						Tuple	)	->       Any:







						"""simple docstring"""







						__UpperCamelCase					:	List[Any]             =      AutoConfig.for_model("""roberta"""	)
						self.assertIsInstance(lowerCAmelCase					,     lowerCAmelCase	)






	def 					lowerCamelCase__     (					self		:						int	)	->       List[Any]:







						"""simple docstring"""







						with tempfile.TemporaryDirectory() as tmp_dir:
											# This model name contains bert and roberta, but roberta ends up being picked.
											__UpperCamelCase					:	Union[str, Any]             =      os.path.join(lowerCAmelCase					,     """fake-roberta"""	)
											os.makedirs(lowerCAmelCase					,     exist_ok=lowerCAmelCase	)
											with open(os.path.join(lowerCAmelCase					,     """config.json"""	)					,     """w"""	) as f:
																f.write(json.dumps({}	)	)
											__UpperCamelCase					:	Dict             =      AutoConfig.from_pretrained(lowerCAmelCase	)
											self.assertEqual(type(lowerCAmelCase	)					,     lowerCAmelCase	)






	def 					lowerCamelCase__     (					self		:						int	)	->       str:







						"""simple docstring"""







						try:
											AutoConfig.register("""custom"""					,     lowerCAmelCase	)
											# Wrong model type will raise an error
											with self.assertRaises(lowerCAmelCase	):
																AutoConfig.register("""model"""					,     lowerCAmelCase	)
											# Trying to register something existing in the Transformers library will raise an error
											with self.assertRaises(lowerCAmelCase	):
																AutoConfig.register("""bert"""					,     lowerCAmelCase	)

											# Now that the config is registered, it can be used as any other config with the auto-API
											__UpperCamelCase					:	Optional[Any]             =      CustomConfig()
											with tempfile.TemporaryDirectory() as tmp_dir:
																config.save_pretrained(lowerCAmelCase	)
																__UpperCamelCase					:	List[str]             =      AutoConfig.from_pretrained(lowerCAmelCase	)
																self.assertIsInstance(lowerCAmelCase					,     lowerCAmelCase	)

						finally:
											if "custom" in CONFIG_MAPPING._extra_content:
																del CONFIG_MAPPING._extra_content["custom"]






	def 					lowerCamelCase__     (					self		:						int	)	->       Optional[Any]:







						"""simple docstring"""







						with self.assertRaisesRegex(
						    lowerCAmelCase					,     """bert-base is not a local folder and is not a valid model identifier"""	):
											__UpperCamelCase					:	Tuple             =      AutoConfig.from_pretrained("""bert-base"""	)






	def 					lowerCamelCase__     (					self		:						Dict	)	->       Any:







						"""simple docstring"""







						with self.assertRaisesRegex(
						    lowerCAmelCase					,     R"""aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"""	):
											__UpperCamelCase					:	Optional[Any]             =      AutoConfig.from_pretrained(lowerCAmelCase					,     revision="""aaaaaa"""	)






	def 					lowerCamelCase__     (					self		:						str	)	->       List[str]:







						"""simple docstring"""







						with self.assertRaisesRegex(
						    lowerCAmelCase					,     """hf-internal-testing/no-config-test-repo does not appear to have a file named config.json."""					,     ):
											__UpperCamelCase					:	Union[str, Any]             =      AutoConfig.from_pretrained("""hf-internal-testing/no-config-test-repo"""	)






	def 					lowerCamelCase__     (					self		:						Optional[int]	)	->       Optional[int]:







						"""simple docstring"""







						with self.assertRaises(lowerCAmelCase	):
											__UpperCamelCase					:	str             =      AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""	)
						# If remote code is disabled, we can't load this config.
						with self.assertRaises(lowerCAmelCase	):
											__UpperCamelCase					:	Tuple             =      AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""					,     trust_remote_code=lowerCAmelCase	)

						__UpperCamelCase					:	List[str]             =      AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""					,     trust_remote_code=lowerCAmelCase	)
						self.assertEqual(config.__class__.__name__					,     """NewModelConfig"""	)

						# Test config can be reloaded.
						with tempfile.TemporaryDirectory() as tmp_dir:
											config.save_pretrained(lowerCAmelCase	)
											__UpperCamelCase					:	str             =      AutoConfig.from_pretrained(lowerCAmelCase					,     trust_remote_code=lowerCAmelCase	)
						self.assertEqual(reloaded_config.__class__.__name__					,     """NewModelConfig"""	)






	def 					lowerCamelCase__     (					self		:						Optional[int]	)	->       str:







						"""simple docstring"""







						class    SCREAMING_SNAKE_CASE_				(       _UpperCamelCase   ):



							"""simple docstring"""


							__magic_name__   :       int      	=		'new-model'

						try:
											AutoConfig.register("""new-model"""					,     lowerCAmelCase	)
											# If remote code is not set, the default is to use local
											__UpperCamelCase					:	Tuple             =      AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""	)
											self.assertEqual(config.__class__.__name__					,     """NewModelConfigLocal"""	)

											# If remote code is disabled, we load the local one.
											__UpperCamelCase					:	Any             =      AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""					,     trust_remote_code=lowerCAmelCase	)
											self.assertEqual(config.__class__.__name__					,     """NewModelConfigLocal"""	)

											# If remote is enabled, we load from the Hub
											__UpperCamelCase					:	List[Any]             =      AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""					,     trust_remote_code=lowerCAmelCase	)
											self.assertEqual(config.__class__.__name__					,     """NewModelConfig"""	)

						finally:
											if "new-model" in CONFIG_MAPPING._extra_content:
																del CONFIG_MAPPING._extra_content["new-model"]






