'''simple docstring'''

from __future__ import annotations

from collections.abc import Callable
from typing import Generic, TypeVar

__a:			int	       =       TypeVar("""T""")
__a:			int	       =       TypeVar("""U""")



class       UpperCAmelCase  (						Generic[T, U] ):





       '''simple docstring'''



       def __init__(						self     ,       __lowerCAmelCase     ,       __lowerCAmelCase			)  ->  List[Any]:
        lowercase__    :   List[Any]						= key
        lowercase__    :   Dict						= val
        lowercase__    :   DoubleLinkedListNode[T, U] | None						= None
        lowercase__    :   DoubleLinkedListNode[T, U] | None						= None


       def __repr__(						self			)  ->  str:
        return (
            F"""Node: key: {self.key}, val: {self.val}, """
            F"""has next: {bool(self.next			)}, has prev: {bool(self.prev			)}"""
        )



class       UpperCAmelCase  (						Generic[T, U] ):





       '''simple docstring'''



       def __init__(						self			)  ->  None:
        lowercase__    :   DoubleLinkedListNode[T, U]						= DoubleLinkedListNode(__lowerCAmelCase     ,       __lowerCAmelCase			)
        lowercase__    :   DoubleLinkedListNode[T, U]						= DoubleLinkedListNode(__lowerCAmelCase     ,       __lowerCAmelCase			)
        lowercase__    :   List[Any]						= self.rear, self.head

       def __repr__(						self			)  ->  str:
        lowercase__    :   Dict						= ['''DoubleLinkedList''']
        lowercase__    :   str						= self.head
        while node.next is not None:
         rep.append(str(__lowerCAmelCase			)			)
         lowercase__    :   Optional[Any]						= node.next
        rep.append(str(self.rear			)			)
        return ",\n    ".join(__lowerCAmelCase			)

       def _lowerCAmelCase(						self     ,       __lowerCAmelCase			)  ->  None:
        lowercase__    :   List[Any]						= self.rear.prev

        # All nodes other than self.head are guaranteed to have non-None previous
        assert previous is not None

        lowercase__    :   Any						= node
        lowercase__    :   Union[str, Any]						= previous
        lowercase__    :   Dict						= node
        lowercase__    :   List[Any]						= self.rear


       def _lowerCAmelCase(						self     ,       __lowerCAmelCase			)  ->  DoubleLinkedListNode[T, U] | None:

        if node.prev is None or node.next is None:
         return None

        lowercase__    :   Optional[Any]						= node.next
        lowercase__    :   List[Any]						= node.prev
        lowercase__    :   List[Any]						= None
        lowercase__    :   Optional[Any]						= None
        return node



class       UpperCAmelCase  (						Generic[T, U] ):





       '''simple docstring'''



       SCREAMING_SNAKE_CASE 		=   {}

       def __init__(						self     ,       __lowerCAmelCase			)  ->  Tuple:
        lowercase__    :   DoubleLinkedList[T, U]						= DoubleLinkedList()
        lowercase__    :   Optional[Any]						= capacity
        lowercase__    :   int						= 0
        lowercase__    :   Union[str, Any]						= 0
        lowercase__    :   Dict						= 0
        lowercase__    :   dict[T, DoubleLinkedListNode[T, U]]						= {}

       def __repr__(						self			)  ->  str:
        return (
            F"""CacheInfo(hits={self.hits}, misses={self.miss}, """
            F"""capacity={self.capacity}, current size={self.num_keys})"""
        )

       def __contains__(						self     ,       __lowerCAmelCase			)  ->  bool:
        return key in self.cache

       def _lowerCAmelCase(						self     ,       __lowerCAmelCase			)  ->  U | None:
        # Note: pythonic interface would throw KeyError rather than return None

        if key in self.cache:
         self.hits += 1
         lowercase__    :   DoubleLinkedListNode[T, U]						= self.cache[key]
         lowercase__    :   Any						= self.list.remove(self.cache[key]			)
         assert node == value_node

         # node is guaranteed not None because it is in self.cache
         assert node is not None
         self.list.add(__lowerCAmelCase			)
         return node.val
        self.miss += 1
        return None

       def _lowerCAmelCase(						self     ,       __lowerCAmelCase     ,       __lowerCAmelCase			)  ->  None:

        if key not in self.cache:
         if self.num_keys >= self.capacity:
          # delete first node (oldest) when over capacity
          lowercase__    :   List[Any]						= self.list.head.next

          # guaranteed to have a non-None first node when num_keys > 0
          # explain to type checker via assertions
          assert first_node is not None
          assert first_node.key is not None
          assert (
              self.list.remove(__lowerCAmelCase			) is not None
          )  # node guaranteed to be in list assert node.key is not None

          del self.cache[first_node.key]
          self.num_keys -= 1
         lowercase__    :   Dict						= DoubleLinkedListNode(__lowerCAmelCase     ,       __lowerCAmelCase			)
         self.list.add(self.cache[key]			)
         self.num_keys += 1

        else:
         # bump node to the end of the list, update value
         lowercase__    :   int						= self.list.remove(self.cache[key]			)
         assert node is not None  # node guaranteed to be in list
         lowercase__    :   Any						= value
         self.list.add(__lowerCAmelCase			)


       @classmethod
       def _lowerCAmelCase(						cls     ,       __lowerCAmelCase = 128			)  ->  Callable[[Callable[[T], U]], Callable[..., U]]:

        def cache_decorator_inner(__lowerCAmelCase			) -> Callable[..., U]:
         def cache_decorator_wrapper(*__lowerCAmelCase			) -> U:
          if func not in cls.decorator_function_to_instance_map:
           lowercase__    :   str						= LRUCache(__lowerCAmelCase			)

          lowercase__    :   Optional[Any]						= cls.decorator_function_to_instance_map[func].get(args[0]			)
          if result is None:
           lowercase__    :   int						= func(*__lowerCAmelCase			)
           cls.decorator_function_to_instance_map[func].put(args[0]     ,       __lowerCAmelCase			)
          return result

         def cache_info() -> LRUCache[T, U]:
          return cls.decorator_function_to_instance_map[func]

         setattr(__lowerCAmelCase     ,       '''cache_info'''     ,       __lowerCAmelCase			)  # noqa: B010

         return cache_decorator_wrapper

        return cache_decorator_inner


