






"""simple docstring"""




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 					_UpperCamelCase		( snake_case_						, snake_case_						, snake_case_						, unittest.TestCase):
				__lowerCamelCase         =  StableDiffusionControlNetInpaintPipeline
				__lowerCamelCase         =  TEXT_GUIDED_IMAGE_INPAINTING_PARAMS
				__lowerCamelCase         =  TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS
				__lowerCamelCase         =  frozenset({"control_image"})  # skip `image` and `mask` for now, only test for control_image
				__lowerCamelCase         =  TEXT_TO_IMAGE_IMAGE_PARAMS






				def  A    (self   ):


						"""simple docstring"""
						torch.manual_seed(0   )
						A__											=							UNetaDConditionModel(
						    block_out_channels=(3_2, 6_4)						,      layers_per_block=2						,      sample_size=3_2						,      in_channels=9						,      out_channels=4						,      down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""")						,      up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""")						,      cross_attention_dim=3_2						,      )
						torch.manual_seed(0   )
						A__											=							ControlNetModel(
						    block_out_channels=(3_2, 6_4)						,      layers_per_block=2						,      in_channels=4						,      down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""")						,      cross_attention_dim=3_2						,      conditioning_embedding_out_channels=(1_6, 3_2)						,      )
						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=[3_2, 6_4]						,      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=3_2						,      intermediate_size=3_7						,      layer_norm_eps=1E-05						,      num_attention_heads=4						,      num_hidden_layers=5						,      pad_token_id=1						,      vocab_size=1_0_0_0						,      )
						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  A    (self						,      lowerCamelCase__						,      lowerCamelCase__=0   ):


						"""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, 3_2 * controlnet_embedder_scale_factor, 3_2 * controlnet_embedder_scale_factor)						,      generator=lowerCamelCase__						,      device=torch.device(lowerCamelCase__   )						,      )
						A__											=							floats_tensor((1, 3, 3_2, 3_2)						,      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((6_4, 6_4)   )
						A__											=							Image.fromarray(np.uinta(init_image + 4   )   ).convert("""RGB"""   ).resize((6_4, 6_4)   )

						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  A    (self   ):


						"""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  A    (self   ):


						"""simple docstring"""
						self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=2E-3   )






				def  A    (self   ):


						"""simple docstring"""
						self._test_inference_batch_single_identical(expected_max_diff=2E-3   )







class 					_UpperCamelCase		( snake_case_):
				__lowerCamelCase         =  StableDiffusionControlNetInpaintPipeline
				__lowerCamelCase         =  TEXT_GUIDED_IMAGE_INPAINTING_PARAMS
				__lowerCamelCase         =  TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS
				__lowerCamelCase         =  frozenset([])






				def  A    (self   ):


						"""simple docstring"""
						torch.manual_seed(0   )
						A__											=							UNetaDConditionModel(
						    block_out_channels=(3_2, 6_4)						,      layers_per_block=2						,      sample_size=3_2						,      in_channels=4						,      out_channels=4						,      down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""")						,      up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""")						,      cross_attention_dim=3_2						,      )
						torch.manual_seed(0   )
						A__											=							ControlNetModel(
						    block_out_channels=(3_2, 6_4)						,      layers_per_block=2						,      in_channels=4						,      down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""")						,      cross_attention_dim=3_2						,      conditioning_embedding_out_channels=(1_6, 3_2)						,      )
						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=[3_2, 6_4]						,      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=3_2						,      intermediate_size=3_7						,      layer_norm_eps=1E-05						,      num_attention_heads=4						,      num_hidden_layers=5						,      pad_token_id=1						,      vocab_size=1_0_0_0						,      )
						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 					_UpperCamelCase		( snake_case_						, snake_case_						, unittest.TestCase):
				__lowerCamelCase         =  StableDiffusionControlNetInpaintPipeline
				__lowerCamelCase         =  TEXT_GUIDED_IMAGE_INPAINTING_PARAMS
				__lowerCamelCase         =  TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS






				def  A    (self   ):


						"""simple docstring"""
						torch.manual_seed(0   )
						A__											=							UNetaDConditionModel(
						    block_out_channels=(3_2, 6_4)						,      layers_per_block=2						,      sample_size=3_2						,      in_channels=9						,      out_channels=4						,      down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""")						,      up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""")						,      cross_attention_dim=3_2						,      )
						torch.manual_seed(0   )

						def init_weights(lowerCamelCase__   ):
								if isinstance(lowerCamelCase__						,      torch.nn.Convad   ):
										torch.nn.init.normal(m.weight   )
										m.bias.data.fill_(1.0   )

						A__											=							ControlNetModel(
						    block_out_channels=(3_2, 6_4)						,      layers_per_block=2						,      in_channels=4						,      down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""")						,      cross_attention_dim=3_2						,      conditioning_embedding_out_channels=(1_6, 3_2)						,      )
						controlneta.controlnet_down_blocks.apply(lowerCamelCase__   )

						torch.manual_seed(0   )
						A__											=							ControlNetModel(
						    block_out_channels=(3_2, 6_4)						,      layers_per_block=2						,      in_channels=4						,      down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""")						,      cross_attention_dim=3_2						,      conditioning_embedding_out_channels=(1_6, 3_2)						,      )
						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=[3_2, 6_4]						,      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=3_2						,      intermediate_size=3_7						,      layer_norm_eps=1E-05						,      num_attention_heads=4						,      num_hidden_layers=5						,      pad_token_id=1						,      vocab_size=1_0_0_0						,      )
						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  A    (self						,      lowerCamelCase__						,      lowerCamelCase__=0   ):


						"""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, 3_2 * controlnet_embedder_scale_factor, 3_2 * controlnet_embedder_scale_factor)						,      generator=lowerCamelCase__						,      device=torch.device(lowerCamelCase__   )						,      ),
						    randn_tensor(
						        (1, 3, 3_2 * controlnet_embedder_scale_factor, 3_2 * controlnet_embedder_scale_factor)						,      generator=lowerCamelCase__						,      device=torch.device(lowerCamelCase__   )						,      ),
						]
						A__											=							floats_tensor((1, 3, 3_2, 3_2)						,      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((6_4, 6_4)   )
						A__											=							Image.fromarray(np.uinta(init_image + 4   )   ).convert("""RGB"""   ).resize((6_4, 6_4)   )

						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  A    (self   ):


						"""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  A    (self   ):


						"""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  A    (self   ):


						"""simple docstring"""
						self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=2E-3   )






				def  A    (self   ):


						"""simple docstring"""
						self._test_inference_batch_single_identical(expected_max_diff=2E-3   )






				def  A    (self   ):


						"""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 					_UpperCamelCase		( unittest.TestCase):






				def  A    (self   ):


						"""simple docstring"""
						super().tearDown()
						gc.collect()
						torch.cuda.empty_cache()






				def  A    (self   ):


						"""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_1_2, 5_1_2)   )

						A__											=							load_image(
						    """https://huggingface.co/datasets/diffusers/test-arrays/resolve/main"""
						    """/stable_diffusion_inpaint/input_bench_mask.png"""   ).resize((5_1_2, 5_1_2)   )

						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_1_2, 5_1_2)   )

						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_1_2, 5_1_2, 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  A    (self   ):


						"""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(3_3   )

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

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

						A__											=							"""a handsome man with ray-ban sunglasses"""

						def make_inpaint_condition(lowerCamelCase__						,      lowerCamelCase__   ):
								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=2_0						,      output_type="""np"""						,      )
						A__											=							output.images[0]

						assert image.shape == (5_1_2, 5_1_2, 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)
lowerCamelCase__			=   "\\n"

lowerCamelCase__			=   "\\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"

lowerCamelCase__			=   "\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"

lowerCamelCase__			=   ["fleurs-asr", "mls", "voxpopuli", "babel", "covost2", "fleurs-lang_id", "minds14"]
lowerCamelCase__			=   ""

try:
      from jiwer import transforms as tr

      lowerCamelCase__			=   True
except ImportError:
      lowerCamelCase__			=   False

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







      class 					_UpperCamelCase		( tr.AbstractTransform):






          def __init__(self						,      lowerCamelCase__ = " "   ):


            """simple docstring"""
            A__											=							sentence_delimiter






          def  A    (self						,      lowerCamelCase__   ):


            """simple docstring"""
            return list(lowerCamelCase__   )






          def  A    (self						,      lowerCamelCase__   ):


            """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
      lowerCamelCase__			=   tr.Compose(
          [tr.RemoveMultipleSpaces(), tr.Strip(), SentencesToListOfCharacters(SENTENCE_DELIMITER)]
      )
