




"""simple docstring"""




from __future__ import annotations

import unittest

from transformers import DistilBertConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
					import tensorflow as tf

					from transformers.models.distilbert.modeling_tf_distilbert import (
					    TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
					    TFDistilBertForMaskedLM,
					    TFDistilBertForMultipleChoice,
					    TFDistilBertForQuestionAnswering,
					    TFDistilBertForSequenceClassification,
					    TFDistilBertForTokenClassification,
					    TFDistilBertModel,
					)






class 					__magic_name__							:






				'''simple docstring'''





				def __init__( self	,				_a	,				):





							"""simple docstring"""
							lowerCamelCase       =      parent
							lowerCamelCase       =      13
							lowerCamelCase       =      7
							lowerCamelCase       =      True
							lowerCamelCase       =      True
							lowerCamelCase       =      False
							lowerCamelCase       =      True
							lowerCamelCase       =      99
							lowerCamelCase       =      32
							lowerCamelCase       =      2
							lowerCamelCase       =      4
							lowerCamelCase       =      37
							lowerCamelCase       =      """gelu"""
							lowerCamelCase       =      0.1
							lowerCamelCase       =      0.1
							lowerCamelCase       =      512
							lowerCamelCase       =      16
							lowerCamelCase       =      2
							lowerCamelCase       =      0.02
							lowerCamelCase       =      3
							lowerCamelCase       =      4
							lowerCamelCase       =      None





				def      _lowerCAmelCase       ( self				):





							"""simple docstring"""
							lowerCamelCase       =      ids_tensor([self.batch_size, self.seq_length]	,				self.vocab_size				)

							lowerCamelCase       =      None
							if self.use_input_mask:
										lowerCamelCase       =      random_attention_mask([self.batch_size, self.seq_length]				)

							lowerCamelCase       =      None
							lowerCamelCase       =      None
							lowerCamelCase       =      None
							if self.use_labels:
										lowerCamelCase       =      ids_tensor([self.batch_size]	,				self.type_sequence_label_size				)
										lowerCamelCase       =      ids_tensor([self.batch_size, self.seq_length]	,				self.num_labels				)
										lowerCamelCase       =      ids_tensor([self.batch_size]	,				self.num_choices				)

							lowerCamelCase       =      DistilBertConfig(
							    vocab_size=self.vocab_size	,				dim=self.hidden_size	,				n_layers=self.num_hidden_layers	,				n_heads=self.num_attention_heads	,				hidden_dim=self.intermediate_size	,				hidden_act=self.hidden_act	,				dropout=self.hidden_dropout_prob	,				attention_dropout=self.attention_probs_dropout_prob	,				max_position_embeddings=self.max_position_embeddings	,				initializer_range=self.initializer_range	,				)

							return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels





				def      _lowerCAmelCase       ( self	,				_a	,				_a	,				_a	,				_a	,				_a	,				_a				):





							"""simple docstring"""
							lowerCamelCase       =      TFDistilBertModel(config=lowercase__				)
							lowerCamelCase       =      {"""input_ids""": input_ids, """attention_mask""": input_mask}

							lowerCamelCase       =      model(lowercase__				)

							lowerCamelCase       =      [input_ids, input_mask]

							lowerCamelCase       =      model(lowercase__				)

							self.parent.assertEqual(result.last_hidden_state.shape	,				(self.batch_size, self.seq_length, self.hidden_size)				)





				def      _lowerCAmelCase       ( self	,				_a	,				_a	,				_a	,				_a	,				_a	,				_a				):





							"""simple docstring"""
							lowerCamelCase       =      TFDistilBertForMaskedLM(config=lowercase__				)
							lowerCamelCase       =      {"""input_ids""": input_ids, """attention_mask""": input_mask}
							lowerCamelCase       =      model(lowercase__				)
							self.parent.assertEqual(result.logits.shape	,				(self.batch_size, self.seq_length, self.vocab_size)				)





				def      _lowerCAmelCase       ( self	,				_a	,				_a	,				_a	,				_a	,				_a	,				_a				):





							"""simple docstring"""
							lowerCamelCase       =      TFDistilBertForQuestionAnswering(config=lowercase__				)
							lowerCamelCase       =      {
							    """input_ids""": input_ids,
							    """attention_mask""": input_mask,
							}
							lowerCamelCase       =      model(lowercase__				)
							self.parent.assertEqual(result.start_logits.shape	,				(self.batch_size, self.seq_length)				)
							self.parent.assertEqual(result.end_logits.shape	,				(self.batch_size, self.seq_length)				)





				def      _lowerCAmelCase       ( self	,				_a	,				_a	,				_a	,				_a	,				_a	,				_a				):





							"""simple docstring"""
							lowerCamelCase       =      self.num_labels
							lowerCamelCase       =      TFDistilBertForSequenceClassification(lowercase__				)
							lowerCamelCase       =      {"""input_ids""": input_ids, """attention_mask""": input_mask}
							lowerCamelCase       =      model(lowercase__				)
							self.parent.assertEqual(result.logits.shape	,				(self.batch_size, self.num_labels)				)





				def      _lowerCAmelCase       ( self	,				_a	,				_a	,				_a	,				_a	,				_a	,				_a				):





							"""simple docstring"""
							lowerCamelCase       =      self.num_choices
							lowerCamelCase       =      TFDistilBertForMultipleChoice(lowercase__				)
							lowerCamelCase       =      tf.tile(tf.expand_dims(lowercase__	,				1				)	,				(1, self.num_choices, 1)				)
							lowerCamelCase       =      tf.tile(tf.expand_dims(lowercase__	,				1				)	,				(1, self.num_choices, 1)				)
							lowerCamelCase       =      {
							    """input_ids""": multiple_choice_inputs_ids,
							    """attention_mask""": multiple_choice_input_mask,
							}
							lowerCamelCase       =      model(lowercase__				)
							self.parent.assertEqual(result.logits.shape	,				(self.batch_size, self.num_choices)				)





				def      _lowerCAmelCase       ( self	,				_a	,				_a	,				_a	,				_a	,				_a	,				_a				):





							"""simple docstring"""
							lowerCamelCase       =      self.num_labels
							lowerCamelCase       =      TFDistilBertForTokenClassification(lowercase__				)
							lowerCamelCase       =      {"""input_ids""": input_ids, """attention_mask""": input_mask}
							lowerCamelCase       =      model(lowercase__				)
							self.parent.assertEqual(result.logits.shape	,				(self.batch_size, self.seq_length, self.num_labels)				)





				def      _lowerCAmelCase       ( self				):





							"""simple docstring"""
							lowerCamelCase       =      self.prepare_config_and_inputs()
							(lowerCamelCase)       =      config_and_inputs
							lowerCamelCase       =      {"""input_ids""": input_ids, """attention_mask""": input_mask}
							return config, inputs_dict






