





'''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 :  Union[str, Any]    							=				logging.get_logger(__name__)






@add_end_docstrings(__lowercase)
class        __snake_case	(       __lowercase):







    """simple docstring"""



    def __init__(       self   :  str   ,						*lowerCamelCase   :  Any   ,						**lowerCamelCase   :  Tuple		)       ->  Any:
      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 __lowercase       (       self   :  Tuple   ,						lowerCamelCase   :  List[str]=None   ,						lowerCamelCase   :  Union[str, Any]=None   ,						lowerCamelCase   :  Any=None		)       ->  Tuple:
      lowerCAmelCase_      :				Tuple      =       {}
      lowerCAmelCase_      :				Union[str, Any]      =       {}

      if prompt is not None:
        lowerCAmelCase_      :				List[Any]      =       prompt

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

    def __call__(       self   :  Tuple   ,						lowerCamelCase   :  Union[str, List[str], "Image.Image", List["Image.Image"]]   ,						**lowerCamelCase   :  List[str]		)       ->  List[Any]:
      return super().__call__(snake_case_   ,						**snake_case_		)

    def __lowercase       (       self   :  List[Any]   ,						lowerCamelCase   :  Any   ,						lowerCamelCase   :  List[str]=None		)       ->  int:
      lowerCAmelCase_      :				Optional[Any]      =       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."""		)

        lowerCAmelCase_      :				Optional[int]      =       self.model.config.model_type

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

        elif model_type == "pix2struct":
          lowerCAmelCase_      :				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
          lowerCAmelCase_      :				Union[str, Any]      =       self.image_processor(images=snake_case_   ,						return_tensors=self.framework		)
          lowerCAmelCase_      :				str      =       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:
        lowerCAmelCase_      :				Dict      =       self.image_processor(images=snake_case_   ,						return_tensors=self.framework		)

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

      return model_inputs

    def __lowercase       (       self   :  str   ,						lowerCamelCase   :  Optional[Any]   ,						lowerCamelCase   :  int=None		)       ->  Dict:
      # 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"""]   ,						snake_case_		)
          and all(x is None for x in model_inputs["""input_ids"""]		)
      ):
        lowerCAmelCase_      :				str      =       None

      if generate_kwargs is None:
        lowerCAmelCase_      :				List[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.
      lowerCAmelCase_      :				str      =       model_inputs.pop(self.model.main_input_name		)
      lowerCAmelCase_      :				Dict      =       self.model.generate(snake_case_   ,						**snake_case_   ,						**snake_case_		)
      return model_outputs





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







'''simple docstring'''






import datetime
import platform
import subprocess
from typing import Optional, Tuple, Union

import numpy as np







def     UpperCamelCase_      (   A__ :       bytes							,   A__ :       int ):
  '''simple docstring'''





  lowerCAmelCase_      :				int      =       f'{sampling_rate}'
  lowerCAmelCase_      :				str      =       """1"""
  lowerCAmelCase_      :				Optional[int]      =       """f32le"""
  lowerCAmelCase_      :				Any      =       [
      """ffmpeg""",
      """-i""",
      """pipe:0""",
      """-ac""",
      ac,
      """-ar""",
      ar,
      """-f""",
      format_for_conversion,
      """-hide_banner""",
      """-loglevel""",
      """quiet""",
      """pipe:1""",
  ]

  try:
    with subprocess.Popen(A__							,   stdin=subprocess.PIPE							,   stdout=subprocess.PIPE ) as ffmpeg_process:
      lowerCAmelCase_      :				Optional[int]      =       ffmpeg_process.communicate(A__ )
  except FileNotFoundError as error:
    raise ValueError("""ffmpeg was not found but is required to load audio files from filename""" ) from error
  lowerCAmelCase_      :				Optional[Any]      =       output_stream[0]
  lowerCAmelCase_      :				Optional[int]      =       np.frombuffer(A__							,   np.floataa )
  if audio.shape[0] == 0:
    raise ValueError("""Malformed soundfile""" )
  return audio







def     UpperCamelCase_      (   A__ :       int							,   A__ :       float							,   A__ :       str = "f32le"							,   ):
  '''simple docstring'''





  lowerCAmelCase_      :				int      =       f'{sampling_rate}'
  lowerCAmelCase_      :				Any      =       """1"""
  if format_for_conversion == "s16le":
    lowerCAmelCase_      :				Optional[Any]      =       2
  elif format_for_conversion == "f32le":
    lowerCAmelCase_      :				Union[str, Any]      =       4
  else:
    raise ValueError(f'Unhandled format `{format_for_conversion}`. Please use `s16le` or `f32le`' )

  lowerCAmelCase_      :				int      =       platform.system()
  if system == "Linux":
    lowerCAmelCase_      :				int      =       """alsa"""
    lowerCAmelCase_      :				int      =       """default"""
  elif system == "Darwin":
    lowerCAmelCase_      :				List[str]      =       """avfoundation"""
    lowerCAmelCase_      :				Union[str, Any]      =       """:0"""
  elif system == "Windows":
    lowerCAmelCase_      :				List[Any]      =       """dshow"""
    lowerCAmelCase_      :				Union[str, Any]      =       """default"""

  lowerCAmelCase_      :				Tuple      =       [
      """ffmpeg""",
      """-f""",
      format_,
      """-i""",
      input_,
      """-ac""",
      ac,
      """-ar""",
      ar,
      """-f""",
      format_for_conversion,
      """-fflags""",
      """nobuffer""",
      """-hide_banner""",
      """-loglevel""",
      """quiet""",
      """pipe:1""",
  ]
  lowerCAmelCase_      :				Tuple      =       int(round(sampling_rate * chunk_length_s ) ) * size_of_sample
  lowerCAmelCase_      :				List[str]      =       _ffmpeg_stream(A__							,   A__ )
  for item in iterator:
    yield item







def     UpperCamelCase_      (   A__ :       int							,   A__ :       float							,   A__ :       Optional[int] = None							,   A__ :       Optional[Union[Tuple[float, float], float]] = None							,   A__ :       str = "f32le"							,   ):
  '''simple docstring'''





  if stream_chunk_s is not None:
    lowerCAmelCase_      :				Union[str, Any]      =       stream_chunk_s
  else:
    lowerCAmelCase_      :				Tuple      =       chunk_length_s

  lowerCAmelCase_      :				List[Any]      =       ffmpeg_microphone(A__							,   A__							,   format_for_conversion=A__ )
  if format_for_conversion == "s16le":
    lowerCAmelCase_      :				Tuple      =       np.intaa
    lowerCAmelCase_      :				List[Any]      =       2
  elif format_for_conversion == "f32le":
    lowerCAmelCase_      :				Dict      =       np.floataa
    lowerCAmelCase_      :				int      =       4
  else:
    raise ValueError(f'Unhandled format `{format_for_conversion}`. Please use `s16le` or `f32le`' )

  if stride_length_s is None:
    lowerCAmelCase_      :				Optional[Any]      =       chunk_length_s / 6
  lowerCAmelCase_      :				List[str]      =       int(round(sampling_rate * chunk_length_s ) ) * size_of_sample
  if isinstance(A__							,   (int, float) ):
    lowerCAmelCase_      :				int      =       [stride_length_s, stride_length_s]

  lowerCAmelCase_      :				str      =       int(round(sampling_rate * stride_length_s[0] ) ) * size_of_sample
  lowerCAmelCase_      :				List[str]      =       int(round(sampling_rate * stride_length_s[1] ) ) * size_of_sample
  lowerCAmelCase_      :				Dict      =       datetime.datetime.now()
  lowerCAmelCase_      :				Any      =       datetime.timedelta(seconds=A__ )
  for item in chunk_bytes_iter(A__							,   A__							,   stride=(stride_left, stride_right)							,   stream=A__ ):
    # Put everything back in numpy scale
    lowerCAmelCase_      :				Optional[int]      =       np.frombuffer(item["""raw"""]							,   dtype=A__ )
    lowerCAmelCase_      :				Dict      =       (
        item["""stride"""][0] // size_of_sample,
        item["""stride"""][1] // size_of_sample,
    )
    lowerCAmelCase_      :				Dict      =       sampling_rate
    audio_time += delta
    if datetime.datetime.now() > audio_time + 10 * delta:
      # We're late !! SKIP
      continue
    yield item







def     UpperCamelCase_      (   A__ :       Any							,   A__ :       int							,   A__ :       Tuple[int, int]							,   A__ :       bool = False ):
  '''simple docstring'''





  lowerCAmelCase_      :				List[str]      =       B""""""
  lowerCAmelCase_,							lowerCAmelCase_      :				Any      =       stride
  if stride_left + stride_right >= chunk_len:
    raise ValueError(
        f'Stride needs to be strictly smaller than chunk_len: ({stride_left}, {stride_right}) vs {chunk_len}' )
  lowerCAmelCase_      :				Union[str, Any]      =       0
  for raw in iterator:
    acc += raw
    if stream and len(A__ ) < chunk_len:
      lowerCAmelCase_      :				Dict      =       (_stride_left, 0)
      yield {"raw": acc[:chunk_len], "stride": stride, "partial": True}
    else:
      while len(A__ ) >= chunk_len:
        # We are flushing the accumulator
        lowerCAmelCase_      :				Optional[Any]      =       (_stride_left, stride_right)
        lowerCAmelCase_      :				List[str]      =       {"""raw""": acc[:chunk_len], """stride""": stride}
        if stream:
          lowerCAmelCase_      :				List[Any]      =       False
        yield item
        lowerCAmelCase_      :				str      =       stride_left
        lowerCAmelCase_      :				str      =       acc[chunk_len - stride_left - stride_right :]
    # Last chunk
  if len(A__ ) > stride_left:
    lowerCAmelCase_      :				Tuple      =       {"""raw""": acc, """stride""": (_stride_left, 0)}
    if stream:
      lowerCAmelCase_      :				Optional[Any]      =       False
    yield item







def     UpperCamelCase_      (   A__ :       List[str]							,   A__ :       int ):
  '''simple docstring'''





  lowerCAmelCase_      :				Dict      =       2**24  # 16Mo
  try:
    with subprocess.Popen(A__							,   stdout=subprocess.PIPE							,   bufsize=A__ ) as ffmpeg_process:
      while True:
        lowerCAmelCase_      :				Union[str, Any]      =       ffmpeg_process.stdout.read(A__ )
        if raw == b"":
          break
        yield raw
  except FileNotFoundError as error:
    raise ValueError("""ffmpeg was not found but is required to stream audio files from filename""" ) from error



