





import argparse

import requests
import torch

# pip3 install salesforce-lavis
# I'm actually installing a slightly modified version: pip3 install git+https://github.com/nielsrogge/LAVIS.git@fix_lavis
from lavis.models import load_model_and_preprocess
from PIL import Image

from transformers import (
    AutoTokenizer,
    BlipaConfig,
    BlipaForConditionalGeneration,
    BlipaProcessor,
    BlipaVisionConfig,
    BlipImageProcessor,
    OPTConfig,
    TaConfig,
)
from transformers.utils.constants import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD





def lowerCAmelCase__()				->      Any:







	'''simple docstring'''







	lowerCamelCase__								=		'''https://storage.googleapis.com/sfr-vision-language-research/LAVIS/assets/merlion.png'''
	lowerCamelCase__								=		Image.open(requests.get(__snake_case       ,stream=__snake_case							).raw							).convert('''RGB'''							)

	return image





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







	'''simple docstring'''







	lowerCamelCase__								=		[]
	# fmt: off

	# vision encoder
	rename_keys.append(('''visual_encoder.cls_token''', '''vision_model.embeddings.class_embedding''')							)
	rename_keys.append(('''visual_encoder.pos_embed''', '''vision_model.embeddings.position_embedding''')							)
	rename_keys.append(('''visual_encoder.patch_embed.proj.weight''', '''vision_model.embeddings.patch_embedding.weight''')							)
	rename_keys.append(('''visual_encoder.patch_embed.proj.bias''', '''vision_model.embeddings.patch_embedding.bias''')							)
	rename_keys.append(('''ln_vision.weight''', '''vision_model.post_layernorm.weight''')							)
	rename_keys.append(('''ln_vision.bias''', '''vision_model.post_layernorm.bias''')							)

	for i in range(config.vision_config.num_hidden_layers							):
		rename_keys.append((F'visual_encoder.blocks.{i}.norm1.weight', F'vision_model.encoder.layers.{i}.layer_norm1.weight')							)
		rename_keys.append((F'visual_encoder.blocks.{i}.norm1.bias', F'vision_model.encoder.layers.{i}.layer_norm1.bias')							)
		rename_keys.append((F'visual_encoder.blocks.{i}.norm2.weight', F'vision_model.encoder.layers.{i}.layer_norm2.weight')							)
		rename_keys.append((F'visual_encoder.blocks.{i}.norm2.bias', F'vision_model.encoder.layers.{i}.layer_norm2.bias')							)
		rename_keys.append((F'visual_encoder.blocks.{i}.attn.qkv.weight', F'vision_model.encoder.layers.{i}.self_attn.qkv.weight')							)
		rename_keys.append((F'visual_encoder.blocks.{i}.attn.proj.weight', F'vision_model.encoder.layers.{i}.self_attn.projection.weight',)							)
		rename_keys.append((F'visual_encoder.blocks.{i}.attn.proj.bias', F'vision_model.encoder.layers.{i}.self_attn.projection.bias')							)
		rename_keys.append((F'visual_encoder.blocks.{i}.mlp.fc1.weight', F'vision_model.encoder.layers.{i}.mlp.fc1.weight')							)
		rename_keys.append((F'visual_encoder.blocks.{i}.mlp.fc1.bias', F'vision_model.encoder.layers.{i}.mlp.fc1.bias')							)
		rename_keys.append((F'visual_encoder.blocks.{i}.mlp.fc2.weight', F'vision_model.encoder.layers.{i}.mlp.fc2.weight')							)
		rename_keys.append((F'visual_encoder.blocks.{i}.mlp.fc2.bias', F'vision_model.encoder.layers.{i}.mlp.fc2.bias')							)

	# QFormer
	rename_keys.append(('''Qformer.bert.embeddings.LayerNorm.weight''', '''qformer.layernorm.weight''')							)
	rename_keys.append(('''Qformer.bert.embeddings.LayerNorm.bias''', '''qformer.layernorm.bias''')							)

	# fmt: on
	return rename_keys





def lowerCAmelCase__(__snake_case       ,__snake_case       ,__snake_case							)				->      List[Any]:







	'''simple docstring'''







	lowerCamelCase__								=		dct.pop(__snake_case							)
	lowerCamelCase__								=		val





