


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

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


if is_tf_available():
      import tensorflow as tf

if is_torch_available():
      import torch


_UpperCAmelCase             =							logging.get_logger(__name__)






class        snake_case_    :






  def __init__(							self			:					Optional[Any]       ,  _snake_case			:					str = None       ,  _snake_case			:					uuid.UUID = None       ,  _snake_case			:					List[str]=None       ,  _snake_case			:					Tuple=None      )->Optional[Any]:






   '''simple docstring'''

   if not conversation_id:
    __lowerCAmelCase					:	Any      =       uuid.uuida()
   if past_user_inputs is None:
    __lowerCAmelCase					:	Optional[Any]      =       []
   if generated_responses is None:
    __lowerCAmelCase					:	Optional[Any]      =       []

   __lowerCAmelCase					:	uuid.UUID      =       conversation_id
   __lowerCAmelCase					:	List[str]      =       past_user_inputs
   __lowerCAmelCase					:	List[str]      =       generated_responses
   __lowerCAmelCase					:	Optional[str]      =       text






  def __eq__(							self			:					str       ,  _snake_case			:					Dict      )->Optional[Any]:






   '''simple docstring'''

   if not isinstance(_snake_case       ,  _snake_case      ):
    return False
   if self.uuid == other.uuid:
    return True
   return (
       self.new_user_input == other.new_user_input
       and self.past_user_inputs == other.past_user_inputs
       and self.generated_responses == other.generated_responses
   )






  def 				UpperCAmelCase__						(							self			:					Optional[Any]       ,  _snake_case			:					str       ,  _snake_case			:					bool = False      )->Any:






   '''simple docstring'''

   if self.new_user_input:
    if overwrite:
     logger.warning(
         F'''User input added while unprocessed input was existing: "{self.new_user_input}" was overwritten '''
         F'''with: "{text}".'''      )
     __lowerCAmelCase					:	int      =       text
    else:
     logger.warning(
         F'''User input added while unprocessed input was existing: "{self.new_user_input}" new input '''
         F'''ignored: "{text}". Set `overwrite` to True to overwrite unprocessed user input'''      )
   else:
    __lowerCAmelCase					:	List[str]      =       text






  def 				UpperCAmelCase__						(							self			:					List[str]      )->int:






   '''simple docstring'''

   if self.new_user_input:
    self.past_user_inputs.append(self.new_user_input      )
   __lowerCAmelCase					:	List[str]      =       None






  def 				UpperCAmelCase__						(							self			:					List[str]       ,  _snake_case			:					str      )->List[str]:






   '''simple docstring'''

   self.generated_responses.append(_snake_case      )






  def 				UpperCAmelCase__						(							self			:					str      )->Optional[Any]:






   '''simple docstring'''

   for user_input, generated_response in zip(self.past_user_inputs       ,  self.generated_responses      ):
    yield True, user_input
    yield False, generated_response
   if self.new_user_input:
    yield True, self.new_user_input






  def __repr__(							self			:					Dict      )->Optional[int]:






   '''simple docstring'''

   __lowerCAmelCase					:	List[Any]      =       F'''Conversation id: {self.uuid} \n'''
   for is_user, text in self.iter_texts():
    __lowerCAmelCase					:	str      =       """user""" if is_user else """bot"""
    output += F'''{name} >> {text} \n'''
   return output






@add_end_docstrings(
    __lowercase					,R'\n        min_length_for_response (`int`, *optional*, defaults to 32):\n            The minimum length (in number of tokens) for a response.\n        minimum_tokens (`int`, *optional*, defaults to 10):\n            The minimum length of tokens to leave for a response.\n    '					,)
