o
    iP                     @   s^  d Z ddlZddlZddlZddlZddlmZ ddlmZm	Z	 ddl
mZ ddlmZmZ ddlmZ dd	lmZmZmZmZ d
dlmZ d
dlmZmZmZmZ eeZeg dZ eee Z!de"fddZ#							dde	e"ej$f dee	e"ej$f  de%dee% dee&e"e"f  dee	e%e"f  dee" de%fddZ'G dd dZ(ddgZ)dS ) zAutoFeatureExtractor class.    N)OrderedDict)OptionalUnion   )PretrainedConfig)get_class_from_dynamic_moduleresolve_trust_remote_code)FeatureExtractionMixin)CONFIG_NAMEFEATURE_EXTRACTOR_NAMEcached_filelogging   )_LazyAutoMapping)CONFIG_MAPPING_NAMES
AutoConfigmodel_type_to_module_name!replace_list_option_in_docstrings)P)zaudio-spectrogram-transformerASTFeatureExtractor)beitBeitFeatureExtractor)chinese_clipChineseCLIPFeatureExtractor)clapClapFeatureExtractor)clipCLIPFeatureExtractor)clipsegViTFeatureExtractor)clvpClvpFeatureExtractor)conditional_detrConditionalDetrFeatureExtractor)convnextConvNextFeatureExtractor)cvtr$   )dacDacFeatureExtractor)zdata2vec-audioWav2Vec2FeatureExtractor)zdata2vec-visionr   )deformable_detrDeformableDetrFeatureExtractor)deitDeiTFeatureExtractor)detrDetrFeatureExtractor)diaDiaFeatureExtractor)dinatr   )z
donut-swinDonutFeatureExtractor)dptDPTFeatureExtractor)encodecEncodecFeatureExtractor)flavaFlavaFeatureExtractor)gemma3nGemma3nAudioFeatureExtractor)glpnGLPNFeatureExtractor)granite_speechGraniteSpeechFeatureExtractor)groupvitr   )hubertr(   )imagegptImageGPTFeatureExtractor)kyutai_speech_to_text"KyutaiSpeechToTextFeatureExtractor)
layoutlmv2LayoutLMv2FeatureExtractor)
layoutlmv3LayoutLMv3FeatureExtractor)levitLevitFeatureExtractor)
maskformerMaskFormerFeatureExtractor)mctctMCTCTFeatureExtractor)mimir6   )mobilenet_v1MobileNetV1FeatureExtractor)mobilenet_v2MobileNetV2FeatureExtractor)	mobilevitMobileViTFeatureExtractor)	moonshiner(   )moshir6   )natr   )owlvitOwlViTFeatureExtractor)parakeet_ctcParakeetFeatureExtractor)parakeet_encoderr\   )	perceiverPerceiverFeatureExtractor)phi4_multimodalPhi4MultimodalFeatureExtractor)
poolformerPoolFormerFeatureExtractor)	pop2pianoPop2PianoFeatureExtractor)regnetr$   )resnetr$   )seamless_m4tSeamlessM4TFeatureExtractor)seamless_m4t_v2ri   )	segformerSegformerFeatureExtractor)sewr(   )zsew-dr(   )speech_to_textSpeech2TextFeatureExtractor)speecht5SpeechT5FeatureExtractor)swiftformerr   )swinr   )swinv2r   )ztable-transformerr.   )timesformerVideoMAEFeatureExtractor)tvltTvltFeatureExtractor)	unispeechr(   )zunispeech-satr(   )univnetUnivNetFeatureExtractor)vanr$   )videomaerv   )viltViltFeatureExtractor)vitr   )vit_maer   )vit_msnr   )wav2vec2r(   )zwav2vec2-bertr(   )zwav2vec2-conformerr(   )wavlmr(   )whisperWhisperFeatureExtractor)xclipr   )xcodecr'   )yolosYolosFeatureExtractor
class_namec              	   C   s   t  D ]'\}}| |v r+t|}td| d}zt|| W   S  ty*   Y qw qtj	 D ]}t|dd | kr?|  S q1td}t
|| rOt|| S d S )N.ztransformers.models__name__transformers)FEATURE_EXTRACTOR_MAPPING_NAMESitemsr   	importlibimport_modulegetattrAttributeErrorFEATURE_EXTRACTOR_MAPPING_extra_contentvalueshasattr)r   module_name
extractorsmodule	extractormain_module r   d/home/ubuntu/.local/lib/python3.10/site-packages/transformers/models/auto/feature_extraction_auto.py!feature_extractor_class_from_name   s$   	


