'''simple docstring'''





import gc
import tempfile
import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionDualGuidedPipeline
from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device


a_					      = False





@nightly
@require_torch_gpu
class 	_lowercase		(      unittest.TestCase ):

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

  """simple docstring"""





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

 def 		SCREAMING_SNAKE_CASE__     (				self			:							Tuple    )     ->		Dict:

  """simple docstring"""





  UpperCamelCase_      :		Optional[int]				=      VersatileDiffusionDualGuidedPipeline.from_pretrained('shi-labs/versatile-diffusion'    )
  # remove text_unet
  pipe.remove_unused_weights()
  pipe.to(_A    )
  pipe.set_progress_bar_config(disable=_A    )

  UpperCamelCase_      :		str				=      load_image(
      'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'    )

  UpperCamelCase_      :		List[str]				=      torch.manual_seed(0    )
  UpperCamelCase_      :		Union[str, Any]				=      pipe(
      prompt='first prompt'     ,   image=_A     ,   text_to_image_strength=0.75     ,   generator=_A     ,   guidance_scale=7.5     ,   num_inference_steps=2     ,   output_type='numpy'     ,   ).images

  with tempfile.TemporaryDirectory() as tmpdirname:
   pipe.save_pretrained(_A    )
   UpperCamelCase_      :		List[str]				=      VersatileDiffusionDualGuidedPipeline.from_pretrained(_A    )

  pipe.to(_A    )
  pipe.set_progress_bar_config(disable=_A    )

  UpperCamelCase_      :		str				=      generator.manual_seed(0    )
  UpperCamelCase_      :		int				=      pipe(
      prompt='first prompt'     ,   image=_A     ,   text_to_image_strength=0.75     ,   generator=_A     ,   guidance_scale=7.5     ,   num_inference_steps=2     ,   output_type='numpy'     ,   ).images

  assert np.abs(image - new_image    ).sum() < 1e-5, "Models don't have the same forward pass"


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

  """simple docstring"""





  UpperCamelCase_      :		int				=      VersatileDiffusionDualGuidedPipeline.from_pretrained('shi-labs/versatile-diffusion'    )
  pipe.remove_unused_weights()
  pipe.to(_A    )
  pipe.set_progress_bar_config(disable=_A    )

  UpperCamelCase_      :		Union[str, Any]				=      'cyberpunk 2077'
  UpperCamelCase_      :		Optional[Any]				=      load_image(
      'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'    )
  UpperCamelCase_      :		Optional[Any]				=      torch.manual_seed(0    )
  UpperCamelCase_      :		Optional[int]				=      pipe(
      prompt=_A     ,   image=_A     ,   text_to_image_strength=0.75     ,   generator=_A     ,   guidance_scale=7.5     ,   num_inference_steps=5_0     ,   output_type='numpy'     ,   ).images

  UpperCamelCase_      :		Tuple				=      image[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1]

  assert image.shape == (1, 5_1_2, 5_1_2, 3)
  UpperCamelCase_      :		Union[str, Any]				=      np.array([0.0787, 0.0849, 0.0826, 0.0812, 0.0807, 0.0795, 0.0818, 0.0798, 0.0779]    )

  assert np.abs(image_slice.flatten() - expected_slice    ).max() < 1e-2




import unittest

from cer import CER


a_					      = CER()





