



'''simple docstring'''






import copy
from typing import Any, Dict, List, Optional, Union

import numpy as np
import torch

from ...audio_utils import mel_filter_bank, spectrogram, window_function
from ...feature_extraction_sequence_utils import SequenceFeatureExtractor
from ...feature_extraction_utils import BatchFeature
from ...utils import TensorType, logging


lowerCAmelCase:   int					=		logging.get_logger(__name__)

class 				a__(   lowerCamelCase__      ):
    lowercase__					=       ["""input_features""", """is_longer"""]







    def __init__( self					:    str		,     __snake_case					:    Optional[int]=64		,     __snake_case					:    List[Any]=4_80_00		,     __snake_case					:    Any=4_80		,     __snake_case					:    Optional[int]=10		,     __snake_case					:    Any=10_24		,     __snake_case					:    Dict=0.0		,     __snake_case					:    List[Any]=False		,     __snake_case					:    float = 0		,     __snake_case					:    float = 1_40_00		,     __snake_case					:    int = None		,     __snake_case					:    str = "fusion"		,     __snake_case					:    str = "repeatpad"		,     **__snake_case					:    List[Any]		,     ):
      super().__init__(
          feature_size=__snake_case		,     sampling_rate=__snake_case		,     padding_value=__snake_case		,     return_attention_mask=__snake_case		,     **__snake_case		,     )
      a		:   List[Any]	  =	top_db
      a		:   str	  =	truncation
      a		:   List[Any]	  =	padding
      a		:   Optional[Any]	  =	fft_window_size
      a		:   Union[str, Any]	  =	(fft_window_size >> 1) + 1
      a		:   Optional[Any]	  =	hop_length
      a		:   str	  =	max_length_s
      a		:   Tuple	  =	max_length_s * sampling_rate
      a		:   Dict	  =	sampling_rate
      a		:   Dict	  =	frequency_min
      a		:   int	  =	frequency_max
      a		:   Union[str, Any]	  =	mel_filter_bank(
          num_frequency_bins=self.nb_frequency_bins		,     num_mel_filters=__snake_case		,     min_frequency=__snake_case		,     max_frequency=__snake_case		,     sampling_rate=__snake_case		,     norm=__snake_case		,     mel_scale='htk'		,     )
      a		:   List[str]	  =	mel_filter_bank(
          num_frequency_bins=self.nb_frequency_bins		,     num_mel_filters=__snake_case		,     min_frequency=__snake_case		,     max_frequency=__snake_case		,     sampling_rate=__snake_case		,     norm='slaney'		,     mel_scale='slaney'		,     )







    def      lowercase_			( self					:    List[Any]      ):
      a		:   Dict	  =	copy.deepcopy(self.__dict__      )
      a		:   str	  =	self.__class__.__name__
      if "mel_filters" in output:
        del output["mel_filters"]
      if "mel_filters_slaney" in output:
        del output["mel_filters_slaney"]
      return output







    def      lowercase_			( self					:    Union[str, Any]		,     __snake_case					:    np.array		,     __snake_case					:    Optional[np.array] = None      ):
      a		:   Optional[int]	  =	spectrogram(
          __snake_case		,     window_function(self.fft_window_size		,     'hann'      )		,     frame_length=self.fft_window_size		,     hop_length=self.hop_length		,     power=2.0		,     mel_filters=__snake_case		,     log_mel='dB'		,     )
      return log_mel_spectrogram.T







    def      lowercase_			( self					:    str		,     __snake_case					:    Tuple		,     __snake_case					:    Union[str, Any]		,     __snake_case					:    Optional[int]      ):
      a		:   Any	  =	np.array_split(list(range(0		,     total_frames - chunk_frames + 1      )      )		,     3      )
      if len(ranges[1]      ) == 0:
        # if the audio is too short, we just use the first chunk
        a		:   Tuple	  =	[0]
      if len(ranges[2]      ) == 0:
        # if the audio is too short, we just use the first chunk
        a		:   str	  =	[0]
      # randomly choose index for each part
      a		:   str	  =	np.random.choice(ranges[0]      )
      a		:   Union[str, Any]	  =	np.random.choice(ranges[1]      )
      a		:   int	  =	np.random.choice(ranges[2]      )

      a		:   List[Any]	  =	mel[idx_front : idx_front + chunk_frames, :]
      a		:   List[Any]	  =	mel[idx_middle : idx_middle + chunk_frames, :]
      a		:   List[Any]	  =	mel[idx_back : idx_back + chunk_frames, :]

      a		:   Dict	  =	torch.tensor(mel[None, None, :]      )
      a		:   Tuple	  =	torch.nn.functional.interpolate(
          __snake_case		,     size=[chunk_frames, 64]		,     mode='bilinear'		,     align_corners=__snake_case      )
      a		:   str	  =	mel_shrink[0][0].numpy()
      a		:   Optional[int]	  =	np.stack([mel_shrink, mel_chunk_front, mel_chunk_middle, mel_chunk_back]		,     axis=0      )
      return mel_fusion







    def      lowercase_			( self					:    Any		,     __snake_case					:    np.array		,     __snake_case					:    Dict		,     __snake_case					:    int		,     __snake_case					:    List[Any]      ):
      if waveform.shape[0] > max_length:
        if truncation == "rand_trunc":
          a		:   List[str]	  =	True
          # random crop to max_length (for compatibility) -> this should be handled by self.pad
          a		:   Tuple	  =	len(__snake_case      ) - max_length
          a		:   List[str]	  =	np.random.randint(0		,     overflow + 1      )
          a		:   Optional[int]	  =	waveform[idx : idx + max_length]
          a		:   Optional[int]	  =	self._np_extract_fbank_features(__snake_case		,     self.mel_filters_slaney      )[None, :]
        elif truncation == "fusion":
          a		:   Tuple	  =	self._np_extract_fbank_features(__snake_case		,     self.mel_filters      )
          a		:   Union[str, Any]	  =	max_length // self.hop_length + 1  # the +1 related to how the spectrogram is computed
          a		:   List[Any]	  =	mel.shape[0]
          if chunk_frames == total_frames:
            # there is a corner case where the audio length is larger than max_length but smaller than max_length+hop_length.
            # In this case, we just use the whole audio.
            a		:   List[str]	  =	np.stack([mel, mel, mel, mel]		,     axis=0      )
            a		:   Tuple	  =	False
          else:
            a		:   Tuple	  =	self._random_mel_fusion(__snake_case		,     __snake_case		,     __snake_case      )
            a		:   Any	  =	True
        else:
          raise NotImplementedError(F"""data_truncating {truncation} not implemented"""      )

      else:
        a		:   List[str]	  =	False
        # only use repeat as a new possible value for padding. you repeat the audio before applying the usual max_length padding
        if waveform.shape[0] < max_length:
          if padding == "repeat":
            a		:   List[Any]	  =	int(max_length / len(__snake_case      )      )
            a		:   List[Any]	  =	np.stack(np.tile(__snake_case		,     n_repeat + 1      )      )[:max_length]
          if padding == "repeatpad":
            a		:   Union[str, Any]	  =	int(max_length / len(__snake_case      )      )
            a		:   Dict	  =	np.stack(np.tile(__snake_case		,     __snake_case      )      )
          a		:   Optional[int]	  =	np.pad(__snake_case		,     (0, max_length - waveform.shape[0])		,     mode='constant'		,     constant_values=0      )

        if truncation == "fusion":
          a		:   List[Any]	  =	self._np_extract_fbank_features(__snake_case		,     self.mel_filters      )
          a		:   List[Any]	  =	np.stack([input_mel, input_mel, input_mel, input_mel]		,     axis=0      )
        else:
          a		:   Any	  =	self._np_extract_fbank_features(__snake_case		,     self.mel_filters_slaney      )[None, :]

      return input_mel, longer







    def __call__( self					:    Dict		,     __snake_case					:    Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]		,     __snake_case					:    str = None		,     __snake_case					:    Optional[str] = None		,     __snake_case					:    Optional[int] = None		,     __snake_case					:    Optional[int] = None		,     __snake_case					:    Optional[Union[str, TensorType]] = None		,     **__snake_case					:    Union[str, Any]		,     ):
      a		:   Dict	  =	truncation if truncation is not None else self.truncation
      a		:   List[str]	  =	padding if padding else self.padding

      if sampling_rate is not None:
        if sampling_rate != self.sampling_rate:
          raise ValueError(
              F"""The model corresponding to this feature extractor: {self.__class__.__name__} was trained using a"""
              F""" sampling rate of {self.sampling_rate}. Please make sure that the provided `raw_speech` input"""
              F""" was sampled with {self.sampling_rate} and not {sampling_rate}."""      )
      else:
        logger.warning(
            'It is strongly recommended to pass the `sampling_rate` argument to this function. '
            'Failing to do so can result in silent errors that might be hard to debug.'      )

      a		:   Dict	  =	isinstance(__snake_case		,     np.ndarray      ) and len(raw_speech.shape      ) > 1
      if is_batched_numpy and len(raw_speech.shape      ) > 2:
        raise ValueError(F"""Only mono-channel audio is supported for input to {self}"""      )
      a		:   int	  =	is_batched_numpy or (
          isinstance(__snake_case		,     (list, tuple)      ) and (isinstance(raw_speech[0]		,     (np.ndarray, tuple, list)      ))
      )

      if is_batched:
        a		:   Optional[Any]	  =	[np.asarray(__snake_case		,     dtype=np.floataa      ) for speech in raw_speech]
      elif not is_batched and not isinstance(__snake_case		,     np.ndarray      ):
        a		:   Tuple	  =	np.asarray(__snake_case		,     dtype=np.floataa      )
      elif isinstance(__snake_case		,     np.ndarray      ) and raw_speech.dtype is np.dtype(np.floataa      ):
        a		:   List[Any]	  =	raw_speech.astype(np.floataa      )

      # always return batch
      if not is_batched:
        a		:   Optional[int]	  =	[np.asarray(__snake_case      )]

      # convert to mel spectrogram, truncate and pad if needed.
      a		:   int	  =	[
          self._get_input_mel(__snake_case		,     max_length if max_length else self.nb_max_samples		,     __snake_case		,     __snake_case      )
          for waveform in raw_speech
      ]

      a		:   Optional[Any]	  =	[]
      a		:   Union[str, Any]	  =	[]
      for mel, longer in padded_inputs:
        input_mel.append(__snake_case      )
        is_longer.append(__snake_case      )

      if truncation == "fusion" and sum(__snake_case      ) == 0:
        # if no audio is longer than 10s, then randomly select one audio to be longer
        a		:   Optional[int]	  =	np.random.randint(0		,     len(__snake_case      )      )
        a		:   List[str]	  =	True

      if isinstance(input_mel[0]		,     __snake_case      ):
        a		:   List[str]	  =	[np.asarray(__snake_case		,     dtype=np.floataa      ) for feature in input_mel]

      # is_longer is a list of bool
      a		:   Optional[int]	  =	[[longer] for longer in is_longer]

      a		:   Union[str, Any]	  =	{'input_features': input_mel, 'is_longer': is_longer}
      a		:   Tuple	  =	BatchFeature(__snake_case      )

      if return_tensors is not None:
        a		:   Tuple	  =	input_features.convert_to_tensors(__snake_case      )

      return input_features




