



# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from ..models.auto import AutoModelForSeqaSeqLM, AutoTokenizer
from .base import PipelineTool

class  __snake_case      (   SCREAMING_SNAKE_CASE):







							'''simple docstring'''






							UpperCamelCase__				:      Optional[Any]         =					"""philschmid/bart-large-cnn-samsum"""
							UpperCamelCase__				:      str         =					(
							    """This is a tool that summarizes an English text. It takes an input `text` containing the text to summarize, """
							    """and returns a summary of the text."""
							)
							UpperCamelCase__				:      Optional[int]         =					"""summarizer"""
							UpperCamelCase__				:      Dict         =					AutoTokenizer
							UpperCamelCase__				:      str         =					AutoModelForSeqaSeqLM

							UpperCamelCase__				:      Any         =					["""text"""]
							UpperCamelCase__				:      str         =					["""text"""]


							def _a      (							self  ,  a_		):
											return self.pre_processor(a_  ,  return_tensors="""pt"""  ,  truncation=a_		)


							def _a      (							self  ,  a_		):
											return self.model.generate(**a_		)[0]







							def _a      (							self  ,  a_		):
											return self.pre_processor.decode(a_  ,  skip_special_tokens=a_  ,  clean_up_tokenization_spaces=a_		)






import os
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple

import sentencepiece as spm

from ...tokenization_utils import PreTrainedTokenizer
from ...utils import logging


UpperCAmelCase							 =					logging.get_logger(__name__)

UpperCAmelCase							 =					{"""vocab_file""": """spiece.model"""}

UpperCAmelCase							 =					{
    """vocab_file""": {
        """bert_for_seq_generation""": (
            """https://huggingface.co/google/bert_for_seq_generation_L-24_bbc_encoder/resolve/main/spiece.model"""
        ),
    }
}

UpperCAmelCase							 =					{"""bert_for_seq_generation""": 512}

class  __snake_case      (   SCREAMING_SNAKE_CASE):







							'''simple docstring'''






							UpperCamelCase__				:      Optional[Any]         =					VOCAB_FILES_NAMES
							UpperCamelCase__				:      Any         =					PRETRAINED_VOCAB_FILES_MAP
							UpperCamelCase__				:      List[str]         =					PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
							UpperCamelCase__				:      List[int]         =					[]
							UpperCamelCase__				:      Optional[int]         =					["""input_ids""", """attention_mask"""]


							def __init__(							self  ,  a_  ,  a_="<s>"  ,  a_="</s>"  ,  a_="<unk>"  ,  a_="<pad>"  ,  a_="<::::>"  ,  a_ = None  ,  **a_  ,  ):
											a__				      =		{} if sp_model_kwargs is None else sp_model_kwargs

											# Add extra_ids to the special token list
											super().__init__(
											    bos_token=a_  ,  eos_token=a_  ,  unk_token=a_  ,  pad_token=a_  ,  sep_token=a_  ,  sp_model_kwargs=self.sp_model_kwargs  ,  **a_  ,  )

											a__				      =		vocab_file

											a__				      =		spm.SentencePieceProcessor(**self.sp_model_kwargs		)
											self.sp_model.Load(a_		)


							@property
							def _a      (							self		):
											return self.sp_model.get_piece_size()


							def _a      (							self		):
											a__				      =		{self.convert_ids_to_tokens(a_		): i for i in range(self.vocab_size		)}
											vocab.update(self.added_tokens_encoder		)
											return vocab


							def __getstate__(							self		):
											a__				      =		self.__dict__.copy()
											a__				      =		None
											return state


							def __setstate__(							self  ,  a_		):
											a__				      =		d

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

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


							def _a      (							self  ,  a_		):
											return self.sp_model.encode(a_  ,  out_type=a_		)


							def _a      (							self  ,  a_		):
											return self.sp_model.piece_to_id(a_		)


							def _a      (							self  ,  a_		):
											a__				      =		self.sp_model.IdToPiece(a_		)
											return token


							def _a      (							self  ,  a_		):
											a__				      =		[]
											a__				      =		""""""
											for token in tokens:
															# make sure that special tokens are not decoded using sentencepiece model
															if token in self.all_special_tokens:
																			out_string += self.sp_model.decode(a_		) + token
																			a__				      =		[]
															else:
																			current_sub_tokens.append(a_		)
											out_string += self.sp_model.decode(a_		)
											return out_string.strip()







							def _a      (							self  ,  a_  ,  a_ = None		):
											if not os.path.isdir(a_		):
															logger.error(F'''Vocabulary path ({save_directory}) should be a directory'''		)
															return
											a__				      =		os.path.join(
											    a_  ,  (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""]		)

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

											return (out_vocab_file,)

