






from collections import deque
from math import floor
from random import random
from time import time

class 				lowerCAmelCase_	:


				def __init__(					self      ):
											_lowercase							:   Optional[int] 							=   {}


				def 						__a					(					self ,						_lowerCAmelCase ,						_lowerCAmelCase ,						_lowerCAmelCase=1      ):
											if self.graph.get(_lowerCAmelCase      ):
																		if self.graph[u].count([w, v]      ) == 0:
																									self.graph[u].append([w, v]      )
											else:
																		_lowercase							:   Tuple 							=   [[w, v]]
											if not self.graph.get(_lowerCAmelCase      ):
																		_lowercase							:   Optional[Any] 							=   []


				def 						__a					(					self      ):
											return list(self.graph      )


				def 						__a					(					self ,						_lowerCAmelCase ,						_lowerCAmelCase      ):
											if self.graph.get(_lowerCAmelCase      ):
																		for _ in self.graph[u]:
																									if _[1] == v:
																																self.graph[u].remove(_lowerCAmelCase      )


				def 						__a					(					self ,						_lowerCAmelCase=-2 ,						_lowerCAmelCase=-1      ):
											if s == d:
																		return []
											_lowercase							:   str 							=   []
											_lowercase							:   Union[str, Any] 							=   []
											if s == -2:
																		_lowercase							:   str 							=   list(self.graph      )[0]
											stack.append(_lowerCAmelCase      )
											visited.append(_lowerCAmelCase      )
											_lowercase							:   Optional[int] 							=   s

											while True:
																		# check if there is any non isolated nodes
																		if len(self.graph[s]      ) != 0:
																									_lowercase							:   Optional[int] 							=   s
																									for node in self.graph[s]:
																																if visited.count(node[1]      ) < 1:
																																							if node[1] == d:
																																														visited.append(_lowerCAmelCase      )
																																														return visited
																																							else:
																																														stack.append(node[1]      )
																																														visited.append(node[1]      )
																																														_lowercase							:   Union[str, Any] 							=   node[1]
																																														break

            # check if all the children are visited
																		if s == ss:
																									stack.pop()
																									if len(_lowerCAmelCase      ) != 0:
																																_lowercase							:   int 							=   stack[len(_lowerCAmelCase      ) - 1]
																		else:
																									_lowercase							:   Optional[int] 							=   ss

																		# check if se have reached the starting point
																		if len(_lowerCAmelCase      ) == 0:
																									return visited


				def 						__a					(					self ,						_lowerCAmelCase=-1      ):
											if c == -1:
																		_lowercase							:   Dict 							=   floor(random() * 1_0_0_0_0      ) + 1_0
											for i in range(_lowerCAmelCase      ):
																		# every vertex has max 100 edges
																		for _ in range(floor(random() * 1_0_2      ) + 1      ):
																									_lowercase							:   str 							=   floor(random() * c      ) + 1
																									if n != i:
																																self.add_pair(_lowerCAmelCase ,						_lowerCAmelCase ,						1      )


				def 						__a					(					self ,						_lowerCAmelCase=-2      ):
											_lowercase							:   str 							=   deque()
											_lowercase							:   Optional[Any] 							=   []
											if s == -2:
																		_lowercase							:   List[Any] 							=   list(self.graph      )[0]
											d.append(_lowerCAmelCase      )
											visited.append(_lowerCAmelCase      )
											while d:
																		_lowercase							:   Tuple 							=   d.popleft()
																		if len(self.graph[s]      ) != 0:
																									for node in self.graph[s]:
																																if visited.count(node[1]      ) < 1:
																																							d.append(node[1]      )
																																							visited.append(node[1]      )
											return visited


				def 						__a					(					self ,						_lowerCAmelCase      ):
											_lowercase							:   str 							=   0
											for x in self.graph:
																		for y in self.graph[x]:
																									if y[1] == u:
																																count += 1
											return count


				def 						__a					(					self ,						_lowerCAmelCase      ):
											return len(self.graph[u]      )


				def 						__a					(					self ,						_lowerCAmelCase=-2      ):
											_lowercase							:   Dict 							=   []
											_lowercase							:   Tuple 							=   []
											if s == -2:
																		_lowercase							:   Union[str, Any] 							=   list(self.graph      )[0]
											stack.append(_lowerCAmelCase      )
											visited.append(_lowerCAmelCase      )
											_lowercase							:   List[Any] 							=   s
											_lowercase							:   Dict 							=   []

											while True:
																		# check if there is any non isolated nodes
																		if len(self.graph[s]      ) != 0:
																									_lowercase							:   int 							=   s
																									for node in self.graph[s]:
																																if visited.count(node[1]      ) < 1:
																																							stack.append(node[1]      )
																																							visited.append(node[1]      )
																																							_lowercase							:   str 							=   node[1]
																																							break

            # check if all the children are visited
																		if s == ss:
																									sorted_nodes.append(stack.pop()      )
																									if len(_lowerCAmelCase      ) != 0:
																																_lowercase							:   str 							=   stack[len(_lowerCAmelCase      ) - 1]
																		else:
																									_lowercase							:   int 							=   ss

																		# check if se have reached the starting point
																		if len(_lowerCAmelCase      ) == 0:
																									return sorted_nodes


				def 						__a					(					self      ):
											_lowercase							:   Tuple 							=   []
											_lowercase							:   Tuple 							=   []
											_lowercase							:   Dict 							=   list(self.graph      )[0]
											stack.append(_lowerCAmelCase      )
											visited.append(_lowerCAmelCase      )
											_lowercase							:   Optional[int] 							=   -2
											_lowercase							:   Tuple 							=   []
											_lowercase							:   Dict 							=   s
											_lowercase							:   List[str] 							=   False
											_lowercase							:   List[Any] 							=   set()

											while True:
																		# check if there is any non isolated nodes
																		if len(self.graph[s]      ) != 0:
																									_lowercase							:   List[Any] 							=   s
																									for node in self.graph[s]:
																																if (
																																    visited.count(node[1]      ) > 0
																																    and node[1] != parent
																																    and indirect_parents.count(node[1]      ) > 0
																																    and not on_the_way_back
																																):
																																							_lowercase							:   Union[str, Any] 							=   len(_lowerCAmelCase      ) - 1
																																							while len_stack >= 0:
																																														if stack[len_stack] == node[1]:
																																																					anticipating_nodes.add(node[1]      )
																																																					break
																																														else:
																																																					anticipating_nodes.add(stack[len_stack]      )
																																																					len_stack -= 1
																																if visited.count(node[1]      ) < 1:
																																							stack.append(node[1]      )
																																							visited.append(node[1]      )
																																							_lowercase							:   Any 							=   node[1]
																																							break

            # check if all the children are visited
																		if s == ss:
																									stack.pop()
																									_lowercase							:   int 							=   True
																									if len(_lowerCAmelCase      ) != 0:
																																_lowercase							:   List[Any] 							=   stack[len(_lowerCAmelCase      ) - 1]
																		else:
																									_lowercase							:   Tuple 							=   False
																									indirect_parents.append(_lowerCAmelCase      )
																									_lowercase							:   Dict 							=   s
																									_lowercase							:   Union[str, Any] 							=   ss

																		# check if se have reached the starting point
																		if len(_lowerCAmelCase      ) == 0:
																									return list(_lowerCAmelCase      )


				def 						__a					(					self      ):
											_lowercase							:   Optional[Any] 							=   []
											_lowercase							:   Optional[Any] 							=   []
											_lowercase							:   List[Any] 							=   list(self.graph      )[0]
											stack.append(_lowerCAmelCase      )
											visited.append(_lowerCAmelCase      )
											_lowercase							:   Dict 							=   -2
											_lowercase							:   Union[str, Any] 							=   []
											_lowercase							:   int 							=   s
											_lowercase							:   List[str] 							=   False
											_lowercase							:   Dict 							=   set()

											while True:
																		# check if there is any non isolated nodes
																		if len(self.graph[s]      ) != 0:
																									_lowercase							:   List[Any] 							=   s
																									for node in self.graph[s]:
																																if (
																																    visited.count(node[1]      ) > 0
																																    and node[1] != parent
																																    and indirect_parents.count(node[1]      ) > 0
																																    and not on_the_way_back
																																):
																																							_lowercase							:   str 							=   len(_lowerCAmelCase      ) - 1
																																							while len_stack_minus_one >= 0:
																																														if stack[len_stack_minus_one] == node[1]:
																																																					anticipating_nodes.add(node[1]      )
																																																					break
																																														else:
																																																					return True
																																if visited.count(node[1]      ) < 1:
																																							stack.append(node[1]      )
																																							visited.append(node[1]      )
																																							_lowercase							:   Dict 							=   node[1]
																																							break

            # check if all the children are visited
																		if s == ss:
																									stack.pop()
																									_lowercase							:   Dict 							=   True
																									if len(_lowerCAmelCase      ) != 0:
																																_lowercase							:   Union[str, Any] 							=   stack[len(_lowerCAmelCase      ) - 1]
																		else:
																									_lowercase							:   Any 							=   False
																									indirect_parents.append(_lowerCAmelCase      )
																									_lowercase							:   Optional[Any] 							=   s
																									_lowercase							:   Any 							=   ss

																		# check if se have reached the starting point
																		if len(_lowerCAmelCase      ) == 0:
																									return False


				def 						__a					(					self ,						_lowerCAmelCase=-2 ,						_lowerCAmelCase=-1      ):
											_lowercase							:   Optional[int] 							=   time()
											self.dfs(_lowerCAmelCase ,						_lowerCAmelCase      )
											_lowercase							:   Optional[int] 							=   time()
											return end - begin


				def 						__a					(					self ,						_lowerCAmelCase=-2      ):
											_lowercase							:   str 							=   time()
											self.bfs(_lowerCAmelCase      )
											_lowercase							:   str 							=   time()
											return end - begin