class        snake_case_    (		__lowercase						):






  def __init__(							self			:					List[Any]       ,  *_snake_case			:					str       ,  **_snake_case			:					List[Any]      )->List[Any]:






   '''simple docstring'''

   super().__init__(*_snake_case       ,  **_snake_case      )
   if self.tokenizer.pad_token_id is None:
    __lowerCAmelCase					:	Optional[int]      =       self.tokenizer.eos_token






  def 				UpperCAmelCase__						(							self			:					Dict       ,  _snake_case			:					Any=None       ,  _snake_case			:					str=None       ,  _snake_case			:					Tuple=None       ,  **_snake_case			:					str      )->str:






   '''simple docstring'''

   __lowerCAmelCase					:	Optional[Any]      =       {}
   __lowerCAmelCase					:	Tuple      =       {}
   __lowerCAmelCase					:	List[str]      =       {}

   if min_length_for_response is not None:
    __lowerCAmelCase					:	Optional[int]      =       min_length_for_response
   if minimum_tokens is not None:
    __lowerCAmelCase					:	Tuple      =       minimum_tokens

   if "max_length" in generate_kwargs:
    __lowerCAmelCase					:	List[str]      =       generate_kwargs["""max_length"""]
    # self.max_length = generate_kwargs.get("max_length", self.model.config.max_length)
   if clean_up_tokenization_spaces is not None:
    __lowerCAmelCase					:	int      =       clean_up_tokenization_spaces

   if generate_kwargs:
    forward_params.update(_snake_case      )
   return preprocess_params, forward_params, postprocess_params






  def __call__(							self			:					int       ,  _snake_case			:					Union[Conversation, List[Conversation]]       ,  _snake_case			:					List[str]=0       ,  **_snake_case			:					Optional[Any]      )->Optional[int]:






   '''simple docstring'''

   __lowerCAmelCase					:	Union[str, Any]      =       super().__call__(_snake_case       ,  num_workers=_snake_case       ,  **_snake_case      )
   if isinstance(_snake_case       ,  _snake_case      ) and len(_snake_case      ) == 1:
    return outputs[0]
   return outputs






  def 				UpperCAmelCase__						(							self			:					str       ,  _snake_case			:					Conversation       ,  _snake_case			:					Tuple=32      )->Dict[str, Any]:






   '''simple docstring'''

   if not isinstance(_snake_case       ,  _snake_case      ):
    raise ValueError("""ConversationalPipeline, expects Conversation as inputs"""      )
   if conversation.new_user_input is None:
    raise ValueError(
        F'''Conversation with UUID {type(conversation.uuid      )} does not contain new user input to process. '''
        """Add user inputs with the conversation's `add_user_input` method"""      )
   if hasattr(self.tokenizer       ,  """_build_conversation_input_ids"""      ):
    __lowerCAmelCase					:	str      =       self.tokenizer._build_conversation_input_ids(_snake_case      )
   else:
    # If the tokenizer cannot handle conversations, we default to only the old version
    __lowerCAmelCase					:	Tuple      =       self._legacy_parse_and_tokenize(_snake_case      )

   if self.framework == "pt":
    __lowerCAmelCase					:	int      =       torch.LongTensor([input_ids]      )
   elif self.framework == "tf":
    __lowerCAmelCase					:	Dict      =       tf.constant([input_ids]      )
   return {"input_ids": input_ids, "conversation": conversation}






  def 				UpperCAmelCase__						(							self			:					List[Any]       ,  _snake_case			:					List[str]       ,  _snake_case			:					Optional[int]=10       ,  **_snake_case			:					int      )->Dict:






   '''simple docstring'''

   __lowerCAmelCase					:	int      =       generate_kwargs.get("""max_length"""       ,  self.model.config.max_length      )

   __lowerCAmelCase					:	Tuple      =       model_inputs["""input_ids"""].shape[1]
   if max_length - minimum_tokens < n:
    logger.warning(F'''Conversation input is to long ({n}), trimming it to ({max_length} - {minimum_tokens})'''      )
    __lowerCAmelCase					:	Dict      =       max_length - minimum_tokens
    __lowerCAmelCase					:	Dict      =       model_inputs["""input_ids"""][:, -trim:]
    if "attention_mask" in model_inputs:
     __lowerCAmelCase					:	List[str]      =       model_inputs["""attention_mask"""][:, -trim:]
   __lowerCAmelCase					:	Union[str, Any]      =       model_inputs.pop("""conversation"""      )
   __lowerCAmelCase					:	Any      =       max_length
   __lowerCAmelCase					:	Union[str, Any]      =       self.model.generate(**_snake_case       ,  **_snake_case      )
   if self.model.config.is_encoder_decoder:
    __lowerCAmelCase					:	str      =       1
   else:
    __lowerCAmelCase					:	Optional[int]      =       n
   return {"output_ids": output_ids[:, start_position:], "conversation": conversation}






  def 				UpperCAmelCase__						(							self			:					str       ,  _snake_case			:					Any       ,  _snake_case			:					Optional[Any]=True      )->Dict:






   '''simple docstring'''

   __lowerCAmelCase					:	str      =       model_outputs["""output_ids"""]
   __lowerCAmelCase					:	str      =       self.tokenizer.decode(
       output_ids[0]       ,  skip_special_tokens=_snake_case       ,  clean_up_tokenization_spaces=_snake_case       ,  )
   __lowerCAmelCase					:	Union[str, Any]      =       model_outputs["""conversation"""]
   conversation.mark_processed()
   conversation.append_response(_snake_case      )
   return conversation






  def 				UpperCAmelCase__						(							self			:					Union[str, Any]       ,  _snake_case			:					Conversation      )->Dict:






   '''simple docstring'''

   __lowerCAmelCase					:	Optional[Any]      =       self.tokenizer.eos_token_id
   __lowerCAmelCase					:	Tuple      =       []
   for is_user, text in conversation.iter_texts():
    if eos_token_id is not None:
     input_ids.extend(self.tokenizer.encode(_snake_case       ,  add_special_tokens=_snake_case      ) + [eos_token_id]      )
    else:
     input_ids.extend(self.tokenizer.encode(_snake_case       ,  add_special_tokens=_snake_case      )      )

   if len(_snake_case      ) > self.tokenizer.model_max_length:
    __lowerCAmelCase					:	Optional[Any]      =       input_ids[-self.tokenizer.model_max_length :]
   return input_ids






