




'''simple docstring'''


# This model implementation is heavily based on:

import gc
import random
import tempfile
import unittest

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

from diffusers import (
    AutoencoderKL,
    ControlNetModel,
    DDIMScheduler,
    StableDiffusionControlNetInpaintPipeline,
    UNetaDConditionModel,
)
from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_controlnet import MultiControlNetModel
from diffusers.utils import floats_tensor, load_image, load_numpy, randn_tensor, slow, torch_device
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import (
    TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS,
    TEXT_GUIDED_IMAGE_INPAINTING_PARAMS,
    TEXT_TO_IMAGE_IMAGE_PARAMS,
)
from ..test_pipelines_common import (
    PipelineKarrasSchedulerTesterMixin,
    PipelineLatentTesterMixin,
    PipelineTesterMixin,
)


enable_full_determinism()

class 			A    (		_a     ,_a     ,_a     ,unittest.TestCase    ):
							lowercase_							=				StableDiffusionControlNetInpaintPipeline
							lowercase_							=				TEXT_GUIDED_IMAGE_INPAINTING_PARAMS
							lowercase_							=				TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS
							lowercase_							=				frozenset({'control_image'}    )  # skip `image` and `mask` for now, only test for control_image
							lowercase_							=				TEXT_TO_IMAGE_IMAGE_PARAMS







							def    __lowerCAmelCase			(  self       :  Any					)			->							int:


														"""simple docstring"""


														torch.manual_seed(0					)
														_a	     =				UNetaDConditionModel(
														    block_out_channels=(32, 64)							,			layers_per_block=2							,			sample_size=32							,			in_channels=9							,			out_channels=4							,			down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''')							,			up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''')							,			cross_attention_dim=32							,			)
														torch.manual_seed(0					)
														_a	     =				ControlNetModel(
														    block_out_channels=(32, 64)							,			layers_per_block=2							,			in_channels=4							,			down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''')							,			cross_attention_dim=32							,			conditioning_embedding_out_channels=(16, 32)							,			)
														torch.manual_seed(0					)
														_a	     =				DDIMScheduler(
														    beta_start=0.0_0_0_8_5							,			beta_end=0.0_1_2							,			beta_schedule='''scaled_linear'''							,			clip_sample=lowerCAmelCase_							,			set_alpha_to_one=lowerCAmelCase_							,			)
														torch.manual_seed(0					)
														_a	     =				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					)
														_a	     =				CLIPTextConfig(
														    bos_token_id=0							,			eos_token_id=2							,			hidden_size=32							,			intermediate_size=37							,			layer_norm_eps=1e-05							,			num_attention_heads=4							,			num_hidden_layers=5							,			pad_token_id=1							,			vocab_size=10_00							,			)
														_a	     =				CLIPTextModel(lowerCAmelCase_					)
														_a	     =				CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''					)

														_a	     =				{
														    '''unet''': unet,
														    '''controlnet''': controlnet,
														    '''scheduler''': scheduler,
														    '''vae''': vae,
														    '''text_encoder''': text_encoder,
														    '''tokenizer''': tokenizer,
														    '''safety_checker''': None,
														    '''feature_extractor''': None,
														}
														return components







							def    __lowerCAmelCase			(  self       :  List[Any]							,			lowerCAmelCase_       :  Tuple							,			lowerCAmelCase_       :  Dict=0					)			->							Optional[Any]:


														"""simple docstring"""


														if str(lowerCAmelCase_					).startswith('''mps'''					):
																					_a	     =				torch.manual_seed(lowerCAmelCase_					)
														else:
																					_a	     =				torch.Generator(device=lowerCAmelCase_					).manual_seed(lowerCAmelCase_					)

														_a	     =				2
														_a	     =				randn_tensor(
														    (1, 3, 32 * controlnet_embedder_scale_factor, 32 * controlnet_embedder_scale_factor)							,			generator=lowerCAmelCase_							,			device=torch.device(lowerCAmelCase_					)							,			)
														_a	     =				floats_tensor((1, 3, 32, 32)							,			rng=random.Random(lowerCAmelCase_					)					).to(lowerCAmelCase_					)
														_a	     =				init_image.cpu().permute(0							,			2							,			3							,			1					)[0]

														_a	     =				Image.fromarray(np.uinta(lowerCAmelCase_					)					).convert('''RGB'''					).resize((64, 64)					)
														_a	     =				Image.fromarray(np.uinta(init_image + 4					)					).convert('''RGB'''					).resize((64, 64)					)

														_a	     =				{
														    '''prompt''': '''A painting of a squirrel eating a burger''',
														    '''generator''': generator,
														    '''num_inference_steps''': 2,
														    '''guidance_scale''': 6.0,
														    '''output_type''': '''numpy''',
														    '''image''': image,
														    '''mask_image''': mask_image,
														    '''control_image''': control_image,
														}

														return inputs







							def    __lowerCAmelCase			(  self       :  Any					)			->							Tuple:


														"""simple docstring"""


														return self._test_attention_slicing_forward_pass(expected_max_diff=2e-3					)







							@unittest.skipIf(
							    torch_device != '''cuda''' or not is_xformers_available()							,			reason='''XFormers attention is only available with CUDA and `xformers` installed'''							,			)
							def    __lowerCAmelCase			(  self       :  Optional[Any]					)			->							List[Any]:


														"""simple docstring"""


														self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=2e-3					)







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


														"""simple docstring"""


														self._test_inference_batch_single_identical(expected_max_diff=2e-3					)



class 			A    (		_a    ):
							lowercase_							=				StableDiffusionControlNetInpaintPipeline
							lowercase_							=				TEXT_GUIDED_IMAGE_INPAINTING_PARAMS
							lowercase_							=				TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS
							lowercase_							=				frozenset([]    )







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


														"""simple docstring"""


														torch.manual_seed(0					)
														_a	     =				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							,			)
														torch.manual_seed(0					)
														_a	     =				ControlNetModel(
														    block_out_channels=(32, 64)							,			layers_per_block=2							,			in_channels=4							,			down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''')							,			cross_attention_dim=32							,			conditioning_embedding_out_channels=(16, 32)							,			)
														torch.manual_seed(0					)
														_a	     =				DDIMScheduler(
														    beta_start=0.0_0_0_8_5							,			beta_end=0.0_1_2							,			beta_schedule='''scaled_linear'''							,			clip_sample=lowerCAmelCase_							,			set_alpha_to_one=lowerCAmelCase_							,			)
														torch.manual_seed(0					)
														_a	     =				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					)
														_a	     =				CLIPTextConfig(
														    bos_token_id=0							,			eos_token_id=2							,			hidden_size=32							,			intermediate_size=37							,			layer_norm_eps=1e-05							,			num_attention_heads=4							,			num_hidden_layers=5							,			pad_token_id=1							,			vocab_size=10_00							,			)
														_a	     =				CLIPTextModel(lowerCAmelCase_					)
														_a	     =				CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''					)

														_a	     =				{
														    '''unet''': unet,
														    '''controlnet''': controlnet,
														    '''scheduler''': scheduler,
														    '''vae''': vae,
														    '''text_encoder''': text_encoder,
														    '''tokenizer''': tokenizer,
														    '''safety_checker''': None,
														    '''feature_extractor''': None,
														}
														return components