def lowerCAmelCase__(__snake_case       ,__snake_case							)				->      Tuple:







	'''simple docstring'''







	for i in range(config.vision_config.num_hidden_layers							):
		# read in original q and v biases
		lowerCamelCase__								=		state_dict.pop(F'visual_encoder.blocks.{i}.attn.q_bias'							)
		lowerCamelCase__								=		state_dict.pop(F'visual_encoder.blocks.{i}.attn.v_bias'							)

		# next, set bias in the state dict
		lowerCamelCase__								=		torch.cat((q_bias, torch.zeros_like(__snake_case       ,requires_grad=__snake_case							), v_bias)							)
		lowerCamelCase__								=		qkv_bias





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







	'''simple docstring'''







	lowerCamelCase__								=		364 if '''coco''' in model_name else 224
	lowerCamelCase__								=		BlipaVisionConfig(image_size=__snake_case							).to_dict()

	# make sure the models have proper bos_token_id and eos_token_id set (important for generation)
	# seems like flan-T5 models don't have bos_token_id properly set?
	if "opt-2.7b" in model_name:
		lowerCamelCase__								=		OPTConfig.from_pretrained('''facebook/opt-2.7b'''       ,eos_token_id=__snake_case							).to_dict()
	elif "opt-6.7b" in model_name:
		lowerCamelCase__								=		OPTConfig.from_pretrained('''facebook/opt-6.7b'''       ,eos_token_id=__snake_case							).to_dict()
	elif "t5-xl" in model_name:
		lowerCamelCase__								=		TaConfig.from_pretrained('''google/flan-t5-xl'''       ,dense_act_fn='''gelu'''       ,bos_token_id=1							).to_dict()
	elif "t5-xxl" in model_name:
		lowerCamelCase__								=		TaConfig.from_pretrained('''google/flan-t5-xxl'''       ,dense_act_fn='''gelu'''       ,bos_token_id=1							).to_dict()

	lowerCamelCase__								=		BlipaConfig(vision_config=__snake_case       ,text_config=__snake_case							)

	return config, image_size





