

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

   '''simple docstring'''




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






     """simple docstring"""






     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(lowercase_,    range(len(lowercase_       )       )       )       )
     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(lowercase_       ) + '''\n'''       )
     with open(self.merges_file,    '''w''',    encoding='''utf-8'''       ) as fp:
       fp.write('''\n'''.join(lowercase_       )       )

     a__  ={
         '''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],
     }
     a__  =os.path.join(self.tmpdirname,    lowercase_       )
     with open(self.image_processor_file,    '''w''',    encoding='''utf-8'''       ) as fp:
       json.dump(lowercase_,    lowercase_       )



   def  _UpperCAmelCase (				self,    **lowercase_       )							->    List[str]:






     """simple docstring"""






     return CLIPTokenizer.from_pretrained(self.tmpdirname,    pad_token='''!''',    **lowercase_       )



   def  _UpperCAmelCase (				self,    **lowercase_       )							->    Tuple:






     """simple docstring"""






     return CLIPTokenizerFast.from_pretrained(self.tmpdirname,    pad_token='''!''',    **lowercase_       )



   def  _UpperCAmelCase (				self,    **lowercase_       )							->    Dict:






     """simple docstring"""






     return OwlViTImageProcessor.from_pretrained(self.tmpdirname,    **lowercase_       )



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






     """simple docstring"""






     shutil.rmtree(self.tmpdirname       )



   def  _UpperCAmelCase (				self       )							->    List[Any]:






     """simple docstring"""






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

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

     return image_inputs



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






     """simple docstring"""






     a__  =self.get_tokenizer()
     a__  =self.get_rust_tokenizer()
     a__  =self.get_image_processor()

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

     a__  =OwlViTProcessor(tokenizer=lowercase_,    image_processor=lowercase_       )
     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,    lowercase_       )
     self.assertIsInstance(processor_fast.tokenizer,    lowercase_       )

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



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






     """simple docstring"""






     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=lowercase_       )

     a__  =OwlViTProcessor.from_pretrained(
         self.tmpdirname,    bos_token='''(BOS)''',    eos_token='''(EOS)''',    do_normalize=lowercase_       )

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

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



   def  _UpperCAmelCase (				self       )							->    Tuple:






     """simple docstring"""






     a__  =self.get_image_processor()
     a__  =self.get_tokenizer()

     a__  =OwlViTProcessor(tokenizer=lowercase_,    image_processor=lowercase_       )

     a__  =self.prepare_image_inputs()

     a__  =image_processor(lowercase_,    return_tensors='''np'''       )
     a__  =processor(images=lowercase_,    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  _UpperCAmelCase (				self       )							->    Union[str, Any]:






     """simple docstring"""






     a__  =self.get_image_processor()
     a__  =self.get_tokenizer()

     a__  =OwlViTProcessor(tokenizer=lowercase_,    image_processor=lowercase_       )

     a__  ='''lower newer'''

     a__  =processor(text=lowercase_,    return_tensors='''np'''       )

     a__  =tokenizer(lowercase_,    return_tensors='''np'''       )

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



   def  _UpperCAmelCase (				self       )							->    List[Any]:






     """simple docstring"""






     a__  =self.get_image_processor()
     a__  =self.get_tokenizer()

     a__  =OwlViTProcessor(tokenizer=lowercase_,    image_processor=lowercase_       )

     a__  ='''lower newer'''
     a__  =self.prepare_image_inputs()

     a__  =processor(text=lowercase_,    images=lowercase_       )

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

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



   def  _UpperCAmelCase (				self       )							->    Tuple:






     """simple docstring"""






     a__  ='''google/owlvit-base-patch32'''
     a__  =OwlViTProcessor.from_pretrained(lowercase_       )

     a__  =['''cat''', '''nasa badge''']
     a__  =processor(text=lowercase_       )

     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(lowercase_       ):
       processor()



   def  _UpperCAmelCase (				self       )							->    Any:






     """simple docstring"""






     a__  ='''google/owlvit-base-patch32'''
     a__  =OwlViTProcessor.from_pretrained(lowercase_       )

     a__  =[['''cat''', '''nasa badge'''], ['''person''']]
     a__  =processor(text=lowercase_       )

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



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






     """simple docstring"""






     a__  ='''google/owlvit-base-patch32'''
     a__  =OwlViTProcessor.from_pretrained(lowercase_       )

     a__  =['''cat''', '''nasa badge''']
     a__  =processor(text=lowercase_       )

     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  _UpperCAmelCase (				self       )							->    Optional[Any]:






     """simple docstring"""






     a__  =self.get_image_processor()
     a__  =self.get_tokenizer()

     a__  =OwlViTProcessor(tokenizer=lowercase_,    image_processor=lowercase_       )

     a__  =self.prepare_image_inputs()
     a__  =self.prepare_image_inputs()

     a__  =processor(images=lowercase_,    query_images=lowercase_       )

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

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



   def  _UpperCAmelCase (				self       )							->    Any:






     """simple docstring"""






     a__  =self.get_image_processor()
     a__  =self.get_tokenizer()

     a__  =OwlViTProcessor(tokenizer=lowercase_,    image_processor=lowercase_       )

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

     a__  =processor.batch_decode(lowercase_       )
     a__  =tokenizer.batch_decode(lowercase_       )

     self.assertListEqual(lowercase_,    lowercase_       )



from collections import UserDict
from typing import Union

import numpy as np
import requests

from ..utils import (
    add_end_docstrings,
    logging,
)
from .audio_classification import ffmpeg_read
from .base import PIPELINE_INIT_ARGS, Pipeline


lowerCamelCase           =					logging.get_logger(__name__)







@add_end_docstrings(lowerCamelCase__							)
class 						__magic_name__			(							lowerCamelCase__							):

   '''simple docstring'''




   def __init__(				self,    **lowercase_       )							->    Dict:






     """simple docstring"""






     super().__init__(**lowercase_       )

     if self.framework != "pt":
       raise ValueError(F"""The {self.__class__} is only available in PyTorch."""       )
     # No specific FOR_XXX available yet



   def __call__(				self,    lowercase_,    **lowercase_       )							->    Tuple:






     """simple docstring"""






     return super().__call__(lowercase_,    **lowercase_       )



   def  _UpperCAmelCase (				self,    **lowercase_       )							->    int:






     """simple docstring"""






     a__  ={}
     if "candidate_labels" in kwargs:
       a__  =kwargs['''candidate_labels''']
     if "hypothesis_template" in kwargs:
       a__  =kwargs['''hypothesis_template''']

     return preprocess_params, {}, {}



   def  _UpperCAmelCase (				self,    lowercase_,    lowercase_=None,    lowercase_="This is a sound of {}."       )							->    Union[str, Any]:






     """simple docstring"""






     if isinstance(lowercase_,    lowercase_       ):
       if audio.startswith('''http://'''       ) or audio.startswith('''https://'''       ):
         # We need to actually check for a real protocol, otherwise it's impossible to use a local file
         # like http_huggingface_co.png
         a__  =requests.get(lowercase_       ).content
       else:
         with open(lowercase_,    '''rb'''       ) as f:
           a__  =f.read()

     if isinstance(lowercase_,    lowercase_       ):
       a__  =ffmpeg_read(lowercase_,    self.feature_extractor.sampling_rate       )

     if not isinstance(lowercase_,    np.ndarray       ):
       raise ValueError('''We expect a numpy ndarray as input'''       )
     if len(audio.shape       ) != 1:
       raise ValueError('''We expect a single channel audio input for ZeroShotAudioClassificationPipeline'''       )

     a__  =self.feature_extractor(
         [audio],    sampling_rate=self.feature_extractor.sampling_rate,    return_tensors='''pt'''       )
     a__  =candidate_labels
     a__  =[hypothesis_template.format(lowercase_       ) for x in candidate_labels]
     a__  =self.tokenizer(lowercase_,    return_tensors=self.framework,    padding=lowercase_       )
     a__  =[text_inputs]
     return inputs



   def  _UpperCAmelCase (				self,    lowercase_       )							->    str:






     """simple docstring"""






     a__  =model_inputs.pop('''candidate_labels'''       )
     a__  =model_inputs.pop('''text_inputs'''       )
     if isinstance(text_inputs[0],    lowercase_       ):
       a__  =text_inputs[0]
     else:
       # Batching case.
       a__  =text_inputs[0][0]

     a__  =self.model(**lowercase_,    **lowercase_       )

     a__  ={
         '''candidate_labels''': candidate_labels,
         '''logits''': outputs.logits_per_audio,
     }
     return model_outputs



   def  _UpperCAmelCase (				self,    lowercase_       )							->    Any:






     """simple docstring"""






     a__  =model_outputs.pop('''candidate_labels'''       )
     a__  =model_outputs['''logits'''][0]

     if self.framework == "pt":
       a__  =logits.softmax(dim=0       )
       a__  =probs.tolist()
     else:
       raise ValueError('''`tf` framework not supported.'''       )

     a__  =[
         {'''score''': score, '''label''': candidate_label}
         for score, candidate_label in sorted(zip(lowercase_,    lowercase_       ),    key=lambda lowercase_       : -x[0]       )
     ]
     return result