class 			A    (		_a     ,_a     ,unittest.TestCase    ):
							lowercase_							=				StableDiffusionControlNetInpaintPipeline
							lowercase_							=				TEXT_GUIDED_IMAGE_INPAINTING_PARAMS
							lowercase_							=				TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS







							def    __lowerCAmelCase			(  self       :  str					)			->							Optional[Any]:


														"""simple docstring"""


														torch.manual_seed(0					)
														_a	     =				UNetaDConditionModel(
														    block_out_channels=(32, 64)							,			layers_per_block=2							,			sample_size=32							,			in_channels=9							,			out_channels=4							,			down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''')							,			up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''')							,			cross_attention_dim=32							,			)
														torch.manual_seed(0					)

														def init_weights(lowerCAmelCase_       :  Optional[int]					):
																					if isinstance(lowerCAmelCase_							,			torch.nn.Convad					):
																												torch.nn.init.normal(m.weight					)
																												m.bias.data.fill_(1.0					)

														_a	     =				ControlNetModel(
														    block_out_channels=(32, 64)							,			layers_per_block=2							,			in_channels=4							,			down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''')							,			cross_attention_dim=32							,			conditioning_embedding_out_channels=(16, 32)							,			)
														controlneta.controlnet_down_blocks.apply(lowerCAmelCase_					)

														torch.manual_seed(0					)
														_a	     =				ControlNetModel(
														    block_out_channels=(32, 64)							,			layers_per_block=2							,			in_channels=4							,			down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''')							,			cross_attention_dim=32							,			conditioning_embedding_out_channels=(16, 32)							,			)
														controlneta.controlnet_down_blocks.apply(lowerCAmelCase_					)

														torch.manual_seed(0					)
														_a	     =				DDIMScheduler(
														    beta_start=0.0_0_0_8_5							,			beta_end=0.0_1_2							,			beta_schedule='''scaled_linear'''							,			clip_sample=lowerCAmelCase_							,			set_alpha_to_one=lowerCAmelCase_							,			)
														torch.manual_seed(0					)
														_a	     =				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					)
														_a	     =				CLIPTextConfig(
														    bos_token_id=0							,			eos_token_id=2							,			hidden_size=32							,			intermediate_size=37							,			layer_norm_eps=1e-05							,			num_attention_heads=4							,			num_hidden_layers=5							,			pad_token_id=1							,			vocab_size=10_00							,			)
														_a	     =				CLIPTextModel(lowerCAmelCase_					)
														_a	     =				CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''					)

														_a	     =				MultiControlNetModel([controlneta, controlneta]					)

														_a	     =				{
														    '''unet''': unet,
														    '''controlnet''': controlnet,
														    '''scheduler''': scheduler,
														    '''vae''': vae,
														    '''text_encoder''': text_encoder,
														    '''tokenizer''': tokenizer,
														    '''safety_checker''': None,
														    '''feature_extractor''': None,
														}
														return components







							def    __lowerCAmelCase			(  self       :  Tuple							,			lowerCAmelCase_       :  int							,			lowerCAmelCase_       :  Tuple=0					)			->							Any:


														"""simple docstring"""


														if str(lowerCAmelCase_					).startswith('''mps'''					):
																					_a	     =				torch.manual_seed(lowerCAmelCase_					)
														else:
																					_a	     =				torch.Generator(device=lowerCAmelCase_					).manual_seed(lowerCAmelCase_					)

														_a	     =				2

														_a	     =				[
														    randn_tensor(
														        (1, 3, 32 * controlnet_embedder_scale_factor, 32 * controlnet_embedder_scale_factor)							,			generator=lowerCAmelCase_							,			device=torch.device(lowerCAmelCase_					)							,			),
														    randn_tensor(
														        (1, 3, 32 * controlnet_embedder_scale_factor, 32 * controlnet_embedder_scale_factor)							,			generator=lowerCAmelCase_							,			device=torch.device(lowerCAmelCase_					)							,			),
														]
														_a	     =				floats_tensor((1, 3, 32, 32)							,			rng=random.Random(lowerCAmelCase_					)					).to(lowerCAmelCase_					)
														_a	     =				init_image.cpu().permute(0							,			2							,			3							,			1					)[0]

														_a	     =				Image.fromarray(np.uinta(lowerCAmelCase_					)					).convert('''RGB'''					).resize((64, 64)					)
														_a	     =				Image.fromarray(np.uinta(init_image + 4					)					).convert('''RGB'''					).resize((64, 64)					)

														_a	     =				{
														    '''prompt''': '''A painting of a squirrel eating a burger''',
														    '''generator''': generator,
														    '''num_inference_steps''': 2,
														    '''guidance_scale''': 6.0,
														    '''output_type''': '''numpy''',
														    '''image''': image,
														    '''mask_image''': mask_image,
														    '''control_image''': control_image,
														}

														return inputs







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


														"""simple docstring"""


														_a	     =				self.get_dummy_components()
														_a	     =				self.pipeline_class(**lowerCAmelCase_					)
														pipe.to(lowerCAmelCase_					)

														_a	     =				1_0.0
														_a	     =				4

														_a	     =				self.get_dummy_inputs(lowerCAmelCase_					)
														_a	     =				steps
														_a	     =				scale
														_a	     =				pipe(**lowerCAmelCase_					)[0]

														_a	     =				self.get_dummy_inputs(lowerCAmelCase_					)
														_a	     =				steps
														_a	     =				scale
														_a	     =				pipe(**lowerCAmelCase_							,			control_guidance_start=0.1							,			control_guidance_end=0.2					)[0]

														_a	     =				self.get_dummy_inputs(lowerCAmelCase_					)
														_a	     =				steps
														_a	     =				scale
														_a	     =				pipe(**lowerCAmelCase_							,			control_guidance_start=[0.1, 0.3]							,			control_guidance_end=[0.2, 0.7]					)[0]

														_a	     =				self.get_dummy_inputs(lowerCAmelCase_					)
														_a	     =				steps
														_a	     =				scale
														_a	     =				pipe(**lowerCAmelCase_							,			control_guidance_start=0.4							,			control_guidance_end=[0.5, 0.8]					)[0]

														# make sure that all outputs are different
														assert np.sum(np.abs(output_a - output_a					)					) > 1e-3
														assert np.sum(np.abs(output_a - output_a					)					) > 1e-3
														assert np.sum(np.abs(output_a - output_a					)					) > 1e-3







							def    __lowerCAmelCase			(  self       :  str					)			->							str:


														"""simple docstring"""


														return self._test_attention_slicing_forward_pass(expected_max_diff=2e-3					)







							@unittest.skipIf(
							    torch_device != '''cuda''' or not is_xformers_available()							,			reason='''XFormers attention is only available with CUDA and `xformers` installed'''							,			)
							def    __lowerCAmelCase			(  self       :  Tuple					)			->							Optional[Any]:


														"""simple docstring"""


														self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=2e-3					)







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


														"""simple docstring"""


														self._test_inference_batch_single_identical(expected_max_diff=2e-3					)







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


														"""simple docstring"""


														_a	     =				self.get_dummy_components()
														_a	     =				self.pipeline_class(**lowerCAmelCase_					)
														pipe.to(lowerCAmelCase_					)
														pipe.set_progress_bar_config(disable=lowerCAmelCase_					)
														with tempfile.TemporaryDirectory() as tmpdir:
																					try:
																												# save_pretrained is not implemented for Multi-ControlNet
																												pipe.save_pretrained(lowerCAmelCase_					)
																					except NotImplementedError:
																												pass



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







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


														"""simple docstring"""


														super().tearDown()
														gc.collect()
														torch.cuda.empty_cache()







							def    __lowerCAmelCase			(  self       :  List[str]					)			->							int:


														"""simple docstring"""


														_a	     =				ControlNetModel.from_pretrained('''lllyasviel/sd-controlnet-canny'''					)

														_a	     =				StableDiffusionControlNetInpaintPipeline.from_pretrained(
														    '''runwayml/stable-diffusion-inpainting'''							,			safety_checker=lowerCAmelCase_							,			controlnet=lowerCAmelCase_					)
														pipe.enable_model_cpu_offload()
														pipe.set_progress_bar_config(disable=lowerCAmelCase_					)

														_a	     =				torch.Generator(device='''cpu'''					).manual_seed(0					)
														_a	     =				load_image(
														    '''https://huggingface.co/lllyasviel/sd-controlnet-canny/resolve/main/images/bird.png'''					).resize((5_12, 5_12)					)

														_a	     =				load_image(
														    '''https://huggingface.co/datasets/diffusers/test-arrays/resolve/main'''
														    '''/stable_diffusion_inpaint/input_bench_mask.png'''					).resize((5_12, 5_12)					)

														_a	     =				'''pitch black hole'''

														_a	     =				load_image(
														    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/bird_canny.png'''					).resize((5_12, 5_12)					)

														_a	     =				pipe(
														    lowerCAmelCase_							,			image=lowerCAmelCase_							,			mask_image=lowerCAmelCase_							,			control_image=lowerCAmelCase_							,			generator=lowerCAmelCase_							,			output_type='''np'''							,			num_inference_steps=3							,			)

														_a	     =				output.images[0]

														assert image.shape == (5_12, 5_12, 3)

														_a	     =				load_numpy(
														    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/inpaint.npy'''					)

														assert np.abs(expected_image - image					).max() < 9e-2







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


														"""simple docstring"""


														_a	     =				ControlNetModel.from_pretrained('''lllyasviel/control_v11p_sd15_inpaint'''					)

														_a	     =				StableDiffusionControlNetInpaintPipeline.from_pretrained(
														    '''runwayml/stable-diffusion-v1-5'''							,			safety_checker=lowerCAmelCase_							,			controlnet=lowerCAmelCase_					)
														_a	     =				DDIMScheduler.from_config(pipe.scheduler.config					)
														pipe.enable_model_cpu_offload()
														pipe.set_progress_bar_config(disable=lowerCAmelCase_					)

														_a	     =				torch.Generator(device='''cpu'''					).manual_seed(33					)

														_a	     =				load_image(
														    '''https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/stable_diffusion_inpaint/boy.png'''					)
														_a	     =				init_image.resize((5_12, 5_12)					)

														_a	     =				load_image(
														    '''https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/stable_diffusion_inpaint/boy_mask.png'''					)
														_a	     =				mask_image.resize((5_12, 5_12)					)

														_a	     =				'''a handsome man with ray-ban sunglasses'''

														def make_inpaint_condition(lowerCAmelCase_       :  int							,			lowerCAmelCase_       :  str					):
																					_a	     =				np.array(image.convert('''RGB'''					)					).astype(np.floataa					) / 2_5_5.0
																					_a	     =				np.array(image_mask.convert('''L'''					)					).astype(np.floataa					) / 2_5_5.0

																					assert image.shape[0:1] == image_mask.shape[0:1], "image and image_mask must have the same image size"
																					_a	     =				-1.0  # set as masked pixel
																					_a	     =				np.expand_dims(lowerCAmelCase_							,			0					).transpose(0							,			3							,			1							,			2					)
																					_a	     =				torch.from_numpy(lowerCAmelCase_					)
																					return image

														_a	     =				make_inpaint_condition(lowerCAmelCase_							,			lowerCAmelCase_					)

														_a	     =				pipe(
														    lowerCAmelCase_							,			image=lowerCAmelCase_							,			mask_image=lowerCAmelCase_							,			control_image=lowerCAmelCase_							,			guidance_scale=9.0							,			eta=1.0							,			generator=lowerCAmelCase_							,			num_inference_steps=20							,			output_type='''np'''							,			)
														_a	     =				output.images[0]

														assert image.shape == (5_12, 5_12, 3)

														_a	     =				load_numpy(
														    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/boy_ray_ban.npy'''					)

														assert np.abs(expected_image - image					).max() < 9e-2







'''simple docstring'''


from typing import List

from packaging import version
from sklearn.metrics import fa_score

import datasets
from datasets.config import PY_VERSION


if PY_VERSION < version.parse('3.8'):
				import importlib_metadata
else:
				import importlib.metadata as importlib_metadata


# TODO(Patrick/Anton)
_snake_case      :      Optional[int]   							=		'\\n'

_snake_case      :      Optional[Any]   							=		'\\n    XTREME-S is a benchmark to evaluate universal cross-lingual speech representations in many languages.\n    XTREME-S covers four task families: speech recognition, classification, speech-to-text translation and retrieval.\n'

_snake_case      :      Union[str, Any]   							=		'\nCompute XTREME-S evaluation metric associated to each XTREME-S dataset.\nArgs:\n    predictions: list of predictions to score.\n        Each translation should be tokenized into a list of tokens.\n    references: list of lists of references for each translation.\n        Each reference should be tokenized into a list of tokens.\n    bleu_kwargs: optional dict of keywords to be passed when computing \'bleu\'.\n        Keywords include Dict can be one of \'smooth_method\', \'smooth_value\', \'force\', \'lowercase\',\n        \'tokenize\', \'use_effective_order\'.\n    wer_kwargs: optional dict of keywords to be passed when computing \'wer\' and \'cer\'.\n        Keywords include \'concatenate_texts\'.\nReturns: depending on the XTREME-S task, one or several of:\n    "accuracy": Accuracy - for \'fleurs-lang_id\', \'minds14\'\n    "f1": F1 score - for \'minds14\'\n    "wer": Word error rate - for \'mls\', \'fleurs-asr\', \'voxpopuli\', \'babel\'\n    "cer": Character error rate - for \'mls\', \'fleurs-asr\', \'voxpopuli\', \'babel\'\n    "bleu": BLEU score according to the `sacrebleu` metric - for \'covost2\'\nExamples:\n\n    >>> xtreme_s_metric = datasets.load_metric(\'xtreme_s\', \'mls\')  # \'mls\', \'voxpopuli\', \'fleurs-asr\' or \'babel\'\n    >>> references = ["it is sunny here", "paper and pen are essentials"]\n    >>> predictions = ["it\'s sunny", "paper pen are essential"]\n    >>> results = xtreme_s_metric.compute(predictions=predictions, references=references)\n    >>> print({k: round(v, 2) for k, v in results.items()})\n    {\'wer\': 0.56, \'cer\': 0.27}\n\n    >>> xtreme_s_metric = datasets.load_metric(\'xtreme_s\', \'covost2\')\n    >>> references = ["bonjour paris", "il est necessaire de faire du sport de temps en temp"]\n    >>> predictions = ["bonjour paris", "il est important de faire du sport souvent"]\n    >>> results = xtreme_s_metric.compute(predictions=predictions, references=references)\n    >>> print({k: round(v, 2) for k, v in results.items()})\n    {\'bleu\': 31.65}\n\n    >>> xtreme_s_metric = datasets.load_metric(\'xtreme_s\', \'fleurs-lang_id\')\n    >>> references = [0, 1, 0, 0, 1]\n    >>> predictions = [0, 1, 1, 0, 0]\n    >>> results = xtreme_s_metric.compute(predictions=predictions, references=references)\n    >>> print({k: round(v, 2) for k, v in results.items()})\n    {\'accuracy\': 0.6}\n\n    >>> xtreme_s_metric = datasets.load_metric(\'xtreme_s\', \'minds14\')\n    >>> references = [0, 1, 0, 0, 1]\n    >>> predictions = [0, 1, 1, 0, 0]\n    >>> results = xtreme_s_metric.compute(predictions=predictions, references=references)\n    >>> print({k: round(v, 2) for k, v in results.items()})\n    {\'f1\': 0.58, \'accuracy\': 0.6}\n'

_snake_case      :      Dict   							=		['fleurs-asr', 'mls', 'voxpopuli', 'babel', 'covost2', 'fleurs-lang_id', 'minds14']
_snake_case      :      List[str]   							=		''

try:
				from jiwer import transforms as tr

				_snake_case      :      Optional[Any]   							=		True
except ImportError:
				_snake_case      :      Any   							=		False

if _jiwer_available and version.parse(importlib_metadata.version('jiwer')) < version.parse('2.3.0'):

				class 			A    (		tr.AbstractTransform    ):







											def __init__(  self       :  Any							,			lowerCAmelCase_       :  str = " "					)			->							Dict:


																		"""simple docstring"""


																		_a	     =				sentence_delimiter







											def    __lowerCAmelCase			(  self       :  Union[str, Any]							,			lowerCAmelCase_       :  str					)			->							Tuple:


																		"""simple docstring"""


																		return list(lowerCAmelCase_					)







											def    __lowerCAmelCase			(  self       :  Optional[int]							,			lowerCAmelCase_       :  List[str]					)			->							List[str]:


																		"""simple docstring"""


																		_a	     =				[]
																		for sent_idx, sentence in enumerate(lowerCAmelCase_					):
																									chars.extend(self.process_string(lowerCAmelCase_					)					)
																									if self.sentence_delimiter is not None and self.sentence_delimiter != "" and sent_idx < len(lowerCAmelCase_					) - 1:
																																chars.append(self.sentence_delimiter					)
																		return chars



				_snake_case      :      List[Any]   							=		tr.Compose(
				    [tr.RemoveMultipleSpaces(), tr.Strip(), SentencesToListOfCharacters(SENTENCE_DELIMITER)]
				)
elif _jiwer_available:
				_snake_case      :      Dict   							=		tr.Compose(
				    [
				        tr.RemoveMultipleSpaces(),
				        tr.Strip(),
				        tr.ReduceToSingleSentence(SENTENCE_DELIMITER),
				        tr.ReduceToListOfListOfChars(),
				    ]
				)
else:
				_snake_case      :      Dict   							=		None
def 		snake_case_    (UpperCamelCase       :  Optional[Any] ,						UpperCamelCase       :  List[str]      ):





							'''simple docstring'''


							return float((preds == labels).mean()      )
def 		snake_case_    (UpperCamelCase       :  Optional[int] ,						UpperCamelCase       :  Optional[int]      ):





							'''simple docstring'''


							return {
							    "f1": float(fa_score(y_true=UpperCamelCase ,						y_pred=UpperCamelCase ,						average='''macro'''      )      ),
							    "accuracy": simple_accuracy(UpperCamelCase ,						UpperCamelCase      ),
							}
def 		snake_case_    (UpperCamelCase       :  Optional[int] ,						UpperCamelCase       :  Any ,						UpperCamelCase       :  Optional[Any]="exp" ,						UpperCamelCase       :  Optional[int]=None ,						UpperCamelCase       :  Tuple=False ,						UpperCamelCase       :  Dict=False ,						UpperCamelCase       :  List[str]=None ,						UpperCamelCase       :  Tuple=False ,						):





							'''simple docstring'''


							_a	     =				[[label] for label in labels]
							_a	     =				list(UpperCamelCase      )
							try:
														import sacrebleu as scb
							except ImportError:
														raise ValueError(
														    '''sacrebleu has to be installed in order to apply the bleu metric for covost2.'''
														    '''You can install it via `pip install sacrebleu`.'''      )

							if version.parse(scb.__version__      ) < version.parse('''1.4.12'''      ):
														raise ImportWarning(
														    '''To use `sacrebleu`, the module `sacrebleu>=1.4.12` is required, and the current version of `sacrebleu` doesn\'t match this condition.\n'''
														    '''You can install it with `pip install "sacrebleu>=1.4.12"`.'''      )

							_a	     =				len(labels[0]      )
							if any(len(UpperCamelCase      ) != references_per_prediction for refs in labels      ):
														raise ValueError('''Sacrebleu requires the same number of references for each prediction'''      )
							_a	     =				[[refs[i] for refs in labels] for i in range(UpperCamelCase      )]
							_a	     =				scb.corpus_bleu(
							    UpperCamelCase ,						UpperCamelCase ,						smooth_method=UpperCamelCase ,						smooth_value=UpperCamelCase ,						force=UpperCamelCase ,						lowercase=UpperCamelCase ,						use_effective_order=UpperCamelCase ,						**({'''tokenize''': tokenize} if tokenize else {}) ,						)
							return {"bleu": output.score}
