



import importlib
import json
import os
from collections import OrderedDict
from typing import Dict, Optional, Union

# Build the list of all feature extractors
from ...configuration_utils import PretrainedConfig
from ...dynamic_module_utils import get_class_from_dynamic_module, resolve_trust_remote_code
from ...feature_extraction_utils import FeatureExtractionMixin
from ...utils import CONFIG_NAME, FEATURE_EXTRACTOR_NAME, get_file_from_repo, logging
from .auto_factory import _LazyAutoMapping
from .configuration_auto import (
    CONFIG_MAPPING_NAMES,
    AutoConfig,
    model_type_to_module_name,
    replace_list_option_in_docstrings,
)


__snake_case		:int   	=  logging.get_logger(__name__)

__snake_case		:str   	=  OrderedDict(
    [
        ('''audio-spectrogram-transformer''', '''ASTFeatureExtractor'''),
        ('''beit''', '''BeitFeatureExtractor'''),
        ('''chinese_clip''', '''ChineseCLIPFeatureExtractor'''),
        ('''clap''', '''ClapFeatureExtractor'''),
        ('''clip''', '''CLIPFeatureExtractor'''),
        ('''clipseg''', '''ViTFeatureExtractor'''),
        ('''conditional_detr''', '''ConditionalDetrFeatureExtractor'''),
        ('''convnext''', '''ConvNextFeatureExtractor'''),
        ('''cvt''', '''ConvNextFeatureExtractor'''),
        ('''data2vec-audio''', '''Wav2Vec2FeatureExtractor'''),
        ('''data2vec-vision''', '''BeitFeatureExtractor'''),
        ('''deformable_detr''', '''DeformableDetrFeatureExtractor'''),
        ('''deit''', '''DeiTFeatureExtractor'''),
        ('''detr''', '''DetrFeatureExtractor'''),
        ('''dinat''', '''ViTFeatureExtractor'''),
        ('''donut-swin''', '''DonutFeatureExtractor'''),
        ('''dpt''', '''DPTFeatureExtractor'''),
        ('''encodec''', '''EncodecFeatureExtractor'''),
        ('''flava''', '''FlavaFeatureExtractor'''),
        ('''glpn''', '''GLPNFeatureExtractor'''),
        ('''groupvit''', '''CLIPFeatureExtractor'''),
        ('''hubert''', '''Wav2Vec2FeatureExtractor'''),
        ('''imagegpt''', '''ImageGPTFeatureExtractor'''),
        ('''layoutlmv2''', '''LayoutLMv2FeatureExtractor'''),
        ('''layoutlmv3''', '''LayoutLMv3FeatureExtractor'''),
        ('''levit''', '''LevitFeatureExtractor'''),
        ('''maskformer''', '''MaskFormerFeatureExtractor'''),
        ('''mctct''', '''MCTCTFeatureExtractor'''),
        ('''mobilenet_v1''', '''MobileNetV1FeatureExtractor'''),
        ('''mobilenet_v2''', '''MobileNetV2FeatureExtractor'''),
        ('''mobilevit''', '''MobileViTFeatureExtractor'''),
        ('''nat''', '''ViTFeatureExtractor'''),
        ('''owlvit''', '''OwlViTFeatureExtractor'''),
        ('''perceiver''', '''PerceiverFeatureExtractor'''),
        ('''poolformer''', '''PoolFormerFeatureExtractor'''),
        ('''regnet''', '''ConvNextFeatureExtractor'''),
        ('''resnet''', '''ConvNextFeatureExtractor'''),
        ('''segformer''', '''SegformerFeatureExtractor'''),
        ('''sew''', '''Wav2Vec2FeatureExtractor'''),
        ('''sew-d''', '''Wav2Vec2FeatureExtractor'''),
        ('''speech_to_text''', '''Speech2TextFeatureExtractor'''),
        ('''speecht5''', '''SpeechT5FeatureExtractor'''),
        ('''swiftformer''', '''ViTFeatureExtractor'''),
        ('''swin''', '''ViTFeatureExtractor'''),
        ('''swinv2''', '''ViTFeatureExtractor'''),
        ('''table-transformer''', '''DetrFeatureExtractor'''),
        ('''timesformer''', '''VideoMAEFeatureExtractor'''),
        ('''tvlt''', '''TvltFeatureExtractor'''),
        ('''unispeech''', '''Wav2Vec2FeatureExtractor'''),
        ('''unispeech-sat''', '''Wav2Vec2FeatureExtractor'''),
        ('''van''', '''ConvNextFeatureExtractor'''),
        ('''videomae''', '''VideoMAEFeatureExtractor'''),
        ('''vilt''', '''ViltFeatureExtractor'''),
        ('''vit''', '''ViTFeatureExtractor'''),
        ('''vit_mae''', '''ViTFeatureExtractor'''),
        ('''vit_msn''', '''ViTFeatureExtractor'''),
        ('''wav2vec2''', '''Wav2Vec2FeatureExtractor'''),
        ('''wav2vec2-conformer''', '''Wav2Vec2FeatureExtractor'''),
        ('''wavlm''', '''Wav2Vec2FeatureExtractor'''),
        ('''whisper''', '''WhisperFeatureExtractor'''),
        ('''xclip''', '''CLIPFeatureExtractor'''),
        ('''yolos''', '''YolosFeatureExtractor'''),
    ]
)

