"""simple docstring"""







import enum
import warnings

from ..tokenization_utils import TruncationStrategy
from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_tf_available():
						import tensorflow as tf

						from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING

if is_torch_available():
						from ..models.auto.modeling_auto import MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING

SCREAMING_SNAKE_CASE														=						logging.get_logger(__name__)



class  _lowerCamelCase			(enum.Enum     ):
				_snake_case     =  0
				_snake_case     =  1



@add_end_docstrings(__lowerCamelCase     )
class  _lowerCamelCase			(__lowerCamelCase     ):
				_snake_case     =  "generated"







				def __init__( self    :       Any			,					*lowerCamelCase_    :       List[Any]			,					**lowerCamelCase_    :       Dict						):

					"""simple docstring"""






					super().__init__(*lowerCamelCase_			,					**lowerCamelCase_						)

					self.check_model_type(
					    TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
					    if self.framework == 'tf'
					    else MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING						)







				def 							__UpperCAmelCase					( self    :       Union[str, Any]			,					lowerCamelCase_    :       Any=None			,					lowerCamelCase_    :       Tuple=None			,					lowerCamelCase_    :       Optional[int]=None			,					lowerCamelCase_    :       Optional[Any]=None			,					lowerCamelCase_    :       int=None			,					lowerCamelCase_    :       Optional[int]=None			,					**lowerCamelCase_    :       Optional[Any]			,					):

					"""simple docstring"""






					_lowercase :    str							=							{}
					if truncation is not None:
						_lowercase :    Union[str, Any]							=							truncation

					_lowercase :    Optional[Any]							=							generate_kwargs

					_lowercase :    Union[str, Any]							=							{}
					if return_tensors is not None and return_type is None:
						_lowercase :    Dict							=							ReturnType.TENSORS if return_tensors else ReturnType.TEXT
					if return_type is not None:
						_lowercase :    Tuple							=							return_type

					if clean_up_tokenization_spaces is not None:
						_lowercase :    Optional[int]							=							clean_up_tokenization_spaces

					if stop_sequence is not None:
						_lowercase :    str							=							self.tokenizer.encode(lowerCamelCase_			,					add_special_tokens=lowerCamelCase_						)
						if len(lowerCamelCase_						) > 1:
							warnings.warn(
							    'Stopping on a multiple token sequence is not yet supported on transformers. The first token of'
							    ' the stop sequence will be used as the stop sequence string in the interim.'						)
						_lowercase :    List[Any]							=							stop_sequence_ids[0]

					return preprocess_params, forward_params, postprocess_params







				def 							__UpperCAmelCase					( self    :       int			,					lowerCamelCase_    :       int			,					lowerCamelCase_    :       int			,					lowerCamelCase_    :       int						):

					"""simple docstring"""






					return True







				def 							__UpperCAmelCase					( self    :       int			,					*lowerCamelCase_    :       Any			,					lowerCamelCase_    :       Dict						):

					"""simple docstring"""






					_lowercase :    Union[str, Any]							=							self.model.config.prefix if self.model.config.prefix is not None else ''
					if isinstance(args[0]			,					lowerCamelCase_						):
						if self.tokenizer.pad_token_id is None:
							raise ValueError('Please make sure that the tokenizer has a pad_token_id when using a batch input'						)
						_lowercase :    Dict							=							([prefix + arg for arg in args[0]],)
						_lowercase :    Optional[int]							=							True

					elif isinstance(args[0]			,					lowerCamelCase_						):
						_lowercase :    Any							=							(prefix + args[0],)
						_lowercase :    str							=							False
					else:
						raise ValueError(
						    F''' `args[0]`: {args[0]} have the wrong format. The should be either of type `str` or type `list`'''						)
					_lowercase :    List[str]							=							self.tokenizer(*lowerCamelCase_			,					padding=lowerCamelCase_			,					truncation=lowerCamelCase_			,					return_tensors=self.framework						)
					# This is produced by tokenizers but is an invalid generate kwargs
					if "token_type_ids" in inputs:
						del inputs["token_type_ids"]
					return inputs







				def __call__( self    :       Dict			,					*lowerCamelCase_    :       str			,					**lowerCamelCase_    :       Optional[int]						):

					"""simple docstring"""






					_lowercase :    Union[str, Any]							=							super().__call__(*lowerCamelCase_			,					**lowerCamelCase_						)
					if (
					    isinstance(args[0]			,					lowerCamelCase_						)
					    and all(isinstance(lowerCamelCase_			,					lowerCamelCase_						) for el in args[0]						)
					    and all(len(lowerCamelCase_						) == 1 for res in result						)
					):
						return [res[0] for res in result]
					return result







				def 							__UpperCAmelCase					( self    :       Union[str, Any]			,					lowerCamelCase_    :       int			,					lowerCamelCase_    :       Union[str, Any]=TruncationStrategy.DO_NOT_TRUNCATE			,					**lowerCamelCase_    :       Optional[int]						):

					"""simple docstring"""






					_lowercase :    int							=							self._parse_and_tokenize(lowerCamelCase_			,					truncation=lowerCamelCase_			,					**lowerCamelCase_						)
					return inputs







				def 							__UpperCAmelCase					( self    :       str			,					lowerCamelCase_    :       Tuple			,					**lowerCamelCase_    :       List[str]						):

					"""simple docstring"""






					if self.framework == "pt":
						_lowercase :    List[Any]							=							model_inputs['input_ids'].shape
					elif self.framework == "tf":
						_lowercase :    List[str]							=							tf.shape(model_inputs['input_ids']						).numpy()

					_lowercase :    List[str]							=							generate_kwargs.get('min_length'			,					self.model.config.min_length						)
					_lowercase :    str							=							generate_kwargs.get('max_length'			,					self.model.config.max_length						)
					self.check_inputs(lowerCamelCase_			,					generate_kwargs['min_length']			,					generate_kwargs['max_length']						)
					_lowercase :    Union[str, Any]							=							self.model.generate(**lowerCamelCase_			,					**lowerCamelCase_						)
					_lowercase :    Optional[int]							=							output_ids.shape[0]
					if self.framework == "pt":
						_lowercase :    List[Any]							=							output_ids.reshape(lowerCamelCase_			,					out_b // in_b			,					*output_ids.shape[1:]						)
					elif self.framework == "tf":
						_lowercase :    Tuple							=							tf.reshape(lowerCamelCase_			,					(in_b, out_b // in_b, *output_ids.shape[1:])						)
					return {"output_ids": output_ids}







				def 							__UpperCAmelCase					( self    :       Tuple			,					lowerCamelCase_    :       str			,					lowerCamelCase_    :       str=ReturnType.TEXT			,					lowerCamelCase_    :       Union[str, Any]=False						):

					"""simple docstring"""






					_lowercase :    Any							=							[]
					for output_ids in model_outputs["output_ids"][0]:
						if return_type == ReturnType.TENSORS:
							_lowercase :    List[str]							=							{F'''{self.return_name}_token_ids''': output_ids}
						elif return_type == ReturnType.TEXT:
							_lowercase :    Tuple							=							{
							    F'''{self.return_name}_text''': self.tokenizer.decode(
							        lowerCamelCase_			,					skip_special_tokens=lowerCamelCase_			,					clean_up_tokenization_spaces=lowerCamelCase_			,					)
							}
						records.append(lowerCamelCase_						)
					return records



@add_end_docstrings(__lowerCamelCase     )
class  _lowerCamelCase			(__lowerCamelCase     ):
				_snake_case     =  "summary"







				def __call__( self    :       Union[str, Any]			,					*lowerCamelCase_    :       Optional[Any]			,					**lowerCamelCase_    :       Tuple						):

					"""simple docstring"""






					return super().__call__(*lowerCamelCase_			,					**lowerCamelCase_						)







				def 							__UpperCAmelCase					( self    :       Dict			,					lowerCamelCase_    :       int			,					lowerCamelCase_    :       int			,					lowerCamelCase_    :       int						):

					"""simple docstring"""






					if max_length < min_length:
						logger.warning(F'''Your min_length={min_length} must be inferior than your max_length={max_length}.'''						)

					if input_length < max_length:
						logger.warning(
						    F'''Your max_length is set to {max_length}, but your input_length is only {input_length}. Since this is '''
						    'a summarization task, where outputs shorter than the input are typically wanted, you might '
						    F'''consider decreasing max_length manually, e.g. summarizer(\'...\', max_length={input_length//2})'''						)



@add_end_docstrings(__lowerCamelCase     )
class  _lowerCamelCase			(__lowerCamelCase     ):
				_snake_case     =  "translation"







				def 							__UpperCAmelCase					( self    :       Union[str, Any]			,					lowerCamelCase_    :       int			,					lowerCamelCase_    :       int			,					lowerCamelCase_    :       int						):

					"""simple docstring"""






					if input_length > 0.9 * max_length:
						logger.warning(
						    F'''Your input_length: {input_length} is bigger than 0.9 * max_length: {max_length}. You might consider '''
						    'increasing your max_length manually, e.g. translator(\'...\', max_length=400)'						)
					return True







				def 							__UpperCAmelCase					( self    :       Union[str, Any]			,					*lowerCamelCase_    :       List[str]			,					lowerCamelCase_    :       Union[str, Any]=TruncationStrategy.DO_NOT_TRUNCATE			,					lowerCamelCase_    :       str=None			,					lowerCamelCase_    :       Optional[int]=None						):

					"""simple docstring"""






					if getattr(self.tokenizer			,					'_build_translation_inputs'			,					lowerCamelCase_						):
						return self.tokenizer._build_translation_inputs(
						    *lowerCamelCase_			,					return_tensors=self.framework			,					truncation=lowerCamelCase_			,					src_lang=lowerCamelCase_			,					tgt_lang=lowerCamelCase_						)
					else:
						return super()._parse_and_tokenize(*lowerCamelCase_			,					truncation=lowerCamelCase_						)







				def 							__UpperCAmelCase					( self    :       Union[str, Any]			,					lowerCamelCase_    :       Any=None			,					lowerCamelCase_    :       Optional[Any]=None			,					**lowerCamelCase_    :       Dict						):

					"""simple docstring"""






					_lowercase :    Dict							=							super()._sanitize_parameters(**lowerCamelCase_						)
					if src_lang is not None:
						_lowercase :    List[Any]							=							src_lang
					if tgt_lang is not None:
						_lowercase :    str							=							tgt_lang
					if src_lang is None and tgt_lang is None:
						# Backward compatibility, direct arguments use is preferred.
						_lowercase :    Optional[Any]							=							kwargs.get('task'			,					self.task						)
						_lowercase :    str							=							task.split('_'						)
						if task and len(lowerCamelCase_						) == 4:
							# translation, XX, to YY
							_lowercase :    List[Any]							=							items[1]
							_lowercase :    str							=							items[3]
					return preprocess_params, forward_params, postprocess_params







				def __call__( self    :       List[Any]			,					*lowerCamelCase_    :       List[str]			,					**lowerCamelCase_    :       Union[str, Any]						):

					"""simple docstring"""






					return super().__call__(*lowerCamelCase_			,					**lowerCamelCase_						)




"""simple docstring"""







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,
)


SCREAMING_SNAKE_CASE									=						logging.get_logger(__name__)

SCREAMING_SNAKE_CASE									=						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'),
    ]
)

