




'''simple docstring'''





import itertools
import random
import unittest

import numpy as np

from transformers import WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST, WavaVecaConfig, WavaVecaFeatureExtractor
from transformers.testing_utils import require_torch, slow

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


_SCREAMING_SNAKE_CASE							:   Optional[int]      		= random.Random()


def      UpperCamelCase_(	snake_case							:       Dict							,				snake_case							:       Union[str, Any]=1.0							,				snake_case							:       Union[str, Any]=None							,				snake_case							:       str=None     ):

							'''simple docstring'''






							if rng is None:
														snake_case_						=      global_rng

							snake_case_						=      []
							for batch_idx in range(shape[0]     ):
														values.append([]     )
														for _ in range(shape[1]     ):
																					values[-1].append(rng.random() * scale     )

							return values




class  _snake_case		(	unittest.TestCase							):




			def __init__(					self				,							a__				,							a__=7				,							a__=400				,							a__=2_000				,							a__=1				,							a__=0.0				,							a__=16_000				,							a__=True				,							a__=True				,							)				->				Optional[int]:

										'''simple docstring'''




										snake_case_						=      parent
										snake_case_						=      batch_size
										snake_case_						=      min_seq_length
										snake_case_						=      max_seq_length
										snake_case_						=      (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
										snake_case_						=      feature_size
										snake_case_						=      padding_value
										snake_case_						=      sampling_rate
										snake_case_						=      return_attention_mask
										snake_case_						=      do_normalize




			def    lowerCAmelCase__				(					self							)				->				Tuple:

										'''simple docstring'''




										return {
										    "feature_size": self.feature_size,
										    "padding_value": self.padding_value,
										    "sampling_rate": self.sampling_rate,
										    "return_attention_mask": self.return_attention_mask,
										    "do_normalize": self.do_normalize,
										}




			def    lowerCAmelCase__				(					self				,							a__=False				,							a__=False							)				->				Tuple:

										'''simple docstring'''




										def _flatten(a__							):
																	return list(itertools.chain(*A__							)							)

										if equal_length:
																	snake_case_						=      floats_list((self.batch_size, self.max_seq_length)							)
										else:
																	# make sure that inputs increase in size
																	snake_case_						=      [
																	    _flatten(floats_list((x, self.feature_size)							)							)
																	    for x in range(self.min_seq_length				,							self.max_seq_length				,							self.seq_length_diff							)
																	]

										if numpify:
																	snake_case_						=      [np.asarray(A__							) for x in speech_inputs]

										return speech_inputs




class  _snake_case		(	lowercase_						,						unittest.TestCase							):
			lowerCAmelCase_     :      Union[str, Any]						       =    WavaVecaFeatureExtractor




			def    lowerCAmelCase__				(					self							)				->				Tuple:

										'''simple docstring'''




										snake_case_						=      WavaVecaFeatureExtractionTester(self							)




			def    lowerCAmelCase__				(					self				,							a__							)				->				List[str]:

										'''simple docstring'''




										self.assertTrue(np.all(np.mean(A__				,							axis=0							) < 1e-3							)							)
										self.assertTrue(np.all(np.abs(np.var(A__				,							axis=0							) - 1							) < 1e-3							)							)




			def    lowerCAmelCase__				(					self							)				->				str:

										'''simple docstring'''




										snake_case_						=      self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()							)
										# create three inputs of length 800, 1000, and 1200
										snake_case_						=      [floats_list((1, x)							)[0] for x in range(800				,							1_400				,							200							)]
										snake_case_						=      [np.asarray(A__							) for speech_input in speech_inputs]

										# Test not batched input
										snake_case_						=      feat_extract(speech_inputs[0]				,							return_tensors="np"							).input_values
										snake_case_						=      feat_extract(np_speech_inputs[0]				,							return_tensors="np"							).input_values
										self.assertTrue(np.allclose(A__				,							A__				,							atol=1e-3							)							)

										# Test batched
										snake_case_						=      feat_extract(A__				,							return_tensors="np"							).input_values
										snake_case_						=      feat_extract(A__				,							return_tensors="np"							).input_values
										for enc_seq_a, enc_seq_a in zip(A__				,							A__							):
																	self.assertTrue(np.allclose(A__				,							A__				,							atol=1e-3							)							)

										# Test 2-D numpy arrays are batched.
										snake_case_						=      [floats_list((1, x)							)[0] for x in (800, 800, 800)]
										snake_case_						=      np.asarray(A__							)
										snake_case_						=      feat_extract(A__				,							return_tensors="np"							).input_values
										snake_case_						=      feat_extract(A__				,							return_tensors="np"							).input_values
										for enc_seq_a, enc_seq_a in zip(A__				,							A__							):
																	self.assertTrue(np.allclose(A__				,							A__				,							atol=1e-3							)							)




			def    lowerCAmelCase__				(					self							)				->				Tuple:

										'''simple docstring'''




										snake_case_						=      self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()							)
										snake_case_						=      [floats_list((1, x)							)[0] for x in range(800				,							1_400				,							200							)]

										snake_case_						=      ["""longest""", """max_length""", """do_not_pad"""]
										snake_case_						=      [None, 1_600, None]
										for max_length, padding in zip(A__				,							A__							):
																	snake_case_						=      feat_extract(A__				,							padding=A__				,							max_length=A__				,							return_tensors="np"							)
																	snake_case_						=      processed.input_values

																	self._check_zero_mean_unit_variance(input_values[0][:800]							)
																	self.assertTrue(input_values[0][800:].sum() < 1e-6							)
																	self._check_zero_mean_unit_variance(input_values[1][:1_000]							)
																	self.assertTrue(input_values[0][1_000:].sum() < 1e-6							)
																	self._check_zero_mean_unit_variance(input_values[2][:1_200]							)




			def    lowerCAmelCase__				(					self							)				->				str:

										'''simple docstring'''




										snake_case_						=      self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()							)
										snake_case_						=      range(800				,							1_400				,							200							)
										snake_case_						=      [floats_list((1, x)							)[0] for x in lengths]

										snake_case_						=      ["""longest""", """max_length""", """do_not_pad"""]
										snake_case_						=      [None, 1_600, None]

										for max_length, padding in zip(A__				,							A__							):
																	snake_case_						=      feat_extract(A__				,							max_length=A__				,							padding=A__							)
																	snake_case_						=      processed.input_values

																	self._check_zero_mean_unit_variance(input_values[0][:800]							)
																	self._check_zero_mean_unit_variance(input_values[1][:1_000]							)
																	self._check_zero_mean_unit_variance(input_values[2][:1_200]							)




			def    lowerCAmelCase__				(					self							)				->				int:

										'''simple docstring'''




										snake_case_						=      self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()							)
										snake_case_						=      [floats_list((1, x)							)[0] for x in range(800				,							1_400				,							200							)]
										snake_case_						=      feat_extract(
										    A__				,							truncation=A__				,							max_length=1_000				,							padding="max_length"				,							return_tensors="np"							)
										snake_case_						=      processed.input_values

										self._check_zero_mean_unit_variance(input_values[0, :800]							)
										self._check_zero_mean_unit_variance(input_values[1]							)
										self._check_zero_mean_unit_variance(input_values[2]							)




			def    lowerCAmelCase__				(					self							)				->				Optional[int]:

										'''simple docstring'''




										snake_case_						=      self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()							)
										snake_case_						=      [floats_list((1, x)							)[0] for x in range(800				,							1_400				,							200							)]
										snake_case_						=      feat_extract(
										    A__				,							truncation=A__				,							max_length=1_000				,							padding="longest"				,							return_tensors="np"							)
										snake_case_						=      processed.input_values

										self._check_zero_mean_unit_variance(input_values[0, :800]							)
										self._check_zero_mean_unit_variance(input_values[1, :1_000]							)
										self._check_zero_mean_unit_variance(input_values[2]							)

										# make sure that if max_length < longest -> then pad to max_length
										self.assertTrue(input_values.shape == (3, 1_000)							)

										snake_case_						=      [floats_list((1, x)							)[0] for x in range(800				,							1_400				,							200							)]
										snake_case_						=      feat_extract(
										    A__				,							truncation=A__				,							max_length=2_000				,							padding="longest"				,							return_tensors="np"							)
										snake_case_						=      processed.input_values

										self._check_zero_mean_unit_variance(input_values[0, :800]							)
										self._check_zero_mean_unit_variance(input_values[1, :1_000]							)
										self._check_zero_mean_unit_variance(input_values[2]							)

										# make sure that if max_length > longest -> then pad to longest
										self.assertTrue(input_values.shape == (3, 1_200)							)




			@require_torch
			def    lowerCAmelCase__				(					self							)				->				Optional[int]:

										'''simple docstring'''




										import torch

										snake_case_						=      self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()							)
										snake_case_						=      np.random.rand(100							).astype(np.floataa							)
										snake_case_						=      np_speech_inputs.tolist()

										for inputs in [py_speech_inputs, np_speech_inputs]:
																	snake_case_						=      feature_extractor.pad([{"input_values": inputs}]				,							return_tensors="np"							)
																	self.assertTrue(np_processed.input_values.dtype == np.floataa							)
																	snake_case_						=      feature_extractor.pad([{"input_values": inputs}]				,							return_tensors="pt"							)
																	self.assertTrue(pt_processed.input_values.dtype == torch.floataa							)




			@slow
			@require_torch
			def    lowerCAmelCase__				(					self							)				->				Any:

										'''simple docstring'''




										for model_id in WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST:
																	snake_case_						=      WavaVecaConfig.from_pretrained(A__							)
																	snake_case_						=      WavaVecaFeatureExtractor.from_pretrained(A__							)

																	# only "layer" feature extraction norm should make use of
																	# attention_mask
																	self.assertEqual(feat_extract.return_attention_mask				,							config.feat_extract_norm == "layer"							)







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


