




import os
from typing import List, Union

import tensorflow as tf
from tensorflow_text import BertTokenizer as BertTokenizerLayer
from tensorflow_text import FastBertTokenizer, ShrinkLongestTrimmer, case_fold_utfa, combine_segments, pad_model_inputs

from .tokenization_bert import BertTokenizer
class       _UpperCamelCase   (					tf.keras.layers.Layer		):
      def __init__(      self :int			,   lowerCamelCase :List			,   lowerCamelCase :bool			,   lowerCamelCase :int = None			,   lowerCamelCase :int = None			,   lowerCamelCase :int = None			,   lowerCamelCase :str = "longest"			,   lowerCamelCase :bool = True			,   lowerCamelCase :int = 512			,   lowerCamelCase :int = None			,   lowerCamelCase :bool = True			,   lowerCamelCase :bool = True			,   lowerCamelCase :bool = True			,   **lowerCamelCase :Optional[Any]			,   )  ->      List[str]:
       super().__init__()
       if use_fast_bert_tokenizer:
        UpperCAmelCase__							=    FastBertTokenizer(
            lowerCamelCase			,   token_out_type=tf.intaa			,   lower_case_nfd_strip_accents=lowerCamelCase			,   **lowerCamelCase   )
       else:
        UpperCAmelCase__							=    tf.lookup.StaticVocabularyTable(
            tf.lookup.KeyValueTensorInitializer(
                keys=lowerCamelCase			,   key_dtype=tf.string			,   values=tf.range(tf.size(lowerCamelCase			,   out_type=tf.intaa   )			,   dtype=tf.intaa   )			,   value_dtype=tf.intaa			,   )			,   num_oov_buckets=1			,   )
        UpperCAmelCase__							=    BertTokenizerLayer(
            lowerCamelCase			,   token_out_type=tf.intaa			,   lower_case=lowerCamelCase			,   **lowerCamelCase   )

       UpperCAmelCase__							=    vocab_list
       UpperCAmelCase__							=    do_lower_case
       UpperCAmelCase__							=    cls_token_id or vocab_list.index("[CLS]"   )
       UpperCAmelCase__							=    sep_token_id or vocab_list.index("[SEP]"   )
       UpperCAmelCase__							=    pad_token_id or vocab_list.index("[PAD]"   )
       UpperCAmelCase__							=    ShrinkLongestTrimmer(max_length - 3			,   axis=1   )  # Allow room for special tokens
       UpperCAmelCase__							=    max_length
       UpperCAmelCase__							=    padding
       UpperCAmelCase__							=    truncation
       UpperCAmelCase__							=    pad_to_multiple_of
       UpperCAmelCase__							=    return_token_type_ids
       UpperCAmelCase__							=    return_attention_mask
      @classmethod
      def 						UpperCAmelCase_    (      cls :List[str]			,   lowerCamelCase :"PreTrainedTokenizerBase"			,   **lowerCamelCase :Dict   )  ->      Dict:  # noqa: F821
       UpperCAmelCase__							=    kwargs.pop("do_lower_case"			,   lowerCamelCase   )
       UpperCAmelCase__							=    tokenizer.do_lower_case if do_lower_case is None else do_lower_case
       UpperCAmelCase__							=    kwargs.pop("cls_token_id"			,   lowerCamelCase   )
       UpperCAmelCase__							=    tokenizer.cls_token_id if cls_token_id is None else cls_token_id
       UpperCAmelCase__							=    kwargs.pop("sep_token_id"			,   lowerCamelCase   )
       UpperCAmelCase__							=    tokenizer.sep_token_id if sep_token_id is None else sep_token_id
       UpperCAmelCase__							=    kwargs.pop("pad_token_id"			,   lowerCamelCase   )
       UpperCAmelCase__							=    tokenizer.pad_token_id if pad_token_id is None else pad_token_id

       UpperCAmelCase__							=    tokenizer.get_vocab()
       UpperCAmelCase__							=    sorted([(wordpiece, idx) for wordpiece, idx in vocab.items()]			,   key=lambda lowerCamelCase   : x[1]   )
       UpperCAmelCase__							=    [entry[0] for entry in vocab]
       return cls(
           vocab_list=lowerCamelCase			,   do_lower_case=lowerCamelCase			,   cls_token_id=lowerCamelCase			,   sep_token_id=lowerCamelCase			,   pad_token_id=lowerCamelCase			,   **lowerCamelCase			,   )
      @classmethod
      def 						UpperCAmelCase_    (      cls :Any			,   lowerCamelCase :Union[str, os.PathLike]			,   *lowerCamelCase :str			,   **lowerCamelCase :List[str]   )  ->      Tuple:
       try:
        UpperCAmelCase__							=    BertTokenizer.from_pretrained(lowerCamelCase			,   *lowerCamelCase			,   **lowerCamelCase   )
       except:  # noqa: E722
        from .tokenization_bert_fast import BertTokenizerFast

        UpperCAmelCase__							=    BertTokenizerFast.from_pretrained(lowerCamelCase			,   *lowerCamelCase			,   **lowerCamelCase   )
       return cls.from_tokenizer(lowerCamelCase			,   **lowerCamelCase   )
      def 						UpperCAmelCase_    (      self :int			,   lowerCamelCase :Optional[int]   )  ->      List[str]:
       if self.do_lower_case:
        UpperCAmelCase__							=    case_fold_utfa(lowerCamelCase   )
       UpperCAmelCase__							=    self.tf_tokenizer.tokenize(lowerCamelCase   )
       return tokens.merge_dims(1			,   -1   )
      def 						UpperCAmelCase_    (      self :Optional[Any]			,   lowerCamelCase :Tuple			,   lowerCamelCase :str=None			,   lowerCamelCase :Tuple=None			,   lowerCamelCase :Any=None			,   lowerCamelCase :Optional[int]=None			,   lowerCamelCase :Optional[Any]=None			,   lowerCamelCase :Optional[Any]=None			,   lowerCamelCase :Tuple=None			,   )  ->      str:
       if padding is None:
        UpperCAmelCase__							=    self.padding
       if padding not in ("longest", "max_length"):
        raise ValueError("Padding must be either 'longest' or 'max_length'!"   )
       if max_length is not None and text_pair is not None:
        # Because we have to instantiate a Trimmer to do it properly
        raise ValueError("max_length cannot be overridden at call time when truncating paired texts!"   )
       if max_length is None:
        UpperCAmelCase__							=    self.max_length
       if truncation is None:
        UpperCAmelCase__							=    self.truncation
       if pad_to_multiple_of is None:
        UpperCAmelCase__							=    self.pad_to_multiple_of
       if return_token_type_ids is None:
        UpperCAmelCase__							=    self.return_token_type_ids
       if return_attention_mask is None:
        UpperCAmelCase__							=    self.return_attention_mask
       if not isinstance(lowerCamelCase			,   tf.Tensor   ):
        UpperCAmelCase__							=    tf.convert_to_tensor(lowerCamelCase   )
       if text_pair is not None and not isinstance(lowerCamelCase			,   tf.Tensor   ):
        UpperCAmelCase__							=    tf.convert_to_tensor(lowerCamelCase   )
       if text_pair is not None:
        if text.shape.rank > 1:
         raise ValueError("text argument should not be multidimensional when a text pair is supplied!"   )
        if text_pair.shape.rank > 1:
         raise ValueError("text_pair should not be multidimensional!"   )
       if text.shape.rank == 2:
        UpperCAmelCase__			,			UpperCAmelCase__							=    text[:, 0], text[:, 1]
       UpperCAmelCase__							=    self.unpaired_tokenize(lowerCamelCase   )
       if text_pair is None:  # Unpaired text
        if truncation:
         UpperCAmelCase__							=    text[:, : max_length - 2]  # Allow room for special tokens
        UpperCAmelCase__			,			UpperCAmelCase__							=    combine_segments(
            (text,)			,   start_of_sequence_id=self.cls_token_id			,   end_of_segment_id=self.sep_token_id   )
       else:  # Paired text
        UpperCAmelCase__							=    self.unpaired_tokenize(lowerCamelCase   )
        if truncation:
         UpperCAmelCase__			,			UpperCAmelCase__							=    self.paired_trimmer.trim([text, text_pair]   )
        UpperCAmelCase__			,			UpperCAmelCase__							=    combine_segments(
            (text, text_pair)			,   start_of_sequence_id=self.cls_token_id			,   end_of_segment_id=self.sep_token_id   )
       if padding == "longest":
        UpperCAmelCase__							=    input_ids.bounding_shape(axis=1   )
        if pad_to_multiple_of is not None:
         # No ceiling division in tensorflow, so we negate floordiv instead
         UpperCAmelCase__							=    pad_to_multiple_of * (-tf.math.floordiv(-pad_length			,   lowerCamelCase   ))
       else:
        UpperCAmelCase__							=    max_length

       UpperCAmelCase__			,			UpperCAmelCase__							=    pad_model_inputs(lowerCamelCase			,   max_seq_length=lowerCamelCase			,   pad_value=self.pad_token_id   )
       UpperCAmelCase__							=    {"input_ids": input_ids}
       if return_attention_mask:
        UpperCAmelCase__							=    attention_mask
       if return_token_type_ids:
        UpperCAmelCase__			,			UpperCAmelCase__							=    pad_model_inputs(
            lowerCamelCase			,   max_seq_length=lowerCamelCase			,   pad_value=self.pad_token_id   )
        UpperCAmelCase__							=    token_type_ids
       return output







      def 						UpperCAmelCase_    (      self :Tuple   )  ->      Tuple:
       return {
           "vocab_list": self.vocab_list,
           "do_lower_case": self.do_lower_case,
           "cls_token_id": self.cls_token_id,
           "sep_token_id": self.sep_token_id,
           "pad_token_id": self.pad_token_id,
       }











