






'''simple docstring'''
import json
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
from transformers import (
    CONFIG_MAPPING,
    FEATURE_EXTRACTOR_MAPPING,
    AutoConfig,
    AutoFeatureExtractor,
    WavaVecaConfig,
    WavaVecaFeatureExtractor,
)
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, get_tests_dir


sys.path.append(str(Path(__file__).parent.parent.parent.parent / 'utils'))

from test_module.custom_configuration import CustomConfig  # noqa E402
from test_module.custom_feature_extraction import CustomFeatureExtractor  # noqa E402


UpperCamelCase__			=      get_tests_dir('fixtures')
UpperCamelCase__			=      get_tests_dir('fixtures/dummy_feature_extractor_config.json')
UpperCamelCase__			=      get_tests_dir('fixtures/dummy-config.json')


class 							_UpperCAmelCase    (		unittest.TestCase      ):




    def 					lowerCAmelCase__					(      self	:							int      ):






       '''simple docstring'''






       lowercase_						:     Optional[int]							     =  0




    def 					lowerCAmelCase__					(      self	:							Tuple      ):






       '''simple docstring'''






       lowercase_						:     Dict							     =  AutoFeatureExtractor.from_pretrained("facebook/wav2vec2-base-960h"      )
       self.assertIsInstance(a	,  a      )




    def 					lowerCAmelCase__					(      self	:							Optional[Any]      ):






       '''simple docstring'''






       lowercase_						:     Any							     =  AutoFeatureExtractor.from_pretrained(a      )
       self.assertIsInstance(a	,  a      )




    def 					lowerCAmelCase__					(      self	:							Any      ):






       '''simple docstring'''






       with tempfile.TemporaryDirectory() as tmpdirname:
          lowercase_						:     Tuple							     =  WavaVecaConfig()

          # remove feature_extractor_type to make sure config.json alone is enough to load feature processor locally
          lowercase_						:     int							     =  AutoFeatureExtractor.from_pretrained(a      ).to_dict()

          config_dict.pop("feature_extractor_type"      )
          lowercase_						:     Optional[Any]							     =  WavaVecaFeatureExtractor(**a      )

          # save in new folder
          model_config.save_pretrained(a      )
          config.save_pretrained(a      )

          lowercase_						:     List[str]							     =  AutoFeatureExtractor.from_pretrained(a      )

          # make sure private variable is not incorrectly saved
          lowercase_						:     Dict							     =  json.loads(config.to_json_string()      )
          self.assertTrue("_processor_class" not in dict_as_saved      )

       self.assertIsInstance(a	,  a      )




    def 					lowerCAmelCase__					(      self	:							Dict      ):






       '''simple docstring'''






       lowercase_						:     List[Any]							     =  AutoFeatureExtractor.from_pretrained(a      )
       self.assertIsInstance(a	,  a      )




    def 					lowerCAmelCase__					(      self	:							int      ):






       '''simple docstring'''






       with self.assertRaisesRegex(
           a	,  "bert-base is not a local folder and is not a valid model identifier"      ):
          lowercase_						:     Optional[int]							     =  AutoFeatureExtractor.from_pretrained("bert-base"      )




    def 					lowerCAmelCase__					(      self	:							List[str]      ):






       '''simple docstring'''






       with self.assertRaisesRegex(
           a	,  R"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"      ):
          lowercase_						:     List[Any]							     =  AutoFeatureExtractor.from_pretrained(a	,  revision="aaaaaa"      )




    def 					lowerCAmelCase__					(      self	:							int      ):






       '''simple docstring'''






       with self.assertRaisesRegex(
           a	,  "hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json."	,  ):
          lowercase_						:     List[Any]							     =  AutoFeatureExtractor.from_pretrained("hf-internal-testing/config-no-model"      )




    def 					lowerCAmelCase__					(      self	:							Dict      ):






       '''simple docstring'''






       with self.assertRaises(a      ):
          lowercase_						:     Optional[int]							     =  AutoFeatureExtractor.from_pretrained(
              "hf-internal-testing/test_dynamic_feature_extractor"      )
       # If remote code is disabled, we can't load this config.
       with self.assertRaises(a      ):
          lowercase_						:     Dict							     =  AutoFeatureExtractor.from_pretrained(
              "hf-internal-testing/test_dynamic_feature_extractor"	,  trust_remote_code=a      )

       lowercase_						:     Tuple							     =  AutoFeatureExtractor.from_pretrained(
           "hf-internal-testing/test_dynamic_feature_extractor"	,  trust_remote_code=a      )
       self.assertEqual(feature_extractor.__class__.__name__	,  "NewFeatureExtractor"      )

       # Test feature extractor can be reloaded.
       with tempfile.TemporaryDirectory() as tmp_dir:
          feature_extractor.save_pretrained(a      )
          lowercase_						:     Union[str, Any]							     =  AutoFeatureExtractor.from_pretrained(a	,  trust_remote_code=a      )
       self.assertEqual(reloaded_feature_extractor.__class__.__name__	,  "NewFeatureExtractor"      )




    def 					lowerCAmelCase__					(      self	:							Tuple      ):






       '''simple docstring'''






       try:
          AutoConfig.register("custom"	,  a      )
          AutoFeatureExtractor.register(a	,  a      )
          # Trying to register something existing in the Transformers library will raise an error
          with self.assertRaises(a      ):
             AutoFeatureExtractor.register(a	,  a      )

          # Now that the config is registered, it can be used as any other config with the auto-API
          lowercase_						:     Any							     =  CustomFeatureExtractor.from_pretrained(a      )
          with tempfile.TemporaryDirectory() as tmp_dir:
             feature_extractor.save_pretrained(a      )
             lowercase_						:     str							     =  AutoFeatureExtractor.from_pretrained(a      )
             self.assertIsInstance(a	,  a      )

       finally:
          if "custom" in CONFIG_MAPPING._extra_content:
             del CONFIG_MAPPING._extra_content["custom"]
          if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content:
             del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig]





    def 					lowerCAmelCase__					(      self	:							Optional[Any]      ):






       '''simple docstring'''






       class 							_UpperCAmelCase    (		snake_case      ):
           __lowerCamelCase: List[Any]									=     True

       try:
          AutoConfig.register("custom"	,  a      )
          AutoFeatureExtractor.register(a	,  a      )
          # If remote code is not set, the default is to use local
          lowercase_						:     Optional[Any]							     =  AutoFeatureExtractor.from_pretrained(
              "hf-internal-testing/test_dynamic_feature_extractor"      )
          self.assertEqual(feature_extractor.__class__.__name__	,  "NewFeatureExtractor"      )
          self.assertTrue(feature_extractor.is_local      )

          # If remote code is disabled, we load the local one.
          lowercase_						:     Dict							     =  AutoFeatureExtractor.from_pretrained(
              "hf-internal-testing/test_dynamic_feature_extractor"	,  trust_remote_code=a      )
          self.assertEqual(feature_extractor.__class__.__name__	,  "NewFeatureExtractor"      )
          self.assertTrue(feature_extractor.is_local      )

          # If remote is enabled, we load from the Hub
          lowercase_						:     List[str]							     =  AutoFeatureExtractor.from_pretrained(
              "hf-internal-testing/test_dynamic_feature_extractor"	,  trust_remote_code=a      )
          self.assertEqual(feature_extractor.__class__.__name__	,  "NewFeatureExtractor"      )
          self.assertTrue(not hasattr(a	,  "is_local"      )      )

       finally:
          if "custom" in CONFIG_MAPPING._extra_content:
             del CONFIG_MAPPING._extra_content["custom"]
          if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content:
             del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig]












