






"""simple docstring"""







from typing import List, Union

from ..utils import (
    add_end_docstrings,
    is_tf_available,
    is_torch_available,
    is_vision_available,
    logging,
    requires_backends,
)
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
	from PIL import Image

	from ..image_utils import load_image

if is_tf_available():
	from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_VISION_2_SEQ_MAPPING

if is_torch_available():
	import torch

	from ..models.auto.modeling_auto import MODEL_FOR_VISION_2_SEQ_MAPPING

__a       :	Any             = logging.get_logger(__name__)







@add_end_docstrings(__lowerCamelCase							)
class        _SCREAMING_SNAKE_CASE						(  __lowerCamelCase							):





							"""simple docstring"""





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

												'''simple docstring'''

												super().__init__(*UpperCamelCase_				,		**UpperCamelCase_    )
												requires_backends(self				,		'''vision'''    )
												self.check_model_type(
												    TF_MODEL_FOR_VISION_2_SEQ_MAPPING if self.framework == '''tf''' else MODEL_FOR_VISION_2_SEQ_MAPPING    )




							def  lowercase		(   self:     str				,		__A:     int=None				,		__A:     Dict=None				,		__A:     Tuple=None    ):

												'''simple docstring'''

												a__          =  {}
												a__          =  {}

												if prompt is not None:
																	a__          =  prompt

												if generate_kwargs is not None:
																	a__          =  generate_kwargs
												if max_new_tokens is not None:
																	if "generate_kwargs" not in forward_kwargs:
																						a__          =  {}
																	if "max_new_tokens" in forward_kwargs["generate_kwargs"]:
																						raise ValueError(
																						    '''\'max_new_tokens\' is defined twice, once in \'generate_kwargs\' and once as a direct parameter,'''
																						    ''' please use only one'''    )
																	a__          =  max_new_tokens
												return preprocess_params, forward_kwargs, {}




							def __call__(   self:     Optional[Any]				,		__A:     Union[str, List[str], "Image.Image", List["Image.Image"]]				,		**__A:     Union[str, Any]    ):

												'''simple docstring'''

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




							def  lowercase		(   self:     Optional[int]				,		__A:     List[Any]				,		__A:     Dict=None    ):

												'''simple docstring'''

												a__          =  load_image(UpperCamelCase_    )

												if prompt is not None:
																	if not isinstance(UpperCamelCase_				,		UpperCamelCase_    ):
																						raise ValueError(
																						    F'Received an invalid text input, got - {type(UpperCamelCase_    )} - but expected a single string. '
																						    '''Note also that one single text can be provided for conditional image to text generation.'''    )

																	a__          =  self.model.config.model_type

																	if model_type == "git":
																						a__          =  self.image_processor(images=UpperCamelCase_				,		return_tensors=self.framework    )
																						a__          =  self.tokenizer(text=UpperCamelCase_				,		add_special_tokens=UpperCamelCase_    ).input_ids
																						a__          =  [self.tokenizer.cls_token_id] + input_ids
																						a__          =  torch.tensor(UpperCamelCase_    ).unsqueeze(0    )
																						model_inputs.update({'''input_ids''': input_ids}    )

																	elif model_type == "pix2struct":
																						a__          =  self.image_processor(images=UpperCamelCase_				,		header_text=UpperCamelCase_				,		return_tensors=self.framework    )

																	elif model_type != "vision-encoder-decoder":
																						# vision-encoder-decoder does not support conditional generation
																						a__          =  self.image_processor(images=UpperCamelCase_				,		return_tensors=self.framework    )
																						a__          =  self.tokenizer(UpperCamelCase_				,		return_tensors=self.framework    )
																						model_inputs.update(UpperCamelCase_    )

																	else:
																						raise ValueError(F'Model type {model_type} does not support conditional text generation'    )

												else:
																	a__          =  self.image_processor(images=UpperCamelCase_				,		return_tensors=self.framework    )

												if self.model.config.model_type == "git" and prompt is None:
																	a__          =  None

												return model_inputs




							def  lowercase		(   self:     str				,		__A:     Optional[Any]				,		__A:     Union[str, Any]=None    ):

												'''simple docstring'''

												if (
												    "input_ids" in model_inputs
												    and isinstance(model_inputs['''input_ids''']				,		UpperCamelCase_    )
												    and all(x is None for x in model_inputs['''input_ids''']    )
												):
																	a__          =  None

												if generate_kwargs is None:
																	a__          =  {}
												# FIXME: We need to pop here due to a difference in how `generation.py` and `generation.tf_utils.py`
												#  parse inputs. In the Tensorflow version, `generate` raises an error if we don't use `input_ids` whereas
												#  the PyTorch version matches it with `self.model.main_input_name` or `self.model.encoder.main_input_name`
												#  in the `_prepare_model_inputs` method.
												a__          =  model_inputs.pop(self.model.main_input_name    )
												a__          =  self.model.generate(UpperCamelCase_				,		**UpperCamelCase_				,		**UpperCamelCase_    )
												return model_outputs





							def  lowercase		(   self:     Optional[Any]				,		__A:     Dict    ):

												'''simple docstring'''

												a__          =  []
												for output_ids in model_outputs:
																	a__          =  {
																	    '''generated_text''': self.tokenizer.decode(
																	        UpperCamelCase_				,		skip_special_tokens=UpperCamelCase_				,		)
																	}
																	records.append(UpperCamelCase_    )
												return records










