





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


__A     =    logging.get_logger(__name__)



@add_end_docstrings(__SCREAMING_SNAKE_CASE   )
class        _SCREAMING_SNAKE_CASE						(     __SCREAMING_SNAKE_CASE   ):







 '''simple docstring'''

 def __init__(self    :       int	,			**UpperCAmelCase_    :       Tuple)	->List[Any]:



   '''simple docstring'''



   super().__init__(**UpperCAmelCase_)

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

 def __call__(self    :       str	,			UpperCAmelCase_    :       Union[np.ndarray, bytes, str]	,			**UpperCAmelCase_    :       Any)	->str:



   '''simple docstring'''



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

 def 							SCREAMING_SNAKE_CASE_ (self    :       Dict	,			**UpperCAmelCase_    :       Union[str, Any])	->int:



   '''simple docstring'''



   lowerCamelCase__:				List[Any]        ={}
   if "candidate_labels" in kwargs:
     lowerCamelCase__:				int        =kwargs["candidate_labels"]
   if "hypothesis_template" in kwargs:
     lowerCamelCase__:				Any        =kwargs["hypothesis_template"]

   return preprocess_params, {}, {}

 def 							SCREAMING_SNAKE_CASE_ (self    :       int	,			UpperCAmelCase_    :       Optional[int]	,			UpperCAmelCase_    :       Optional[Any]=None	,			UpperCAmelCase_    :       Any="This is a sound of {}.")	->List[str]:



   '''simple docstring'''



   if isinstance(UpperCAmelCase_	,			UpperCAmelCase_):
     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
       lowerCamelCase__:				Tuple        =requests.get(UpperCAmelCase_).content
     else:
       with open(UpperCAmelCase_	,			"rb") as f:
         lowerCamelCase__:				Tuple        =f.read()

   if isinstance(UpperCAmelCase_	,			UpperCAmelCase_):
     lowerCamelCase__:				Optional[Any]        =ffmpeg_read(UpperCAmelCase_	,			self.feature_extractor.sampling_rate)

   if not isinstance(UpperCAmelCase_	,			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")

   lowerCamelCase__:				Tuple        =self.feature_extractor(
       [audio]	,			sampling_rate=self.feature_extractor.sampling_rate	,			return_tensors="pt")
   lowerCamelCase__:				Dict        =candidate_labels
   lowerCamelCase__:				List[Any]        =[hypothesis_template.format(UpperCAmelCase_) for x in candidate_labels]
   lowerCamelCase__:				Optional[int]        =self.tokenizer(UpperCAmelCase_	,			return_tensors=self.framework	,			padding=UpperCAmelCase_)
   lowerCamelCase__:				List[str]        =[text_inputs]
   return inputs

 def 							SCREAMING_SNAKE_CASE_ (self    :       Any	,			UpperCAmelCase_    :       Any)	->Dict:



   '''simple docstring'''



   lowerCamelCase__:				Optional[int]        =model_inputs.pop("candidate_labels")
   lowerCamelCase__:				str        =model_inputs.pop("text_inputs")
   if isinstance(text_inputs[0]	,			UpperCAmelCase_):
     lowerCamelCase__:				Any        =text_inputs[0]
   else:
     # Batching case.
     lowerCamelCase__:				Tuple        =text_inputs[0][0]

   lowerCamelCase__:				Tuple        =self.model(**UpperCAmelCase_	,			**UpperCAmelCase_)

   lowerCamelCase__:				Any        ={
       "candidate_labels": candidate_labels,
       "logits": outputs.logits_per_audio,
   }
   return model_outputs







 def 							SCREAMING_SNAKE_CASE_ (self    :       List[Any]	,			UpperCAmelCase_    :       List[Any])	->List[str]:



   '''simple docstring'''



   lowerCamelCase__:				List[Any]        =model_outputs.pop("candidate_labels")
   lowerCamelCase__:				str        =model_outputs["logits"][0]

   if self.framework == "pt":
     lowerCamelCase__:				Union[str, Any]        =logits.softmax(dim=0)
     lowerCamelCase__:				Union[str, Any]        =probs.tolist()
   else:
     raise ValueError("`tf` framework not supported.")

   lowerCamelCase__:				List[Any]        =[
       {"score": score, "label": candidate_label}
       for score, candidate_label in sorted(zip(UpperCAmelCase_	,			UpperCAmelCase_)	,			key=lambda UpperCAmelCase_: -x[0])
   ]
   return result








from pathlib import Path
from typing import List

from transformers import is_torch_available, is_vision_available
from transformers.testing_utils import get_tests_dir, is_tool_test
from transformers.tools.agent_types import AGENT_TYPE_MAPPING, AgentAudio, AgentImage, AgentText


if is_torch_available():
     import torch

if is_vision_available():
     from PIL import Image


__A     =    ["text", "image", "audio"]





def 					lowerCAmelCase_			(	__a		)					->		Optional[Any]:







  """simple docstring"""





  lowerCamelCase__:				Tuple        =[]

  for input_type in input_types:
    if input_type == "text":
      inputs.append("Text input"		)
    elif input_type == "image":
      inputs.append(
          Image.open(Path(get_tests_dir("fixtures/tests_samples/COCO"		)		) / "000000039769.png"		).resize((512, 512)		)		)
    elif input_type == "audio":
      inputs.append(torch.ones(3000		)		)
    elif isinstance(__a     ,   __a		):
      inputs.append(create_inputs(__a		)		)
    else:
      raise ValueError(F"""Invalid type requested: {input_type}"""		)

  return inputs





def 					lowerCAmelCase_			(	__a		)					->		Union[str, Any]:







  """simple docstring"""





  lowerCamelCase__:				Union[str, Any]        =[]

  for output in outputs:
    if isinstance(__a     ,   (str, AgentText)		):
      output_types.append("text"		)
    elif isinstance(__a     ,   (Image.Image, AgentImage)		):
      output_types.append("image"		)
    elif isinstance(__a     ,   (torch.Tensor, AgentAudio)		):
      output_types.append("audio"		)
    else:
      raise ValueError(F"""Invalid output: {output}"""		)

  return output_types






@is_tool_test
class        _SCREAMING_SNAKE_CASE						:







 '''simple docstring'''

 def 							SCREAMING_SNAKE_CASE_ (self    :       List[str])	->Dict:



   '''simple docstring'''



   self.assertTrue(hasattr(self.tool	,			"inputs"))
   self.assertTrue(hasattr(self.tool	,			"outputs"))

   lowerCamelCase__:				Tuple        =self.tool.inputs
   for _input in inputs:
     if isinstance(_input	,			UpperCAmelCase_):
       for __input in _input:
         self.assertTrue(__input in authorized_types)
     else:
       self.assertTrue(_input in authorized_types)

   lowerCamelCase__:				Optional[Any]        =self.tool.outputs
   for _output in outputs:
     self.assertTrue(_output in authorized_types)

 def 							SCREAMING_SNAKE_CASE_ (self    :       Optional[Any])	->str:



   '''simple docstring'''



   lowerCamelCase__:				List[str]        =create_inputs(self.tool.inputs)
   lowerCamelCase__:				str        =self.tool(*UpperCAmelCase_)

   # There is a single output
   if len(self.tool.outputs) == 1:
     lowerCamelCase__:				Optional[Any]        =[outputs]

   self.assertListEqual(output_types(UpperCAmelCase_)	,			self.tool.outputs)

 def 							SCREAMING_SNAKE_CASE_ (self    :       Dict)	->Any:



   '''simple docstring'''



   self.assertTrue(hasattr(self.tool	,			"description"))
   self.assertTrue(hasattr(self.tool	,			"default_checkpoint"))
   self.assertTrue(self.tool.description.startswith("This is a tool that"))

 def 							SCREAMING_SNAKE_CASE_ (self    :       Union[str, Any])	->Optional[int]:



   '''simple docstring'''



   lowerCamelCase__:				str        =create_inputs(self.tool.inputs)
   lowerCamelCase__:				Dict        =self.tool(*UpperCAmelCase_)

   if not isinstance(UpperCAmelCase_	,			UpperCAmelCase_):
     lowerCamelCase__:				Tuple        =[outputs]

   self.assertEqual(len(UpperCAmelCase_)	,			len(self.tool.outputs))

   for output, output_type in zip(UpperCAmelCase_	,			self.tool.outputs):
     lowerCamelCase__:				Any        =AGENT_TYPE_MAPPING[output_type]
     self.assertTrue(isinstance(UpperCAmelCase_	,			UpperCAmelCase_))







 def 							SCREAMING_SNAKE_CASE_ (self    :       Dict)	->str:



   '''simple docstring'''



   lowerCamelCase__:				Any        =create_inputs(self.tool.inputs)

   lowerCamelCase__:				int        =[]

   for _input, input_type in zip(UpperCAmelCase_	,			self.tool.inputs):
     if isinstance(UpperCAmelCase_	,			UpperCAmelCase_):
       _inputs.append([AGENT_TYPE_MAPPING[_input_type](_input) for _input_type in input_type])
     else:
       _inputs.append(AGENT_TYPE_MAPPING[input_type](_input))

        # Should not raise an error
   lowerCamelCase__:				Union[str, Any]        =self.tool(*UpperCAmelCase_)

   if not isinstance(UpperCAmelCase_	,			UpperCAmelCase_):
     lowerCamelCase__:				str        =[outputs]

   self.assertEqual(len(UpperCAmelCase_)	,			len(self.tool.outputs))