if __name__ == "__main__":
  import doctest

  doctest.testmod()


'''simple docstring'''

import unittest

from datasets import load_dataset

from transformers.pipelines import pipeline
from transformers.testing_utils import is_pipeline_test, nested_simplify, require_torch, slow



@is_pipeline_test
@require_torch
class       UpperCAmelCase  (						unittest.TestCase ):





       '''simple docstring'''



       @require_torch
       def _lowerCAmelCase(						self			)  ->  int:
        lowercase__    :   str						= pipeline(
            task='''zero-shot-audio-classification'''     ,       model='''hf-internal-testing/tiny-clap-htsat-unfused'''			)
        lowercase__    :   Optional[Any]						= load_dataset('''ashraq/esc50'''			)
        lowercase__    :   Tuple						= dataset['''train''']['''audio'''][-1]['''array''']
        lowercase__    :   Optional[Any]						= audio_classifier(__lowerCAmelCase     ,       candidate_labels=['''Sound of a dog''', '''Sound of vaccum cleaner''']			)
        self.assertEqual(
            nested_simplify(__lowerCAmelCase			)     ,       [{'''score''': 0.5_0_1, '''label''': '''Sound of a dog'''}, {'''score''': 0.4_9_9, '''label''': '''Sound of vaccum cleaner'''}]     ,       )

       @unittest.skip('''No models are available in TF'''			)
       def _lowerCAmelCase(						self			)  ->  str:
        pass

       @slow
       @require_torch
       def _lowerCAmelCase(						self			)  ->  Tuple:
        lowercase__    :   List[str]						= pipeline(
            task='''zero-shot-audio-classification'''     ,       model='''laion/clap-htsat-unfused'''     ,       )
        # This is an audio of a dog
        lowercase__    :   int						= load_dataset('''ashraq/esc50'''			)
        lowercase__    :   str						= dataset['''train''']['''audio'''][-1]['''array''']
        lowercase__    :   Any						= audio_classifier(__lowerCAmelCase     ,       candidate_labels=['''Sound of a dog''', '''Sound of vaccum cleaner''']			)

        self.assertEqual(
            nested_simplify(__lowerCAmelCase			)     ,       [
                {'''score''': 0.9_9_9, '''label''': '''Sound of a dog'''},
                {'''score''': 0.0_0_1, '''label''': '''Sound of vaccum cleaner'''},
            ]     ,       )

        lowercase__    :   Dict						= audio_classifier([audio] * 5     ,       candidate_labels=['''Sound of a dog''', '''Sound of vaccum cleaner''']			)
        self.assertEqual(
            nested_simplify(__lowerCAmelCase			)     ,       [
                [
                    {'''score''': 0.9_9_9, '''label''': '''Sound of a dog'''},
                    {'''score''': 0.0_0_1, '''label''': '''Sound of vaccum cleaner'''},
                ],
            ]
            * 5     ,       )
        lowercase__    :   Any						= audio_classifier(
            [audio] * 5     ,       candidate_labels=['''Sound of a dog''', '''Sound of vaccum cleaner''']     ,       batch_size=5			)
        self.assertEqual(
            nested_simplify(__lowerCAmelCase			)     ,       [
                [
                    {'''score''': 0.9_9_9, '''label''': '''Sound of a dog'''},
                    {'''score''': 0.0_0_1, '''label''': '''Sound of vaccum cleaner'''},
                ],
            ]
            * 5     ,       )


       @unittest.skip('''No models are available in TF'''			)
       def _lowerCAmelCase(						self			)  ->  Union[str, Any]:
        pass