__snake_case		:Dict   	=  _LazyAutoMapping(CONFIG_MAPPING_NAMES, FEATURE_EXTRACTOR_MAPPING_NAMES)





def   __snake_case			(    _UpperCAmelCase       ):
    for module_name, extractors in FEATURE_EXTRACTOR_MAPPING_NAMES.items():
        if class_name in extractors:
            __a		 =       model_type_to_module_name(_UpperCAmelCase       )

            __a		 =       importlib.import_module(f'.{module_name}'     ,				'''transformers.models'''       )
            try:
                return getattr(_UpperCAmelCase     ,				_UpperCAmelCase       )
            except AttributeError:
                continue

    for _, extractor in FEATURE_EXTRACTOR_MAPPING._extra_content.items():
        if getattr(_UpperCAmelCase     ,				'''__name__'''     ,				_UpperCAmelCase       ) == class_name:
            return extractor

    # We did not fine the class, but maybe it's because a dep is missing. In that case, the class will be in the main
    # init and we return the proper dummy to get an appropriate error message.
    __a		 =       importlib.import_module('''transformers'''       )
    if hasattr(_UpperCAmelCase     ,				_UpperCAmelCase       ):
        return getattr(_UpperCAmelCase     ,				_UpperCAmelCase       )

    return None





def   __snake_case			(    _UpperCAmelCase     ,				_UpperCAmelCase = None     ,				_UpperCAmelCase = False     ,				_UpperCAmelCase = False     ,				_UpperCAmelCase = None     ,				_UpperCAmelCase = None     ,				_UpperCAmelCase = None     ,				_UpperCAmelCase = False     ,				**_UpperCAmelCase     ,				):
    __a		 =       get_file_from_repo(
        _UpperCAmelCase     ,				_UpperCAmelCase     ,				cache_dir=_UpperCAmelCase     ,				force_download=_UpperCAmelCase     ,				resume_download=_UpperCAmelCase     ,				proxies=_UpperCAmelCase     ,				use_auth_token=_UpperCAmelCase     ,				revision=_UpperCAmelCase     ,				local_files_only=_UpperCAmelCase     ,				)
    if resolved_config_file is None:
        logger.info(
            '''Could not locate the feature extractor configuration file, will try to use the model config instead.'''       )
        return {}

    with open(_UpperCAmelCase     ,				encoding='''utf-8'''       ) as reader:
        return json.load(_UpperCAmelCase       )