SCREAMING_SNAKE_CASE									=						_LazyAutoMapping(CONFIG_MAPPING_NAMES, FEATURE_EXTRACTOR_MAPPING_NAMES)


def      __lowerCAmelCase(		__UpperCAmelCase			):






	"""simple docstring"""





	for module_name, extractors in FEATURE_EXTRACTOR_MAPPING_NAMES.items():
		if class_name in extractors:
			_lowercase :    int							=							model_type_to_module_name(__UpperCAmelCase			)

			_lowercase :    int							=							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.
	_lowercase :    List[str]							=							importlib.import_module('transformers'			)
	if hasattr(__UpperCAmelCase     ,__UpperCAmelCase			):
		return getattr(__UpperCAmelCase     ,__UpperCAmelCase			)

	return None


def      __lowerCAmelCase(		__UpperCAmelCase     ,__UpperCAmelCase = None     ,__UpperCAmelCase = False     ,__UpperCAmelCase = False     ,__UpperCAmelCase = None     ,__UpperCAmelCase = None     ,__UpperCAmelCase = None     ,__UpperCAmelCase = False     ,**__UpperCAmelCase     ,):






	"""simple docstring"""





	_lowercase :    Any							=							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  _lowerCamelCase			:







				def __init__( self    :       List[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(lowerCamelCase_						)
				def 							__UpperCAmelCase					( cls    :       Union[str, Any]			,					lowerCamelCase_    :       str			,					**lowerCamelCase_    :       int						):

					"""simple docstring"""






					_lowercase :    Optional[Any]							=							kwargs.pop('config'			,					lowerCamelCase_						)
					_lowercase :    Optional[Any]							=							kwargs.pop('trust_remote_code'			,					lowerCamelCase_						)
					_lowercase :    Optional[int]							=							True

					_lowercase				,      _lowercase :    Dict							=							FeatureExtractionMixin.get_feature_extractor_dict(lowerCamelCase_			,					**lowerCamelCase_						)
					_lowercase :    Dict							=							config_dict.get('feature_extractor_type'			,					lowerCamelCase_						)
					_lowercase :    str							=							None
					if "AutoFeatureExtractor" in config_dict.get('auto_map'			,					{}						):
						_lowercase :    int							=							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(lowerCamelCase_			,					lowerCamelCase_						):
							_lowercase :    Optional[int]							=							AutoConfig.from_pretrained(lowerCamelCase_			,					**lowerCamelCase_						)
						# It could be in `config.feature_extractor_type``
						_lowercase :    Tuple							=							getattr(lowerCamelCase_			,					'feature_extractor_type'			,					lowerCamelCase_						)
						if hasattr(lowerCamelCase_			,					'auto_map'						) and "AutoFeatureExtractor" in config.auto_map:
							_lowercase :    Optional[int]							=							config.auto_map['AutoFeatureExtractor']

					if feature_extractor_class is not None:
						_lowercase :    Optional[Any]							=							feature_extractor_class_from_name(lowerCamelCase_						)

					_lowercase :    int							=							feature_extractor_auto_map is not None
					_lowercase :    Dict							=							feature_extractor_class is not None or type(lowerCamelCase_						) in FEATURE_EXTRACTOR_MAPPING
					_lowercase :    List[str]							=							resolve_trust_remote_code(
					    lowerCamelCase_			,					lowerCamelCase_			,					lowerCamelCase_			,					lowerCamelCase_						)

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

					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 							__UpperCAmelCase					( lowerCamelCase_    :       Tuple			,					lowerCamelCase_    :       Dict						):

					"""simple docstring"""






					FEATURE_EXTRACTOR_MAPPING.register(lowerCamelCase_			,					lowerCamelCase_						)