r   Fpretrained_model_name_or_path	cache_dirforce_downloadresume_downloadproxiestokenrevisionlocal_files_onlyc                 K   s   | dd}	|	durtdt |durtd|	}t| t|||||||dddd}
|
du r5td i S t	|
dd	}t
|W  d   S 1 sKw   Y  dS )
a*  
    Loads the tokenizer configuration from a pretrained model tokenizer configuration.

    Args:
        pretrained_model_name_or_path (`str` or `os.PathLike`):
            This can be either:

            - a string, the *model id* of a pretrained model configuration hosted inside a model repo on
              huggingface.co.
            - a path to a *directory* containing a configuration file saved using the
              [`~PreTrainedTokenizer.save_pretrained`] method, e.g., `./my_model_directory/`.

        cache_dir (`str` or `os.PathLike`, *optional*):
            Path to a directory in which a downloaded pretrained model configuration should be cached if the standard
            cache should not be used.
        force_download (`bool`, *optional*, defaults to `False`):
            Whether or not to force to (re-)download the configuration files and override the cached versions if they
            exist.
        resume_download:
            Deprecated and ignored. All downloads are now resumed by default when possible.
            Will be removed in v5 of Transformers.
        proxies (`dict[str, str]`, *optional*):
            A dictionary of proxy servers to use by protocol or endpoint, e.g., `{'http': 'foo.bar:3128',
            'http://hostname': 'foo.bar:4012'}.` The proxies are used on each request.
        token (`str` or *bool*, *optional*):
            The token to use as HTTP bearer authorization for remote files. If `True`, will use the token generated
            when running `hf auth login` (stored in `~/.huggingface`).
        revision (`str`, *optional*, defaults to `"main"`):
            The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use a
            git-based system for storing models and other artifacts on huggingface.co, so `revision` can be any
            identifier allowed by git.
        local_files_only (`bool`, *optional*, defaults to `False`):
            If `True`, will only try to load the tokenizer configuration from local files.

    <Tip>

    Passing `token=True` is required when you want to use a private model.

    </Tip>

    Returns:
        `Dict`: The configuration of the tokenizer.

    Examples:

    ```python
    # Download configuration from huggingface.co and cache.
    tokenizer_config = get_tokenizer_config("google-bert/bert-base-uncased")
    # This model does not have a tokenizer config so the result will be an empty dict.
    tokenizer_config = get_tokenizer_config("FacebookAI/xlm-roberta-base")

    # Save a pretrained tokenizer locally and you can reload its config
    from transformers import AutoTokenizer

    tokenizer = AutoTokenizer.from_pretrained("google-bert/bert-base-cased")
    tokenizer.save_pretrained("tokenizer-test")
    tokenizer_config = get_tokenizer_config("tokenizer-test")
    ```use_auth_tokenNrThe `use_auth_token` argument is deprecated and will be removed in v5 of Transformers. Please use `token` instead.V`token` and `use_auth_token` are both specified. Please set only the argument `token`.F)
r   r   r   r   r   r   r    _raise_exceptions_for_gated_repo%_raise_exceptions_for_missing_entries'_raise_exceptions_for_connection_errorszdCould not locate the feature extractor configuration file, will try to use the model config instead.zutf-8)encoding)popwarningswarnFutureWarning
ValueErrorr   r   loggerinfoopenjsonload)r   r   r   r   r   r   r   r   kwargsr   resolved_config_filereaderr   r   r   get_feature_extractor_config   s>   E$r   c                   @   s:   e Zd ZdZdd Zeeedd Ze	d
ddZ
d	S )AutoFeatureExtractora+  
    This is a generic feature extractor class that will be instantiated as one of the feature extractor classes of the
    library when created with the [`AutoFeatureExtractor.from_pretrained`] class method.

    This class cannot be instantiated directly using `__init__()` (throws an error).
    c                 C   s   t d)NzAutoFeatureExtractor is designed to be instantiated using the `AutoFeatureExtractor.from_pretrained(pretrained_model_name_or_path)` method.)OSError)selfr   r   r   __init__  s   zAutoFeatureExtractor.__init__c                 K   s  | dd}|durtdt |ddurtd||d< | dd}| dd}d|d	< tj|fi |\}}|d