class 					_A :



       def __init__(	self							:  Any):


           '''simple docstring'''

           raise EnvironmentError(
               '''AutoFeatureExtractor is designed to be instantiated '''
               '''using the `AutoFeatureExtractor.from_pretrained(pretrained_model_name_or_path)` method.''')



       @classmethod
       @replace_list_option_in_docstrings(__SCREAMING_SNAKE_CASE)
       def 	_lowerCamelCase    (	cls							:  List[str]  ,	__SCREAMING_SNAKE_CASE							:  List[Any]  ,	**__SCREAMING_SNAKE_CASE							:  int):


           '''simple docstring'''

           __a		 =       kwargs.pop('''config'''  ,	__SCREAMING_SNAKE_CASE)
           __a		 =       kwargs.pop('''trust_remote_code'''  ,	__SCREAMING_SNAKE_CASE)
           __a		 =       True

           __a ,     __a		 =       FeatureExtractionMixin.get_feature_extractor_dict(__SCREAMING_SNAKE_CASE  ,	**__SCREAMING_SNAKE_CASE)
           __a		 =       config_dict.get('''feature_extractor_type'''  ,	__SCREAMING_SNAKE_CASE)
           __a		 =       None
           if "AutoFeatureExtractor" in config_dict.get('''auto_map'''  ,	{}):
               __a		 =       config_dict['''auto_map''']['''AutoFeatureExtractor''']

           # If we don't find the feature extractor class in the feature extractor config, let's try the model config.
           if feature_extractor_class is None and feature_extractor_auto_map is None:
               if not isinstance(__SCREAMING_SNAKE_CASE  ,	__SCREAMING_SNAKE_CASE):
                   __a		 =       AutoConfig.from_pretrained(__SCREAMING_SNAKE_CASE  ,	**__SCREAMING_SNAKE_CASE)
               # It could be in `config.feature_extractor_type``
               __a		 =       getattr(__SCREAMING_SNAKE_CASE  ,	'''feature_extractor_type'''  ,	__SCREAMING_SNAKE_CASE)
               if hasattr(__SCREAMING_SNAKE_CASE  ,	'''auto_map''') and "AutoFeatureExtractor" in config.auto_map:
                   __a		 =       config.auto_map['''AutoFeatureExtractor''']

           if feature_extractor_class is not None:
               __a		 =       feature_extractor_class_from_name(__SCREAMING_SNAKE_CASE)

           __a		 =       feature_extractor_auto_map is not None
           __a		 =       feature_extractor_class is not None or type(__SCREAMING_SNAKE_CASE) in FEATURE_EXTRACTOR_MAPPING
           __a		 =       resolve_trust_remote_code(
               __SCREAMING_SNAKE_CASE  ,	__SCREAMING_SNAKE_CASE  ,	__SCREAMING_SNAKE_CASE  ,	__SCREAMING_SNAKE_CASE)

           if has_remote_code and trust_remote_code:
               __a		 =       get_class_from_dynamic_module(
                   __SCREAMING_SNAKE_CASE  ,	__SCREAMING_SNAKE_CASE  ,	**__SCREAMING_SNAKE_CASE)
               __a		 =       kwargs.pop('''code_revision'''  ,	__SCREAMING_SNAKE_CASE)
               if os.path.isdir(__SCREAMING_SNAKE_CASE):
                   feature_extractor_class.register_for_auto_class()
               return feature_extractor_class.from_dict(__SCREAMING_SNAKE_CASE  ,	**__SCREAMING_SNAKE_CASE)
           elif feature_extractor_class is not None:
               return feature_extractor_class.from_dict(__SCREAMING_SNAKE_CASE  ,	**__SCREAMING_SNAKE_CASE)
           # Last try: we use the FEATURE_EXTRACTOR_MAPPING.
           elif type(__SCREAMING_SNAKE_CASE) in FEATURE_EXTRACTOR_MAPPING:
               __a		 =       FEATURE_EXTRACTOR_MAPPING[type(__SCREAMING_SNAKE_CASE)]
               return feature_extractor_class.from_dict(__SCREAMING_SNAKE_CASE  ,	**__SCREAMING_SNAKE_CASE)

           raise ValueError(
               F'Unrecognized feature extractor in {pretrained_model_name_or_path}. Should have a '
               F'`feature_extractor_type` key in its {FEATURE_EXTRACTOR_NAME} of {CONFIG_NAME}, or one of the following '
               F'`model_type` keys in its {CONFIG_NAME}: {", ".join(c for c in FEATURE_EXTRACTOR_MAPPING_NAMES.keys())}')





       @staticmethod
       def 	_lowerCamelCase    (	__SCREAMING_SNAKE_CASE							:  List[Any]  ,	__SCREAMING_SNAKE_CASE							:  Optional[int]):


           '''simple docstring'''

           FEATURE_EXTRACTOR_MAPPING.register(__SCREAMING_SNAKE_CASE  ,	__SCREAMING_SNAKE_CASE)