'''simple docstring'''






# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import re

from ..models.auto import AutoProcessor
from ..models.vision_encoder_decoder import VisionEncoderDecoderModel
from ..utils import is_vision_available
from .base import PipelineTool


if is_vision_available():
     from PIL import Image

class 				a__(   lowerCamelCase__      ):
    lowercase__					=       """naver-clova-ix/donut-base-finetuned-docvqa"""
    lowercase__					=       (
        """This is a tool that answers a question about an document (pdf). It takes an input named `document` which """
        """should be the document containing the information, as well as a `question` that is the question about the """
        """document. It returns a text that contains the answer to the question."""
    )
    lowercase__					=       """document_qa"""
    lowercase__					=       AutoProcessor
    lowercase__					=       VisionEncoderDecoderModel

    lowercase__					=       ["""image""", """text"""]
    lowercase__					=       ["""text"""]







    def __init__( self					:    str		,     *__snake_case					:    Any		,     **__snake_case					:    List[Any]      ):
      if not is_vision_available():
        raise ValueError('Pillow must be installed to use the DocumentQuestionAnsweringTool.'      )

      super().__init__(*__snake_case		,     **__snake_case      )







    def      lowercase_			( self					:    Union[str, Any]		,     __snake_case					:    "Image"		,     __snake_case					:    str      ):
      a		:   Union[str, Any]	  =	'<s_docvqa><s_question>{user_input}</s_question><s_answer>'
      a		:   Any	  =	task_prompt.replace('{user_input}'		,     __snake_case      )
      a		:   Dict	  =	self.pre_processor.tokenizer(
          __snake_case		,     add_special_tokens=__snake_case		,     return_tensors='pt'      ).input_ids
      a		:   Tuple	  =	self.pre_processor(__snake_case		,     return_tensors='pt'      ).pixel_values

      return {"decoder_input_ids": decoder_input_ids, "pixel_values": pixel_values}







    def      lowercase_			( self					:    Any		,     __snake_case					:    int      ):
      return self.model.generate(
          inputs['pixel_values'].to(self.device      )		,     decoder_input_ids=inputs['decoder_input_ids'].to(self.device      )		,     max_length=self.model.decoder.config.max_position_embeddings		,     early_stopping=__snake_case		,     pad_token_id=self.pre_processor.tokenizer.pad_token_id		,     eos_token_id=self.pre_processor.tokenizer.eos_token_id		,     use_cache=__snake_case		,     num_beams=1		,     bad_words_ids=[[self.pre_processor.tokenizer.unk_token_id]]		,     return_dict_in_generate=__snake_case		,     ).sequences







    def      lowercase_			( self					:    List[str]		,     __snake_case					:    Tuple      ):
      a		:   str	  =	self.pre_processor.batch_decode(__snake_case      )[0]
      a		:   str	  =	sequence.replace(self.pre_processor.tokenizer.eos_token		,     ''      )
      a		:   Union[str, Any]	  =	sequence.replace(self.pre_processor.tokenizer.pad_token		,     ''      )
      a		:   List[str]	  =	re.sub(r'<.*?>'		,     ''		,     __snake_case		,     count=1      ).strip()  # remove first task start token
      a		:   Dict	  =	self.pre_processor.tokenajson(__snake_case      )

      return sequence["answer"]