
'''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(A				)
class 							UpperCAmelCase_							(    A				):

 '''simple docstring'''







 def __init__(					self		:  Tuple			,				*snake_case__		:  int			,				**snake_case__		:  Any ):



   '''simple docstring'''




   super().__init__(*snake_case__			,				**snake_case__ )
   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 							UpperCamelCase  (					self		:  Union[str, Any]			,				snake_case__		:  Optional[int]=None			,				snake_case__		:  Optional[int]=None			,				snake_case__		:  List[str]=None ):



   '''simple docstring'''




   UpperCAmelCase__      :					int				       =							{}
   UpperCAmelCase__      :					Any				       =							{}

   if prompt is not None:
     UpperCAmelCase__      :					Optional[Any]				       =							prompt

   if generate_kwargs is not None:
     UpperCAmelCase__      :					Optional[int]				       =							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__      :					Optional[Any]				       =							max_new_tokens
   return preprocess_params, forward_kwargs, {}







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



   '''simple docstring'''




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







 def 							UpperCamelCase  (					self		:  Tuple			,				snake_case__		:  Optional[Any]			,				snake_case__		:  Union[str, Any]=None ):



   '''simple docstring'''




   UpperCAmelCase__      :					List[str]				       =							load_image(snake_case__ )

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

     UpperCAmelCase__      :					Union[str, Any]				       =							self.model.config.model_type

     if model_type == "git":
       UpperCAmelCase__      :					List[Any]				       =							self.image_processor(images=snake_case__			,				return_tensors=self.framework )
       UpperCAmelCase__      :					Dict				       =							self.tokenizer(text=snake_case__			,				add_special_tokens=snake_case__ ).input_ids
       UpperCAmelCase__      :					Tuple				       =							[self.tokenizer.cls_token_id] + input_ids
       UpperCAmelCase__      :					int				       =							torch.tensor(snake_case__ ).unsqueeze(0 )
       model_inputs.update({"input_ids": input_ids} )

     elif model_type == "pix2struct":
       UpperCAmelCase__      :					Optional[int]				       =							self.image_processor(images=snake_case__			,				header_text=snake_case__			,				return_tensors=self.framework )

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

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

   else:
     UpperCAmelCase__      :					Union[str, Any]				       =							self.image_processor(images=snake_case__			,				return_tensors=self.framework )

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

   return model_inputs







 def 							UpperCamelCase  (					self		:  Tuple			,				snake_case__		:  List[str]			,				snake_case__		:  List[Any]=None ):



   '''simple docstring'''




   if (
       "input_ids" in model_inputs
       and isinstance(model_inputs["input_ids"]			,				snake_case__ )
       and all(x is None for x in model_inputs["input_ids"] )
   ):
     UpperCAmelCase__      :					Optional[Any]				       =							None

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







 def 							UpperCamelCase  (					self		:  str			,				snake_case__		:  Any ):



   '''simple docstring'''




   UpperCAmelCase__      :					Optional[int]				       =							[]
   for output_ids in model_outputs:
     UpperCAmelCase__      :					List[str]				       =							{
         "generated_text": self.tokenizer.decode(
             snake_case__			,				skip_special_tokens=snake_case__			,				)
     }
     records.append(snake_case__ )
   return records





'''simple docstring'''





from ..utils import DummyObject, requires_backends






class 							UpperCAmelCase_							(    metaclass=A				):

 '''simple docstring'''
 lowercase_					:    Optional[int]				=							["torch", "transformers", "onnx"]







 def __init__(					self		:  Tuple			,				*snake_case__		:  str			,				**snake_case__		:  List[str] ):



   '''simple docstring'''




   requires_backends(self			,				["torch", "transformers", "onnx"] )







 @classmethod
 def 							UpperCamelCase  (					cls		:  Optional[int]			,				*snake_case__		:  Any			,				**snake_case__		:  List[Any] ):



   '''simple docstring'''




   requires_backends(cls			,				["torch", "transformers", "onnx"] )







 @classmethod
 def 							UpperCamelCase  (					cls		:  List[str]			,				*snake_case__		:  Any			,				**snake_case__		:  Tuple ):



   '''simple docstring'''




   requires_backends(cls			,				["torch", "transformers", "onnx"] )






class 							UpperCAmelCase_							(    metaclass=A				):

 '''simple docstring'''
 lowercase_					:    Tuple				=							["torch", "transformers", "onnx"]







 def __init__(					self		:  List[str]			,				*snake_case__		:  List[Any]			,				**snake_case__		:  Any ):



   '''simple docstring'''




   requires_backends(self			,				["torch", "transformers", "onnx"] )







 @classmethod
 def 							UpperCamelCase  (					cls		:  Dict			,				*snake_case__		:  Any			,				**snake_case__		:  Optional[int] ):



   '''simple docstring'''




   requires_backends(cls			,				["torch", "transformers", "onnx"] )







 @classmethod
 def 							UpperCamelCase  (					cls		:  int			,				*snake_case__		:  Dict			,				**snake_case__		:  List[Any] ):



   '''simple docstring'''




   requires_backends(cls			,				["torch", "transformers", "onnx"] )






class 							UpperCAmelCase_							(    metaclass=A				):

 '''simple docstring'''
 lowercase_					:    Optional[int]				=							["torch", "transformers", "onnx"]







 def __init__(					self		:  str			,				*snake_case__		:  Optional[int]			,				**snake_case__		:  str ):



   '''simple docstring'''




   requires_backends(self			,				["torch", "transformers", "onnx"] )







 @classmethod
 def 							UpperCamelCase  (					cls		:  Any			,				*snake_case__		:  List[str]			,				**snake_case__		:  Dict ):



   '''simple docstring'''




   requires_backends(cls			,				["torch", "transformers", "onnx"] )







 @classmethod
 def 							UpperCamelCase  (					cls		:  str			,				*snake_case__		:  Dict			,				**snake_case__		:  Dict ):



   '''simple docstring'''




   requires_backends(cls			,				["torch", "transformers", "onnx"] )






class 							UpperCAmelCase_							(    metaclass=A				):

 '''simple docstring'''
 lowercase_					:    int				=							["torch", "transformers", "onnx"]







 def __init__(					self		:  Any			,				*snake_case__		:  List[str]			,				**snake_case__		:  str ):



   '''simple docstring'''




   requires_backends(self			,				["torch", "transformers", "onnx"] )







 @classmethod
 def 							UpperCamelCase  (					cls		:  List[str]			,				*snake_case__		:  List[str]			,				**snake_case__		:  Optional[Any] ):



   '''simple docstring'''




   requires_backends(cls			,				["torch", "transformers", "onnx"] )







 @classmethod
 def 							UpperCamelCase  (					cls		:  Optional[int]			,				*snake_case__		:  List[Any]			,				**snake_case__		:  int ):



   '''simple docstring'''




   requires_backends(cls			,				["torch", "transformers", "onnx"] )






class 							UpperCAmelCase_							(    metaclass=A				):

 '''simple docstring'''
 lowercase_					:    Dict				=							["torch", "transformers", "onnx"]







 def __init__(					self		:  Any			,				*snake_case__		:  Optional[int]			,				**snake_case__		:  Any ):



   '''simple docstring'''




   requires_backends(self			,				["torch", "transformers", "onnx"] )







 @classmethod
 def 							UpperCamelCase  (					cls		:  Union[str, Any]			,				*snake_case__		:  str			,				**snake_case__		:  Optional[Any] ):



   '''simple docstring'''




   requires_backends(cls			,				["torch", "transformers", "onnx"] )







 @classmethod
 def 							UpperCamelCase  (					cls		:  int			,				*snake_case__		:  int			,				**snake_case__		:  Tuple ):



   '''simple docstring'''




   requires_backends(cls			,				["torch", "transformers", "onnx"] )






class 							UpperCAmelCase_							(    metaclass=A				):

 '''simple docstring'''
 lowercase_					:    Dict				=							["torch", "transformers", "onnx"]







 def __init__(					self		:  List[str]			,				*snake_case__		:  Tuple			,				**snake_case__		:  int ):



   '''simple docstring'''




   requires_backends(self			,				["torch", "transformers", "onnx"] )







 @classmethod
 def 							UpperCamelCase  (					cls		:  Optional[int]			,				*snake_case__		:  Tuple			,				**snake_case__		:  Dict ):



   '''simple docstring'''




   requires_backends(cls			,				["torch", "transformers", "onnx"] )







 @classmethod
 def 							UpperCamelCase  (					cls		:  List[str]			,				*snake_case__		:  Dict			,				**snake_case__		:  List[Any] ):



   '''simple docstring'''




   requires_backends(cls			,				["torch", "transformers", "onnx"] )