@torch.no_grad()
def lowerCAmelCase__(__snake_case       ,__snake_case=None       ,__snake_case=False							)				->      str:







	'''simple docstring'''







	lowerCamelCase__								=		(
	    AutoTokenizer.from_pretrained('''facebook/opt-2.7b'''							)
	    if '''opt''' in model_name
	    else AutoTokenizer.from_pretrained('''google/flan-t5-xl'''							)
	)
	lowerCamelCase__								=		tokenizer('''\n'''       ,add_special_tokens=__snake_case							).input_ids[0]
	lowerCamelCase__				,	lowerCamelCase__								=		get_blipa_config(__snake_case       ,eos_token_id=__snake_case							)

	lowerCamelCase__								=		BlipaForConditionalGeneration(__snake_case							).eval()

	lowerCamelCase__								=		{
	    '''blip2-opt-2.7b''': ('''blip2_opt''', '''pretrain_opt2.7b'''),
	    '''blip2-opt-6.7b''': ('''blip2_opt''', '''pretrain_opt6.7b'''),
	    '''blip2-opt-2.7b-coco''': ('''blip2_opt''', '''caption_coco_opt2.7b'''),
	    '''blip2-opt-6.7b-coco''': ('''blip2_opt''', '''caption_coco_opt6.7b'''),
	    '''blip2-flan-t5-xl''': ('''blip2_t5''', '''pretrain_flant5xl'''),
	    '''blip2-flan-t5-xl-coco''': ('''blip2_t5''', '''caption_coco_flant5xl'''),
	    '''blip2-flan-t5-xxl''': ('''blip2_t5''', '''pretrain_flant5xxl'''),
	}

	lowerCamelCase__				,	lowerCamelCase__								=		model_name_to_original[model_name]

	# load original model
	print('''Loading original model...'''							)
	lowerCamelCase__								=		'''cuda''' if torch.cuda.is_available() else '''cpu'''
	lowerCamelCase__				,	lowerCamelCase__				,	lowerCamelCase__								=		load_model_and_preprocess(
	    name=__snake_case       ,model_type=__snake_case       ,is_eval=__snake_case       ,device=__snake_case							)
	original_model.eval()
	print('''Done!'''							)

	# update state dict keys
	lowerCamelCase__								=		original_model.state_dict()
	lowerCamelCase__								=		create_rename_keys(__snake_case							)
	for src, dest in rename_keys:
		rename_key(__snake_case       ,__snake_case       ,__snake_case							)

	# some keys can be renamed efficiently
	for key, val in state_dict.copy().items():
		lowerCamelCase__								=		state_dict.pop(__snake_case							)
		if key.startswith('''Qformer.bert'''							):
			lowerCamelCase__								=		key.replace('''Qformer.bert'''       ,'''qformer'''							)
		if "attention.self" in key:
			lowerCamelCase__								=		key.replace('''self'''       ,'''attention'''							)
		if "opt_proj" in key:
			lowerCamelCase__								=		key.replace('''opt_proj'''       ,'''language_projection'''							)
		if "t5_proj" in key:
			lowerCamelCase__								=		key.replace('''t5_proj'''       ,'''language_projection'''							)
		if key.startswith('''opt'''							):
			lowerCamelCase__								=		key.replace('''opt'''       ,'''language'''							)
		if key.startswith('''t5'''							):
			lowerCamelCase__								=		key.replace('''t5'''       ,'''language'''							)
		lowerCamelCase__								=		val

	# read in qv biases
	read_in_q_v_bias(__snake_case       ,__snake_case							)

	lowerCamelCase__				,	lowerCamelCase__								=		hf_model.load_state_dict(__snake_case       ,strict=__snake_case							)
	assert len(__snake_case							) == 0
	assert unexpected_keys == ["qformer.embeddings.position_ids"]

	lowerCamelCase__								=		load_demo_image()
	lowerCamelCase__								=		vis_processors['''eval'''](__snake_case							).unsqueeze(0							).to(__snake_case							)
	lowerCamelCase__								=		tokenizer(['''\n''']       ,return_tensors='''pt'''							).input_ids.to(__snake_case							)

	# create processor
	lowerCamelCase__								=		BlipImageProcessor(
	    size={'''height''': image_size, '''width''': image_size}       ,image_mean=__snake_case       ,image_std=__snake_case							)
	lowerCamelCase__								=		BlipaProcessor(image_processor=__snake_case       ,tokenizer=__snake_case							)
	lowerCamelCase__								=		processor(images=__snake_case       ,return_tensors='''pt'''							).pixel_values.to(__snake_case							)

	# make sure processor creates exact same pixel values
	assert torch.allclose(__snake_case       ,__snake_case							)

	original_model.to(__snake_case							)
	hf_model.to(__snake_case							)
	with torch.no_grad():
		if "opt" in model_name:
			lowerCamelCase__								=		original_model({'''image''': original_pixel_values, '''text_input''': ['''''']}							).logits
			lowerCamelCase__								=		hf_model(__snake_case       ,__snake_case							).logits
		else:
			lowerCamelCase__								=		original_model(
			    {'''image''': original_pixel_values, '''text_input''': ['''\n'''], '''text_output''': ['''\n''']}							).logits
			lowerCamelCase__								=		input_ids.masked_fill(input_ids == tokenizer.pad_token_id       ,-100							)
			lowerCamelCase__								=		hf_model(__snake_case       ,__snake_case       ,labels=__snake_case							).logits

	assert original_logits.shape == logits.shape
	print('''First values of original logits:'''       ,original_logits[0, :3, :3]							)
	print('''First values of HF logits:'''       ,logits[0, :3, :3]							)

	# assert values
	if model_name == "blip2-flan-t5-xl":
		lowerCamelCase__								=		torch.tensor(
		    [[-4_1.5_8_5_0, -4.4_4_4_0, -8.9_9_2_2], [-4_7.4_3_2_2, -5.9_1_4_3, -1.7_3_4_0]]       ,device=__snake_case							)
		assert torch.allclose(logits[0, :3, :3]       ,__snake_case       ,atol=1E-4							)
	elif model_name == "blip2-flan-t5-xl-coco":
		lowerCamelCase__								=		torch.tensor(
		    [[-5_7.0_1_0_9, -9.8_9_6_7, -1_2.6_2_8_0], [-6_8.6_5_7_8, -1_2.7_1_9_1, -1_0.5_0_6_5]]       ,device=__snake_case							)
	else:
		# cast to same type
		lowerCamelCase__								=		logits.dtype
		assert torch.allclose(original_logits.to(__snake_case							)       ,__snake_case       ,atol=1E-2							)
	print('''Looks ok!'''							)

	print('''Generating a caption...'''							)
	lowerCamelCase__								=		''''''
	lowerCamelCase__								=		tokenizer(__snake_case       ,return_tensors='''pt'''							).input_ids.to(__snake_case							)

	lowerCamelCase__								=		original_model.generate({'''image''': original_pixel_values}							)
	lowerCamelCase__								=		hf_model.generate(
	    __snake_case       ,__snake_case       ,do_sample=__snake_case       ,num_beams=5       ,max_length=30       ,min_length=1       ,top_p=0.9       ,repetition_penalty=1.0       ,length_penalty=1.0       ,temperature=1       ,)
	print('''Original generation:'''       ,__snake_case							)
	lowerCamelCase__								=		input_ids.shape[1]
	lowerCamelCase__								=		processor.batch_decode(outputs[:, prompt_length:]       ,skip_special_tokens=__snake_case							)
	lowerCamelCase__								=		[text.strip() for text in output_text]
	print('''HF generation:'''       ,__snake_case							)

	if pytorch_dump_folder_path is not None:
		processor.save_pretrained(__snake_case							)
		hf_model.save_pretrained(__snake_case							)

	if push_to_hub:
		processor.push_to_hub(F'nielsr/{model_name}'							)
		hf_model.push_to_hub(F'nielsr/{model_name}'							)