class 				lowerCAmelCase_	:


				def __init__(					self      ):
											_lowercase							:   str 							=   {}


				def 						__a					(					self ,						_lowerCAmelCase ,						_lowerCAmelCase ,						_lowerCAmelCase=1      ):
											# check if the u exists
											if self.graph.get(_lowerCAmelCase      ):
																		# if there already is a edge
																		if self.graph[u].count([w, v]      ) == 0:
																									self.graph[u].append([w, v]      )
											else:
																		# if u does not exist
																		_lowercase							:   Any 							=   [[w, v]]
											# add the other way
											if self.graph.get(_lowerCAmelCase      ):
																		# if there already is a edge
																		if self.graph[v].count([w, u]      ) == 0:
																									self.graph[v].append([w, u]      )
											else:
																		# if u does not exist
																		_lowercase							:   List[Any] 							=   [[w, u]]


				def 						__a					(					self ,						_lowerCAmelCase ,						_lowerCAmelCase      ):
											if self.graph.get(_lowerCAmelCase      ):
																		for _ in self.graph[u]:
																									if _[1] == v:
																																self.graph[u].remove(_lowerCAmelCase      )
        # the other way round
											if self.graph.get(_lowerCAmelCase      ):
																		for _ in self.graph[v]:
																									if _[1] == u:
																																self.graph[v].remove(_lowerCAmelCase      )


				def 						__a					(					self ,						_lowerCAmelCase=-2 ,						_lowerCAmelCase=-1      ):
											if s == d:
																		return []
											_lowercase							:   Dict 							=   []
											_lowercase							:   Dict 							=   []
											if s == -2:
																		_lowercase							:   Tuple 							=   list(self.graph      )[0]
											stack.append(_lowerCAmelCase      )
											visited.append(_lowerCAmelCase      )
											_lowercase							:   str 							=   s

											while True:
																		# check if there is any non isolated nodes
																		if len(self.graph[s]      ) != 0:
																									_lowercase							:   Any 							=   s
																									for node in self.graph[s]:
																																if visited.count(node[1]      ) < 1:
																																							if node[1] == d:
																																														visited.append(_lowerCAmelCase      )
																																														return visited
																																							else:
																																														stack.append(node[1]      )
																																														visited.append(node[1]      )
																																														_lowercase							:   Union[str, Any] 							=   node[1]
																																														break

            # check if all the children are visited
																		if s == ss:
																									stack.pop()
																									if len(_lowerCAmelCase      ) != 0:
																																_lowercase							:   Any 							=   stack[len(_lowerCAmelCase      ) - 1]
																		else:
																									_lowercase							:   Tuple 							=   ss

																		# check if se have reached the starting point
																		if len(_lowerCAmelCase      ) == 0:
																									return visited


				def 						__a					(					self ,						_lowerCAmelCase=-1      ):
											if c == -1:
																		_lowercase							:   Dict 							=   floor(random() * 1_0_0_0_0      ) + 1_0
											for i in range(_lowerCAmelCase      ):
																		# every vertex has max 100 edges
																		for _ in range(floor(random() * 1_0_2      ) + 1      ):
																									_lowercase							:   Optional[Any] 							=   floor(random() * c      ) + 1
																									if n != i:
																																self.add_pair(_lowerCAmelCase ,						_lowerCAmelCase ,						1      )


				def 						__a					(					self ,						_lowerCAmelCase=-2      ):
											_lowercase							:   Optional[int] 							=   deque()
											_lowercase							:   Any 							=   []
											if s == -2:
																		_lowercase							:   Optional[int] 							=   list(self.graph      )[0]
											d.append(_lowerCAmelCase      )
											visited.append(_lowerCAmelCase      )
											while d:
																		_lowercase							:   Any 							=   d.popleft()
																		if len(self.graph[s]      ) != 0:
																									for node in self.graph[s]:
																																if visited.count(node[1]      ) < 1:
																																							d.append(node[1]      )
																																							visited.append(node[1]      )
											return visited


				def 						__a					(					self ,						_lowerCAmelCase      ):
											return len(self.graph[u]      )


				def 						__a					(					self      ):
											_lowercase							:   Any 							=   []
											_lowercase							:   Optional[Any] 							=   []
											_lowercase							:   Optional[int] 							=   list(self.graph      )[0]
											stack.append(_lowerCAmelCase      )
											visited.append(_lowerCAmelCase      )
											_lowercase							:   Optional[int] 							=   -2
											_lowercase							:   Dict 							=   []
											_lowercase							:   int 							=   s
											_lowercase							:   Dict 							=   False
											_lowercase							:   Dict 							=   set()

											while True:
																		# check if there is any non isolated nodes
																		if len(self.graph[s]      ) != 0:
																									_lowercase							:   Any 							=   s
																									for node in self.graph[s]:
																																if (
																																    visited.count(node[1]      ) > 0
																																    and node[1] != parent
																																    and indirect_parents.count(node[1]      ) > 0
																																    and not on_the_way_back
																																):
																																							_lowercase							:   str 							=   len(_lowerCAmelCase      ) - 1
																																							while len_stack >= 0:
																																														if stack[len_stack] == node[1]:
																																																					anticipating_nodes.add(node[1]      )
																																																					break
																																														else:
																																																					anticipating_nodes.add(stack[len_stack]      )
																																																					len_stack -= 1
																																if visited.count(node[1]      ) < 1:
																																							stack.append(node[1]      )
																																							visited.append(node[1]      )
																																							_lowercase							:   str 							=   node[1]
																																							break

            # check if all the children are visited
																		if s == ss:
																									stack.pop()
																									_lowercase							:   Any 							=   True
																									if len(_lowerCAmelCase      ) != 0:
																																_lowercase							:   List[Any] 							=   stack[len(_lowerCAmelCase      ) - 1]
																		else:
																									_lowercase							:   Union[str, Any] 							=   False
																									indirect_parents.append(_lowerCAmelCase      )
																									_lowercase							:   int 							=   s
																									_lowercase							:   Any 							=   ss

																		# check if se have reached the starting point
																		if len(_lowerCAmelCase      ) == 0:
																									return list(_lowerCAmelCase      )


				def 						__a					(					self      ):
											_lowercase							:   Union[str, Any] 							=   []
											_lowercase							:   Dict 							=   []
											_lowercase							:   List[str] 							=   list(self.graph      )[0]
											stack.append(_lowerCAmelCase      )
											visited.append(_lowerCAmelCase      )
											_lowercase							:   Any 							=   -2
											_lowercase							:   Optional[Any] 							=   []
											_lowercase							:   List[str] 							=   s
											_lowercase							:   Optional[int] 							=   False
											_lowercase							:   Tuple 							=   set()

											while True:
																		# check if there is any non isolated nodes
																		if len(self.graph[s]      ) != 0:
																									_lowercase							:   Union[str, Any] 							=   s
																									for node in self.graph[s]:
																																if (
																																    visited.count(node[1]      ) > 0
																																    and node[1] != parent
																																    and indirect_parents.count(node[1]      ) > 0
																																    and not on_the_way_back
																																):
																																							_lowercase							:   Tuple 							=   len(_lowerCAmelCase      ) - 1
																																							while len_stack_minus_one >= 0:
																																														if stack[len_stack_minus_one] == node[1]:
																																																					anticipating_nodes.add(node[1]      )
																																																					break
																																														else:
																																																					return True
																																if visited.count(node[1]      ) < 1:
																																							stack.append(node[1]      )
																																							visited.append(node[1]      )
																																							_lowercase							:   Dict 							=   node[1]
																																							break

            # check if all the children are visited
																		if s == ss:
																									stack.pop()
																									_lowercase							:   str 							=   True
																									if len(_lowerCAmelCase      ) != 0:
																																_lowercase							:   List[str] 							=   stack[len(_lowerCAmelCase      ) - 1]
																		else:
																									_lowercase							:   Optional[Any] 							=   False
																									indirect_parents.append(_lowerCAmelCase      )
																									_lowercase							:   Optional[int] 							=   s
																									_lowercase							:   List[str] 							=   ss

																		# check if se have reached the starting point
																		if len(_lowerCAmelCase      ) == 0:
																									return False


				def 						__a					(					self      ):
											return list(self.graph      )


				def 						__a					(					self ,						_lowerCAmelCase=-2 ,						_lowerCAmelCase=-1      ):
											_lowercase							:   Any 							=   time()
											self.dfs(_lowerCAmelCase ,						_lowerCAmelCase      )
											_lowercase							:   Optional[int] 							=   time()
											return end - begin


				def 						__a					(					self ,						_lowerCAmelCase=-2      ):
											_lowercase							:   Dict 							=   time()
											self.bfs(_lowerCAmelCase      )
											_lowercase							:   int 							=   time()
											return end - begin









