readme: ---
  language:
  - ru
  ---
  
  # Model Card for Model ID
  
  <!-- Provide a quick summary of what the model is/does. -->
  
  
  
  # Model Details
  
  ## Model Description
  
  <!-- Provide a longer summary of what this model is. -->
  
  - **Developed by:** Deeppavlov team
  - **Model type:** seq2seq
  - **Language(s) (NLP):** Russian
  - **License:** MIT
  - **Finetuned from model:** [facebook/mbart-large-50](facebook/mbart-large-50)
  
  ## Model Sources
  
  <!-- Provide the basic links for the model. -->
  
  - **Repository:** [Model commit checkpoint here](https://github.com/dmitrymailk/persona_bot/blob/be6af678bbf08945aa489d748149ea4e94991996/dimweb_persona_bot/experiments/seq2seq_modeling_experiments/seq2seq_hypothesis_3.py#L35)
  - **Wandb metrics:** [wandb dashboard metrics](https://wandb.ai/dimweb/persona_bot_2/runs/1zbrflmj?workspace=user-dimweb)
  - 
  
  # Uses
  
  <!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
  
  
  ## Direct Use
  
  <!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
  
  ```python
  from typing import List, TypedDict
  from dataclasses import dataclass
  from itertools import chain
  
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
  import torch
  
  
  @dataclass
  class H2PersonaChatHyperparametersV1:
      """
      chat_history_pair_length: int - количество пар диалога с конца
      """
  
      model_name: str = "facebook/bart-base"
      chat_history_pair_length: int = 7
  
      persona_max_length: int = 14
      chat_max_length: int = 25
  
      debug_status: int = 0
  
  
  class PersonaChatDatasetSampleV1(TypedDict):
      """
      persona: List[str] - набор предложений фактов персоны
      history: List[str] - набор предложений истории переписки
      """
  
      persona: List[str]
      history: List[str]
      sample_id: str
  
  
  class H2Seq2SeqInferenceSampleDictV1(TypedDict):
      input_ids: List[int]
      attention_mask: List[int]
  
  
  class H2Seq2SeqInferenceSampleDictV2(TypedDict):
      input_ids: torch.Tensor
      attention_mask: torch.Tensor
  
  
  def flat_list(list_of_lists: List[List]) -> List:
      return list(chain.from_iterable(list_of_lists))
  
  
  class H2Seq2SeqInferencePersonaSampleV1:
      def __init__(
          self,
          dataset_sample: PersonaChatDatasetSampleV1,
          tokenizer: AutoTokenizer,
          hyperparameters: H2PersonaChatHyperparametersV1,
      ) -> None:
          self.dataset_sample = dataset_sample
          self.tokenizer = tokenizer
          self.hyperparameters = hyperparameters
  
      def add_spaces_after(
          self,
          items: List[str],
      ) -> List[str]:
          items = [item + " " for item in items]
          return items
  
      @property
      def bos_token_id(self):
          if "t5" in self.hyperparameters.model_name:
              return []
  
          if self.tokenizer.bos_token_id is None:
              return []
  
          return [self.tokenizer.bos_token_id]
  
      @property
      def eos_token_id(self):
          if self.tokenizer.eos_token_id is None:
              return []
  
          return [self.tokenizer.eos_token_id]
  
      def add_sep_beetween(self, items: List[str], sep=" EOS ") -> List[str]:
          for i in range(1, len(items)):
              items[i] = sep + items[i]
  
          return items
  
      def add_spaces_between(self, items: List[str]) -> List[str]:
          items = self.add_spaces_after(items)
          items[-1] = items[-1].strip()
          return items
  
      def get_sample(self) -> H2Seq2SeqInferenceSampleDictV1:
  
          dialog_history = self.dataset_sample["history"]
          dialog_history = dialog_history[-self.hyperparameters.chat_history_pair_length * 2 - 1 :]
          dialog_history = self.add_sep_beetween(dialog_history)
  
          persona = self.dataset_sample["persona"]
          persona = self.add_sep_beetween(
              persona,
              sep=" ",
          )
  
          KNOWLEDGE_IDS = self.tokenizer.encode(
              " [KNOWLEDGE] ",
              add_special_tokens=False,
          )
          CONTEXT_IDS = self.tokenizer.encode(
              " [CONTEXT]",
              add_special_tokens=False,
          )
  
          encoded_history = self.tokenizer.batch_encode_plus(
              dialog_history,
              add_special_tokens=False,
              truncation=True,
              max_length=self.hyperparameters.chat_max_length,
          )
          encoded_history = flat_list(encoded_history["input_ids"])
  
          encoded_persona = self.tokenizer.batch_encode_plus(
              persona,
              add_special_tokens=False,
              truncation=True,
              max_length=self.hyperparameters.persona_max_length,
          )
  
          encoded_persona = flat_list(encoded_persona["input_ids"])
  
          input_ids = [
              *self.bos_token_id,
              *CONTEXT_IDS,
              *encoded_history,
              *KNOWLEDGE_IDS,
              *encoded_persona,
              *self.eos_token_id,
          ]
  
          attention_mask = [1] * len(input_ids)
  
          return H2Seq2SeqInferenceSampleDictV1(
              input_ids=inpu