



"""simple docstring"""


import copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


__A   :							Optional[int]										=   logging.get_logger(__name__)

__A   :							Tuple										=   {
    "google/pix2struct-textcaps-base": (
        "https://huggingface.co/google/pix2struct-textcaps-base/resolve/main/config.json"
    ),
}
class 			_a (					lowerCAmelCase):
			"""simple docstring"""
			UpperCamelCase__			      =	"""pix2struct_text_model"""
			UpperCamelCase__			      =	["""past_key_values"""]
			UpperCamelCase__			      =	{
			    """hidden_size""": """hidden_size""",
			    """num_attention_heads""": """num_heads""",
			    """num_hidden_layers""": """num_layers""",
			}



			def __init__(		self     :    int				,   __UpperCamelCase     :    Dict=5_0_2_4_4				,   __UpperCamelCase     :    int=7_6_8				,   __UpperCamelCase     :    Optional[Any]=6_4				,   __UpperCamelCase     :    Any=2_0_4_8				,   __UpperCamelCase     :    Tuple=1_2				,   __UpperCamelCase     :    List[str]=1_2				,   __UpperCamelCase     :    int=3_2				,   __UpperCamelCase     :    Tuple=1_2_8				,   __UpperCamelCase     :    Union[str, Any]=0.1				,   __UpperCamelCase     :    List[str]=1e-6				,   __UpperCamelCase     :    Tuple=1.0				,   __UpperCamelCase     :    str="gelu_new"				,   __UpperCamelCase     :    Optional[int]=0				,   __UpperCamelCase     :    int=False				,   __UpperCamelCase     :    Optional[int]=0				,   __UpperCamelCase     :    List[Any]=1				,   __UpperCamelCase     :    Optional[int]=False				,   __UpperCamelCase     :    Optional[Any]=True				,   **__UpperCamelCase     :    Optional[int]				,   )->List[Any]:
							_UpperCAmelCase					=    vocab_size
							_UpperCAmelCase					=    hidden_size
							_UpperCAmelCase					=    d_kv
							_UpperCAmelCase					=    d_ff
							_UpperCAmelCase					=    num_layers
							_UpperCAmelCase					=    num_heads
							_UpperCAmelCase					=    relative_attention_num_buckets
							_UpperCAmelCase					=    relative_attention_max_distance
							_UpperCAmelCase					=    dropout_rate
							_UpperCAmelCase					=    layer_norm_epsilon
							_UpperCAmelCase					=    initializer_factor
							_UpperCAmelCase					=    use_cache

							_UpperCAmelCase					=    eos_token_id
							_UpperCAmelCase					=    decoder_start_token_id

							# for backwards compatibility
							_UpperCAmelCase					=    dense_act_fn

							super().__init__(
							    pad_token_id=__UpperCamelCase				,   eos_token_id=__UpperCamelCase				,   decoder_start_token_id=__UpperCamelCase				,   tie_word_embeddings=__UpperCamelCase				,   is_decoder=__UpperCamelCase				,   **__UpperCamelCase				,   )



			@classmethod
			def  lowercase__						(		cls     :    str				,   __UpperCamelCase     :    Union[str, os.PathLike]				,   **__UpperCamelCase     :    Dict   )->"PretrainedConfig":
							cls._set_token_in_kwargs(__UpperCamelCase   )

							_UpperCAmelCase		,						_UpperCAmelCase					=    cls.get_config_dict(__UpperCamelCase				,   **__UpperCamelCase   )

							# get the text config dict if we are loading from Pix2StructConfig
							if config_dict.get('''model_type'''   ) == "pix2struct":
											_UpperCAmelCase					=    config_dict['''text_config''']

							if "model_type" in config_dict and hasattr(cls				,   '''model_type'''   ) and config_dict["model_type"] != cls.model_type:
											logger.warning(
											    F'You are using a model of type {config_dict["model_type"]} to instantiate a model of type '
											    F'{cls.model_type}. This is not supported for all configurations of models and can yield errors.'   )

							return cls.from_dict(__UpperCamelCase				,   **__UpperCamelCase   )



