

'''simple docstring'''







import tempfile
import unittest

import numpy as np
from huggingface_hub import HfFolder, delete_repo
from requests.exceptions import HTTPError

from transformers import BertConfig, is_flax_available
from transformers.testing_utils import TOKEN, USER, is_staging_test, require_flax


if is_flax_available():
      import os

      from flax.core.frozen_dict import unfreeze
      from flax.traverse_util import flatten_dict

      from transformers import FlaxBertModel

      __lowerCamelCase		       =    '''0.12'''  # assumed parallelism: 8


@require_flax
@is_staging_test
class 					A__      (      unittest.TestCase			):



    @classmethod
    def 					snake_case_			(					cls       )						->      Union[str, Any]:





           '''simple docstring'''
           A_						=      TOKEN
           HfFolder.save_token(UpperCamelCase__       )



    @classmethod
    def 					snake_case_			(					cls       )						->      Tuple:





           '''simple docstring'''
           try:
                  delete_repo(token=cls._token		,				repo_id="""test-model-flax"""       )
           except HTTPError:
                  pass

           try:
                  delete_repo(token=cls._token		,				repo_id="""valid_org/test-model-flax-org"""       )
           except HTTPError:
                  pass



    def 					snake_case_			(					self       )						->      List[Any]:





           '''simple docstring'''
           A_						=      BertConfig(
               vocab_size=99		,				hidden_size=32		,				num_hidden_layers=5		,				num_attention_heads=4		,				intermediate_size=37       )
           A_						=      FlaxBertModel(UpperCamelCase__       )
           model.push_to_hub("""test-model-flax"""		,				use_auth_token=self._token       )

           A_						=      FlaxBertModel.from_pretrained(f'''{USER}/test-model-flax'''       )

           A_						=      flatten_dict(unfreeze(model.params       )       )
           A_						=      flatten_dict(unfreeze(new_model.params       )       )

           for key in base_params.keys():
                  A_						=      (base_params[key] - new_params[key]).sum().item()
                  self.assertLessEqual(UpperCamelCase__		,				1e-3		,				msg=f'''{key} not identical'''       )

           # Reset repo
           delete_repo(token=self._token		,				repo_id="""test-model-flax"""       )

           # Push to hub via save_pretrained
           with tempfile.TemporaryDirectory() as tmp_dir:
                  model.save_pretrained(UpperCamelCase__		,				repo_id="""test-model-flax"""		,				push_to_hub=UpperCamelCase__		,				use_auth_token=self._token       )

           A_						=      FlaxBertModel.from_pretrained(f'''{USER}/test-model-flax'''       )

           A_						=      flatten_dict(unfreeze(model.params       )       )
           A_						=      flatten_dict(unfreeze(new_model.params       )       )

           for key in base_params.keys():
                  A_						=      (base_params[key] - new_params[key]).sum().item()
                  self.assertLessEqual(UpperCamelCase__		,				1e-3		,				msg=f'''{key} not identical'''       )







    def 					snake_case_			(					self       )						->      Optional[Any]:





           '''simple docstring'''
           A_						=      BertConfig(
               vocab_size=99		,				hidden_size=32		,				num_hidden_layers=5		,				num_attention_heads=4		,				intermediate_size=37       )
           A_						=      FlaxBertModel(UpperCamelCase__       )
           model.push_to_hub("""valid_org/test-model-flax-org"""		,				use_auth_token=self._token       )

           A_						=      FlaxBertModel.from_pretrained("""valid_org/test-model-flax-org"""       )

           A_						=      flatten_dict(unfreeze(model.params       )       )
           A_						=      flatten_dict(unfreeze(new_model.params       )       )

           for key in base_params.keys():
                  A_						=      (base_params[key] - new_params[key]).sum().item()
                  self.assertLessEqual(UpperCamelCase__		,				1e-3		,				msg=f'''{key} not identical'''       )

           # Reset repo
           delete_repo(token=self._token		,				repo_id="""valid_org/test-model-flax-org"""       )

           # Push to hub via save_pretrained
           with tempfile.TemporaryDirectory() as tmp_dir:
                  model.save_pretrained(
                      UpperCamelCase__		,				repo_id="""valid_org/test-model-flax-org"""		,				push_to_hub=UpperCamelCase__		,				use_auth_token=self._token       )

           A_						=      FlaxBertModel.from_pretrained("""valid_org/test-model-flax-org"""       )

           A_						=      flatten_dict(unfreeze(model.params       )       )
           A_						=      flatten_dict(unfreeze(new_model.params       )       )

           for key in base_params.keys():
                  A_						=      (base_params[key] - new_params[key]).sum().item()
                  self.assertLessEqual(UpperCamelCase__		,				1e-3		,				msg=f'''{key} not identical'''       )

def       UpperCAmelCase__    (      UpperCAmelCase__,       UpperCAmelCase__				)			->			Dict:
       A_						=      True
       A_						=      flatten_dict(modela.params				)
       A_						=      flatten_dict(modela.params				)
       for key in flat_params_a.keys():
              if np.sum(np.abs(flat_params_a[key] - flat_params_a[key]				)				) > 1e-4:
                     A_						=      False

       return models_are_equal