if __name__ == "__main__":
			_a 						=      argparse.ArgumentParser()
			_a 						=      [
			    "blip2-opt-2.7b",
			    "blip2-opt-6.7b",
			    "blip2-opt-2.7b-coco",
			    "blip2-opt-6.7b-coco",
			    "blip2-flan-t5-xl",
			    "blip2-flan-t5-xl-coco",
			    "blip2-flan-t5-xxl",
			]
			parser.add_argument(
			    "--model_name",
			    default="blip2-opt-2.7b",
			    choices=choices,
			    type=str,
			    help="Path to hf config.json of model to convert",
			)
			parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.")
			parser.add_argument(
			    "--push_to_hub",
			    action="store_true",
			    help="Whether to push the model and processor to the hub after converting",
			)

			_a 						=      parser.parse_args()

			convert_blipa_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)










import os
import re
import unicodedata
from shutil import copyfile
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union

import sentencepiece as spm

from ...tokenization_utils import PreTrainedTokenizer
from ...utils import is_torch_available, logging


if is_torch_available():
			import torch


if TYPE_CHECKING:
			from transformers.pipelines.conversational import Conversation


_a 						=      logging.get_logger(__name__)
_a 						=      {"vocab_file": "spiece.model"}

_a 						=      {
    "vocab_file": {
        "AI-Sweden/gpt-sw3-126m": "https://huggingface.co/AI-Sweden/gpt-sw3-126m/resolve/main/spiece.model",
        "AI-Sweden/gpt-sw3-350m": "https://huggingface.co/AI-Sweden/gpt-sw3-350m/resolve/main/spiece.model",
        "AI-Sweden/gpt-sw3-1.6b": "https://huggingface.co/AI-Sweden/gpt-sw3-1.6b/resolve/main/spiece.model",
        "AI-Sweden/gpt-sw3-6.7b": "https://huggingface.co/AI-Sweden/gpt-sw3-6.7b/resolve/main/spiece.model",
        "AI-Sweden/gpt-sw3-20b": "https://huggingface.co/AI-Sweden/gpt-sw3-20b/resolve/main/spiece.model",
    }
}

_a 						=      {
    "AI-Sweden/gpt-sw3-126m": 2_048,
    "AI-Sweden/gpt-sw3-350m": 2_048,
    "AI-Sweden/gpt-sw3-1.6b": 2_048,
    "AI-Sweden/gpt-sw3-6.7b": 2_048,
    "AI-Sweden/gpt-sw3-20b": 2_048,
}