elif _jiwer_available:
      lowerCamelCase__			=   tr.Compose(
          [
              tr.RemoveMultipleSpaces(),
              tr.Strip(),
              tr.ReduceToSingleSentence(SENTENCE_DELIMITER),
              tr.ReduceToListOfListOfChars(),
          ]
      )
else:
      lowerCamelCase__			=   None






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






def 						_SCREAMING_SNAKE_CASE							( UpperCamelCase     :	Union[str, Any]		,				UpperCamelCase     :	str    ):
  return {
      "f1": float(fa_score(y_true=UpperCamelCase		,				y_pred=UpperCamelCase		,				average="""macro"""    )    ),
      "accuracy": simple_accuracy(UpperCamelCase		,				UpperCamelCase    ),
  }






def 						_SCREAMING_SNAKE_CASE							( UpperCamelCase     :	Union[str, Any]		,				UpperCamelCase     :	Dict		,				UpperCamelCase     :	Any="exp"		,				UpperCamelCase     :	int=None		,				UpperCamelCase     :	Optional[int]=False		,				UpperCamelCase     :	str=False		,				UpperCamelCase     :	Optional[Any]=None		,				UpperCamelCase     :	Dict=False		,				):
  # xtreme-s can only have one label
  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 						_SCREAMING_SNAKE_CASE							( UpperCamelCase     :	List[Any]		,				UpperCamelCase     :	List[str]		,				UpperCamelCase     :	Dict		,				UpperCamelCase     :	Any    ):
  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     :	Union[str, Any]		,				UpperCamelCase     :	List[str]		,				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 					_UpperCamelCase		( datasets.Metric):






    def  A    (self   ):


      """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  A    (self						,      lowerCamelCase__						,      lowerCamelCase__						,      lowerCamelCase__=None						,      lowerCamelCase__=None   ):


      """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}"""   )