class 	_lowercase		(      unittest.TestCase ):

 def 		SCREAMING_SNAKE_CASE__     (				self			:							int    )     ->		str:

  """simple docstring"""





  UpperCamelCase_      :		List[Any]				=      ['White House']
  UpperCamelCase_      :		Optional[Any]				=      ['white house']
  # S = 2, D = 0, I = 0, N = 11, CER = 2 / 11
  UpperCamelCase_      :		int				=      cer.compute(predictions=snake_case     ,   references=snake_case    )
  self.assertTrue(abs(char_error_rate - 0.1818181818    ) < 1e-6    )

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

  """simple docstring"""





  UpperCamelCase_      :		str				=      ['were wolf']
  UpperCamelCase_      :		str				=      ['werewolf']
  # S = 0, D = 0, I = 1, N = 9, CER = 1 / 9
  UpperCamelCase_      :		List[str]				=      cer.compute(predictions=snake_case     ,   references=snake_case    )
  self.assertTrue(abs(char_error_rate - 0.1111111    ) < 1e-6    )

  UpperCamelCase_      :		Tuple				=      ['werewolf']
  UpperCamelCase_      :		Union[str, Any]				=      ['weae     wolf']
  # S = 1, D = 1, I = 0, N = 8, CER = 0.25
  UpperCamelCase_      :		Optional[int]				=      cer.compute(predictions=snake_case     ,   references=snake_case    )
  self.assertTrue(abs(char_error_rate - 0.25    ) < 1e-6    )

  # consecutive whitespaces case 1
  UpperCamelCase_      :		Optional[int]				=      ['were wolf']
  UpperCamelCase_      :		Dict				=      ['were               wolf']
  # S = 0, D = 0, I = 0, N = 9, CER = 0
  UpperCamelCase_      :		Optional[int]				=      cer.compute(predictions=snake_case     ,   references=snake_case    )
  self.assertTrue(abs(char_error_rate - 0.0    ) < 1e-6    )

  # consecutive whitespaces case 2
  UpperCamelCase_      :		Optional[Any]				=      ['were   wolf']
  UpperCamelCase_      :		int				=      ['were               wolf']
  # S = 0, D = 0, I = 0, N = 9, CER = 0
  UpperCamelCase_      :		str				=      cer.compute(predictions=snake_case     ,   references=snake_case    )
  self.assertTrue(abs(char_error_rate - 0.0    ) < 1e-6    )

 def 		SCREAMING_SNAKE_CASE__     (				self			:							str    )     ->		str:

  """simple docstring"""





  UpperCamelCase_      :		Tuple				=      ['werewolf']
  UpperCamelCase_      :		int				=      ['weaewolf']
  # S = 1, D = 0, I = 0, N = 8, CER = 0.125
  UpperCamelCase_      :		Optional[int]				=      cer.compute(predictions=snake_case     ,   references=snake_case    )
  self.assertTrue(abs(char_error_rate - 0.125    ) < 1e-6    )

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

  """simple docstring"""





  UpperCamelCase_      :		List[str]				=      ['werewolf']
  UpperCamelCase_      :		Dict				=      ['wereawolf']
  # S = 0, D = 1, I = 0, N = 8, CER = 0.125
  UpperCamelCase_      :		str				=      cer.compute(predictions=snake_case     ,   references=snake_case    )
  self.assertTrue(abs(char_error_rate - 0.125    ) < 1e-6    )

 def 		SCREAMING_SNAKE_CASE__     (				self			:							Any    )     ->		str:

  """simple docstring"""





  UpperCamelCase_      :		List[str]				=      ['werewolf']
  UpperCamelCase_      :		Any				=      ['wereolf']
  # S = 0, D = 0, I = 1, N = 8, CER = 0.125
  UpperCamelCase_      :		Optional[Any]				=      cer.compute(predictions=snake_case     ,   references=snake_case    )
  self.assertTrue(abs(char_error_rate - 0.125    ) < 1e-6    )

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

  """simple docstring"""





  UpperCamelCase_      :		Any				=      ['werewolf']
  UpperCamelCase_      :		str				=      cer.compute(predictions=snake_case     ,   references=snake_case    )
  self.assertEqual(snake_case     ,   0.0    )

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

  """simple docstring"""





  UpperCamelCase_      :		Union[str, Any]				=      ['werewolf', 'I am your father']
  UpperCamelCase_      :		Optional[int]				=      cer.compute(predictions=snake_case     ,   references=snake_case    )
  self.assertEqual(snake_case     ,   0.0    )

  UpperCamelCase_      :		Any				=      ['werewolf', 'I am your father', 'doge']
  UpperCamelCase_      :		List[str]				=      ['werxwolf', 'I       am your father', 'doge']
  # S = 1, D = 0, I = 0, N = 28, CER = 1 / 28
  UpperCamelCase_      :		Dict				=      cer.compute(predictions=snake_case     ,   references=snake_case    )
  self.assertTrue(abs(char_error_rate - 0.03571428    ) < 1e-6    )

 def 		SCREAMING_SNAKE_CASE__     (				self			:							List[str]    )     ->		Dict:

  """simple docstring"""





  UpperCamelCase_      :		Any				=      ['My hovercraft', 'is full of eels']
  UpperCamelCase_      :		Union[str, Any]				=      ['My hovercraft is full', ' of eels']
  # S = 0, D = 0, I = 2, N = 28, CER = 2 / 28
  # whitespace at the front of " of eels" will be strip during preporcessing
  # so need to insert 2 whitespaces
  UpperCamelCase_      :		Optional[int]				=      cer.compute(predictions=snake_case     ,   references=snake_case     ,   concatenate_texts=snake_case    )
  self.assertTrue(abs(char_error_rate - 0.071428    ) < 1e-6    )

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

  """simple docstring"""





  UpperCamelCase_      :		Dict				=      ['我能吞下玻璃而不伤身体']
  UpperCamelCase_      :		int				=      [' 能吞虾玻璃而 不霜身体啦']
  # S = 3, D = 2, I = 0, N = 11, CER = 5 / 11
  UpperCamelCase_      :		Dict				=      cer.compute(predictions=snake_case     ,   references=snake_case    )
  self.assertTrue(abs(char_error_rate - 0.4545454545    ) < 1e-6    )

  UpperCamelCase_      :		Union[str, Any]				=      ['我能吞下玻璃', '而不伤身体']
  UpperCamelCase_      :		int				=      ['我    能 吞 下 玻 璃', '而不伤身体']
  # S = 0, D = 5, I = 0, N = 11, CER = 5 / 11
  UpperCamelCase_      :		Any				=      cer.compute(predictions=snake_case     ,   references=snake_case    )
  self.assertTrue(abs(char_error_rate - 0.454545454545    ) < 1e-6    )

  UpperCamelCase_      :		Any				=      ['我能吞下玻璃而不伤身体']
  UpperCamelCase_      :		int				=      cer.compute(predictions=snake_case     ,   references=snake_case    )
  self.assertFalse(snake_case     ,   0.0    )


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

  """simple docstring"""





  UpperCamelCase_      :		str				=      ['']
  UpperCamelCase_      :		Optional[Any]				=      ['Hypothesis']
  with self.assertRaises(snake_case    ):
   cer.compute(predictions=snake_case     ,   references=snake_case    )


if __name__ == "__main__":
   unittest.main()



