


'''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

__snake_case							      =     logging.get_logger(__name__)
@add_end_docstrings(_a					)
class    lowercase	(							_a					):

		"""simple docstring"""

		def __init__(		self    ,   *UpperCamelCase_    ,   **UpperCamelCase_							):

							'''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     lowerCAmelCase__				(		self    ,   UpperCamelCase_=None    ,   UpperCamelCase_=None    ,   UpperCamelCase_=None							):

							'''simple docstring'''


							UpperCamelCase__   :Tuple												= {}
							UpperCamelCase__   :List[Any]												= {}

							if prompt is not None:
												UpperCamelCase__   :List[str]												= prompt

							if generate_kwargs is not None:
												UpperCamelCase__   :Optional[Any]												= generate_kwargs
							if max_new_tokens is not None:
												if "generate_kwargs" not in forward_kwargs:
																	UpperCamelCase__   :str												= {}
												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'''							)
												UpperCamelCase__   :Union[str, Any]												= max_new_tokens
							return preprocess_params, forward_kwargs, {}




		def __call__(		self    ,   UpperCamelCase_    ,   **UpperCamelCase_							):

							'''simple docstring'''


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




		def     lowerCAmelCase__				(		self    ,   UpperCamelCase_    ,   UpperCamelCase_=None							):

							'''simple docstring'''


							UpperCamelCase__   :Any												= 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.'''							)

												UpperCamelCase__   :int												= self.model.config.model_type

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

												elif model_type == "pix2struct":
																	UpperCamelCase__   :List[Any]												= 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
																	UpperCamelCase__   :Optional[int]												= self.image_processor(images=UpperCamelCase_    ,   return_tensors=self.framework							)
																	UpperCamelCase__   :Optional[int]												= 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:
												UpperCamelCase__   :int												= self.image_processor(images=UpperCamelCase_    ,   return_tensors=self.framework							)

							if self.model.config.model_type == "git" and prompt is None:
												UpperCamelCase__   :Optional[Any]												= None

							return model_inputs




		def     lowerCAmelCase__				(		self    ,   UpperCamelCase_    ,   UpperCamelCase_=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''']							)
							):
												UpperCamelCase__   :List[Any]												= None

							if generate_kwargs is None:
												UpperCamelCase__   :int												= {}
							# 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.
							UpperCamelCase__   :Optional[int]												= model_inputs.pop(self.model.main_input_name							)
							UpperCamelCase__   :Optional[Any]												= self.model.generate(UpperCamelCase_    ,   **UpperCamelCase_    ,   **UpperCamelCase_							)
							return model_outputs




		def     lowerCAmelCase__				(		self    ,   UpperCamelCase_							):

							'''simple docstring'''


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



from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_speech_available,
    is_tf_available,
    is_torch_available,
)


SCREAMING_SNAKE_CASE       :      List[str]												= {
    "configuration_speech_to_text": ["SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP", "Speech2TextConfig"],
    "processing_speech_to_text": ["Speech2TextProcessor"],
}

try:
 if not is_sentencepiece_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 SCREAMING_SNAKE_CASE       :      Union[str, Any]												= ["Speech2TextTokenizer"]

try:
 if not is_speech_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 SCREAMING_SNAKE_CASE       :      int												= ["Speech2TextFeatureExtractor"]

try:
 if not is_tf_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 SCREAMING_SNAKE_CASE       :      Optional[Any]												= [
     "TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST",
     "TFSpeech2TextForConditionalGeneration",
     "TFSpeech2TextModel",
     "TFSpeech2TextPreTrainedModel",
 ]

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 SCREAMING_SNAKE_CASE       :      str												= [
     "SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST",
     "Speech2TextForConditionalGeneration",
     "Speech2TextModel",
     "Speech2TextPreTrainedModel",
 ]


if TYPE_CHECKING:
 from .configuration_speech_to_text import SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP, SpeechaTextConfig
 from .processing_speech_to_text import SpeechaTextProcessor

 try:
  if not is_sentencepiece_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .tokenization_speech_to_text import SpeechaTextTokenizer

 try:
  if not is_speech_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .feature_extraction_speech_to_text import SpeechaTextFeatureExtractor

 try:
  if not is_tf_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_tf_speech_to_text import (
      TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST,
      TFSpeechaTextForConditionalGeneration,
      TFSpeechaTextModel,
      TFSpeechaTextPreTrainedModel,
  )

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_speech_to_text import (
      SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST,
      SpeechaTextForConditionalGeneration,
      SpeechaTextModel,
      SpeechaTextPreTrainedModel,
  )

else:
 import sys

 SCREAMING_SNAKE_CASE       :      Optional[Any]												= _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)