class 			_a (					lowerCAmelCase):
			"""simple docstring"""
			UpperCamelCase__			      =	"""pix2struct_vision_model"""



			def __init__(		self     :    Optional[Any]				,   __UpperCamelCase     :    Optional[Any]=7_6_8				,   __UpperCamelCase     :    Any=7_6_8				,   __UpperCamelCase     :    Tuple=2_0_4_8				,   __UpperCamelCase     :    List[Any]=6_4				,   __UpperCamelCase     :    Dict=1_2				,   __UpperCamelCase     :    Dict=1_2				,   __UpperCamelCase     :    Union[str, Any]="gelu_new"				,   __UpperCamelCase     :    Optional[int]=1e-6				,   __UpperCamelCase     :    str=0.0				,   __UpperCamelCase     :    int=0.0				,   __UpperCamelCase     :    Optional[int]=1e-10				,   __UpperCamelCase     :    Any=1.0				,   __UpperCamelCase     :    List[str]=4_0_9_6				,   __UpperCamelCase     :    Any=3_2				,   __UpperCamelCase     :    List[Any]=1_2_8				,   **__UpperCamelCase     :    Union[str, Any]				,   )->Tuple:
							super().__init__(**__UpperCamelCase   )

							_UpperCAmelCase					=    hidden_size
							_UpperCAmelCase					=    patch_embed_hidden_size
							_UpperCAmelCase					=    d_ff
							_UpperCAmelCase					=    dropout_rate
							_UpperCAmelCase					=    num_hidden_layers
							_UpperCAmelCase					=    num_attention_heads
							_UpperCAmelCase					=    initializer_range
							_UpperCAmelCase					=    initializer_factor
							_UpperCAmelCase					=    attention_dropout
							_UpperCAmelCase					=    layer_norm_eps
							_UpperCAmelCase					=    dense_act_fn
							_UpperCAmelCase					=    seq_len
							_UpperCAmelCase					=    relative_attention_num_buckets
							_UpperCAmelCase					=    relative_attention_max_distance
							_UpperCAmelCase					=    d_kv



			@classmethod
			def  lowercase__						(		cls     :    Optional[Any]				,   __UpperCamelCase     :    Union[str, os.PathLike]				,   **__UpperCamelCase     :    List[Any]   )->"PretrainedConfig":
							cls._set_token_in_kwargs(__UpperCamelCase   )

							_UpperCAmelCase		,						_UpperCAmelCase					=    cls.get_config_dict(__UpperCamelCase				,   **__UpperCamelCase   )

							# get the vision config dict if we are loading from Pix2StructConfig
							if config_dict.get('''model_type'''   ) == "pix2struct":
											_UpperCAmelCase					=    config_dict['''vision_config''']

							if "model_type" in config_dict and hasattr(cls				,   '''model_type'''   ) and config_dict["model_type"] != cls.model_type:
											logger.warning(
											    F'You are using a model of type {config_dict["model_type"]} to instantiate a model of type '
											    F'{cls.model_type}. This is not supported for all configurations of models and can yield errors.'   )

							return cls.from_dict(__UpperCamelCase				,   **__UpperCamelCase   )



class 			_a (					lowerCAmelCase):
			"""simple docstring"""
			UpperCamelCase__			      =	"""pix2struct"""
			UpperCamelCase__			      =	True



			def __init__(		self     :    Optional[Any]				,   __UpperCamelCase     :    int=None				,   __UpperCamelCase     :    Union[str, Any]=None				,   __UpperCamelCase     :    Dict=1.0				,   __UpperCamelCase     :    List[Any]=0.0_2				,   __UpperCamelCase     :    Dict=False				,   __UpperCamelCase     :    Tuple=False				,   __UpperCamelCase     :    int=True				,   **__UpperCamelCase     :    int				,   )->Union[str, Any]:
							super().__init__(tie_word_embeddings=__UpperCamelCase				,   is_encoder_decoder=__UpperCamelCase				,   **__UpperCamelCase   )

							if text_config is None:
											_UpperCAmelCase					=    {}
											logger.info('''text_config is None. Initializing the Pix2StructTextConfig with default values.'''   )

							if vision_config is None:
											_UpperCAmelCase					=    {}
											logger.info('''vision_config is None. Initializing the Pix2StructVisionConfig with default values.'''   )

							_UpperCAmelCase					=    PixaStructTextConfig(**__UpperCamelCase   )
							_UpperCAmelCase					=    PixaStructVisionConfig(**__UpperCamelCase   )

							_UpperCAmelCase					=    self.text_config.decoder_start_token_id
							_UpperCAmelCase					=    self.text_config.pad_token_id
							_UpperCAmelCase					=    self.text_config.eos_token_id

							_UpperCAmelCase					=    initializer_factor
							_UpperCAmelCase					=    initializer_range

							_UpperCAmelCase					=    self.initializer_range
							_UpperCAmelCase					=    self.initializer_range

							_UpperCAmelCase					=    is_vqa



			@classmethod
			def  lowercase__						(		cls     :    List[str]				,   __UpperCamelCase     :    PixaStructTextConfig				,   __UpperCamelCase     :    PixaStructVisionConfig				,   **__UpperCamelCase     :    int   )->int:
							return cls(text_config=text_config.to_dict()				,   vision_config=vision_config.to_dict()				,   **__UpperCamelCase   )



			def  lowercase__						(		self     :    Union[str, Any]   )->List[Any]:
							_UpperCAmelCase					=    copy.deepcopy(self.__dict__   )
							_UpperCAmelCase					=    self.text_config.to_dict()
							_UpperCAmelCase					=    self.vision_config.to_dict()
							_UpperCAmelCase					=    self.__class__.model_type
							return output








"""simple docstring"""


