





'''simple docstring'''


import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import CLIPTokenizer, CLIPTokenizerFast
from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES
from transformers.testing_utils import require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
       from PIL import Image

       from transformers import OwlViTImageProcessor, OwlViTProcessor

@require_vision
class    lowerCAmelCase_		(		unittest.TestCase		):




 def 							_snake_case   (	self		)    ->							List[Any]:
     _lowerCAmelCase				=      tempfile.mkdtemp()

     # fmt: off
     _lowerCAmelCase				=      ["", "l", "o", "w", "e", "r", "s", "t", "i", "d", "n", "lo", "l</w>", "w</w>", "r</w>", "t</w>", "low</w>", "er</w>", "lowest</w>", "newer</w>", "wider", "<unk>", "<|startoftext|>", "<|endoftext|>"]
     # fmt: on
     _lowerCAmelCase				=      dict(zip(_lowerCAmelCase			,     range(len(_lowerCAmelCase		)		)		)		)
     _lowerCAmelCase				=      ["#version: 0.2", "l o", "lo w</w>", "e r</w>", ""]
     _lowerCAmelCase				=      {"unk_token": "<unk>"}

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

     _lowerCAmelCase				=      {
         "do_resize": True,
         "size": 20,
         "do_center_crop": True,
         "crop_size": 18,
         "do_normalize": True,
         "image_mean": [0.48145466, 0.4578275, 0.40821073],
         "image_std": [0.26862954, 0.26130258, 0.27577711],
     }
     _lowerCAmelCase				=      os.path.join(self.tmpdirname			,     _lowerCAmelCase		)
     with open(self.image_processor_file			,     "w"			,     encoding="utf-8"		) as fp:
         json.dump(_lowerCAmelCase			,     _lowerCAmelCase		)




 def 							_snake_case   (	self			,     **_lowerCAmelCase		)    ->							Dict:
     return CLIPTokenizer.from_pretrained(self.tmpdirname			,     pad_token="!"			,     **_lowerCAmelCase		)




 def 							_snake_case   (	self			,     **_lowerCAmelCase		)    ->							Optional[Any]:
     return CLIPTokenizerFast.from_pretrained(self.tmpdirname			,     pad_token="!"			,     **_lowerCAmelCase		)




 def 							_snake_case   (	self			,     **_lowerCAmelCase		)    ->							Optional[Any]:
     return OwlViTImageProcessor.from_pretrained(self.tmpdirname			,     **_lowerCAmelCase		)




 def 							_snake_case   (	self		)    ->							Dict:
     shutil.rmtree(self.tmpdirname		)




 def 							_snake_case   (	self		)    ->							Dict:
     _lowerCAmelCase				=      [np.random.randint(255			,     size=(3, 30, 400)			,     dtype=np.uinta		)]

     _lowerCAmelCase				=      [Image.fromarray(np.moveaxis(_lowerCAmelCase			,     0			,     -1		)		) for x in image_inputs]

     return image_inputs




 def 							_snake_case   (	self		)    ->							Dict:
     _lowerCAmelCase				=      self.get_tokenizer()
     _lowerCAmelCase				=      self.get_rust_tokenizer()
     _lowerCAmelCase				=      self.get_image_processor()

     _lowerCAmelCase				=      OwlViTProcessor(tokenizer=_lowerCAmelCase			,     image_processor=_lowerCAmelCase		)
     processor_slow.save_pretrained(self.tmpdirname		)
     _lowerCAmelCase				=      OwlViTProcessor.from_pretrained(self.tmpdirname			,     use_fast=_lowerCAmelCase		)

     _lowerCAmelCase				=      OwlViTProcessor(tokenizer=_lowerCAmelCase			,     image_processor=_lowerCAmelCase		)
     processor_fast.save_pretrained(self.tmpdirname		)
     _lowerCAmelCase				=      OwlViTProcessor.from_pretrained(self.tmpdirname		)

     self.assertEqual(processor_slow.tokenizer.get_vocab()			,     tokenizer_slow.get_vocab()		)
     self.assertEqual(processor_fast.tokenizer.get_vocab()			,     tokenizer_fast.get_vocab()		)
     self.assertEqual(tokenizer_slow.get_vocab()			,     tokenizer_fast.get_vocab()		)
     self.assertIsInstance(processor_slow.tokenizer			,     _lowerCAmelCase		)
     self.assertIsInstance(processor_fast.tokenizer			,     _lowerCAmelCase		)

     self.assertEqual(processor_slow.image_processor.to_json_string()			,     image_processor.to_json_string()		)
     self.assertEqual(processor_fast.image_processor.to_json_string()			,     image_processor.to_json_string()		)
     self.assertIsInstance(processor_slow.image_processor			,     _lowerCAmelCase		)
     self.assertIsInstance(processor_fast.image_processor			,     _lowerCAmelCase		)




 def 							_snake_case   (	self		)    ->							Union[str, Any]:
     _lowerCAmelCase				=      OwlViTProcessor(tokenizer=self.get_tokenizer()			,     image_processor=self.get_image_processor()		)
     processor.save_pretrained(self.tmpdirname		)

     _lowerCAmelCase				=      self.get_tokenizer(bos_token="(BOS)"			,     eos_token="(EOS)"		)
     _lowerCAmelCase				=      self.get_image_processor(do_normalize=_lowerCAmelCase		)

     _lowerCAmelCase				=      OwlViTProcessor.from_pretrained(
         self.tmpdirname			,     bos_token="(BOS)"			,     eos_token="(EOS)"			,     do_normalize=_lowerCAmelCase		)

     self.assertEqual(processor.tokenizer.get_vocab()			,     tokenizer_add_kwargs.get_vocab()		)
     self.assertIsInstance(processor.tokenizer			,     _lowerCAmelCase		)

     self.assertEqual(processor.image_processor.to_json_string()			,     image_processor_add_kwargs.to_json_string()		)
     self.assertIsInstance(processor.image_processor			,     _lowerCAmelCase		)




 def 							_snake_case   (	self		)    ->							Tuple:
     _lowerCAmelCase				=      self.get_image_processor()
     _lowerCAmelCase				=      self.get_tokenizer()

     _lowerCAmelCase				=      OwlViTProcessor(tokenizer=_lowerCAmelCase			,     image_processor=_lowerCAmelCase		)

     _lowerCAmelCase				=      self.prepare_image_inputs()

     _lowerCAmelCase				=      image_processor(_lowerCAmelCase			,     return_tensors="np"		)
     _lowerCAmelCase				=      processor(images=_lowerCAmelCase			,     return_tensors="np"		)

     for key in input_image_proc.keys():
         self.assertAlmostEqual(input_image_proc[key].sum()			,     input_processor[key].sum()			,     delta=1E-2		)




 def 							_snake_case   (	self		)    ->							Optional[Any]:
     _lowerCAmelCase				=      self.get_image_processor()
     _lowerCAmelCase				=      self.get_tokenizer()

     _lowerCAmelCase				=      OwlViTProcessor(tokenizer=_lowerCAmelCase			,     image_processor=_lowerCAmelCase		)

     _lowerCAmelCase				=      "lower newer"

     _lowerCAmelCase				=      processor(text=_lowerCAmelCase			,     return_tensors="np"		)

     _lowerCAmelCase				=      tokenizer(_lowerCAmelCase			,     return_tensors="np"		)

     for key in encoded_tok.keys():
         self.assertListEqual(encoded_tok[key][0].tolist()			,     encoded_processor[key][0].tolist()		)




 def 							_snake_case   (	self		)    ->							List[str]:
     _lowerCAmelCase				=      self.get_image_processor()
     _lowerCAmelCase				=      self.get_tokenizer()

     _lowerCAmelCase				=      OwlViTProcessor(tokenizer=_lowerCAmelCase			,     image_processor=_lowerCAmelCase		)

     _lowerCAmelCase				=      "lower newer"
     _lowerCAmelCase				=      self.prepare_image_inputs()

     _lowerCAmelCase				=      processor(text=_lowerCAmelCase			,     images=_lowerCAmelCase		)

     self.assertListEqual(list(inputs.keys()		)			,     ["input_ids", "attention_mask", "pixel_values"]		)

     # test if it raises when no input is passed
     with pytest.raises(_lowerCAmelCase		):
         processor()




 def 							_snake_case   (	self		)    ->							int:
     _lowerCAmelCase				=      "google/owlvit-base-patch32"
     _lowerCAmelCase				=      OwlViTProcessor.from_pretrained(_lowerCAmelCase		)

     _lowerCAmelCase				=      ["cat", "nasa badge"]
     _lowerCAmelCase				=      processor(text=_lowerCAmelCase		)

     _lowerCAmelCase				=      16
     self.assertListEqual(list(inputs.keys()		)			,     ["input_ids", "attention_mask"]		)
     self.assertEqual(inputs["input_ids"].shape			,     (2, seq_length)		)

     # test if it raises when no input is passed
     with pytest.raises(_lowerCAmelCase		):
         processor()




 def 							_snake_case   (	self		)    ->							Optional[Any]:
     _lowerCAmelCase				=      "google/owlvit-base-patch32"
     _lowerCAmelCase				=      OwlViTProcessor.from_pretrained(_lowerCAmelCase		)

     _lowerCAmelCase				=      [["cat", "nasa badge"], ["person"]]
     _lowerCAmelCase				=      processor(text=_lowerCAmelCase		)

     _lowerCAmelCase				=      16
     _lowerCAmelCase				=      len(_lowerCAmelCase		)
     _lowerCAmelCase				=      max([len(_lowerCAmelCase		) for texts in input_texts]		)

     self.assertListEqual(list(inputs.keys()		)			,     ["input_ids", "attention_mask"]		)
     self.assertEqual(inputs["input_ids"].shape			,     (batch_size * num_max_text_queries, seq_length)		)

     # test if it raises when no input is passed
     with pytest.raises(_lowerCAmelCase		):
         processor()




 def 							_snake_case   (	self		)    ->							Any:
     _lowerCAmelCase				=      "google/owlvit-base-patch32"
     _lowerCAmelCase				=      OwlViTProcessor.from_pretrained(_lowerCAmelCase		)

     _lowerCAmelCase				=      ["cat", "nasa badge"]
     _lowerCAmelCase				=      processor(text=_lowerCAmelCase		)

     _lowerCAmelCase				=      16
     _lowerCAmelCase				=      inputs["input_ids"]
     _lowerCAmelCase				=      [
         [49406, 2368, 49407, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
         [49406, 6841, 11301, 49407, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     ]

     self.assertListEqual(list(inputs.keys()		)			,     ["input_ids", "attention_mask"]		)
     self.assertEqual(inputs["input_ids"].shape			,     (2, seq_length)		)
     self.assertListEqual(list(input_ids[0]		)			,     predicted_ids[0]		)
     self.assertListEqual(list(input_ids[1]		)			,     predicted_ids[1]		)




 def 							_snake_case   (	self		)    ->							Optional[Any]:
     _lowerCAmelCase				=      self.get_image_processor()
     _lowerCAmelCase				=      self.get_tokenizer()

     _lowerCAmelCase				=      OwlViTProcessor(tokenizer=_lowerCAmelCase			,     image_processor=_lowerCAmelCase		)

     _lowerCAmelCase				=      self.prepare_image_inputs()
     _lowerCAmelCase				=      self.prepare_image_inputs()

     _lowerCAmelCase				=      processor(images=_lowerCAmelCase			,     query_images=_lowerCAmelCase		)

     self.assertListEqual(list(inputs.keys()		)			,     ["query_pixel_values", "pixel_values"]		)

     # test if it raises when no input is passed
     with pytest.raises(_lowerCAmelCase		):
         processor()





 def 							_snake_case   (	self		)    ->							Optional[int]:
     _lowerCAmelCase				=      self.get_image_processor()
     _lowerCAmelCase				=      self.get_tokenizer()

     _lowerCAmelCase				=      OwlViTProcessor(tokenizer=_lowerCAmelCase			,     image_processor=_lowerCAmelCase		)

     _lowerCAmelCase				=      [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

     _lowerCAmelCase				=      processor.batch_decode(_lowerCAmelCase		)
     _lowerCAmelCase				=      tokenizer.batch_decode(_lowerCAmelCase		)

     self.assertListEqual(_lowerCAmelCase			,     _lowerCAmelCase		)









'''simple docstring'''


import unittest

from transformers import (
    MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
    TextClassificationPipeline,
    pipeline,
)
from transformers.testing_utils import is_pipeline_test, nested_simplify, require_tf, require_torch, slow

from .test_pipelines_common import ANY


# These 2 model types require different inputs than those of the usual text models.
_SCREAMING_SNAKE_CASE	 =							{"LayoutLMv2Config", "LayoutLMv3Config"}

@is_pipeline_test
class    lowerCAmelCase_		(		unittest.TestCase		):
 __lowerCamelCase						:							Optional[Any]         =					MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
 __lowerCamelCase						:							str         =					TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

 if model_mapping is not None:
  __lowerCamelCase						:							Optional[int]         =					{config: model for config, model in model_mapping.items() if config.__name__ not in _TO_SKIP}
 if tf_model_mapping is not None:
  __lowerCamelCase						:							List[Any]         =					{
      config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP
  }




 @require_torch
 def 							_snake_case   (	self		)    ->							str:
     _lowerCAmelCase				=      pipeline(
         task="text-classification"			,     model="hf-internal-testing/tiny-random-distilbert"			,     framework="pt"		)

     _lowerCAmelCase				=      text_classifier("This is great !"		)
     self.assertEqual(nested_simplify(_lowerCAmelCase		)			,     [{"label": "LABEL_0", "score": 0.504}]		)

     _lowerCAmelCase				=      text_classifier("This is great !"			,     top_k=2		)
     self.assertEqual(
         nested_simplify(_lowerCAmelCase		)			,     [{"label": "LABEL_0", "score": 0.504}, {"label": "LABEL_1", "score": 0.496}]		)

     _lowerCAmelCase				=      text_classifier(["This is great !", "This is bad"]			,     top_k=2		)
     self.assertEqual(
         nested_simplify(_lowerCAmelCase		)			,     [
             [{"label": "LABEL_0", "score": 0.504}, {"label": "LABEL_1", "score": 0.496}],
             [{"label": "LABEL_0", "score": 0.504}, {"label": "LABEL_1", "score": 0.496}],
         ]			,     )

     _lowerCAmelCase				=      text_classifier("This is great !"			,     top_k=1		)
     self.assertEqual(nested_simplify(_lowerCAmelCase		)			,     [{"label": "LABEL_0", "score": 0.504}]		)

     # Legacy behavior
     _lowerCAmelCase				=      text_classifier("This is great !"			,     return_all_scores=_lowerCAmelCase		)
     self.assertEqual(nested_simplify(_lowerCAmelCase		)			,     [{"label": "LABEL_0", "score": 0.504}]		)

     _lowerCAmelCase				=      text_classifier("This is great !"			,     return_all_scores=_lowerCAmelCase		)
     self.assertEqual(
         nested_simplify(_lowerCAmelCase		)			,     [[{"label": "LABEL_0", "score": 0.504}, {"label": "LABEL_1", "score": 0.496}]]		)

     _lowerCAmelCase				=      text_classifier(["This is great !", "Something else"]			,     return_all_scores=_lowerCAmelCase		)
     self.assertEqual(
         nested_simplify(_lowerCAmelCase		)			,     [
             [{"label": "LABEL_0", "score": 0.504}, {"label": "LABEL_1", "score": 0.496}],
             [{"label": "LABEL_0", "score": 0.504}, {"label": "LABEL_1", "score": 0.496}],
         ]			,     )

     _lowerCAmelCase				=      text_classifier(["This is great !", "Something else"]			,     return_all_scores=_lowerCAmelCase		)
     self.assertEqual(
         nested_simplify(_lowerCAmelCase		)			,     [
             {"label": "LABEL_0", "score": 0.504},
             {"label": "LABEL_0", "score": 0.504},
         ]			,     )




 @require_torch
 def 							_snake_case   (	self		)    ->							Tuple:
     import torch

     _lowerCAmelCase				=      pipeline(
         task="text-classification"			,     model="hf-internal-testing/tiny-random-distilbert"			,     framework="pt"			,     device=torch.device("cpu"		)			,     )

     _lowerCAmelCase				=      text_classifier("This is great !"		)
     self.assertEqual(nested_simplify(_lowerCAmelCase		)			,     [{"label": "LABEL_0", "score": 0.504}]		)




 @require_tf
 def 							_snake_case   (	self		)    ->							Any:
     _lowerCAmelCase				=      pipeline(
         task="text-classification"			,     model="hf-internal-testing/tiny-random-distilbert"			,     framework="tf"		)

     _lowerCAmelCase				=      text_classifier("This is great !"		)
     self.assertEqual(nested_simplify(_lowerCAmelCase		)			,     [{"label": "LABEL_0", "score": 0.504}]		)




 @slow
 @require_torch
 def 							_snake_case   (	self		)    ->							Dict:
     _lowerCAmelCase				=      pipeline("text-classification"		)

     _lowerCAmelCase				=      text_classifier("This is great !"		)
     self.assertEqual(nested_simplify(_lowerCAmelCase		)			,     [{"label": "POSITIVE", "score": 1.0}]		)
     _lowerCAmelCase				=      text_classifier("This is bad !"		)
     self.assertEqual(nested_simplify(_lowerCAmelCase		)			,     [{"label": "NEGATIVE", "score": 1.0}]		)
     _lowerCAmelCase				=      text_classifier("Birds are a type of animal"		)
     self.assertEqual(nested_simplify(_lowerCAmelCase		)			,     [{"label": "POSITIVE", "score": 0.988}]		)




 @slow
 @require_tf
 def 							_snake_case   (	self		)    ->							Any:
     _lowerCAmelCase				=      pipeline("text-classification"			,     framework="tf"		)

     _lowerCAmelCase				=      text_classifier("This is great !"		)
     self.assertEqual(nested_simplify(_lowerCAmelCase		)			,     [{"label": "POSITIVE", "score": 1.0}]		)
     _lowerCAmelCase				=      text_classifier("This is bad !"		)
     self.assertEqual(nested_simplify(_lowerCAmelCase		)			,     [{"label": "NEGATIVE", "score": 1.0}]		)
     _lowerCAmelCase				=      text_classifier("Birds are a type of animal"		)
     self.assertEqual(nested_simplify(_lowerCAmelCase		)			,     [{"label": "POSITIVE", "score": 0.988}]		)




 def 							_snake_case   (	self			,     _lowerCAmelCase			,     _lowerCAmelCase			,     _lowerCAmelCase		)    ->							str:
     _lowerCAmelCase				=      TextClassificationPipeline(model=_lowerCAmelCase			,     tokenizer=_lowerCAmelCase		)
     return text_classifier, ["HuggingFace is in", "This is another test"]





 def 							_snake_case   (	self			,     _lowerCAmelCase			,     _lowerCAmelCase		)    ->							Union[str, Any]:
     _lowerCAmelCase				=      text_classifier.model
     # Small inputs because BartTokenizer tiny has maximum position embeddings = 22
     _lowerCAmelCase				=      "HuggingFace is in"
     _lowerCAmelCase				=      text_classifier(_lowerCAmelCase		)

     self.assertEqual(nested_simplify(_lowerCAmelCase		)			,     [{"label": ANY(_lowerCAmelCase		), "score": ANY(_lowerCAmelCase		)}]		)
     self.assertTrue(outputs[0]["label"] in model.config.idalabel.values()		)

     _lowerCAmelCase				=      ["HuggingFace is in ", "Paris is in France"]
     _lowerCAmelCase				=      text_classifier(_lowerCAmelCase		)
     self.assertEqual(
         nested_simplify(_lowerCAmelCase		)			,     [{"label": ANY(_lowerCAmelCase		), "score": ANY(_lowerCAmelCase		)}, {"label": ANY(_lowerCAmelCase		), "score": ANY(_lowerCAmelCase		)}]			,     )
     self.assertTrue(outputs[0]["label"] in model.config.idalabel.values()		)
     self.assertTrue(outputs[1]["label"] in model.config.idalabel.values()		)

     # Forcing to get all results with `top_k=None`
     # This is NOT the legacy format
     _lowerCAmelCase				=      text_classifier(_lowerCAmelCase			,     top_k=_lowerCAmelCase		)
     _lowerCAmelCase				=      len(model.config.idalabel.values()		)
     self.assertEqual(
         nested_simplify(_lowerCAmelCase		)			,     [[{"label": ANY(_lowerCAmelCase		), "score": ANY(_lowerCAmelCase		)}] * N, [{"label": ANY(_lowerCAmelCase		), "score": ANY(_lowerCAmelCase		)}] * N]			,     )

     _lowerCAmelCase				=      {"text": "HuggingFace is in ", "text_pair": "Paris is in France"}
     _lowerCAmelCase				=      text_classifier(_lowerCAmelCase		)
     self.assertEqual(
         nested_simplify(_lowerCAmelCase		)			,     {"label": ANY(_lowerCAmelCase		), "score": ANY(_lowerCAmelCase		)}			,     )
     self.assertTrue(outputs["label"] in model.config.idalabel.values()		)

     # This might be used a text pair, but tokenizer + pipe interaction
     # makes it hard to understand that it's not using the pair properly
     # https://github.com/huggingface/transformers/issues/17305
     # We disabled this usage instead as it was outputting wrong outputs.
     _lowerCAmelCase				=      [["HuggingFace is in ", "Paris is in France"]]
     with self.assertRaises(_lowerCAmelCase		):
         text_classifier(_lowerCAmelCase		)

     # This used to be valid for doing text pairs
     # We're keeping it working because of backward compatibility
     _lowerCAmelCase				=      text_classifier([[["HuggingFace is in ", "Paris is in France"]]]		)
     self.assertEqual(
         nested_simplify(_lowerCAmelCase		)			,     [{"label": ANY(_lowerCAmelCase		), "score": ANY(_lowerCAmelCase		)}]			,     )
     self.assertTrue(outputs[0]["label"] in model.config.idalabel.values()		)


