






'''simple docstring'''



# 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 typing import TYPE_CHECKING

from ..models.auto import AutoModelForVisionaSeq
from ..utils import requires_backends
from .base import PipelineTool


if TYPE_CHECKING:
 from PIL import Image
class 				__SCREAMING_SNAKE_CASE							(				_lowerCAmelCase  ):
      __a	       ="Salesforce/blip-image-captioning-base"
      __a	       =(
          "This is a tool that generates a description of an image. It takes an input named `image` which should be the "
          "image to caption, and returns a text that contains the description in English."
      )
      __a	       ="image_captioner"
      __a	       =AutoModelForVisionaSeq

      __a	       =["image"]
      __a	       =["text"]




      def __init__(	self			, *lowerCamelCase			, **lowerCamelCase   )				->int:



         '''simple docstring'''
         requires_backends(self			, ['vision']   )
         super().__init__(*lowerCamelCase			, **lowerCamelCase   )




      def 			__UpperCamelCase				(	self			, lowerCamelCase   )				->int:



         '''simple docstring'''
         return self.pre_processor(images=lowerCamelCase			, return_tensors='pt'   )




      def 			__UpperCamelCase				(	self			, lowerCamelCase   )				->Any:



         '''simple docstring'''
         return self.model.generate(**lowerCamelCase   )






      def 			__UpperCamelCase				(	self			, lowerCamelCase   )				->str:



         '''simple docstring'''
         return self.pre_processor.batch_decode(lowerCamelCase			, skip_special_tokens=lowerCamelCase   )[0].strip()







'''simple docstring'''



import copy
import random

from transformers import CLIPTokenizer
class 				__SCREAMING_SNAKE_CASE							(				_lowerCAmelCase  ):




      def __init__(	self			, *lowerCamelCase			, **lowerCamelCase   )				->Union[str, Any]:



         '''simple docstring'''
         super().__init__(*lowerCamelCase			, **lowerCamelCase   )
         __a					  =     {}




      def 			__UpperCamelCase				(	self			, lowerCamelCase			, *lowerCamelCase			, **lowerCamelCase   )				->Dict:



         '''simple docstring'''
         __a					  =     super().add_tokens(lowerCamelCase			, *lowerCamelCase			, **lowerCamelCase   )
         if num_added_tokens == 0:
            raise ValueError(
                F"""The tokenizer already contains the token {placeholder_token}. Please pass a different"""
                ' `placeholder_token` that is not already in the tokenizer.'   )




      def 			__UpperCamelCase				(	self			, lowerCamelCase			, *lowerCamelCase			, lowerCamelCase=1			, **lowerCamelCase   )				->List[str]:



         '''simple docstring'''
         __a					  =     []
         if num_vec_per_token == 1:
            self.try_adding_tokens(lowerCamelCase			, *lowerCamelCase			, **lowerCamelCase   )
            output.append(lowerCamelCase   )
         else:
            __a					  =     []
            for i in range(lowerCamelCase   ):
               __a					  =     placeholder_token + F"""_{i}"""
               self.try_adding_tokens(lowerCamelCase			, *lowerCamelCase			, **lowerCamelCase   )
               output.append(lowerCamelCase   )
        # handle cases where there is a new placeholder token that contains the current placeholder token but is larger
         for token in self.token_map:
            if token in placeholder_token:
               raise ValueError(
                   F"""The tokenizer already has placeholder token {token} that can get confused with"""
                   F""" {placeholder_token}keep placeholder tokens independent"""   )
         __a					  =     output




      def 			__UpperCamelCase				(	self			, lowerCamelCase			, lowerCamelCase=False			, lowerCamelCase=1.0   )				->int:



         '''simple docstring'''
         if isinstance(lowerCamelCase			, lowerCamelCase   ):
            __a					  =     []
            for i in range(len(lowerCamelCase   )   ):
               output.append(self.replace_placeholder_tokens_in_text(text[i]			, vector_shuffle=lowerCamelCase   )   )
            return output
         for placeholder_token in self.token_map:
            if placeholder_token in text:
               __a					  =     self.token_map[placeholder_token]
               __a					  =     tokens[: 1 + int(len(lowerCamelCase   ) * prop_tokens_to_load   )]
               if vector_shuffle:
                  __a					  =     copy.copy(lowerCamelCase   )
                  random.shuffle(lowerCamelCase   )
               __a					  =     text.replace(lowerCamelCase			, ' '.join(lowerCamelCase   )   )
         return text




      def __call__(	self			, lowerCamelCase			, *lowerCamelCase			, lowerCamelCase=False			, lowerCamelCase=1.0			, **lowerCamelCase   )				->Optional[int]:



         '''simple docstring'''
         return super().__call__(
             self.replace_placeholder_tokens_in_text(
                 lowerCamelCase			, vector_shuffle=lowerCamelCase			, prop_tokens_to_load=lowerCamelCase   )			, *lowerCamelCase			, **lowerCamelCase			, )






      def 			__UpperCamelCase				(	self			, lowerCamelCase			, *lowerCamelCase			, lowerCamelCase=False			, lowerCamelCase=1.0			, **lowerCamelCase   )				->List[Any]:



         '''simple docstring'''
         return super().encode(
             self.replace_placeholder_tokens_in_text(
                 lowerCamelCase			, vector_shuffle=lowerCamelCase			, prop_tokens_to_load=lowerCamelCase   )			, *lowerCamelCase			, **lowerCamelCase			, )