def 		snake_case_    (UpperCamelCase       :  Optional[Any] ,						UpperCamelCase       :  Dict ,						UpperCamelCase       :  str ,						UpperCamelCase       :  List[Any]      ):





							'''simple docstring'''


							try:
														from jiwer import compute_measures
							except ImportError:
														raise ValueError(
														    f'jiwer has to be installed in order to apply the wer metric for {config_name}.'
														    '''You can install it via `pip install jiwer`.'''      )

							if concatenate_texts:
														_a	     =				compute_measures(UpperCamelCase ,						UpperCamelCase      )['''wer''']

														_a	     =				compute_measures(UpperCamelCase ,						UpperCamelCase ,						truth_transform=UpperCamelCase ,						hypothesis_transform=UpperCamelCase      )['''wer''']
														return {"wer": wer, "cer": cer}
							else:

														def compute_score(UpperCamelCase       :  Tuple ,						UpperCamelCase       :  Optional[int] ,						UpperCamelCase       :  Dict="wer"      ):
																					_a	     =				0
																					_a	     =				0
																					for prediction, reference in zip(UpperCamelCase ,						UpperCamelCase      ):
																												if score_type == "wer":
																																			_a	     =				compute_measures(UpperCamelCase ,						UpperCamelCase      )
																												elif score_type == "cer":
																																			_a	     =				compute_measures(
																																			    UpperCamelCase ,						UpperCamelCase ,						truth_transform=UpperCamelCase ,						hypothesis_transform=UpperCamelCase      )
																												incorrect += measures["substitutions"] + measures["deletions"] + measures["insertions"]
																												total += measures["substitutions"] + measures["deletions"] + measures["hits"]
																					return incorrect / total

														return {"wer": compute_score(UpperCamelCase ,						UpperCamelCase ,						'''wer'''      ), "cer": compute_score(UpperCamelCase ,						UpperCamelCase ,						'''cer'''      )}

