





"""simple docstring"""






import copy
import random

from transformers import CLIPTokenizer




class 			__lowercase     (     _UpperCamelCase							):







					'''simple docstring'''




					def __init__(  self		,     *_UpperCAmelCase		,     **_UpperCAmelCase       ):
									super().__init__(*_UpperCAmelCase		,     **_UpperCAmelCase       )
									__a	:  Dict         =					{}







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase		,     *_UpperCAmelCase		,     **_UpperCAmelCase       ):
									__a	:  List[str]         =					super().add_tokens(_UpperCAmelCase		,     *_UpperCAmelCase		,     **_UpperCAmelCase       )
									if num_added_tokens == 0:
													raise ValueError(
													    f"""The tokenizer already contains the token {placeholder_token}. Please pass a different"""
													    ''' `placeholder_token` that is not already in the tokenizer.'''       )







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase		,     *_UpperCAmelCase		,     _UpperCAmelCase=1		,     **_UpperCAmelCase       ):
									__a	:  List[Any]         =					[]
									if num_vec_per_token == 1:
													self.try_adding_tokens(_UpperCAmelCase		,     *_UpperCAmelCase		,     **_UpperCAmelCase       )
													output.append(_UpperCAmelCase       )
									else:
													__a	:  Tuple         =					[]
													for i in range(_UpperCAmelCase       ):
																	__a	:  Tuple         =					placeholder_token + f"""_{i}"""
																	self.try_adding_tokens(_UpperCAmelCase		,     *_UpperCAmelCase		,     **_UpperCAmelCase       )
																	output.append(_UpperCAmelCase       )
        # handle cases where there is a new placeholder token that contains the current placeholder token but is larger
									for token in self.token_map:
													if token in placeholder_token:
																	raise ValueError(
																	    f"""The tokenizer already has placeholder token {token} that can get confused with"""
																	    f""" {placeholder_token}keep placeholder tokens independent"""       )
									__a	:  Dict         =					output







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase		,     _UpperCAmelCase=False		,     _UpperCAmelCase=1.0       ):
									if isinstance(_UpperCAmelCase		,     _UpperCAmelCase       ):
													__a	:  Tuple         =					[]
													for i in range(len(_UpperCAmelCase       )       ):
																	output.append(self.replace_placeholder_tokens_in_text(text[i]		,     vector_shuffle=_UpperCAmelCase       )       )
													return output
									for placeholder_token in self.token_map:
													if placeholder_token in text:
																	__a	:  Optional[int]         =					self.token_map[placeholder_token]
																	__a	:  Any         =					tokens[: 1 + int(len(_UpperCAmelCase       ) * prop_tokens_to_load       )]
																	if vector_shuffle:
																					__a	:  Any         =					copy.copy(_UpperCAmelCase       )
																					random.shuffle(_UpperCAmelCase       )
																	__a	:  int         =					text.replace(_UpperCAmelCase		,     ''' '''.join(_UpperCAmelCase       )       )
									return text







					def __call__(  self		,     _UpperCAmelCase		,     *_UpperCAmelCase		,     _UpperCAmelCase=False		,     _UpperCAmelCase=1.0		,     **_UpperCAmelCase       ):
									return super().__call__(
									    self.replace_placeholder_tokens_in_text(
									        _UpperCAmelCase		,     vector_shuffle=_UpperCAmelCase		,     prop_tokens_to_load=_UpperCAmelCase       )		,     *_UpperCAmelCase		,     **_UpperCAmelCase		,     )







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase		,     *_UpperCAmelCase		,     _UpperCAmelCase=False		,     _UpperCAmelCase=1.0		,     **_UpperCAmelCase       ):
									return super().encode(
									    self.replace_placeholder_tokens_in_text(
									        _UpperCAmelCase		,     vector_shuffle=_UpperCAmelCase		,     prop_tokens_to_load=_UpperCAmelCase       )		,     *_UpperCAmelCase		,     **_UpperCAmelCase		,     )






"""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 			__lowercase     (     _UpperCamelCase							):







					'''simple docstring'''




					__lowerCAmelCase					=					'''openai/whisper-base'''
					__lowerCAmelCase					=					(
					    '''This is a tool that transcribes an audio into text. It takes an input named `audio` and returns the '''
					    '''transcribed text.'''
					)
					__lowerCAmelCase					=					'''transcriber'''
					__lowerCAmelCase					=					WhisperProcessor
					__lowerCAmelCase					=					WhisperForConditionalGeneration

					__lowerCAmelCase					=					['''audio''']
					__lowerCAmelCase					=					['''text''']







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase       ):
									return self.pre_processor(_UpperCAmelCase		,     return_tensors='''pt'''       ).input_features







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase       ):
									return self.model.generate(inputs=_UpperCAmelCase       )







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase       ):
									return self.pre_processor.batch_decode(_UpperCAmelCase		,     skip_special_tokens=_UpperCAmelCase       )[0]