from collections import UserDict
from typing import Union

import numpy as np
import requests

from ..utils import (
    add_end_docstrings,
    logging,
)
from .audio_classification import ffmpeg_read
from .base import PIPELINE_INIT_ARGS, Pipeline


__A   :							List[str]										=   logging.get_logger(__name__)
@add_end_docstrings(lowerCAmelCase)
class 			_a (					lowerCAmelCase):
			"""simple docstring"""



			def __init__(		self     :    Union[str, Any]				,   **__UpperCamelCase     :    Optional[Any]   )->List[Any]:
							super().__init__(**__UpperCamelCase   )

							if self.framework != "pt":
											raise ValueError(F'The {self.__class__} is only available in PyTorch.'   )
							# No specific FOR_XXX available yet



			def __call__(		self     :    Optional[int]				,   __UpperCamelCase     :    Union[np.ndarray, bytes, str]				,   **__UpperCamelCase     :    Tuple   )->List[str]:
							return super().__call__(__UpperCamelCase				,   **__UpperCamelCase   )



			def  lowercase__						(		self     :    Any				,   **__UpperCamelCase     :    Any   )->Union[str, Any]:
							_UpperCAmelCase					=    {}
							if "candidate_labels" in kwargs:
											_UpperCAmelCase					=    kwargs['''candidate_labels''']
							if "hypothesis_template" in kwargs:
											_UpperCAmelCase					=    kwargs['''hypothesis_template''']

							return preprocess_params, {}, {}



			def  lowercase__						(		self     :    Optional[int]				,   __UpperCamelCase     :    Union[str, Any]				,   __UpperCamelCase     :    List[Any]=None				,   __UpperCamelCase     :    Tuple="This is a sound of {}."   )->int:
							if isinstance(__UpperCamelCase				,   __UpperCamelCase   ):
											if audio.startswith('''http://'''   ) or audio.startswith('''https://'''   ):
															# We need to actually check for a real protocol, otherwise it's impossible to use a local file
															# like http_huggingface_co.png
															_UpperCAmelCase					=    requests.get(__UpperCamelCase   ).content
											else:
															with open(__UpperCamelCase				,   '''rb'''   ) as f:
																			_UpperCAmelCase					=    f.read()

							if isinstance(__UpperCamelCase				,   __UpperCamelCase   ):
											_UpperCAmelCase					=    ffmpeg_read(__UpperCamelCase				,   self.feature_extractor.sampling_rate   )

							if not isinstance(__UpperCamelCase				,   np.ndarray   ):
											raise ValueError('''We expect a numpy ndarray as input'''   )
							if len(audio.shape   ) != 1:
											raise ValueError('''We expect a single channel audio input for ZeroShotAudioClassificationPipeline'''   )

							_UpperCAmelCase					=    self.feature_extractor(
							    [audio]				,   sampling_rate=self.feature_extractor.sampling_rate				,   return_tensors='''pt'''   )
							_UpperCAmelCase					=    candidate_labels
							_UpperCAmelCase					=    [hypothesis_template.format(__UpperCamelCase   ) for x in candidate_labels]
							_UpperCAmelCase					=    self.tokenizer(__UpperCamelCase				,   return_tensors=self.framework				,   padding=__UpperCamelCase   )
							_UpperCAmelCase					=    [text_inputs]
							return inputs



			def  lowercase__						(		self     :    Union[str, Any]				,   __UpperCamelCase     :    Optional[int]   )->Any:
							_UpperCAmelCase					=    model_inputs.pop('''candidate_labels'''   )
							_UpperCAmelCase					=    model_inputs.pop('''text_inputs'''   )
							if isinstance(text_inputs[0]				,   __UpperCamelCase   ):
											_UpperCAmelCase					=    text_inputs[0]
							else:
											# Batching case.
											_UpperCAmelCase					=    text_inputs[0][0]

							_UpperCAmelCase					=    self.model(**__UpperCamelCase				,   **__UpperCamelCase   )

							_UpperCAmelCase					=    {
							    '''candidate_labels''': candidate_labels,
							    '''logits''': outputs.logits_per_audio,
							}
							return model_outputs



			def  lowercase__						(		self     :    List[str]				,   __UpperCamelCase     :    Any   )->List[Any]:
							_UpperCAmelCase					=    model_outputs.pop('''candidate_labels'''   )
							_UpperCAmelCase					=    model_outputs['''logits'''][0]

							if self.framework == "pt":
											_UpperCAmelCase					=    logits.softmax(dim=0   )
											_UpperCAmelCase					=    probs.tolist()
							else:
											raise ValueError('''`tf` framework not supported.'''   )

							_UpperCAmelCase					=    [
							    {'''score''': score, '''label''': candidate_label}
							    for score, candidate_label in sorted(zip(__UpperCamelCase				,   __UpperCamelCase   )				,   key=lambda __UpperCamelCase   : -x[0]   )
							]
							return result