import functools
import operator

from ...configuration_utils import PretrainedConfig
from ...utils import logging


_UpperCAmelCase             =							logging.get_logger(__name__)

_UpperCAmelCase             =							{
    'microsoft/unispeech-sat-base-100h-libri-ft': (
        'https://huggingface.co/microsoft/unispeech-sat-base-100h-libri-ft/resolve/main/config.json'
    ),
    # See all UniSpeechSat models at https://huggingface.co/models?filter=unispeech_sat
}






class        snake_case_    (		__lowercase						):
  A_      							=   'unispeech-sat'






  def __init__(							self			:					str       ,  _snake_case			:					List[Any]=32       ,  _snake_case			:					Union[str, Any]=768       ,  _snake_case			:					Tuple=12       ,  _snake_case			:					Optional[int]=12       ,  _snake_case			:					Optional[Any]=3072       ,  _snake_case			:					Tuple="gelu"       ,  _snake_case			:					int=0.1       ,  _snake_case			:					List[Any]=0.1       ,  _snake_case			:					Union[str, Any]=0.1       ,  _snake_case			:					str=0.0       ,  _snake_case			:					List[str]=0.0       ,  _snake_case			:					int=0.1       ,  _snake_case			:					Optional[Any]=0.1       ,  _snake_case			:					Optional[Any]=0.02       ,  _snake_case			:					int=1E-5       ,  _snake_case			:					Dict="group"       ,  _snake_case			:					Optional[Any]="gelu"       ,  _snake_case			:					Optional[Any]=(512, 512, 512, 512, 512, 512, 512)       ,  _snake_case			:					int=(5, 2, 2, 2, 2, 2, 2)       ,  _snake_case			:					int=(10, 3, 3, 3, 3, 2, 2)       ,  _snake_case			:					Any=False       ,  _snake_case			:					Optional[Any]=128       ,  _snake_case			:					Tuple=16       ,  _snake_case			:					str=False       ,  _snake_case			:					Dict=True       ,  _snake_case			:					Tuple=0.05       ,  _snake_case			:					str=10       ,  _snake_case			:					Tuple=2       ,  _snake_case			:					List[Any]=0.0       ,  _snake_case			:					str=10       ,  _snake_case			:					Any=0       ,  _snake_case			:					List[Any]=320       ,  _snake_case			:					Union[str, Any]=2       ,  _snake_case			:					Dict=0.1       ,  _snake_case			:					Dict=100       ,  _snake_case			:					Union[str, Any]=256       ,  _snake_case			:					int=256       ,  _snake_case			:					Union[str, Any]=0.1       ,  _snake_case			:					Optional[Any]="mean"       ,  _snake_case			:					int=False       ,  _snake_case			:					str=False       ,  _snake_case			:					str=256       ,  _snake_case			:					List[Any]=(512, 512, 512, 512, 1500)       ,  _snake_case			:					Optional[int]=(5, 3, 3, 1, 1)       ,  _snake_case			:					Tuple=(1, 2, 3, 1, 1)       ,  _snake_case			:					Dict=512       ,  _snake_case			:					Union[str, Any]=0       ,  _snake_case			:					List[str]=1       ,  _snake_case			:					Optional[Any]=2       ,  _snake_case			:					Optional[int]=504       ,  **_snake_case			:					Optional[int]       ,  )->Union[str, Any]:






   '''simple docstring'''

   super().__init__(**_snake_case       ,  pad_token_id=_snake_case       ,  bos_token_id=_snake_case       ,  eos_token_id=_snake_case      )
   __lowerCAmelCase					:	Dict      =       hidden_size
   __lowerCAmelCase					:	List[Any]      =       feat_extract_norm
   __lowerCAmelCase					:	int      =       feat_extract_activation
   __lowerCAmelCase					:	Union[str, Any]      =       list(_snake_case      )
   __lowerCAmelCase					:	str      =       list(_snake_case      )
   __lowerCAmelCase					:	Optional[Any]      =       list(_snake_case      )
   __lowerCAmelCase					:	Optional[int]      =       conv_bias
   __lowerCAmelCase					:	Dict      =       num_conv_pos_embeddings
   __lowerCAmelCase					:	List[Any]      =       num_conv_pos_embedding_groups
   __lowerCAmelCase					:	Tuple      =       len(self.conv_dim      )
   __lowerCAmelCase					:	int      =       num_hidden_layers
   __lowerCAmelCase					:	str      =       intermediate_size
   __lowerCAmelCase					:	str      =       hidden_act
   __lowerCAmelCase					:	Any      =       num_attention_heads
   __lowerCAmelCase					:	Optional[int]      =       hidden_dropout
   __lowerCAmelCase					:	str      =       attention_dropout
   __lowerCAmelCase					:	int      =       activation_dropout
   __lowerCAmelCase					:	Union[str, Any]      =       feat_proj_dropout
   __lowerCAmelCase					:	List[str]      =       final_dropout
   __lowerCAmelCase					:	Dict      =       layerdrop
   __lowerCAmelCase					:	Tuple      =       layer_norm_eps
   __lowerCAmelCase					:	Optional[Any]      =       initializer_range
   __lowerCAmelCase					:	str      =       vocab_size
   __lowerCAmelCase					:	Optional[int]      =       num_clusters
   __lowerCAmelCase					:	List[Any]      =       do_stable_layer_norm
   __lowerCAmelCase					:	Tuple      =       use_weighted_layer_sum

   if (
       (len(self.conv_stride      ) != self.num_feat_extract_layers)
       or (len(self.conv_kernel      ) != self.num_feat_extract_layers)
       or (len(self.conv_dim      ) != self.num_feat_extract_layers)
   ):
    raise ValueError(
        """Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =="""
        """ `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ="""
        F''' {len(self.conv_dim      )}`, `len(config.conv_stride) = {len(self.conv_stride      )}`,'''
        F''' `len(config.conv_kernel) = {len(self.conv_kernel      )}`.'''      )

   # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
   __lowerCAmelCase					:	Dict      =       apply_spec_augment
   __lowerCAmelCase					:	List[Any]      =       mask_time_prob
   __lowerCAmelCase					:	List[str]      =       mask_time_length
   __lowerCAmelCase					:	Dict      =       mask_time_min_masks
   __lowerCAmelCase					:	Tuple      =       mask_feature_prob
   __lowerCAmelCase					:	List[str]      =       mask_feature_length
   __lowerCAmelCase					:	str      =       mask_feature_min_masks

   # parameters for pretraining with codevector quantized representations
   __lowerCAmelCase					:	Optional[int]      =       num_codevectors_per_group
   __lowerCAmelCase					:	List[Any]      =       num_codevector_groups
   __lowerCAmelCase					:	int      =       contrastive_logits_temperature
   __lowerCAmelCase					:	str      =       feat_quantizer_dropout
   __lowerCAmelCase					:	int      =       num_negatives
   __lowerCAmelCase					:	str      =       codevector_dim
   __lowerCAmelCase					:	Any      =       proj_codevector_dim
   __lowerCAmelCase					:	Any      =       diversity_loss_weight

   # ctc loss
   __lowerCAmelCase					:	Tuple      =       ctc_loss_reduction
   __lowerCAmelCase					:	Any      =       ctc_zero_infinity

   # SequenceClassification-specific parameter. Feel free to ignore for other classes.
   __lowerCAmelCase					:	Any      =       classifier_proj_size

   # XVector-specific parameters. Feel free to ignore for other classes.
   __lowerCAmelCase					:	List[str]      =       list(_snake_case      )
   __lowerCAmelCase					:	List[str]      =       list(_snake_case      )
   __lowerCAmelCase					:	Optional[int]      =       list(_snake_case      )
   __lowerCAmelCase					:	Optional[int]      =       xvector_output_dim






  @property
  def 				UpperCAmelCase__						(							self			:					Optional[Any]      )->Any:






   '''simple docstring'''

   return functools.reduce(operator.mul       ,  self.conv_stride       ,  1      )