d}d}	d|di v rP|d d }	|du r}|	du r}t|t	sht
j|fd|i|}t|d
d}t|dr}d|jv r}|jd }	|durt|}|	du}
|dupt|tv }|
rd|	v r|	dd }nd}t||||
|}|
r|rt|	|fi |}| dd}|  |j|fi |S |dur|j|fi |S t|tv rtt| }|j|fi |S td| dt dt dt dddd tD  
)a}  
        Instantiate one of the feature extractor classes of the library from a pretrained model vocabulary.

        The feature extractor class to instantiate is selected based on the `model_type` property of the config object
        (either passed as an argument or loaded from `pretrained_model_name_or_path` if possible), or when it's
        missing, by falling back to using pattern matching on `pretrained_model_name_or_path`:

        List options

        Params:
            pretrained_model_name_or_path (`str` or `os.PathLike`):
                This can be either:

                - a string, the *model id* of a pretrained feature_extractor hosted inside a model repo on
                  huggingface.co.
                - a path to a *directory* containing a feature extractor file saved using the
                  [`~feature_extraction_utils.FeatureExtractionMixin.save_pretrained`] method, e.g.,
                  `./my_model_directory/`.
                - a path or url to a saved feature extractor JSON *file*, e.g.,
                  `./my_model_directory/preprocessor_config.json`.
            cache_dir (`str` or `os.PathLike`, *optional*):
                Path to a directory in which a downloaded pretrained model feature extractor should be cached if the
                standard cache should not be used.
            force_download (`bool`, *optional*, defaults to `False`):
                Whether or not to force to (re-)download the feature extractor files and override the cached versions
                if they exist.
            resume_download:
                Deprecated and ignored. All downloads are now resumed by default when possible.
                Will be removed in v5 of Transformers.
            proxies (`dict[str, str]`, *optional*):
                A dictionary of proxy servers to use by protocol or endpoint, e.g., `{'http': 'foo.bar:3128',
                'http://hostname': 'foo.bar:4012'}.` The proxies are used on each request.
            token (`str` or *bool*, *optional*):
                The token to use as HTTP bearer authorization for remote files. If `True`, will use the token generated
                when running `hf auth login` (stored in `~/.huggingface`).
            revision (`str`, *optional*, defaults to `"main"`):
                The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use a
                git-based system for storing models and other artifacts on huggingface.co, so `revision` can be any
                identifier allowed by git.
            return_unused_kwargs (`bool`, *optional*, defaults to `False`):
                If `False`, then this function returns just the final feature extractor object. If `True`, then this
                functions returns a `Tuple(feature_extractor, unused_kwargs)` where *unused_kwargs* is a dictionary
                consisting of the key/value pairs whose keys are not feature extractor attributes: i.e., the part of
                `kwargs` which has not been used to update `feature_extractor` and is otherwise ignored.
            trust_remote_code (`bool`, *optional*, defaults to `False`):
                Whether or not to allow for custom models defined on the Hub in their own modeling files. This option
                should only be set to `True` for repositories you trust and in which you have read the code, as it will
                execute code present on the Hub on your local machine.
            kwargs (`dict[str, Any]`, *optional*):
                The values in kwargs of any keys which are feature extractor attributes will be used to override the
                loaded values. Behavior concerning key/value pairs whose keys are *not* feature extractor attributes is
                controlled by the `return_unused_kwargs` keyword parameter.

        <Tip>

        Passing `token=True` is required when you want to use a private model.

        </Tip>

        Examples:

        ```python
        >>> from transformers import AutoFeatureExtractor

        >>> # Download feature extractor from huggingface.co and cache.
        >>> feature_extractor = AutoFeatureExtractor.from_pretrained("facebook/wav2vec2-base-960h")

        >>> # If feature extractor files are in a directory (e.g. feature extractor was saved using *save_pretrained('./test/saved_model/')*)
        >>> # feature_extractor = AutoFeatureExtractor.from_pretrained("./test/saved_model/")
        ```r   Nr   r   r   configtrust_remote_codeT
_from_autofeature_extractor_typer   auto_mapz--r   code_revisionz"Unrecognized feature extractor in z4. Should have a `feature_extractor_type` key in its z of z3, or one of the following `model_type` keys in its z: z, c                 s   s    | ]}|V  qd S )Nr   ).0cr   r   r   	<genexpr>  s    z7AutoFeatureExtractor.from_pretrained.<locals>.<genexpr>)r   r   r   r   getr   r	   get_feature_extractor_dict
isinstancer   r   from_pretrainedr   r   r   r   typer   splitr   r   register_for_auto_class	from_dictr   r
   joinr   )clsr   r   r   r   r   config_dict_feature_extractor_classfeature_extractor_auto_maphas_remote_codehas_local_codeupstream_repor   r   r   r     s   I


z$AutoFeatureExtractor.from_pretrainedFc                 C   s   t j| ||d dS )a0  
        Register a new feature extractor for this class.

        Args:
            config_class ([`PretrainedConfig`]):
                The configuration corresponding to the model to register.
            feature_extractor_class ([`FeatureExtractorMixin`]): The feature extractor to register.
        )exist_okN)r   register)config_classr   r   r   r   r   r     s   
zAutoFeatureExtractor.registerN)F)r   
__module____qualname____doc__r   classmethodr   r   r   staticmethodr   r   r   r   r   r      s     r   r   )NFNNNNF)*r   r   r   osr   collectionsr   typingr   r   configuration_utilsr   dynamic_module_utilsr   r   feature_extraction_utilsr	   utilsr
   r   r   r   auto_factoryr   configuration_autor   r   r   r   
get_loggerr   r   r   r   strr   PathLikebooldictr   r   __all__r   r   r   r   <module>   s\   

U
g (