# Lint as: python3
import sys
from collections.abc import Mapping
from typing import TYPE_CHECKING, Dict, Optional

import numpy as np
import pyarrow as pa

from .. import config
from ..utils.logging import get_logger
from ..utils.py_utils import map_nested
from .formatting import TensorFormatter


if TYPE_CHECKING:
 import jax
 import jaxlib

__snake_case		:str   	=  get_logger()

__snake_case		:Optional[dict]   	=  None

class 					_A (			TensorFormatter[Mapping, '''jax.Array''', Mapping]					):



       def __init__(	self							:  str  ,	__SCREAMING_SNAKE_CASE							:  int=None  ,	__SCREAMING_SNAKE_CASE							:  List[Any]=None  ,	**__SCREAMING_SNAKE_CASE							:  Any):


           '''simple docstring'''

           super().__init__(features=__SCREAMING_SNAKE_CASE)
           import jax
           from jaxlib.xla_client import Device

           if isinstance(__SCREAMING_SNAKE_CASE  ,	__SCREAMING_SNAKE_CASE):
               raise ValueError(
                   F'Expected {device} to be a `str` not {type(__SCREAMING_SNAKE_CASE)}, as `jaxlib.xla_extension.Device` '
                   '''is not serializable neither with `pickle` nor with `dill`. Instead you can surround '''
                   '''the device with `str()` to get its string identifier that will be internally mapped '''
                   '''to the actual `jaxlib.xla_extension.Device`.''')
           __a		 =       device if isinstance(__SCREAMING_SNAKE_CASE  ,	__SCREAMING_SNAKE_CASE) else str(jax.devices()[0])
           # using global variable since `jaxlib.xla_extension.Device` is not serializable neither
           # with `pickle` nor with `dill`, so we need to use a global variable instead
           global DEVICE_MAPPING
           if DEVICE_MAPPING is None:
               __a		 =       self._map_devices_to_str()
           if self.device not in list(DEVICE_MAPPING.keys()):
               logger.warning(
                   F'Device with string identifier {self.device} not listed among the available '
                   F'devices: {list(DEVICE_MAPPING.keys())}, so falling back to the default '
                   F'device: {str(jax.devices()[0])}.')
               __a		 =       str(jax.devices()[0])
           __a		 =       jnp_array_kwargs



       @staticmethod
       def 	_lowerCamelCase    (	):


           '''simple docstring'''

           import jax

           return {str(__SCREAMING_SNAKE_CASE): device for device in jax.devices()}



       def 	_lowerCamelCase    (	self							:  Union[str, Any]  ,	__SCREAMING_SNAKE_CASE							:  Tuple):


           '''simple docstring'''

           import jax
           import jax.numpy as jnp

           if isinstance(__SCREAMING_SNAKE_CASE  ,	__SCREAMING_SNAKE_CASE) and column:
               if all(
                   isinstance(__SCREAMING_SNAKE_CASE  ,	jax.Array) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column):
                   return jnp.stack(__SCREAMING_SNAKE_CASE  ,	axis=0)
           return column



       def 	_lowerCamelCase    (	self							:  str  ,	__SCREAMING_SNAKE_CASE							:  str):


           '''simple docstring'''

           import jax
           import jax.numpy as jnp

           if isinstance(__SCREAMING_SNAKE_CASE  ,	(str, bytes, type(__SCREAMING_SNAKE_CASE))):
               return value
           elif isinstance(__SCREAMING_SNAKE_CASE  ,	(np.character, np.ndarray)) and np.issubdtype(value.dtype  ,	np.character):
               return value.tolist()

           __a		 =       {}

           if isinstance(__SCREAMING_SNAKE_CASE  ,	(np.number, np.ndarray)) and np.issubdtype(value.dtype  ,	np.integer):
               # the default int precision depends on the jax config
               # see https://jax.readthedocs.io/en/latest/notebooks/Common_Gotchas_in_JAX.html#double-64bit-precision
               if jax.config.jax_enable_xaa:
                   __a		 =       {'''dtype''': jnp.intaa}
               else:
                   __a		 =       {'''dtype''': jnp.intaa}
           elif isinstance(__SCREAMING_SNAKE_CASE  ,	(np.number, np.ndarray)) and np.issubdtype(value.dtype  ,	np.floating):
               __a		 =       {'''dtype''': jnp.floataa}
           elif config.PIL_AVAILABLE and "PIL" in sys.modules:
               import PIL.Image

               if isinstance(__SCREAMING_SNAKE_CASE  ,	PIL.Image.Image):
                   __a		 =       np.asarray(__SCREAMING_SNAKE_CASE)

        # using global variable since `jaxlib.xla_extension.Device` is not serializable neither
        # with `pickle` nor with `dill`, so we need to use a global variable instead
           global DEVICE_MAPPING
           if DEVICE_MAPPING is None:
               __a		 =       self._map_devices_to_str()

           with jax.default_device(DEVICE_MAPPING[self.device]):
               # calling jnp.array on a np.ndarray does copy the data
               # see https://github.com/google/jax/issues/4486
               return jnp.array(__SCREAMING_SNAKE_CASE  ,	**{**default_dtype, **self.jnp_array_kwargs})



       def 	_lowerCamelCase    (	self							:  Tuple  ,	__SCREAMING_SNAKE_CASE							:  str):


           '''simple docstring'''

           import jax

           # support for torch, tf, jax etc.
           if config.TORCH_AVAILABLE and "torch" in sys.modules:
               import torch

               if isinstance(__SCREAMING_SNAKE_CASE  ,	torch.Tensor):
                   return self._tensorize(data_struct.detach().cpu().numpy()[()])
           if hasattr(__SCREAMING_SNAKE_CASE  ,	'''__array__''') and not isinstance(__SCREAMING_SNAKE_CASE  ,	jax.Array):
               __a		 =       data_struct.__array__()
           # support for nested types like struct of list of struct
           if isinstance(__SCREAMING_SNAKE_CASE  ,	np.ndarray):
               if data_struct.dtype == object:  # jax arrays cannot be instantied from an array of objects
                   return self._consolidate([self.recursive_tensorize(__SCREAMING_SNAKE_CASE) for substruct in data_struct])
           elif isinstance(__SCREAMING_SNAKE_CASE  ,	(list, tuple)):
               return self._consolidate([self.recursive_tensorize(__SCREAMING_SNAKE_CASE) for substruct in data_struct])
           return self._tensorize(__SCREAMING_SNAKE_CASE)



       def 	_lowerCamelCase    (	self							:  Tuple  ,	__SCREAMING_SNAKE_CASE							:  dict):


           '''simple docstring'''

           return map_nested(self._recursive_tensorize  ,	__SCREAMING_SNAKE_CASE  ,	map_list=__SCREAMING_SNAKE_CASE)



       def 	_lowerCamelCase    (	self							:  str  ,	__SCREAMING_SNAKE_CASE							:  pa.Table):


           '''simple docstring'''

           __a		 =       self.numpy_arrow_extractor().extract_row(__SCREAMING_SNAKE_CASE)
           __a		 =       self.python_features_decoder.decode_row(__SCREAMING_SNAKE_CASE)
           return self.recursive_tensorize(__SCREAMING_SNAKE_CASE)



       def 	_lowerCamelCase    (	self							:  Tuple  ,	__SCREAMING_SNAKE_CASE							:  pa.Table):


           '''simple docstring'''

           __a		 =       self.numpy_arrow_extractor().extract_column(__SCREAMING_SNAKE_CASE)
           __a		 =       self.python_features_decoder.decode_column(__SCREAMING_SNAKE_CASE  ,	pa_table.column_names[0])
           __a		 =       self.recursive_tensorize(__SCREAMING_SNAKE_CASE)
           __a		 =       self._consolidate(__SCREAMING_SNAKE_CASE)
           return column





       def 	_lowerCamelCase    (	self							:  Union[str, Any]  ,	__SCREAMING_SNAKE_CASE							:  pa.Table):


           '''simple docstring'''

           __a		 =       self.numpy_arrow_extractor().extract_batch(__SCREAMING_SNAKE_CASE)
           __a		 =       self.python_features_decoder.decode_batch(__SCREAMING_SNAKE_CASE)
           __a		 =       self.recursive_tensorize(__SCREAMING_SNAKE_CASE)
           for column_name in batch:
               __a		 =       self._consolidate(batch[column_name])
           return batch