import subprocess
from typing import Union

import numpy as np
import requests

from ..utils import add_end_docstrings, is_torch_available, is_torchaudio_available, logging
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_torch_available():
  from ..models.auto.modeling_auto import MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING

_lowerCAmelCase    : List[str]		= logging.get_logger(__name__)



def     lowerCAmelCase		(		_lowerCAmelCase				: bytes    ,				_lowerCAmelCase				: int						):




 """simple docstring"""







 UpperCAmelCase__							=    F'''{sampling_rate}'''
 UpperCAmelCase__							=    "1"
 UpperCAmelCase__							=    "f32le"
 UpperCAmelCase__							=    [
     "ffmpeg",
     "-i",
     "pipe:0",
     "-ac",
     ac,
     "-ar",
     ar,
     "-f",
     format_for_conversion,
     "-hide_banner",
     "-loglevel",
     "quiet",
     "pipe:1",
 ]

 try:
  UpperCAmelCase__							=    subprocess.Popen(_lowerCAmelCase    ,				stdin=subprocess.PIPE    ,				stdout=subprocess.PIPE						)
 except FileNotFoundError:
  raise ValueError("ffmpeg was not found but is required to load audio files from filename"						)
 UpperCAmelCase__							=    ffmpeg_process.communicate(_lowerCAmelCase						)
 UpperCAmelCase__							=    output_stream[0]

 UpperCAmelCase__							=    np.frombuffer(_lowerCAmelCase    ,				np.floataa						)
 if audio.shape[0] == 0:
  raise ValueError("Malformed soundfile"						)
 return audio


