






'''simple docstring'''

import json
import os
import unittest

from transformers.models.xlm.tokenization_xlm import VOCAB_FILES_NAMES, XLMTokenizer
from transformers.testing_utils import slow

from ...test_tokenization_common import TokenizerTesterMixin






class     a_						(      lowerCamelCase     ,	unittest.TestCase    ):
  lowercase							=				XLMTokenizer
  lowercase							=				False

  def   A__						(				self				)  ->				str:



        """simple docstring"""






        super().setUp()

        # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
        UpperCamelCase      = [
            """l""",
            """o""",
            """w""",
            """e""",
            """r""",
            """s""",
            """t""",
            """i""",
            """d""",
            """n""",
            """w</w>""",
            """r</w>""",
            """t</w>""",
            """lo""",
            """low""",
            """er</w>""",
            """low</w>""",
            """lowest</w>""",
            """newer</w>""",
            """wider</w>""",
            """<unk>""",
        ]
        UpperCamelCase      = dict(zip(_lowerCAmelCase      ,		range(len(_lowerCAmelCase				)				)				)				)
        UpperCamelCase      = ["""l o 123""", """lo w 1456""", """e r</w> 1789""", """"""]

        UpperCamelCase      = os.path.join(self.tmpdirname      ,		VOCAB_FILES_NAMES["""vocab_file"""]				)
        UpperCamelCase      = os.path.join(self.tmpdirname      ,		VOCAB_FILES_NAMES["""merges_file"""]				)
        with open(self.vocab_file      ,		"""w"""				) as fp:
              fp.write(json.dumps(_lowerCAmelCase				)				)
        with open(self.merges_file      ,		"""w"""				) as fp:
              fp.write("""\n""".join(_lowerCAmelCase				)				)

  def   A__						(				self      ,		_SCREAMING_SNAKE_CASE				)  ->				Optional[int]:



        """simple docstring"""






        UpperCamelCase      = """lower newer"""
        UpperCamelCase      = """lower newer"""
        return input_text, output_text

  def   A__						(				self				)  ->				List[str]:



        """simple docstring"""






        UpperCamelCase      = XLMTokenizer(self.vocab_file      ,		self.merges_file				)

        UpperCamelCase      = """lower"""
        UpperCamelCase      = ["""low""", """er</w>"""]
        UpperCamelCase      = tokenizer.tokenize(_lowerCAmelCase				)
        self.assertListEqual(_lowerCAmelCase      ,		_lowerCAmelCase				)

        UpperCamelCase      = tokens + ["""<unk>"""]
        UpperCamelCase      = [14, 15, 20]
        self.assertListEqual(tokenizer.convert_tokens_to_ids(_lowerCAmelCase				)      ,		_lowerCAmelCase				)


  @slow
  def   A__						(				self				)  ->				Optional[Any]:



        """simple docstring"""






        UpperCamelCase      = XLMTokenizer.from_pretrained("""xlm-mlm-en-2048"""				)

        UpperCamelCase      = tokenizer.encode("""sequence builders"""      ,		add_special_tokens=_lowerCAmelCase				)
        UpperCamelCase      = tokenizer.encode("""multi-sequence build"""      ,		add_special_tokens=_lowerCAmelCase				)

        UpperCamelCase      = tokenizer.build_inputs_with_special_tokens(_lowerCAmelCase				)
        UpperCamelCase      = tokenizer.build_inputs_with_special_tokens(_lowerCAmelCase      ,		_lowerCAmelCase				)

        assert encoded_sentence == [0] + text + [1]
        assert encoded_pair == [0] + text + [1] + text_a + [1]










'''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     a_						(      lowerCamelCase    ):
  lowercase							=				"""Salesforce/blip-image-captioning-base"""
  lowercase							=				(
      """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."""
  )
  lowercase							=				"""image_captioner"""
  lowercase							=				AutoModelForVisionaSeq

  lowercase							=				["""image"""]
  lowercase							=				["""text"""]

  def __init__(				self      ,		*_SCREAMING_SNAKE_CASE      ,		**_SCREAMING_SNAKE_CASE				)  ->				Any:



        """simple docstring"""






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

  def   A__						(				self      ,		_SCREAMING_SNAKE_CASE				)  ->				str:



        """simple docstring"""






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

  def   A__						(				self      ,		_SCREAMING_SNAKE_CASE				)  ->				str:



        """simple docstring"""






        return self.model.generate(**_SCREAMING_SNAKE_CASE				)


  def   A__						(				self      ,		_SCREAMING_SNAKE_CASE				)  ->				Union[str, Any]:



        """simple docstring"""






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