





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

_SCREAMING_SNAKE_CASE	 =							logging.get_logger(__name__)

@add_end_docstrings(__magic_name__		)
class    lowerCAmelCase_		(		__magic_name__		):




 def __init__(	self			,     *_lowerCAmelCase			,     **_lowerCAmelCase		)    ->							Union[str, Any]:
     super().__init__(*_lowerCAmelCase			,     **_lowerCAmelCase		)
     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 							_snake_case   (	self			,     _lowerCAmelCase=None			,     _lowerCAmelCase=None			,     _lowerCAmelCase=None		)    ->							Union[str, Any]:
     _lowerCAmelCase				=      {}
     _lowerCAmelCase				=      {}

     if prompt is not None:
         _lowerCAmelCase				=      prompt

     if generate_kwargs is not None:
         _lowerCAmelCase				=      generate_kwargs
     if max_new_tokens is not None:
         if "generate_kwargs" not in forward_kwargs:
             _lowerCAmelCase				=      {}
         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"		)
         _lowerCAmelCase				=      max_new_tokens
     return preprocess_params, forward_kwargs, {}




 def __call__(	self			,     _lowerCAmelCase			,     **_lowerCAmelCase		)    ->							Tuple:
     return super().__call__(_lowerCAmelCase			,     **_lowerCAmelCase		)




 def 							_snake_case   (	self			,     _lowerCAmelCase			,     _lowerCAmelCase=None		)    ->							Union[str, Any]:
     _lowerCAmelCase				=      load_image(_lowerCAmelCase		)

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

         _lowerCAmelCase				=      self.model.config.model_type

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

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

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

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

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

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

     return model_inputs




 def 							_snake_case   (	self			,     _lowerCAmelCase			,     _lowerCAmelCase=None		)    ->							Optional[Any]:
     # Git model sets `model_inputs["input_ids"] = None` in `preprocess` (when `prompt=None`). In batch model, the
     # pipeline will group them into a list of `None`, which fail `_forward`. Avoid this by checking it first.
     if (
         "input_ids" in model_inputs
         and isinstance(model_inputs["input_ids"]			,     _lowerCAmelCase		)
         and all(x is None for x in model_inputs["input_ids"]		)
     ):
         _lowerCAmelCase				=      None

     if generate_kwargs is None:
         _lowerCAmelCase				=      {}
     # 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.
     _lowerCAmelCase				=      model_inputs.pop(self.model.main_input_name		)
     _lowerCAmelCase				=      self.model.generate(_lowerCAmelCase			,     **_lowerCAmelCase			,     **_lowerCAmelCase		)
     return model_outputs





 def 							_snake_case   (	self			,     _lowerCAmelCase		)    ->							Union[str, Any]:
     _lowerCAmelCase				=      []
     for output_ids in model_outputs:
         _lowerCAmelCase				=      {
             "generated_text": self.tokenizer.decode(
                 _lowerCAmelCase			,     skip_special_tokens=_lowerCAmelCase			,     )
         }
         records.append(_lowerCAmelCase		)
     return records









'''simple docstring'''


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


_SCREAMING_SNAKE_CASE	 =							logging.get_logger(__name__)

_SCREAMING_SNAKE_CASE	 =							{
    "facebook/s2t-wav2vec2-large-en-de": (
        "https://huggingface.co/facebook/s2t-wav2vec2-large-en-de/resolve/main/config.json"
    ),
    # See all Speech2Text models at https://huggingface.co/models?filter=speech2text2
}

class    lowerCAmelCase_		(		__magic_name__		):
 __lowerCamelCase						:							str         =					"speech_to_text_2"
 __lowerCamelCase						:							Union[str, Any]         =					["past_key_values"]
 __lowerCamelCase						:							Tuple         =					{"num_attention_heads": "decoder_attention_heads", "hidden_size": "d_model"}




 def __init__(	self			,     _lowerCAmelCase=10000			,     _lowerCAmelCase=6			,     _lowerCAmelCase=2048			,     _lowerCAmelCase=4			,     _lowerCAmelCase=0.0			,     _lowerCAmelCase=True			,     _lowerCAmelCase="relu"			,     _lowerCAmelCase=256			,     _lowerCAmelCase=0.1			,     _lowerCAmelCase=0.0			,     _lowerCAmelCase=0.0			,     _lowerCAmelCase=0.02			,     _lowerCAmelCase=2			,     _lowerCAmelCase=True			,     _lowerCAmelCase=1			,     _lowerCAmelCase=0			,     _lowerCAmelCase=2			,     _lowerCAmelCase=1024			,     **_lowerCAmelCase			,     )    ->							Dict:
     _lowerCAmelCase				=      vocab_size
     _lowerCAmelCase				=      d_model
     _lowerCAmelCase				=      decoder_ffn_dim
     _lowerCAmelCase				=      decoder_layers
     _lowerCAmelCase				=      decoder_attention_heads
     _lowerCAmelCase				=      dropout
     _lowerCAmelCase				=      attention_dropout
     _lowerCAmelCase				=      activation_dropout
     _lowerCAmelCase				=      activation_function
     _lowerCAmelCase				=      init_std
     _lowerCAmelCase				=      decoder_layerdrop
     _lowerCAmelCase				=      use_cache
     _lowerCAmelCase				=      decoder_layers
     _lowerCAmelCase				=      scale_embedding  # scale factor will be sqrt(d_model) if True
     _lowerCAmelCase				=      max_target_positions

     super().__init__(
         pad_token_id=_lowerCAmelCase			,     bos_token_id=_lowerCAmelCase			,     eos_token_id=_lowerCAmelCase			,     decoder_start_token_id=_lowerCAmelCase			,     **_lowerCAmelCase			,     )