@add_end_docstrings(lowerCAmelCase		)
class       _UpperCamelCase   (					lowerCAmelCase		):
      def __init__(      self :Any			,   *lowerCamelCase :Any			,   **lowerCamelCase :Optional[Any]   )  ->      Optional[int]:
       # Default, might be overriden by the model.config.
       UpperCAmelCase__							=    5
       super().__init__(*lowerCamelCase			,   **lowerCamelCase   )

       if self.framework != "pt":
        raise ValueError(f'''The {self.__class__} is only available in PyTorch.'''   )

       self.check_model_type(lowerCamelCase   )
      def __call__(      self :Optional[Any]			,   lowerCamelCase :Union[np.ndarray, bytes, str]			,   **lowerCamelCase :List[Any]			,   )  ->      Tuple:
       return super().__call__(lowerCamelCase			,   **lowerCamelCase   )
      def 						UpperCAmelCase_    (      self :str			,   lowerCamelCase :Dict=None			,   **lowerCamelCase :str   )  ->      int:
       # No parameters on this pipeline right now
       UpperCAmelCase__							=    {}
       if top_k is not None:
        if top_k > self.model.config.num_labels:
         UpperCAmelCase__							=    self.model.config.num_labels
        UpperCAmelCase__							=    top_k
       return {}, {}, postprocess_params
      def 						UpperCAmelCase_    (      self :Optional[Any]			,   lowerCamelCase :Dict   )  ->      Optional[int]:
       if isinstance(lowerCamelCase			,   lowerCamelCase   ):
        if inputs.startswith("http://"   ) or inputs.startswith("https://"   ):
         # We need to actually check for a real protocol, otherwise it's impossible to use a local file
         # like http_huggingface_co.png
         UpperCAmelCase__							=    requests.get(lowerCamelCase   ).content
        else:
         with open(lowerCamelCase			,   "rb"   ) as f:
          UpperCAmelCase__							=    f.read()

       if isinstance(lowerCamelCase			,   lowerCamelCase   ):
        UpperCAmelCase__							=    ffmpeg_read(lowerCamelCase			,   self.feature_extractor.sampling_rate   )

       if isinstance(lowerCamelCase			,   lowerCamelCase   ):
        # Accepting `"array"` which is the key defined in `datasets` for
        # better integration
        if not ("sampling_rate" in inputs and ("raw" in inputs or "array" in inputs)):
         raise ValueError(
             "When passing a dictionary to AudioClassificationPipeline, the dict needs to contain a "
             "\"raw\" key containing the numpy array representing the audio and a \"sampling_rate\" key, "
             "containing the sampling_rate associated with that array"   )

        UpperCAmelCase__							=    inputs.pop("raw"			,   lowerCamelCase   )
        if _inputs is None:
         # Remove path which will not be used from `datasets`.
         inputs.pop("path"			,   lowerCamelCase   )
         UpperCAmelCase__							=    inputs.pop("array"			,   lowerCamelCase   )
        UpperCAmelCase__							=    inputs.pop("sampling_rate"   )
        UpperCAmelCase__							=    _inputs
        if in_sampling_rate != self.feature_extractor.sampling_rate:
         import torch

         if is_torchaudio_available():
          from torchaudio import functional as F
         else:
          raise ImportError(
              "torchaudio is required to resample audio samples in AudioClassificationPipeline. "
              "The torchaudio package can be installed through: `pip install torchaudio`."   )

         UpperCAmelCase__							=    F.resample(
             torch.from_numpy(lowerCamelCase   )			,   lowerCamelCase			,   self.feature_extractor.sampling_rate   ).numpy()

       if not isinstance(lowerCamelCase			,   np.ndarray   ):
        raise ValueError("We expect a numpy ndarray as input"   )
       if len(inputs.shape   ) != 1:
        raise ValueError("We expect a single channel audio input for AudioClassificationPipeline"   )

       UpperCAmelCase__							=    self.feature_extractor(
           lowerCamelCase			,   sampling_rate=self.feature_extractor.sampling_rate			,   return_tensors="pt"   )
       return processed
      def 						UpperCAmelCase_    (      self :Tuple			,   lowerCamelCase :Union[str, Any]   )  ->      List[Any]:
       UpperCAmelCase__							=    self.model(**lowerCamelCase   )
       return model_outputs







      def 						UpperCAmelCase_    (      self :List[str]			,   lowerCamelCase :Optional[Any]			,   lowerCamelCase :List[Any]=5   )  ->      Tuple:
       UpperCAmelCase__							=    model_outputs.logits[0].softmax(-1   )
       UpperCAmelCase__			,			UpperCAmelCase__							=    probs.topk(lowerCamelCase   )

       UpperCAmelCase__							=    scores.tolist()
       UpperCAmelCase__							=    ids.tolist()

       UpperCAmelCase__							=    [{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(lowerCamelCase			,   lowerCamelCase   )]

       return labels





