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


UpperCamelCase     :    str       =			logging.get_logger(__name__)

class __lowerCAmelCase		:





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

      '''simple docstring'''




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

      __UpperCamelCase      =	conversation_id
      __UpperCamelCase      =	past_user_inputs
      __UpperCamelCase      =	generated_responses
      __UpperCamelCase      =	text





     def __eq__(     self     ,					__UpperCAmelCase				):

      '''simple docstring'''




      if not isinstance(__UpperCAmelCase     ,					__UpperCAmelCase				):
       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     ,					__UpperCAmelCase     ,					__UpperCAmelCase = 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}".'				)
        __UpperCamelCase      =	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:
       __UpperCamelCase      =	text





     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      if self.new_user_input:
       self.past_user_inputs.append(self.new_user_input				)
      __UpperCamelCase      =	None





     def 						UpperCAmelCase (     self     ,					__UpperCAmelCase				):

      '''simple docstring'''




      self.generated_responses.append(__UpperCAmelCase				)





     def 						UpperCAmelCase (     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'''




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


@add_end_docstrings(
    __SCREAMING_SNAKE_CASE      ,  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 __lowerCAmelCase		(				__SCREAMING_SNAKE_CASE      ):





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

      '''simple docstring'''




      super().__init__(*__UpperCAmelCase     ,					**__UpperCAmelCase				)
      if self.tokenizer.pad_token_id is None:
       __UpperCamelCase      =	self.tokenizer.eos_token





     def 						UpperCAmelCase (     self     ,					__UpperCAmelCase=None     ,					__UpperCAmelCase=None     ,					__UpperCAmelCase=None     ,					**__UpperCAmelCase				):

      '''simple docstring'''




      __UpperCamelCase      =	{}
      __UpperCamelCase      =	{}
      __UpperCamelCase      =	{}

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

      if "max_length" in generate_kwargs:
       __UpperCamelCase      =	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:
       __UpperCamelCase      =	clean_up_tokenization_spaces

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





     def __call__(     self     ,					__UpperCAmelCase     ,					__UpperCAmelCase=0     ,					**__UpperCAmelCase				):

      '''simple docstring'''




      __UpperCamelCase      =	super().__call__(__UpperCAmelCase     ,					num_workers=__UpperCAmelCase     ,					**__UpperCAmelCase				)
      if isinstance(__UpperCAmelCase     ,					__UpperCAmelCase				) and len(__UpperCAmelCase				) == 1:
       return outputs[0]
      return outputs





     def 						UpperCAmelCase (     self     ,					__UpperCAmelCase     ,					__UpperCAmelCase=32				):

      '''simple docstring'''




      if not isinstance(__UpperCAmelCase     ,					__UpperCAmelCase				):
       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'				):
       __UpperCamelCase      =	self.tokenizer._build_conversation_input_ids(__UpperCAmelCase				)
      else:
       # If the tokenizer cannot handle conversations, we default to only the old version
       __UpperCamelCase      =	self._legacy_parse_and_tokenize(__UpperCAmelCase				)

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





     def 						UpperCAmelCase (     self     ,					__UpperCAmelCase     ,					__UpperCAmelCase=10     ,					**__UpperCAmelCase				):

      '''simple docstring'''




      __UpperCamelCase      =	generate_kwargs.get('max_length'     ,					self.model.config.max_length				)

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





     def 						UpperCAmelCase (     self     ,					__UpperCAmelCase     ,					__UpperCAmelCase=True				):

      '''simple docstring'''




      __UpperCamelCase      =	model_outputs['output_ids']
      __UpperCamelCase      =	self.tokenizer.decode(
          output_ids[0]     ,					skip_special_tokens=__UpperCAmelCase     ,					clean_up_tokenization_spaces=__UpperCAmelCase     ,					)
      __UpperCamelCase      =	model_outputs['conversation']
      conversation.mark_processed()
      conversation.append_response(__UpperCAmelCase				)
      return conversation







     def 						UpperCAmelCase (     self     ,					__UpperCAmelCase				):

      '''simple docstring'''




      __UpperCamelCase      =	self.tokenizer.eos_token_id
      __UpperCamelCase      =	[]
      for is_user, text in conversation.iter_texts():
       if eos_token_id is not None:
        input_ids.extend(self.tokenizer.encode(__UpperCAmelCase     ,					add_special_tokens=__UpperCAmelCase				) + [eos_token_id]				)
       else:
        input_ids.extend(self.tokenizer.encode(__UpperCAmelCase     ,					add_special_tokens=__UpperCAmelCase				)				)

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





"""simple docstring"""

from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


UpperCamelCase     :    Union[str, Any]       =			logging.get_logger(__name__)

UpperCamelCase     :    int       =			{
    "distilbert-base-uncased": "https://huggingface.co/distilbert-base-uncased/resolve/main/config.json",
    "distilbert-base-uncased-distilled-squad": (
        "https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/config.json"
    ),
    "distilbert-base-cased": "https://huggingface.co/distilbert-base-cased/resolve/main/config.json",
    "distilbert-base-cased-distilled-squad": (
        "https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/config.json"
    ),
    "distilbert-base-german-cased": "https://huggingface.co/distilbert-base-german-cased/resolve/main/config.json",
    "distilbert-base-multilingual-cased": (
        "https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/config.json"
    ),
    "distilbert-base-uncased-finetuned-sst-2-english": (
        "https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english/resolve/main/config.json"
    ),
}

class __lowerCAmelCase		(				__SCREAMING_SNAKE_CASE      ):
     lowercase						       =						"distilbert"
     lowercase						       =						{
         "hidden_size": "dim",
         "num_attention_heads": "n_heads",
         "num_hidden_layers": "n_layers",
     }





     def __init__(     self     ,					__UpperCAmelCase=3_0522     ,					__UpperCAmelCase=512     ,					__UpperCAmelCase=False     ,					__UpperCAmelCase=6     ,					__UpperCAmelCase=12     ,					__UpperCAmelCase=768     ,					__UpperCAmelCase=4 * 768     ,					__UpperCAmelCase=0.1     ,					__UpperCAmelCase=0.1     ,					__UpperCAmelCase="gelu"     ,					__UpperCAmelCase=0.0_2     ,					__UpperCAmelCase=0.1     ,					__UpperCAmelCase=0.2     ,					__UpperCAmelCase=0     ,					**__UpperCAmelCase     ,					):

      '''simple docstring'''




      __UpperCamelCase      =	vocab_size
      __UpperCamelCase      =	max_position_embeddings
      __UpperCamelCase      =	sinusoidal_pos_embds
      __UpperCamelCase      =	n_layers
      __UpperCamelCase      =	n_heads
      __UpperCamelCase      =	dim
      __UpperCamelCase      =	hidden_dim
      __UpperCamelCase      =	dropout
      __UpperCamelCase      =	attention_dropout
      __UpperCamelCase      =	activation
      __UpperCamelCase      =	initializer_range
      __UpperCamelCase      =	qa_dropout
      __UpperCamelCase      =	seq_classif_dropout
      super().__init__(**__UpperCAmelCase     ,					pad_token_id=__UpperCAmelCase				)


class __lowerCAmelCase		(				__SCREAMING_SNAKE_CASE      ):





     @property
     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      if self.task == "multiple-choice":
       __UpperCamelCase      =	{0: 'batch', 1: 'choice', 2: 'sequence'}
      else:
       __UpperCamelCase      =	{0: 'batch', 1: 'sequence'}
      return OrderedDict(
          [
              ('input_ids', dynamic_axis),
              ('attention_mask', dynamic_axis),
          ]				)



