


'''simple docstring'''

import unittest

from cer import CER


__lowercase:		Tuple    						=  CER()







class 			UpperCAmelCase			(							unittest.TestCase):

 def 		lowercase_    (			self			:						int       ):





    """simple docstring"""



    UpperCamelCase__			    =    ["White House"]
    UpperCamelCase__			    =    ["white house"]
    # S = 2, D = 0, I = 0, N = 11, CER = 2 / 11
    UpperCamelCase__			    =    cer.compute(predictions=a_,       references=a_       )
    self.assertTrue(abs(char_error_rate - 0.1_818_181_818       ) < 1e-6       )

 def 		lowercase_    (			self			:						List[Any]       ):





    """simple docstring"""



    UpperCamelCase__			    =    ["were wolf"]
    UpperCamelCase__			    =    ["werewolf"]
    # S = 0, D = 0, I = 1, N = 9, CER = 1 / 9
    UpperCamelCase__			    =    cer.compute(predictions=a_,       references=a_       )
    self.assertTrue(abs(char_error_rate - 0.1_111_111       ) < 1e-6       )

    UpperCamelCase__			    =    ["werewolf"]
    UpperCamelCase__			    =    ["weae     wolf"]
    # S = 1, D = 1, I = 0, N = 8, CER = 0.25
    UpperCamelCase__			    =    cer.compute(predictions=a_,       references=a_       )
    self.assertTrue(abs(char_error_rate - 0.25       ) < 1e-6       )

    # consecutive whitespaces case 1
    UpperCamelCase__			    =    ["were wolf"]
    UpperCamelCase__			    =    ["were               wolf"]
    # S = 0, D = 0, I = 0, N = 9, CER = 0
    UpperCamelCase__			    =    cer.compute(predictions=a_,       references=a_       )
    self.assertTrue(abs(char_error_rate - 0.0       ) < 1e-6       )

    # consecutive whitespaces case 2
    UpperCamelCase__			    =    ["were   wolf"]
    UpperCamelCase__			    =    ["were               wolf"]
    # S = 0, D = 0, I = 0, N = 9, CER = 0
    UpperCamelCase__			    =    cer.compute(predictions=a_,       references=a_       )
    self.assertTrue(abs(char_error_rate - 0.0       ) < 1e-6       )

 def 		lowercase_    (			self			:						Optional[Any]       ):





    """simple docstring"""



    UpperCamelCase__			    =    ["werewolf"]
    UpperCamelCase__			    =    ["weaewolf"]
    # S = 1, D = 0, I = 0, N = 8, CER = 0.125
    UpperCamelCase__			    =    cer.compute(predictions=a_,       references=a_       )
    self.assertTrue(abs(char_error_rate - 0.125       ) < 1e-6       )

 def 		lowercase_    (			self			:						Dict       ):





    """simple docstring"""



    UpperCamelCase__			    =    ["werewolf"]
    UpperCamelCase__			    =    ["wereawolf"]
    # S = 0, D = 1, I = 0, N = 8, CER = 0.125
    UpperCamelCase__			    =    cer.compute(predictions=a_,       references=a_       )
    self.assertTrue(abs(char_error_rate - 0.125       ) < 1e-6       )

 def 		lowercase_    (			self			:						List[str]       ):





    """simple docstring"""



    UpperCamelCase__			    =    ["werewolf"]
    UpperCamelCase__			    =    ["wereolf"]
    # S = 0, D = 0, I = 1, N = 8, CER = 0.125
    UpperCamelCase__			    =    cer.compute(predictions=a_,       references=a_       )
    self.assertTrue(abs(char_error_rate - 0.125       ) < 1e-6       )

 def 		lowercase_    (			self			:						str       ):





    """simple docstring"""



    UpperCamelCase__			    =    ["werewolf"]
    UpperCamelCase__			    =    cer.compute(predictions=a_,       references=a_       )
    self.assertEqual(a_,       0.0       )

 def 		lowercase_    (			self			:						List[Any]       ):





    """simple docstring"""



    UpperCamelCase__			    =    ["werewolf", "I am your father"]
    UpperCamelCase__			    =    cer.compute(predictions=a_,       references=a_       )
    self.assertEqual(a_,       0.0       )

    UpperCamelCase__			    =    ["werewolf", "I am your father", "doge"]
    UpperCamelCase__			    =    ["werxwolf", "I       am your father", "doge"]
    # S = 1, D = 0, I = 0, N = 28, CER = 1 / 28
    UpperCamelCase__			    =    cer.compute(predictions=a_,       references=a_       )
    self.assertTrue(abs(char_error_rate - 0.03_571_428       ) < 1e-6       )

 def 		lowercase_    (			self			:						List[Any]       ):





    """simple docstring"""



    UpperCamelCase__			    =    ["My hovercraft", "is full of eels"]
    UpperCamelCase__			    =    ["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__			    =    cer.compute(predictions=a_,       references=a_,       concatenate_texts=a_       )
    self.assertTrue(abs(char_error_rate - 0.071_428       ) < 1e-6       )

 def 		lowercase_    (			self			:						Optional[Any]       ):





    """simple docstring"""



    UpperCamelCase__			    =    ["我能吞下玻璃而不伤身体"]
    UpperCamelCase__			    =    [" 能吞虾玻璃而 不霜身体啦"]
    # S = 3, D = 2, I = 0, N = 11, CER = 5 / 11
    UpperCamelCase__			    =    cer.compute(predictions=a_,       references=a_       )
    self.assertTrue(abs(char_error_rate - 0.4_545_454_545       ) < 1e-6       )

    UpperCamelCase__			    =    ["我能吞下玻璃", "而不伤身体"]
    UpperCamelCase__			    =    ["我    能 吞 下 玻 璃", "而不伤身体"]
    # S = 0, D = 5, I = 0, N = 11, CER = 5 / 11
    UpperCamelCase__			    =    cer.compute(predictions=a_,       references=a_       )
    self.assertTrue(abs(char_error_rate - 0.454_545_454_545       ) < 1e-6       )

    UpperCamelCase__			    =    ["我能吞下玻璃而不伤身体"]
    UpperCamelCase__			    =    cer.compute(predictions=a_,       references=a_       )
    self.assertFalse(a_,       0.0       )

 def 		lowercase_    (			self			:						Union[str, Any]       ):





    """simple docstring"""



    UpperCamelCase__			    =    [""]
    UpperCamelCase__			    =    ["Hypothesis"]
    with self.assertRaises(a_       ):
       cer.compute(predictions=a_,       references=a_       )


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



'''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


lowercase__					  =		False





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






     def 			a_       (      self		)							->     Union[str, Any]:
          # clean up the VRAM after each test
          super().tearDown()
          gc.collect()
          torch.cuda.empty_cache()






     def 			a_       (      self		)							->     Dict:
          _a            =		VersatileDiffusionDualGuidedPipeline.from_pretrained("shi-labs/versatile-diffusion"		)
          # remove text_unet
          pipe.remove_unused_weights()
          pipe.to(__UpperCamelCase		)
          pipe.set_progress_bar_config(disable=__UpperCamelCase		)

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

          _a            =		torch.manual_seed(0		)
          _a            =		pipe(
              prompt="first prompt"  ,				image=__UpperCamelCase  ,				text_to_image_strength=0.75  ,				generator=__UpperCamelCase  ,				guidance_scale=7.5  ,				num_inference_steps=2  ,				output_type="numpy"  ,				).images

          with tempfile.TemporaryDirectory() as tmpdirname:
               pipe.save_pretrained(__UpperCamelCase		)
               _a            =		VersatileDiffusionDualGuidedPipeline.from_pretrained(__UpperCamelCase		)

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

          _a            =		generator.manual_seed(0		)
          _a            =		pipe(
              prompt="first prompt"  ,				image=__UpperCamelCase  ,				text_to_image_strength=0.75  ,				generator=__UpperCamelCase  ,				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 			a_       (      self		)							->     int:
          _a            =		VersatileDiffusionDualGuidedPipeline.from_pretrained("shi-labs/versatile-diffusion"		)
          pipe.remove_unused_weights()
          pipe.to(__UpperCamelCase		)
          pipe.set_progress_bar_config(disable=__UpperCamelCase		)

          _a            =		"cyberpunk 2077"
          _a            =		load_image(
              "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg"		)
          _a            =		torch.manual_seed(0		)
          _a            =		pipe(
              prompt=__UpperCamelCase  ,				image=__UpperCamelCase  ,				text_to_image_strength=0.75  ,				generator=__UpperCamelCase  ,				guidance_scale=7.5  ,				num_inference_steps=50  ,				output_type="numpy"  ,				).images

          _a            =		image[0, 253:256, 253:256, -1]

          assert image.shape == (1, 512, 512, 3)
          _a            =		np.array([0.07_87, 0.08_49, 0.08_26, 0.08_12, 0.08_07, 0.07_95, 0.08_18, 0.07_98, 0.07_79]		)

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