'''simple docstring'''
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


snake_case_					:      List[Any]    			=    logging.get_logger(__name__)






@add_end_docstrings(lowercase  )
class 				lowercase__      (				lowercase  ):





	def __init__(					self   :							Tuple					,**lowerCamelCase__   :							Union[str, Any]					):







							'''simple docstring'''
							super().__init__(**lowerCamelCase__					)

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





	def __call__(					self   :							Dict					,lowerCamelCase__   :							Union[np.ndarray, bytes, str]					,**lowerCamelCase__   :							Any					):







							'''simple docstring'''
							return super().__call__(lowerCamelCase__					,**lowerCamelCase__					)





	def       UpperCamelCase_    (					self   :							int					,**lowerCamelCase__   :							Optional[int]					):







							'''simple docstring'''
							_UpperCamelCase   :						List[Any]												=  {}
							if "candidate_labels" in kwargs:
													_UpperCamelCase   :						Any												=  kwargs['candidate_labels']
							if "hypothesis_template" in kwargs:
													_UpperCamelCase   :						Any												=  kwargs['hypothesis_template']

							return preprocess_params, {}, {}





	def       UpperCamelCase_    (					self   :							Any					,lowerCamelCase__   :							List[str]					,lowerCamelCase__   :							str=None					,lowerCamelCase__   :							List[Any]="This is a sound of {}."					):







							'''simple docstring'''
							if isinstance(lowerCamelCase__					,lowerCamelCase__					):
													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
																			_UpperCamelCase   :						Optional[Any]												=  requests.get(lowerCamelCase__					).content
													else:
																			with open(lowerCamelCase__					,'rb'					) as f:
																									_UpperCamelCase   :						List[str]												=  f.read()

							if isinstance(lowerCamelCase__					,lowerCamelCase__					):
													_UpperCamelCase   :						int												=  ffmpeg_read(lowerCamelCase__					,self.feature_extractor.sampling_rate					)

							if not isinstance(lowerCamelCase__					,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'					)

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





	def       UpperCamelCase_    (					self   :							str					,lowerCamelCase__   :							Tuple					):







							'''simple docstring'''
							_UpperCamelCase   :						Any												=  model_inputs.pop('candidate_labels'					)
							_UpperCamelCase   :						str												=  model_inputs.pop('text_inputs'					)
							if isinstance(text_inputs[0]					,lowerCamelCase__					):
													_UpperCamelCase   :						Union[str, Any]												=  text_inputs[0]
							else:
													# Batching case.
													_UpperCamelCase   :						int												=  text_inputs[0][0]

							_UpperCamelCase   :						List[str]												=  self.model(**lowerCamelCase__					,**lowerCamelCase__					)

							_UpperCamelCase   :						Optional[Any]												=  {
							    'candidate_labels': candidate_labels,
							    'logits': outputs.logits_per_audio,
							}
							return model_outputs





	def       UpperCamelCase_    (					self   :							int					,lowerCamelCase__   :							Optional[Any]					):







							'''simple docstring'''
							_UpperCamelCase   :						Optional[Any]												=  model_outputs.pop('candidate_labels'					)
							_UpperCamelCase   :						int												=  model_outputs['logits'][0]

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

							_UpperCamelCase   :						Union[str, Any]												=  [
							    {'score': score, 'label': candidate_label}
							    for score, candidate_label in sorted(zip(lowerCamelCase__					,lowerCamelCase__					)					,key=lambda lowerCamelCase__					: -x[0]					)
							]
							return result


