




import gc
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    StableDiffusionSAGPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()




class 			UpperCAmelCase_      (   A		,      A		,      unittest.TestCase       ):




							'''simple docstring'''



							a__						  =     StableDiffusionSAGPipeline
							a__						  =     TEXT_TO_IMAGE_PARAMS
							a__						  =     TEXT_TO_IMAGE_BATCH_PARAMS
							a__						  =     TEXT_TO_IMAGE_IMAGE_PARAMS
							a__						  =     TEXT_TO_IMAGE_IMAGE_PARAMS
							a__						  =     False





							def    _UpperCAmelCase     (					self       :	Any			)			->					Dict:
										torch.manual_seed(0			)
										SCREAMING_SNAKE_CASE           =					UNetaDConditionModel(
										    block_out_channels=(32, 64)       ,     layers_per_block=2       ,     sample_size=32       ,     in_channels=4       ,     out_channels=4       ,     down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""")       ,     up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""")       ,     cross_attention_dim=32       ,     )
										SCREAMING_SNAKE_CASE           =					DDIMScheduler(
										    beta_start=0.0_0085       ,     beta_end=0.012       ,     beta_schedule="""scaled_linear"""       ,     clip_sample=a       ,     set_alpha_to_one=a       ,     )
										torch.manual_seed(0			)
										SCREAMING_SNAKE_CASE           =					AutoencoderKL(
										    block_out_channels=[32, 64]       ,     in_channels=3       ,     out_channels=3       ,     down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""]       ,     up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""]       ,     latent_channels=4       ,     )
										torch.manual_seed(0			)
										SCREAMING_SNAKE_CASE           =					CLIPTextConfig(
										    bos_token_id=0       ,     eos_token_id=2       ,     hidden_size=32       ,     intermediate_size=37       ,     layer_norm_eps=1E-0_5       ,     num_attention_heads=4       ,     num_hidden_layers=5       ,     pad_token_id=1       ,     vocab_size=1_000       ,     )
										SCREAMING_SNAKE_CASE           =					CLIPTextModel(a			)
										SCREAMING_SNAKE_CASE           =					CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""			)

										SCREAMING_SNAKE_CASE           =					{
										    """unet""": unet,
										    """scheduler""": scheduler,
										    """vae""": vae,
										    """text_encoder""": text_encoder,
										    """tokenizer""": tokenizer,
										    """safety_checker""": None,
										    """feature_extractor""": None,
										}
										return components





							def    _UpperCAmelCase     (					self       :	int       ,     a       :	Any       ,     a       :	Optional[int]=0			)			->					List[str]:
										if str(a			).startswith("""mps"""			):
													SCREAMING_SNAKE_CASE           =					torch.manual_seed(a			)
										else:
													SCREAMING_SNAKE_CASE           =					torch.Generator(device=a			).manual_seed(a			)
										SCREAMING_SNAKE_CASE           =					{
										    """prompt""": """.""",
										    """generator""": generator,
										    """num_inference_steps""": 2,
										    """guidance_scale""": 1.0,
										    """sag_scale""": 1.0,
										    """output_type""": """numpy""",
										}
										return inputs







							def    _UpperCAmelCase     (					self       :	Optional[Any]			)			->					List[str]:
										super().test_inference_batch_single_identical(expected_max_diff=3E-3			)