@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION     ,_KWARGS_DESCRIPTION    )
class 			A    (		datasets.Metric    ):







							def    __lowerCAmelCase			(  self       :  Tuple					)			->							Dict:


														"""simple docstring"""


														if self.config_name not in _CONFIG_NAMES:
																					raise KeyError(F'You should supply a configuration name selected in {_CONFIG_NAMES}'					)

														_a	     =				'''int64''' if self.config_name in ['''fleurs-lang_id''', '''minds14'''] else '''string'''

														return datasets.MetricInfo(
														    description=_DESCRIPTION							,			citation=_CITATION							,			inputs_description=_KWARGS_DESCRIPTION							,			features=datasets.Features(
														        {'''predictions''': datasets.Value(lowerCAmelCase_					), '''references''': datasets.Value(lowerCAmelCase_					)}					)							,			codebase_urls=[]							,			reference_urls=[]							,			format='''numpy'''							,			)







							def    __lowerCAmelCase			(  self       :  Optional[int]							,			lowerCAmelCase_       :  Union[str, Any]							,			lowerCAmelCase_       :  Optional[Any]							,			lowerCAmelCase_       :  List[Any]=None							,			lowerCAmelCase_       :  int=None					)			->							Tuple:


														"""simple docstring"""


														_a	     =				bleu_kwargs if bleu_kwargs is not None else {}
														_a	     =				wer_kwargs if wer_kwargs is not None else {}

														if self.config_name == "fleurs-lang_id":
																					return {"accuracy": simple_accuracy(lowerCAmelCase_							,			lowerCAmelCase_					)}
														elif self.config_name == "minds14":
																					return fa_and_simple_accuracy(lowerCAmelCase_							,			lowerCAmelCase_					)
														elif self.config_name == "covost2":
																					_a	     =				bleu_kwargs.pop('''smooth_method'''							,			'''exp'''					)
																					_a	     =				bleu_kwargs.pop('''smooth_value'''							,			lowerCAmelCase_					)
																					_a	     =				bleu_kwargs.pop('''force'''							,			lowerCAmelCase_					)
																					_a	     =				bleu_kwargs.pop('''lowercase'''							,			lowerCAmelCase_					)
																					_a	     =				bleu_kwargs.pop('''tokenize'''							,			lowerCAmelCase_					)
																					_a	     =				bleu_kwargs.pop('''use_effective_order'''							,			lowerCAmelCase_					)
																					return bleu(
																					    preds=lowerCAmelCase_							,			labels=lowerCAmelCase_							,			smooth_method=lowerCAmelCase_							,			smooth_value=lowerCAmelCase_							,			force=lowerCAmelCase_							,			lowercase=lowerCAmelCase_							,			tokenize=lowerCAmelCase_							,			use_effective_order=lowerCAmelCase_							,			)
														elif self.config_name in ["fleurs-asr", "mls", "voxpopuli", "babel"]:
																					_a	     =				wer_kwargs.pop('''concatenate_texts'''							,			lowerCAmelCase_					)
																					return wer_and_cer(lowerCAmelCase_							,			lowerCAmelCase_							,			lowerCAmelCase_							,			self.config_name					)
														else:
																					raise KeyError(F'You should supply a configuration name selected in {_CONFIG_NAMES}'					)