class 					__A    (    lowerCAmelCase ):

			'''simple docstring'''







			lowerCAmelCase_							     =						VOCAB_FILES_NAMES
			lowerCAmelCase_							     =						PRETRAINED_VOCAB_FILES_MAP
			lowerCAmelCase_							     =						PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
			lowerCAmelCase_							     =						["""input_ids""", """attention_mask"""]





			def __init__(    self       ,  __lowerCAmelCase       ,  __lowerCAmelCase=False       ,  __lowerCAmelCase=False       ,  __lowerCAmelCase=False       ,  __lowerCAmelCase=None       ,  __lowerCAmelCase=None       ,  __lowerCAmelCase=None       ,  __lowerCAmelCase=None       ,  __lowerCAmelCase = None       ,  **__lowerCAmelCase       ,  ):



				'''simple docstring'''







				lowerCamelCase__								=		{} if sp_model_kwargs is None else sp_model_kwargs

				lowerCamelCase__								=		kwargs.get('''name_or_path'''							)
				if name_or_path is None:
					logger.warning(
					    '''name_or_path not provided, will work for all GPTSw3 models except gpt-sw3-7b,'''
					    ''' you are testing the model, this can safely be ignored'''							)
					lowerCamelCase__								=		'''None'''

				# Default definitions for our 2 tokenizer versions, with None-checks to enable proper testing
				lowerCamelCase__								=		'''<|endoftext|>''' if eos_token is None else eos_token
				lowerCamelCase__								=		'''<unk>''' if unk_token is None else unk_token
				if "gpt-sw3-7b" in name_or_path:
					lowerCamelCase__								=		unk_token if pad_token is None else pad_token
					lowerCamelCase__								=		eos_token if bos_token is None else bos_token
				else:
					lowerCamelCase__								=		'''<pad>''' if pad_token is None else pad_token
					lowerCamelCase__								=		'''<s>''' if bos_token is None else bos_token

				super().__init__(
				    do_lower_case=__lowerCAmelCase       ,  remove_space=__lowerCAmelCase       ,  keep_accents=__lowerCAmelCase       ,  bos_token=__lowerCAmelCase       ,  eos_token=__lowerCAmelCase       ,  unk_token=__lowerCAmelCase       ,  pad_token=__lowerCAmelCase       ,  sp_model_kwargs=self.sp_model_kwargs       ,  **__lowerCAmelCase       ,  )

				lowerCamelCase__								=		do_lower_case
				lowerCamelCase__								=		remove_space
				lowerCamelCase__								=		keep_accents
				lowerCamelCase__								=		vocab_file

				lowerCamelCase__								=		spm.SentencePieceProcessor(**self.sp_model_kwargs							)
				self.sp_model.Load(__lowerCAmelCase							)

				# Used for whitespace normalization in input texts
				# fmt : off
				lowerCamelCase__								=		{''' ''', ''' ''', ''' ''', ''' ''', ''' ''', '''　''', ''' ''', ''' ''', ''' ''', ''' ''', '''￼''', ''''''}
				# fmt : on

				# Regular expression to remove non-printing characters (e.g. some unicode control chars) in preprocessing
				lowerCamelCase__								=		re.compile(
				    F'[{"".join(map(__lowerCAmelCase       ,  list(range(0       ,  9							)							) + list(range(1_1       ,  3_2							)							) + list(range(1_2_7       ,  1_6_0							)							) + [1_6_0, 1_7_3, 8_2_0_3]							)							)}]'							)





			def __getstate__(    self							):



				'''simple docstring'''







				lowerCamelCase__								=		self.__dict__.copy()
				lowerCamelCase__								=		None
				return state





			def __setstate__(    self       ,  __lowerCAmelCase							):



				'''simple docstring'''







				lowerCamelCase__								=		d

				# for backward compatibility
				if not hasattr(self       ,  '''sp_model_kwargs'''							):
					lowerCamelCase__								=		{}

				lowerCamelCase__								=		spm.SentencePieceProcessor(**self.sp_model_kwargs							)
				self.sp_model.Load(self.vocab_file							)





			@property
			# Copied from transformers.models.albert.tokenization_albert.AlbertTokenizer.vocab_size
			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				return len(self.sp_model							)





			def 		__lowerCamelCase				(    self       ,  __lowerCAmelCase							):



				'''simple docstring'''







				lowerCamelCase__								=		self.non_printing_characters_re.sub(''''''       ,  __lowerCAmelCase							)

				# Normalize whitespaces
				lowerCamelCase__								=		''''''.join([char if char not in self.whitespaces else ''' ''' for char in text]							)

				# NFC Unicode normalization
				lowerCamelCase__								=		unicodedata.normalize('''NFC'''       ,  __lowerCAmelCase							)
				return text





			def 		__lowerCamelCase				(    self       ,  __lowerCAmelCase       ,  **__lowerCAmelCase							):



				'''simple docstring'''







				lowerCamelCase__								=		self.preprocess_text(__lowerCAmelCase							)
				return self.sp_model.encode(__lowerCAmelCase       ,  out_type=__lowerCAmelCase							)





			def 		__lowerCamelCase				(    self       ,  __lowerCAmelCase							):



				'''simple docstring'''







				return self.sp_model.PieceToId(__lowerCAmelCase							)





			def 		__lowerCamelCase				(    self       ,  __lowerCAmelCase							):



				'''simple docstring'''







				return self.sp_model.IdToPiece(__lowerCAmelCase							)





			@staticmethod
			def 		__lowerCamelCase				(    __lowerCAmelCase							):



				'''simple docstring'''







				return out_string





			def 		__lowerCamelCase				(    self       ,  __lowerCAmelCase							):



				'''simple docstring'''







				lowerCamelCase__								=		[]
				lowerCamelCase__								=		''''''
				lowerCamelCase__								=		False
				for token in tokens:
					# make sure that special tokens are not decoded using sentencepiece model
					if token in self.all_special_tokens:
						# TODO: Check if this is needed, as it ensures that decode(encode(doc)) != doc by adding extra whitespace in the decoded document
						if not prev_is_special:
							out_string += " "

						out_string += self.sp_model.decode(__lowerCAmelCase							) + token
						lowerCamelCase__								=		True
						lowerCamelCase__								=		[]
					else:
						current_sub_tokens.append(__lowerCAmelCase							)
						lowerCamelCase__								=		False
				out_string += self.sp_model.decode(__lowerCAmelCase							)

				return out_string





			def 		__lowerCamelCase				(    self							):



				'''simple docstring'''







				lowerCamelCase__								=		{self.convert_ids_to_tokens(__lowerCAmelCase							): i for i in range(self.vocab_size							)}
				vocab.update(self.added_tokens_encoder							)
				return vocab





			def 		__lowerCamelCase				(    self       ,  __lowerCAmelCase       ,  __lowerCAmelCase = None							):



				'''simple docstring'''







				if not os.path.isdir(__lowerCAmelCase							):
					logger.error(F'Vocabulary path ({save_directory}) should be a directory'							)
					return
				lowerCamelCase__								=		os.path.join(
				    __lowerCAmelCase       ,  (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file''']							)

				if os.path.abspath(self.vocab_file							) != os.path.abspath(__lowerCAmelCase							) and os.path.isfile(self.vocab_file							):
					copyfile(self.vocab_file       ,  __lowerCAmelCase							)
				elif not os.path.isfile(self.vocab_file							):
					with open(__lowerCAmelCase       ,  '''wb'''							) as fi:
						lowerCamelCase__								=		self.sp_model.serialized_model_proto()
						fi.write(__lowerCAmelCase							)

				return (out_vocab_file,)





			def 		__lowerCamelCase				(    self       ,  __lowerCAmelCase       ,  __lowerCAmelCase = False							):



				'''simple docstring'''







				if isinstance(__lowerCAmelCase       ,  __lowerCAmelCase							):
					lowerCamelCase__								=		self.preprocess_text(__lowerCAmelCase							)
					lowerCamelCase__								=		self.sp_model.encode(__lowerCAmelCase							)
				else:
					lowerCamelCase__								=		[self.preprocess_text(__lowerCAmelCase							) for t in text]
					lowerCamelCase__								=		self.sp_model.encode(__lowerCAmelCase							)

				if return_tensors is True or return_tensors == "pt":
					lowerCamelCase__								=		torch.tensor(__lowerCAmelCase							)

				return token_ids





			def 		__lowerCamelCase				(    self       ,  __lowerCAmelCase							):



				'''simple docstring'''







				return self.sp_model.decode(__lowerCAmelCase							)





			def 		__lowerCamelCase				(    self       ,  __lowerCAmelCase							):



				'''simple docstring'''







				lowerCamelCase__								=		[F'User: {text}' if is_user else F'Bot: {text}' for is_user, text in conversation.iter_texts()]
				lowerCamelCase__								=		(
				    F'{self.eos_token}{self.bos_token}' + F'{self.bos_token}'.join(__lowerCAmelCase							) + F'{self.bos_token}Bot:'
				)
				return self.encode(text=__lowerCAmelCase							)