"""simple docstring"""







from typing import List, Optional

import numpy as np

from ...processing_utils import ProcessorMixin
from ...utils import to_numpy







class        _SCREAMING_SNAKE_CASE						(  __snake_case							):





							"""simple docstring"""





							_SCREAMING_SNAKE_CASE    ='EncodecFeatureExtractor'
							_SCREAMING_SNAKE_CASE    =('T5Tokenizer', 'T5TokenizerFast')




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

												'''simple docstring'''

												super().__init__(__A				,		__A    )
												a__          =  self.feature_extractor
												a__          =  False




							def  lowercase		(   self:     Union[str, Any]				,		__A:     List[Any]=None				,		__A:     Optional[Any]=None				,		__A:     List[Any]=True    ):

												'''simple docstring'''

												return self.tokenizer.get_decoder_prompt_ids(task=__A				,		language=__A				,		no_timestamps=__A    )




							def __call__(   self:     Union[str, Any]				,		*__A:     int				,		**__A:     Dict    ):

												'''simple docstring'''

												if self._in_target_context_manager:
																	return self.current_processor(*__A				,		**__A    )

												a__          =  kwargs.pop('''audio'''				,		__A    )
												a__          =  kwargs.pop('''sampling_rate'''				,		__A    )
												a__          =  kwargs.pop('''text'''				,		__A    )
												if len(__A    ) > 0:
																	a__          =  args[0]
																	a__          =  args[1:]

												if audio is None and text is None:
																	raise ValueError('''You need to specify either an `audio` or `text` input to process.'''    )

												if text is not None:
																	a__          =  self.tokenizer(__A				,		**__A    )

												if audio is not None:
																	a__          =  self.feature_extractor(__A				,		*__A				,		sampling_rate=__A				,		**__A    )

												if audio is None:
																	return inputs

												elif text is None:
																	return audio_inputs

												else:
																	a__          =  audio_inputs['''input_values''']
																	if "padding_mask" in audio_inputs:
																						a__          =  audio_inputs['''padding_mask''']
																	return inputs




							def  lowercase		(   self:     Union[str, Any]				,		*__A:     List[str]				,		**__A:     Tuple    ):

												'''simple docstring'''

												a__          =  kwargs.pop('''audio'''				,		__A    )
												a__          =  kwargs.pop('''padding_mask'''				,		__A    )

												if len(__A    ) > 0:
																	a__          =  args[0]
																	a__          =  args[1:]

												if audio_values is not None:
																	return self._decode_audio(__A				,		padding_mask=__A    )
												else:
																	return self.tokenizer.batch_decode(*__A				,		**__A    )




							def  lowercase		(   self:     Union[str, Any]				,		*__A:     Optional[int]				,		**__A:     Optional[Any]    ):

												'''simple docstring'''

												return self.tokenizer.decode(*__A				,		**__A    )





							def  lowercase		(   self:     Union[str, Any]				,		__A:     Dict				,		__A:     Optional = None    ):

												'''simple docstring'''

												a__          =  to_numpy(__A    )
												a__       ,a__       ,a__          =  audio_values.shape

												if padding_mask is None:
																	return list(__A    )

												a__          =  to_numpy(__A    )

												# match the sequence length of the padding mask to the generated audio arrays by padding with the **non-padding**
												# token (so that the generated audio values are **not** treated as padded tokens)
												a__          =  seq_len - padding_mask.shape[-1]
												a__          =  1 - self.feature_extractor.padding_value
												a__          =  np.pad(__A				,		((0, 0), (0, difference))				,		'''constant'''				,		constant_values=__A    )

												a__          =  audio_values.tolist()
												for i in range(__A    ):
																	a__          =  np.asarray(audio_values[i]    )[
																	    padding_mask[i][None, :] != self.feature_extractor.padding_value
																	]
																	a__          =  sliced_audio.reshape(__A				,		-1    )

												return audio_values