@require_tf
class 					__magic_name__							( _UpperCAmelCase		,      _UpperCAmelCase		,      unittest.TestCase       ):






				'''simple docstring'''
				__UpperCamelCase    						=		(
				    (
				        TFDistilBertModel,
				        TFDistilBertForMaskedLM,
				        TFDistilBertForQuestionAnswering,
				        TFDistilBertForSequenceClassification,
				        TFDistilBertForTokenClassification,
				        TFDistilBertForMultipleChoice,
				    )
				    if is_tf_available()
				    else None
				)
				__UpperCamelCase    						=		(
				    {
				        """feature-extraction""": TFDistilBertModel,
				        """fill-mask""": TFDistilBertForMaskedLM,
				        """question-answering""": TFDistilBertForQuestionAnswering,
				        """text-classification""": TFDistilBertForSequenceClassification,
				        """token-classification""": TFDistilBertForTokenClassification,
				        """zero-shot""": TFDistilBertForSequenceClassification,
				    }
				    if is_tf_available()
				    else {}
				)
				__UpperCamelCase    						=		False
				__UpperCamelCase    						=		False





				def      _lowerCAmelCase       ( self				):





							"""simple docstring"""
							lowerCamelCase       =      TFDistilBertModelTester(self				)
							lowerCamelCase       =      ConfigTester(self	,				config_class=lowercase__	,				dim=37				)





				def      _lowerCAmelCase       ( self				):





							"""simple docstring"""
							self.config_tester.run_common_tests()





				def      _lowerCAmelCase       ( self				):





							"""simple docstring"""
							lowerCamelCase       =      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_distilbert_model(*lowercase__				)





				def      _lowerCAmelCase       ( self				):





							"""simple docstring"""
							lowerCamelCase       =      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_distilbert_for_masked_lm(*lowercase__				)





				def      _lowerCAmelCase       ( self				):





							"""simple docstring"""
							lowerCamelCase       =      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_distilbert_for_question_answering(*lowercase__				)





				def      _lowerCAmelCase       ( self				):





							"""simple docstring"""
							lowerCamelCase       =      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_distilbert_for_sequence_classification(*lowercase__				)





				def      _lowerCAmelCase       ( self				):





							"""simple docstring"""
							lowerCamelCase       =      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_distilbert_for_multiple_choice(*lowercase__				)





				def      _lowerCAmelCase       ( self				):





							"""simple docstring"""
							lowerCamelCase       =      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_distilbert_for_token_classification(*lowercase__				)





				@slow
				def      _lowerCAmelCase       ( self				):





							"""simple docstring"""
							for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]				):
										lowerCamelCase       =      TFDistilBertModel.from_pretrained(lowercase__				)
										self.assertIsNotNone(lowercase__				)






