



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

__UpperCAmelCase	       =	logging.get_logger(__name__)



@add_end_docstrings(a__							)
class     lowerCAmelCase_		(  a__							):
       def __init__(      self,      *SCREAMING_SNAKE_CASE_,      **SCREAMING_SNAKE_CASE_	)					->		Union[str, Any]:
          super().__init__(*SCREAMING_SNAKE_CASE_,      **SCREAMING_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  snake_case_	(      self,      SCREAMING_SNAKE_CASE_=None,      SCREAMING_SNAKE_CASE_=None,      SCREAMING_SNAKE_CASE_=None	)					->		Optional[int]:
          UpperCamelCase   :  Tuple						     =     {}
          UpperCamelCase   :  Dict						     =     {}

          if prompt is not None:
             UpperCamelCase   :  Tuple						     =     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   :  int						     =     max_new_tokens
          return preprocess_params, forward_kwargs, {}
       def __call__(      self,      SCREAMING_SNAKE_CASE_,      **SCREAMING_SNAKE_CASE_	)					->		List[Any]:
          return super().__call__(SCREAMING_SNAKE_CASE_,      **SCREAMING_SNAKE_CASE_	)
       def  snake_case_	(      self,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_=None	)					->		int:
          UpperCamelCase   :  List[str]						     =     load_image(SCREAMING_SNAKE_CASE_	)

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

             UpperCamelCase   :  Dict						     =     self.model.config.model_type

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

             elif model_type == "pix2struct":
                UpperCamelCase   :  List[str]						     =     self.image_processor(images=SCREAMING_SNAKE_CASE_,      header_text=SCREAMING_SNAKE_CASE_,      return_tensors=self.framework	)

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

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

          else:
             UpperCamelCase   :  Tuple						     =     self.image_processor(images=SCREAMING_SNAKE_CASE_,      return_tensors=self.framework	)

          if self.model.config.model_type == "git" and prompt is None:
             UpperCamelCase   :  List[str]						     =     None

          return model_inputs
       def  snake_case_	(      self,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_=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'],      SCREAMING_SNAKE_CASE_	)
              and all(x is None for x in model_inputs['input_ids']	)
          ):
             UpperCamelCase   :  Tuple						     =     None

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






       def  snake_case_	(      self,      SCREAMING_SNAKE_CASE_	)					->		str:
          UpperCamelCase   :  Optional[Any]						     =     []
          for output_ids in model_outputs:
             UpperCamelCase   :  List[Any]						     =     {
                 'generated_text': self.tokenizer.decode(
                     SCREAMING_SNAKE_CASE_,      skip_special_tokens=SCREAMING_SNAKE_CASE_,      )
             }
             records.append(SCREAMING_SNAKE_CASE_	)
          return records








from typing import Optional

import pyspark

from .. import Features, NamedSplit
from ..download import DownloadMode
from ..packaged_modules.spark.spark import Spark
from .abc import AbstractDatasetReader



class     lowerCAmelCase_		(  a__							):
       def __init__(      self,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_ = None,      SCREAMING_SNAKE_CASE_ = None,      SCREAMING_SNAKE_CASE_ = True,      SCREAMING_SNAKE_CASE_ = None,      SCREAMING_SNAKE_CASE_ = False,      SCREAMING_SNAKE_CASE_ = None,      SCREAMING_SNAKE_CASE_ = True,      SCREAMING_SNAKE_CASE_ = "arrow",      **SCREAMING_SNAKE_CASE_,      )					->		int:
          super().__init__(
              split=SCREAMING_SNAKE_CASE_,      features=SCREAMING_SNAKE_CASE_,      cache_dir=SCREAMING_SNAKE_CASE_,      keep_in_memory=SCREAMING_SNAKE_CASE_,      streaming=SCREAMING_SNAKE_CASE_,      **SCREAMING_SNAKE_CASE_,      )
          UpperCamelCase   :  Any						     =     load_from_cache_file
          UpperCamelCase   :  Any						     =     file_format
          UpperCamelCase   :  str						     =     Spark(
              df=SCREAMING_SNAKE_CASE_,      features=SCREAMING_SNAKE_CASE_,      cache_dir=SCREAMING_SNAKE_CASE_,      working_dir=SCREAMING_SNAKE_CASE_,      **SCREAMING_SNAKE_CASE_,      )






       def  snake_case_	(      self	)					->		Tuple:
          if self.streaming:
             return self.builder.as_streaming_dataset(split=self.split	)
          UpperCamelCase   :  Dict						     =     None if self._load_from_cache_file else DownloadMode.FORCE_REDOWNLOAD
          self.builder.download_and_prepare(
              download_mode=SCREAMING_SNAKE_CASE_,      file_format=self._file_format,      )
          return self.builder.as_dataset(split=self.split	)



