






import os
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple

import sentencepiece as spm

from ...tokenization_utils import PreTrainedTokenizer
from ...utils import logging


SCREAMING_SNAKE_CASE__				     =			logging.get_logger(__name__)

SCREAMING_SNAKE_CASE__				     =			'''▁'''

SCREAMING_SNAKE_CASE__				     =			{'''vocab_file''': '''sentencepiece.bpe.model'''}

SCREAMING_SNAKE_CASE__				     =			{
    '''vocab_file''': {
        '''facebook/xglm-564M''': '''https://huggingface.co/facebook/xglm-564M/resolve/main/sentencepiece.bpe.model''',
    }
}

SCREAMING_SNAKE_CASE__				     =			{
    '''facebook/xglm-564M''': 2048,
}






class 			_UpperCamelCase(       __lowerCamelCase		):
		__SCREAMING_SNAKE_CASE  :       Optional[Any]							=							VOCAB_FILES_NAMES
		__SCREAMING_SNAKE_CASE  :       Union[str, Any]							=							PRETRAINED_VOCAB_FILES_MAP
		__SCREAMING_SNAKE_CASE  :       str							=							PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
		__SCREAMING_SNAKE_CASE  :       Any							=							['''input_ids''', '''attention_mask''']

		def __init__(					self		:    Union[str, Any]    ,       SCREAMING_SNAKE_CASE__		:    Optional[int]    ,       SCREAMING_SNAKE_CASE__		:    List[Any]="<s>"    ,       SCREAMING_SNAKE_CASE__		:    List[str]="</s>"    ,       SCREAMING_SNAKE_CASE__		:    Optional[Any]="</s>"    ,       SCREAMING_SNAKE_CASE__		:    Union[str, Any]="<s>"    ,       SCREAMING_SNAKE_CASE__		:    str="<unk>"    ,       SCREAMING_SNAKE_CASE__		:    Dict="<pad>"    ,       SCREAMING_SNAKE_CASE__		:    Optional[Dict[str, Any]] = None    ,       **SCREAMING_SNAKE_CASE__		:    List[str]    ,       ):



					'''simple docstring'''



					__a						:  Union[str, Any]									=     {} if sp_model_kwargs is None else sp_model_kwargs

					# Compatibility with the original tokenizer
					__a						:  Any									=     7
					__a						:  Union[str, Any]									=     [f'''<madeupword{i}>''' for i in range(self.num_madeup_words    )]

					__a						:  Union[str, Any]									=     kwargs.get('additional_special_tokens'    ,       []    )
					kwargs["additional_special_tokens"] += [
					    word for word in madeup_words if word not in kwargs["additional_special_tokens"]
					]

					super().__init__(
					    bos_token=SCREAMING_SNAKE_CASE__    ,       eos_token=SCREAMING_SNAKE_CASE__    ,       unk_token=SCREAMING_SNAKE_CASE__    ,       sep_token=SCREAMING_SNAKE_CASE__    ,       cls_token=SCREAMING_SNAKE_CASE__    ,       pad_token=SCREAMING_SNAKE_CASE__    ,       sp_model_kwargs=self.sp_model_kwargs    ,       **SCREAMING_SNAKE_CASE__    ,       )

					__a						:  List[str]									=     spm.SentencePieceProcessor(**self.sp_model_kwargs    )
					self.sp_model.Load(str(SCREAMING_SNAKE_CASE__    )    )
					__a						:  str									=     vocab_file

					# Original fairseq vocab and spm vocab must be "aligned":
					# Vocab    |    0    |    1    |   2    |    3    |  4  |  5  |  6  |   7   |   8   |  9
					# -------- | ------- | ------- | ------ | ------- | --- | --- | --- | ----- | ----- | ----
					# fairseq  | '<s>'   | '<pad>' | '</s>' | '<unk>' | ',' | '.' | '▁' | 's'   | '▁de' | '-'
					# spm      | '<unk>' | '<s>'   | '</s>' | ','     | '.' | '▁' | 's' | '▁de' | '-'   | '▁a'

					# The first "real" token "," has position 4 in the original fairseq vocab and position 3 in the spm vocab
					__a						:  Any									=     1

					# Mimic fairseq token-to-id alignment for the first 4 token
					__a						:  str									=     {'<s>': 0, '<pad>': 1, '</s>': 2, '<unk>': 3}

					__a						:  List[str]									=     len(self.sp_model    )
					__a						:  Optional[int]									=     {f'''<madeupword{i}>''': sp_size + i + self.fairseq_offset for i in range(self.num_madeup_words    )}
					self.fairseq_tokens_to_ids.update(SCREAMING_SNAKE_CASE__    )

					__a						:  Dict									=     {v: k for k, v in self.fairseq_tokens_to_ids.items()}

		def __getstate__(					self		:    List[str]    ):



					'''simple docstring'''



					__a						:  Tuple									=     self.__dict__.copy()
					__a						:  List[str]									=     None
					__a						:  Optional[int]									=     self.sp_model.serialized_model_proto()
					return state

		def __setstate__(					self		:    List[str]    ,       SCREAMING_SNAKE_CASE__		:    Dict    ):



					'''simple docstring'''



					__a						:  int									=     d

					# for backward compatibility
					if not hasattr(self    ,       'sp_model_kwargs'    ):
								__a						:  Dict									=     {}

					__a						:  Tuple									=     spm.SentencePieceProcessor(**self.sp_model_kwargs    )
					self.sp_model.LoadFromSerializedProto(self.sp_model_proto    )

		def    __lowerCAmelCase  (					self		:    Optional[Any]    ,       SCREAMING_SNAKE_CASE__		:    List[int]    ,       SCREAMING_SNAKE_CASE__		:    Optional[List[int]] = None    ):



					'''simple docstring'''



					if token_ids_a is None:
								return [self.sep_token_id] + token_ids_a
					__a						:  Optional[Any]									=     [self.sep_token_id]
					return sep + token_ids_a + sep + sep + token_ids_a

		def    __lowerCAmelCase  (					self		:    Dict    ,       SCREAMING_SNAKE_CASE__		:    List[int]    ,       SCREAMING_SNAKE_CASE__		:    Optional[List[int]] = None    ,       SCREAMING_SNAKE_CASE__		:    bool = False    ):



					'''simple docstring'''



					if already_has_special_tokens:
								return super().get_special_tokens_mask(
								    token_ids_a=SCREAMING_SNAKE_CASE__    ,       token_ids_a=SCREAMING_SNAKE_CASE__    ,       already_has_special_tokens=SCREAMING_SNAKE_CASE__    )

					if token_ids_a is None:
								return [1] + ([0] * len(SCREAMING_SNAKE_CASE__    ))
					return [1] + ([0] * len(SCREAMING_SNAKE_CASE__    )) + [1, 1] + ([0] * len(SCREAMING_SNAKE_CASE__    ))

		def    __lowerCAmelCase  (					self		:    List[Any]    ,       SCREAMING_SNAKE_CASE__		:    List[int]    ,       SCREAMING_SNAKE_CASE__		:    Optional[List[int]] = None    ):



					'''simple docstring'''



					__a						:  Optional[int]									=     [self.sep_token_id]

					if token_ids_a is None:
								return len(sep + token_ids_a    ) * [0]
					return len(sep + token_ids_a + sep + sep + token_ids_a    ) * [0]

		@property
		def    __lowerCAmelCase  (					self		:    Optional[int]    ):



					'''simple docstring'''



					return len(self.sp_model    ) + self.fairseq_offset + self.num_madeup_words

		def    __lowerCAmelCase  (					self		:    Tuple    ):



					'''simple docstring'''



					__a						:  str									=     {self.convert_ids_to_tokens(SCREAMING_SNAKE_CASE__    ): i for i in range(self.vocab_size    )}
					vocab.update(self.added_tokens_encoder    )
					return vocab

		def    __lowerCAmelCase  (					self		:    Any    ,       SCREAMING_SNAKE_CASE__		:    str    ):



					'''simple docstring'''



					return self.sp_model.encode(SCREAMING_SNAKE_CASE__    ,       out_type=SCREAMING_SNAKE_CASE__    )

		def    __lowerCAmelCase  (					self		:    List[Any]    ,       SCREAMING_SNAKE_CASE__		:    List[Any]    ):



					'''simple docstring'''



					if token in self.fairseq_tokens_to_ids:
								return self.fairseq_tokens_to_ids[token]
					__a						:  List[str]									=     self.sp_model.PieceToId(SCREAMING_SNAKE_CASE__    )

					# Need to return unknown token if the SP model returned 0
					return spm_id + self.fairseq_offset if spm_id else self.unk_token_id

		def    __lowerCAmelCase  (					self		:    Optional[int]    ,       SCREAMING_SNAKE_CASE__		:    Dict    ):



					'''simple docstring'''



					if index in self.fairseq_ids_to_tokens:
								return self.fairseq_ids_to_tokens[index]
					return self.sp_model.IdToPiece(index - self.fairseq_offset    )

		def    __lowerCAmelCase  (					self		:    Tuple    ,       SCREAMING_SNAKE_CASE__		:    List[Any]    ):



					'''simple docstring'''



					__a						:  Optional[int]									=     ''.join(SCREAMING_SNAKE_CASE__    ).replace(SCREAMING_SNAKE_CASE__    ,       ' '    ).strip()
					return out_string



		def    __lowerCAmelCase  (					self		:    Dict    ,       SCREAMING_SNAKE_CASE__		:    str    ,       SCREAMING_SNAKE_CASE__		:    Optional[str] = None    ):



					'''simple docstring'''



					if not os.path.isdir(SCREAMING_SNAKE_CASE__    ):
								logger.error(f'''Vocabulary path ({save_directory}) should be a directory'''    )
								return
					__a						:  Any									=     os.path.join(
					    SCREAMING_SNAKE_CASE__    ,       (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file']    )

					if os.path.abspath(self.vocab_file    ) != os.path.abspath(SCREAMING_SNAKE_CASE__    ) and os.path.isfile(self.vocab_file    ):
								copyfile(self.vocab_file    ,       SCREAMING_SNAKE_CASE__    )
					elif not os.path.isfile(self.vocab_file    ):
								with open(SCREAMING_SNAKE_CASE__    ,       'wb'    ) as fi:
											__a						:  List[Any]									=     self.sp_model.serialized_model_proto()
											fi.write(SCREAMING_SNAKE_CASE__    )

					return (out_vocab_file,)







'''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.
from ..models.whisper import WhisperForConditionalGeneration, WhisperProcessor
from .base import PipelineTool




class    lowerCAmelCase       (     UpperCamelCase_  ):
 A_				:				int	=			"""openai/whisper-base"""
 A_				:				str	=			(
     """This is a tool that transcribes an audio into text. It takes an input named `audio` and returns the """
     """transcribed text."""
 )
 A_				:				Union[str, Any]	=			"""transcriber"""
 A_				:				Tuple	=			WhisperProcessor
 A_				:				Dict	=			WhisperForConditionalGeneration

 A_				:				Tuple	=			["""audio"""]
 A_				:				Optional[int]	=			["""text"""]


 def    _A	(    self  : Optional[int]			,							a__  : Optional[int]    ):
  '''simple docstring'''

  return self.pre_processor(a__			,							return_tensors="pt"    ).input_features


 def    _A	(    self  : List[str]			,							a__  : Union[str, Any]    ):
  '''simple docstring'''

  return self.model.generate(inputs=a__    )


 def    _A	(    self  : Optional[Any]			,							a__  : Dict    ):
  '''simple docstring'''

  return self.pre_processor.batch_decode(a__			,							skip_special_tokens=a__    )[0]





