

# 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        a__       (   UpperCAmelCase       ):







  """simple docstring"""




  UpperCAmelCase__      : int			       ="""Salesforce/blip-image-captioning-base"""
  UpperCAmelCase__      : Any			       =(
      """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."""
  )
  UpperCAmelCase__      : int			       ="""image_captioner"""
  UpperCAmelCase__      : int			       =AutoModelForVisionaSeq

  UpperCAmelCase__      : Optional[int]			       =["""image"""]
  UpperCAmelCase__      : List[str]			       =["""text"""]


  def __init__(			self    : Optional[Any]				,				*UpperCAmelCase__    : Union[str, Any]				,				**UpperCAmelCase__    : Optional[Any]		)					->Tuple:
        """simple docstring"""







        requires_backends(self				,				["""vision"""]		)
        super().__init__(*UpperCAmelCase__				,				**UpperCAmelCase__		)


  def  _lowercase      (			self    : Optional[Any]				,				UpperCAmelCase__    : "Image"		)					->Any:
        """simple docstring"""







        return self.pre_processor(images=UpperCAmelCase__				,				return_tensors="""pt"""		)


  def  _lowercase      (			self    : Tuple				,				UpperCAmelCase__    : Tuple		)					->Optional[Any]:
        """simple docstring"""







        return self.model.generate(**UpperCAmelCase__		)




  def  _lowercase      (			self    : Dict				,				UpperCAmelCase__    : Dict		)					->Dict:
        """simple docstring"""







        return self.pre_processor.batch_decode(UpperCAmelCase__				,				skip_special_tokens=UpperCAmelCase__		)[0].strip()







def 					__lowercase      (     _A			)     ->						str:
      SCREAMING_SNAKE_CASE :      Optional[Any]       =   """"""
      for ch in key:
            if ch == " " or ch not in key_no_dups and ch.isalpha():
                  key_no_dups += ch
      return key_no_dups






def 					__lowercase      (     _A			)     ->						dict[str, str]:
      SCREAMING_SNAKE_CASE :      int       =   [chr(i + 65			) for i in range(26			)]
      # Remove duplicate characters from key
      SCREAMING_SNAKE_CASE :      List[str]       =   remove_duplicates(key.upper()			)
      SCREAMING_SNAKE_CASE :      Dict       =   len(_A			)
      # First fill cipher with key characters
      SCREAMING_SNAKE_CASE :      int       =   {alphabet[i]: char for i, char in enumerate(_A			)}
      # Then map remaining characters in alphabet to
      # the alphabet from the beginning
      for i in range(len(_A			)						,    26			):
            SCREAMING_SNAKE_CASE :      List[Any]       =   alphabet[i - offset]
            # Ensure we are not mapping letters to letters previously mapped
            while char in key:
                  offset -= 1
                  SCREAMING_SNAKE_CASE :      str       =   alphabet[i - offset]
            SCREAMING_SNAKE_CASE :      List[Any]       =   char
      return cipher_alphabet






def 					__lowercase      (     _A						,    _A			)     ->						str:
      return "".join(cipher_map.get(_A						,    _A			) for ch in message.upper()			)






def 					__lowercase      (     _A						,    _A			)     ->						str:
      SCREAMING_SNAKE_CASE :      Optional[int]       =   {v: k for k, v in cipher_map.items()}
      return "".join(rev_cipher_map.get(_A						,    _A			) for ch in message.upper()			)






def 					__lowercase      (     )     ->						None:
      SCREAMING_SNAKE_CASE :      List[Any]       =   input("""Enter message to encode or decode: """			).strip()
      SCREAMING_SNAKE_CASE :      str       =   input("""Enter keyword: """			).strip()
      SCREAMING_SNAKE_CASE :      Dict       =   input("""Encipher or decipher? E/D:"""			).strip()[0].lower()
      try:
            SCREAMING_SNAKE_CASE :      int       =   {"""e""": encipher, """d""": decipher}[option]
      except KeyError:
            raise KeyError("""invalid input option"""			)
      SCREAMING_SNAKE_CASE :      Optional[int]       =   create_cipher_map(_A			)
      print(func(_A						,    _A			)			)


if __name__ == "__main__":
  import doctest

  doctest.testmod()
  main()