A_       : List[Any]         =			logging.get_logger(__name__)



class     _a						:


					'''simple docstring'''






					def __init__( self    ,       A__ = None    ,       A__ = None    ,       A__=None    ,       A__=None			):
						if not conversation_id:
							A__							:    List[Any]        =		uuid.uuida()
						if past_user_inputs is None:
							A__							:    Dict        =		[]
						if generated_responses is None:
							A__							:    int        =		[]

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






					def __eq__( self    ,       A__			):
						if not isinstance(A__    ,       A__			):
							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   __A			( self    ,       A__    ,       A__ = False			):
						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}\"."""			)
								A__							:    str        =		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:
							A__							:    Tuple        =		text






					def   __A			( self			):
						if self.new_user_input:
							self.past_user_inputs.append(self.new_user_input			)
						A__							:    Tuple        =		None






					def   __A			( self    ,       A__			):
						self.generated_responses.append(A__			)






					def   __A			( self			):
						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			):
						A__							:    Optional[Any]        =		F"""Conversation id: {self.uuid} \n"""
						for is_user, text in self.iter_texts():
							A__							:    str        =		"""user""" if is_user else """bot"""
							output += F"""{name} >> {text} \n"""
						return output



@add_end_docstrings(
    __magic_name__		,   r'''
        min_length_for_response (`int`, *optional*, defaults to 32):
            The minimum length (in number of tokens) for a response.
        minimum_tokens (`int`, *optional*, defaults to 10):
            The minimum length of tokens to leave for a response.
    '''		,   )