@slow
@require_torch_gpu
class 			UpperCAmelCase_      (   unittest.TestCase       ):




							'''simple docstring'''



							def    _UpperCAmelCase     (					self       :	int			)			->					Optional[Any]:
										# clean up the VRAM after each test
										super().tearDown()
										gc.collect()
										torch.cuda.empty_cache()





							def    _UpperCAmelCase     (					self       :	List[Any]			)			->					Dict:
										SCREAMING_SNAKE_CASE           =					StableDiffusionSAGPipeline.from_pretrained("""CompVis/stable-diffusion-v1-4"""			)
										SCREAMING_SNAKE_CASE           =					sag_pipe.to(a			)
										sag_pipe.set_progress_bar_config(disable=a			)

										SCREAMING_SNAKE_CASE           =					"""."""
										SCREAMING_SNAKE_CASE           =					torch.manual_seed(0			)
										SCREAMING_SNAKE_CASE           =					sag_pipe(
										    [prompt]       ,     generator=a       ,     guidance_scale=7.5       ,     sag_scale=1.0       ,     num_inference_steps=20       ,     output_type="""np"""			)

										SCREAMING_SNAKE_CASE           =					output.images

										SCREAMING_SNAKE_CASE           =					image[0, -3:, -3:, -1]

										assert image.shape == (1, 512, 512, 3)
										SCREAMING_SNAKE_CASE           =					np.array([0.1568, 0.1738, 0.1695, 0.1693, 0.1507, 0.1705, 0.1547, 0.1751, 0.1949]			)

										assert np.abs(image_slice.flatten() - expected_slice			).max() < 5E-2





							def    _UpperCAmelCase     (					self       :	Dict			)			->					int:
										SCREAMING_SNAKE_CASE           =					StableDiffusionSAGPipeline.from_pretrained("""stabilityai/stable-diffusion-2-1-base"""			)
										SCREAMING_SNAKE_CASE           =					sag_pipe.to(a			)
										sag_pipe.set_progress_bar_config(disable=a			)

										SCREAMING_SNAKE_CASE           =					"""."""
										SCREAMING_SNAKE_CASE           =					torch.manual_seed(0			)
										SCREAMING_SNAKE_CASE           =					sag_pipe(
										    [prompt]       ,     generator=a       ,     guidance_scale=7.5       ,     sag_scale=1.0       ,     num_inference_steps=20       ,     output_type="""np"""			)

										SCREAMING_SNAKE_CASE           =					output.images

										SCREAMING_SNAKE_CASE           =					image[0, -3:, -3:, -1]

										assert image.shape == (1, 512, 512, 3)
										SCREAMING_SNAKE_CASE           =					np.array([0.3459, 0.2876, 0.2537, 0.3002, 0.2671, 0.2160, 0.3026, 0.2262, 0.2371]			)

										assert np.abs(image_slice.flatten() - expected_slice			).max() < 5E-2







							def    _UpperCAmelCase     (					self       :	Optional[Any]			)			->					Any:
										SCREAMING_SNAKE_CASE           =					StableDiffusionSAGPipeline.from_pretrained("""stabilityai/stable-diffusion-2-1-base"""			)
										SCREAMING_SNAKE_CASE           =					sag_pipe.to(a			)
										sag_pipe.set_progress_bar_config(disable=a			)

										SCREAMING_SNAKE_CASE           =					"""."""
										SCREAMING_SNAKE_CASE           =					torch.manual_seed(0			)
										SCREAMING_SNAKE_CASE           =					sag_pipe(
										    [prompt]       ,     width=768       ,     height=512       ,     generator=a       ,     guidance_scale=7.5       ,     sag_scale=1.0       ,     num_inference_steps=20       ,     output_type="""np"""       ,     )

										SCREAMING_SNAKE_CASE           =					output.images

										assert image.shape == (1, 512, 768, 3)












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 					lowerCamelCase_    (    SCREAMING_SNAKE_CASE					,	SCREAMING_SNAKE_CASE   ):
			'''simple docstring'''
			SCREAMING_SNAKE_CASE           =					args.log_outputs
			SCREAMING_SNAKE_CASE           =					"""_""".join(args.dataset.split("""/"""   ) + [args.config, args.split]   )

			# load metric
			SCREAMING_SNAKE_CASE           =					load_metric("""wer"""   )
			SCREAMING_SNAKE_CASE           =					load_metric("""cer"""   )

			# compute metrics
			SCREAMING_SNAKE_CASE           =					wer.compute(references=result["""target"""]					,	predictions=result["""prediction"""]   )
			SCREAMING_SNAKE_CASE           =					cer.compute(references=result["""target"""]					,	predictions=result["""prediction"""]   )

			# print & log results
			SCREAMING_SNAKE_CASE           =					f"""WER: {wer_result}\nCER: {cer_result}"""
			print(SCREAMING_SNAKE_CASE   )

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

			# log all results in text file. Possibly interesting for analysis
			if log_outputs is not None:
						SCREAMING_SNAKE_CASE           =					f"""log_{dataset_id}_predictions.txt"""
						SCREAMING_SNAKE_CASE           =					f"""log_{dataset_id}_targets.txt"""

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

									result.map(SCREAMING_SNAKE_CASE					,	with_indices=SCREAMING_SNAKE_CASE   )
def 					lowerCamelCase_    (    SCREAMING_SNAKE_CASE   ):
			'''simple docstring'''
			SCREAMING_SNAKE_CASE           =					"""[,?.!\-\;\:\"“%‘”�—’…–]"""  # noqa: W605 IMPORTANT: this should correspond to the chars that were ignored during training

			SCREAMING_SNAKE_CASE           =					re.sub(SCREAMING_SNAKE_CASE					,	""""""					,	text.lower()   )

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

			for t in token_sequences_to_ignore:
						SCREAMING_SNAKE_CASE           =					""" """.join(text.split(SCREAMING_SNAKE_CASE   )   )

			return text
def 					lowerCamelCase_    (    SCREAMING_SNAKE_CASE   ):
			'''simple docstring'''
			# load dataset
			SCREAMING_SNAKE_CASE           =					load_dataset(args.dataset					,	args.config					,	split=args.split					,	use_auth_token=SCREAMING_SNAKE_CASE   )

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

			# load processor
			SCREAMING_SNAKE_CASE           =					AutoFeatureExtractor.from_pretrained(args.model_id   )
			SCREAMING_SNAKE_CASE           =					feature_extractor.sampling_rate

			# resample audio
			SCREAMING_SNAKE_CASE           =					dataset.cast_column("""audio"""					,	Audio(sampling_rate=SCREAMING_SNAKE_CASE   )   )

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

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

						SCREAMING_SNAKE_CASE           =					prediction["""text"""]
						SCREAMING_SNAKE_CASE           =					normalize_text(batch["""sentence"""]   )
						return batch

			# run inference on all examples
			SCREAMING_SNAKE_CASE           =					dataset.map(SCREAMING_SNAKE_CASE					,	remove_columns=dataset.column_names   )

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


if __name__ == "__main__":
		__A						:							List[Any]							=  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						:							Tuple							=  parser.parse_args()

		main(args)