@require_tf
class 					__magic_name__							( unittest.TestCase       ):






				'''simple docstring'''





				@slow
				def      _lowerCAmelCase       ( self				):





							"""simple docstring"""
							lowerCamelCase       =      TFDistilBertModel.from_pretrained("""distilbert-base-uncased"""				)
							lowerCamelCase       =      tf.constant([[0, 1, 2, 3, 4, 5]]				)
							lowerCamelCase       =      model(lowercase__				)[0]

							lowerCamelCase       =      [1, 6, 768]
							self.assertEqual(output.shape	,				lowercase__				)

							lowerCamelCase       =      tf.constant(
							    [
							        [
							            [0.19_261_885, -0.13_732_955, 0.4_119_799],
							            [0.22_150_156, -0.07_422_661, 0.39_037_204],
							            [0.22_756_018, -0.0_896_414, 0.3_701_467],
							        ]
							    ]				)
							tf.debugging.assert_near(output[:, :3, :3]	,				lowercase__	,				atol=1e-4				)









"""simple docstring"""




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


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






class 					__magic_name__							:






				'''simple docstring'''





				def __init__( self	,				_a = None	,				_a = None	,				_a=None	,				_a=None				):





							"""simple docstring"""
							if not conversation_id:
										lowerCamelCase       =      uuid.uuida()
							if past_user_inputs is None:
										lowerCamelCase       =      []
							if generated_responses is None:
										lowerCamelCase       =      []

							lowerCamelCase       =      conversation_id
							lowerCamelCase       =      past_user_inputs
							lowerCamelCase       =      generated_responses
							lowerCamelCase       =      text





				def __eq__( self	,				_a				):





							"""simple docstring"""
							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      _lowerCAmelCase       ( self	,				_a	,				_a = False				):





							"""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       =      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       =      text





				def      _lowerCAmelCase       ( self				):





							"""simple docstring"""
							if self.new_user_input:
										self.past_user_inputs.append(self.new_user_input				)
							lowerCamelCase       =      None





				def      _lowerCAmelCase       ( self	,				_a				):





							"""simple docstring"""
							self.generated_responses.append(_a				)





				def      _lowerCAmelCase       ( self				):





							"""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				):





							"""simple docstring"""
							lowerCamelCase       =      f'Conversation id: {self.uuid} \n'
							for is_user, text in self.iter_texts():
										lowerCamelCase       =      """user""" if is_user else """bot"""
										output += f'{name} >> {text} \n'
							return output






@add_end_docstrings(
    UpperCAmelCase__		,      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 					__magic_name__							( UpperCAmelCase__       ):






				'''simple docstring'''





				def __init__( self	,				*_a	,				**_a				):





							"""simple docstring"""
							super().__init__(*_a	,				**_a				)
							if self.tokenizer.pad_token_id is None:
										lowerCamelCase       =      self.tokenizer.eos_token





				def      _lowerCAmelCase       ( self	,				_a=None	,				_a=None	,				_a=None	,				**_a				):





							"""simple docstring"""
							lowerCamelCase       =      {}
							lowerCamelCase       =      {}
							lowerCamelCase       =      {}

							if min_length_for_response is not None:
										lowerCamelCase       =      min_length_for_response
							if minimum_tokens is not None:
										lowerCamelCase       =      minimum_tokens

							if "max_length" in generate_kwargs:
										lowerCamelCase       =      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       =      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				):





							"""simple docstring"""
							lowerCamelCase       =      super().__call__(_a	,				num_workers=_a	,				**_a				)
							if isinstance(_a	,				_a				) and len(_a				) == 1:
										return outputs[0]
							return outputs





				def      _lowerCAmelCase       ( self	,				_a	,				_a=32				):





							"""simple docstring"""
							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"""				):
										lowerCamelCase       =      self.tokenizer._build_conversation_input_ids(_a				)
							else:
										# If the tokenizer cannot handle conversations, we default to only the old version
										lowerCamelCase       =      self._legacy_parse_and_tokenize(_a				)

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





				def      _lowerCAmelCase       ( self	,				_a	,				_a=10	,				**_a				):





							"""simple docstring"""
							lowerCamelCase       =      generate_kwargs.get("""max_length"""	,				self.model.config.max_length				)

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





				def      _lowerCAmelCase       ( self	,				_a	,				_a=True				):





							"""simple docstring"""
							lowerCamelCase       =      model_outputs["""output_ids"""]
							lowerCamelCase       =      self.tokenizer.decode(
							    output_ids[0]	,				skip_special_tokens=_a	,				clean_up_tokenization_spaces=_a	,				)
							lowerCamelCase       =      model_outputs["""conversation"""]
							conversation.mark_processed()
							conversation.append_response(_a				)
							return conversation





				def      _lowerCAmelCase       ( self	,				_a				):





							"""simple docstring"""
							lowerCamelCase       =      self.tokenizer.eos_token_id
							lowerCamelCase       =      []
							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:
										lowerCamelCase       =      input_ids[-self.tokenizer.model_max_length :]
							return input_ids