class     _a						(__magic_name__				):


					'''simple docstring'''






					def __init__( self    ,       *A__    ,       **A__			):
						super().__init__(*A__    ,       **A__			)
						if self.tokenizer.pad_token_id is None:
							A__							:    Tuple        =		self.tokenizer.eos_token






					def   __A			( self    ,       A__=None    ,       A__=None    ,       A__=None    ,       **A__			):
						A__							:    Tuple        =		{}
						A__							:    List[str]        =		{}
						A__							:    Union[str, Any]        =		{}

						if min_length_for_response is not None:
							A__							:    str        =		min_length_for_response
						if minimum_tokens is not None:
							A__							:    List[str]        =		minimum_tokens

						if "max_length" in generate_kwargs:
							A__							:    List[Any]        =		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:
							A__							:    Optional[int]        =		clean_up_tokenization_spaces

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






					def __call__( self    ,       A__    ,       A__=0    ,       **A__			):
						A__							:    Optional[Any]        =		super().__call__(A__    ,       num_workers=A__    ,       **A__			)
						if isinstance(A__    ,       A__			) and len(A__			) == 1:
							return outputs[0]
						return outputs






					def   __A			( self    ,       A__    ,       A__=32			):
						if not isinstance(A__    ,       A__			):
							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"""			):
							A__							:    List[str]        =		self.tokenizer._build_conversation_input_ids(A__			)
						else:
							# If the tokenizer cannot handle conversations, we default to only the old version
							A__							:    Tuple        =		self._legacy_parse_and_tokenize(A__			)

						if self.framework == "pt":
							A__							:    List[str]        =		torch.LongTensor([input_ids]			)
						elif self.framework == "tf":
							A__							:    Optional[Any]        =		tf.constant([input_ids]			)
						return {"input_ids": input_ids, "conversation": conversation}






					def   __A			( self    ,       A__    ,       A__=10    ,       **A__			):
						A__							:    List[Any]        =		generate_kwargs.get("""max_length"""    ,       self.model.config.max_length			)

						A__							:    Optional[int]        =		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})"""			)
							A__							:    Dict        =		max_length - minimum_tokens
							A__							:    Optional[int]        =		model_inputs["""input_ids"""][:, -trim:]
							if "attention_mask" in model_inputs:
								A__							:    str        =		model_inputs["""attention_mask"""][:, -trim:]
						A__							:    List[str]        =		model_inputs.pop("""conversation"""			)
						A__							:    Dict        =		max_length
						A__							:    str        =		self.model.generate(**A__    ,       **A__			)
						if self.model.config.is_encoder_decoder:
							A__							:    Union[str, Any]        =		1
						else:
							A__							:    Optional[Any]        =		n
						return {"output_ids": output_ids[:, start_position:], "conversation": conversation}






					def   __A			( self    ,       A__    ,       A__=True			):
						A__							:    Dict        =		model_outputs["""output_ids"""]
						A__							:    str        =		self.tokenizer.decode(
						    output_ids[0]    ,       skip_special_tokens=A__    ,       clean_up_tokenization_spaces=A__    ,       )
						A__							:    Optional[int]        =		model_outputs["""conversation"""]
						conversation.mark_processed()
						conversation.append_response(A__			)
						return conversation






					def   __A			( self    ,       A__			):
						A__							:    str        =		self.tokenizer.eos_token_id
						A__							:    Tuple        =		[]
						for is_user, text in conversation.iter_texts():
							if eos_token_id is not None:
								input_ids.extend(self.tokenizer.encode(A__    ,       add_special_tokens=A__			) + [eos_token_id]			)
							else:
								input_ids.extend(self.tokenizer.encode(A__    ,       add_special_tokens=A__			)			)

						if len(A__			) > self.tokenizer.model_max_length:
							A__							:    str        =		input_ids[-self.tokenizer.model_max_length :]
						return input_ids