@require_flax
class 					A__      (      unittest.TestCase			):



    def 					snake_case_			(					self       )						->      int:





           '''simple docstring'''
           A_						=      BertConfig.from_pretrained("""hf-internal-testing/tiny-bert-flax-only"""       )
           A_						=      FlaxBertModel(UpperCamelCase__       )

           A_						=      """bert"""
           with tempfile.TemporaryDirectory() as tmp_dir:
                  model.save_pretrained(os.path.join(UpperCamelCase__		,				UpperCamelCase__       )       )

                  with self.assertRaises(UpperCamelCase__       ):
                         A_						=      FlaxBertModel.from_pretrained(UpperCamelCase__       )

                  A_						=      FlaxBertModel.from_pretrained(UpperCamelCase__		,				subfolder=UpperCamelCase__       )

           self.assertTrue(check_models_equal(UpperCamelCase__		,				UpperCamelCase__       )       )



    def 					snake_case_			(					self       )						->      int:





           '''simple docstring'''
           A_						=      BertConfig.from_pretrained("""hf-internal-testing/tiny-bert-flax-only"""       )
           A_						=      FlaxBertModel(UpperCamelCase__       )

           A_						=      """bert"""
           with tempfile.TemporaryDirectory() as tmp_dir:
                  model.save_pretrained(os.path.join(UpperCamelCase__		,				UpperCamelCase__       )		,				max_shard_size="""10KB"""       )

                  with self.assertRaises(UpperCamelCase__       ):
                         A_						=      FlaxBertModel.from_pretrained(UpperCamelCase__       )

                  A_						=      FlaxBertModel.from_pretrained(UpperCamelCase__		,				subfolder=UpperCamelCase__       )

           self.assertTrue(check_models_equal(UpperCamelCase__		,				UpperCamelCase__       )       )



    def 					snake_case_			(					self       )						->      Tuple:





           '''simple docstring'''
           A_						=      """bert"""
           A_						=      """hf-internal-testing/tiny-random-bert-subfolder"""

           with self.assertRaises(UpperCamelCase__       ):
                  A_						=      FlaxBertModel.from_pretrained(UpperCamelCase__       )

           A_						=      FlaxBertModel.from_pretrained(UpperCamelCase__		,				subfolder=UpperCamelCase__       )

           self.assertIsNotNone(UpperCamelCase__       )







    def 					snake_case_			(					self       )						->      List[Any]:





           '''simple docstring'''
           A_						=      """bert"""
           A_						=      """hf-internal-testing/tiny-random-bert-sharded-subfolder"""
           with self.assertRaises(UpperCamelCase__       ):
                  A_						=      FlaxBertModel.from_pretrained(UpperCamelCase__       )

           A_						=      FlaxBertModel.from_pretrained(UpperCamelCase__		,				subfolder=UpperCamelCase__       )

           self.assertIsNotNone(UpperCamelCase__       )



'''simple docstring'''







from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding


class 					A__      (      _snake_case			):
    lowercase						=		"ClapFeatureExtractor"
    lowercase						=		("RobertaTokenizer", "RobertaTokenizerFast")



    def __init__(					self		,				UpperCamelCase__		,				UpperCamelCase__       )						->      Tuple:





           '''simple docstring'''
           super().__init__(UpperCamelCase__		,				UpperCamelCase__       )



    def __call__(					self		,				UpperCamelCase__=None		,				UpperCamelCase__=None		,				UpperCamelCase__=None		,				**UpperCamelCase__       )						->      Tuple:





           '''simple docstring'''
           A_						=      kwargs.pop("""sampling_rate"""		,				UpperCamelCase__       )

           if text is None and audios is None:
                  raise ValueError("""You have to specify either text or audios. Both cannot be none."""       )

           if text is not None:
                  A_						=      self.tokenizer(UpperCamelCase__		,				return_tensors=UpperCamelCase__		,				**UpperCamelCase__       )

           if audios is not None:
                  A_						=      self.feature_extractor(
                      UpperCamelCase__		,				sampling_rate=UpperCamelCase__		,				return_tensors=UpperCamelCase__		,				**UpperCamelCase__       )

           if text is not None and audios is not None:
                  A_						=      audio_features.input_features
                  return encoding
           elif text is not None:
                  return encoding
           else:
                  return BatchEncoding(data=dict(**UpperCamelCase__       )		,				tensor_type=UpperCamelCase__       )



    def 					snake_case_			(					self		,				*UpperCamelCase__		,				**UpperCamelCase__       )						->      Optional[int]:





           '''simple docstring'''
           return self.tokenizer.batch_decode(*UpperCamelCase__		,				**UpperCamelCase__       )



    def 					snake_case_			(					self		,				*UpperCamelCase__		,				**UpperCamelCase__       )						->      str:





           '''simple docstring'''
           return self.tokenizer.decode(*UpperCamelCase__		,				**UpperCamelCase__       )







    @property
    def 					snake_case_			(					self       )						->      int:





           '''simple docstring'''
           A_						=      self.tokenizer.model_input_names
           A_						=      self.feature_extractor.model_input_names
           return list(dict.fromkeys(tokenizer_input_names + feature_extractor_input_names       )       )
