"""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 				snake_case__     (   unittest.TestCase   ):


 def 		a__ (				self		):
        __a     				=  tempfile.mkdtemp()

        # fmt: off
        __a     				=  ["", "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
        __a     				=  dict(zip(A__  ,						range(len(A__		)		)		)		)
        __a     				=  ["#version: 0.2", "l o", "lo w</w>", "e r</w>", ""]
        __a     				=  {"unk_token": "<unk>"}

        __a     				=  os.path.join(self.tmpdirname  ,						VOCAB_FILES_NAMES["vocab_file"]		)
        __a     				=  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(A__		) + "\n"		)
        with open(self.merges_file  ,						"w"  ,						encoding="utf-8"		) as fp:
               fp.write("\n".join(A__		)		)

        __a     				=  {
            "do_resize": True,
            "size": 20,
            "do_center_crop": True,
            "crop_size": 18,
            "do_normalize": True,
            "image_mean": [0.4814_5466, 0.457_8275, 0.4082_1073],
            "image_std": [0.2686_2954, 0.2613_0258, 0.2757_7711],
        }
        __a     				=  os.path.join(self.tmpdirname  ,						A__		)
        with open(self.image_processor_file  ,						"w"  ,						encoding="utf-8"		) as fp:
               json.dump(A__  ,						A__		)


 def 		a__ (				self  ,						**lowerCamelCase		):
        return CLIPTokenizer.from_pretrained(self.tmpdirname  ,						pad_token="!"  ,						**A__		)


 def 		a__ (				self  ,						**lowerCamelCase		):
        return CLIPTokenizerFast.from_pretrained(self.tmpdirname  ,						pad_token="!"  ,						**A__		)


 def 		a__ (				self  ,						**lowerCamelCase		):
        return OwlViTImageProcessor.from_pretrained(self.tmpdirname  ,						**A__		)


 def 		a__ (				self		):
        shutil.rmtree(self.tmpdirname		)


 def 		a__ (				self		):
        __a     				=  [np.random.randint(255  ,						size=(3, 30, 400)  ,						dtype=np.uinta		)]

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

        return image_inputs


 def 		a__ (				self		):
        __a     				=  self.get_tokenizer()
        __a     				=  self.get_rust_tokenizer()
        __a     				=  self.get_image_processor()

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

        __a     				=  OwlViTProcessor(tokenizer=A__  ,						image_processor=A__		)
        processor_fast.save_pretrained(self.tmpdirname		)
        __a     				=  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  ,						A__		)
        self.assertIsInstance(processor_fast.tokenizer  ,						A__		)

        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  ,						A__		)
        self.assertIsInstance(processor_fast.image_processor  ,						A__		)


 def 		a__ (				self		):
        __a     				=  OwlViTProcessor(tokenizer=self.get_tokenizer()  ,						image_processor=self.get_image_processor()		)
        processor.save_pretrained(self.tmpdirname		)

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

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

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

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


 def 		a__ (				self		):
        __a     				=  self.get_image_processor()
        __a     				=  self.get_tokenizer()

        __a     				=  OwlViTProcessor(tokenizer=A__  ,						image_processor=A__		)

        __a     				=  self.prepare_image_inputs()

        __a     				=  image_processor(A__  ,						return_tensors="np"		)
        __a     				=  processor(images=A__  ,						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 		a__ (				self		):
        __a     				=  self.get_image_processor()
        __a     				=  self.get_tokenizer()

        __a     				=  OwlViTProcessor(tokenizer=A__  ,						image_processor=A__		)

        __a     				=  "lower newer"

        __a     				=  processor(text=A__  ,						return_tensors="np"		)

        __a     				=  tokenizer(A__  ,						return_tensors="np"		)

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


 def 		a__ (				self		):
        __a     				=  self.get_image_processor()
        __a     				=  self.get_tokenizer()

        __a     				=  OwlViTProcessor(tokenizer=A__  ,						image_processor=A__		)

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

        __a     				=  processor(text=A__  ,						images=A__		)

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

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


 def 		a__ (				self		):
        __a     				=  "google/owlvit-base-patch32"
        __a     				=  OwlViTProcessor.from_pretrained(A__		)

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

        __a     				=  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(A__		):
               processor()


 def 		a__ (				self		):
        __a     				=  "google/owlvit-base-patch32"
        __a     				=  OwlViTProcessor.from_pretrained(A__		)

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

        __a     				=  16
        __a     				=  len(A__		)
        __a     				=  max([len(A__		) 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(A__		):
               processor()


 def 		a__ (				self		):
        __a     				=  "google/owlvit-base-patch32"
        __a     				=  OwlViTProcessor.from_pretrained(A__		)

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

        __a     				=  16
        __a     				=  inputs["input_ids"]
        __a     				=  [
            [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 		a__ (				self		):
        __a     				=  self.get_image_processor()
        __a     				=  self.get_tokenizer()

        __a     				=  OwlViTProcessor(tokenizer=A__  ,						image_processor=A__		)

        __a     				=  self.prepare_image_inputs()
        __a     				=  self.prepare_image_inputs()

        __a     				=  processor(images=A__  ,						query_images=A__		)

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

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






 def 		a__ (				self		):
        __a     				=  self.get_image_processor()
        __a     				=  self.get_tokenizer()

        __a     				=  OwlViTProcessor(tokenizer=A__  ,						image_processor=A__		)

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

        __a     				=  processor.batch_decode(A__		)
        __a     				=  tokenizer.batch_decode(A__		)

        self.assertListEqual(A__  ,						A__		)

"""simple docstring"""



from typing import List, Union

from ..utils import (
    add_end_docstrings,
    is_tf_available,
    is_torch_available,
    is_vision_available,
    logging,
    requires_backends,
)
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
   from PIL import Image

   from ..image_utils import load_image

if is_tf_available():
   import tensorflow as tf

   from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
   from ..tf_utils import stable_softmax

if is_torch_available():
   from ..models.auto.modeling_auto import MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING

SCREAMING_SNAKE_CASE__:Optional[Any]			=	logging.get_logger(__name__)






@add_end_docstrings(snake_case_   )
class 				snake_case__     (   snake_case_   ):


 def __init__(				self  ,						*lowerCamelCase  ,						**lowerCamelCase		):
        super().__init__(*lowerCamelCase  ,						**lowerCamelCase		)
        requires_backends(self  ,						"vision"		)
        self.check_model_type(
            TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
            if self.framework == "tf"
            else MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING		)


 def 		a__ (				self  ,						lowerCamelCase=None		):
        __a     				=  {}
        if top_k is not None:
               __a     				=  top_k
        return {}, {}, postprocess_params


 def __call__(				self  ,						lowerCamelCase  ,						**lowerCamelCase		):
        return super().__call__(lowerCamelCase  ,						**lowerCamelCase		)


 def 		a__ (				self  ,						lowerCamelCase		):
        __a     				=  load_image(lowerCamelCase		)
        __a     				=  self.image_processor(images=lowerCamelCase  ,						return_tensors=self.framework		)
        return model_inputs


 def 		a__ (				self  ,						lowerCamelCase		):
        __a     				=  self.model(**lowerCamelCase		)
        return model_outputs






 def 		a__ (				self  ,						lowerCamelCase  ,						lowerCamelCase=5		):
        if top_k > self.model.config.num_labels:
               __a     				=  self.model.config.num_labels

        if self.framework == "pt":
               __a     				=  model_outputs.logits.softmax(-1		)[0]
               __a				, __a     				=  probs.topk(lowerCamelCase		)
        elif self.framework == "tf":
               __a     				=  stable_softmax(model_outputs.logits  ,						axis=-1		)[0]
               __a     				=  tf.math.top_k(lowerCamelCase  ,						k=lowerCamelCase		)
               __a				, __a     				=  topk.values.numpy(), topk.indices.numpy()
        else:
               raise ValueError(F"Unsupported framework: {self.framework}"		)

        __a     				=  scores.tolist()
        __a     				=  ids.tolist()
        return [{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(lowerCamelCase  ,						lowerCamelCase		)]