'''simple docstring'''
import unittest

import numpy as np

from diffusers import OnnxStableDiffusionInpaintPipelineLegacy
from diffusers.utils.testing_utils import (
    is_onnx_available,
    load_image,
    load_numpy,
    nightly,
    require_onnxruntime,
    require_torch_gpu,
)


if is_onnx_available():
 import onnxruntime as ort


@nightly
@require_onnxruntime
@require_torch_gpu
class 							_UpperCAmelCase    (		unittest.TestCase      ):




    @property
    def 					lowerCAmelCase__					(      self	:							Union[str, Any]      ):






       '''simple docstring'''






       return (
           "CUDAExecutionProvider",
           {
               "gpu_mem_limit": "15000000000",  # 15GB
               "arena_extend_strategy": "kSameAsRequested",
           },
       )




    @property
    def 					lowerCAmelCase__					(      self	:							List[Any]      ):






       '''simple docstring'''






       lowercase_						:     Union[str, Any]							     =  ort.SessionOptions()
       lowercase_						:     Optional[int]							     =  False
       return options





    def 					lowerCAmelCase__					(      self	:							str      ):






       '''simple docstring'''






       lowercase_						:     List[Any]							     =  load_image(
           "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
           "/in_paint/overture-creations-5sI6fQgYIuo.png"      )
       lowercase_						:     Tuple							     =  load_image(
           "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
           "/in_paint/overture-creations-5sI6fQgYIuo_mask.png"      )
       lowercase_						:     List[str]							     =  load_numpy(
           "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
           "/in_paint/red_cat_sitting_on_a_park_bench_onnx.npy"      )

       # using the PNDM scheduler by default
       lowercase_						:     List[Any]							     =  OnnxStableDiffusionInpaintPipelineLegacy.from_pretrained(
           "CompVis/stable-diffusion-v1-4"	,  revision="onnx"	,  safety_checker=a	,  feature_extractor=a	,  provider=self.gpu_provider	,  sess_options=self.gpu_options	,  )
       pipe.set_progress_bar_config(disable=a      )

       lowercase_						:     List[str]							     =  "A red cat sitting on a park bench"

       lowercase_						:     int							     =  np.random.RandomState(0      )
       lowercase_						:     int							     =  pipe(
           prompt=a	,  image=a	,  mask_image=a	,  strength=0.75	,  guidance_scale=7.5	,  num_inference_steps=1_5	,  generator=a	,  output_type="np"	,  )

       lowercase_						:     Tuple							     =  output.images[0]

       assert image.shape == (5_1_2, 5_1_2, 3)
       assert np.abs(expected_image - image      ).max() < 1e-2




