
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


UpperCAmelCase	      = logging.get_logger(__name__)







@add_end_docstrings(__lowerCamelCase   )
class        A_   (			__lowerCamelCase   ):




	'''simple docstring'''


	def __init__(       self		,    **snake_case       ):
		super().__init__(**snake_case       )

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





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





	def 	SCREAMING_SNAKE_CASE__						(       self		,    **snake_case       ):
		lowercase       =    {}
		if "candidate_labels" in kwargs:
			lowercase       =    kwargs['candidate_labels']
		if "hypothesis_template" in kwargs:
			lowercase       =    kwargs['hypothesis_template']

		return preprocess_params, {}, {}





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case		,    snake_case=None		,    snake_case="This is a sound of {}."       ):
		if isinstance(snake_case		,    snake_case       ):
			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
				lowercase       =    requests.get(snake_case       ).content
			else:
				with open(snake_case		,    'rb'       ) as f:
					lowercase       =    f.read()

		if isinstance(snake_case		,    snake_case       ):
			lowercase       =    ffmpeg_read(snake_case		,    self.feature_extractor.sampling_rate       )

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

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





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case       ):
		lowercase       =    model_inputs.pop('candidate_labels'       )
		lowercase       =    model_inputs.pop('text_inputs'       )
		if isinstance(text_inputs[0]		,    snake_case       ):
			lowercase       =    text_inputs[0]
		else:
			# Batching case.
			lowercase       =    text_inputs[0][0]

		lowercase       =    self.model(**snake_case		,    **snake_case       )

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





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case       ):
		lowercase       =    model_outputs.pop('candidate_labels'       )
		lowercase       =    model_outputs['logits'][0]

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

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



from manim import *







class        A_   (			__lowerCamelCase   ):




	'''simple docstring'''


	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		lowercase       =    Rectangle(height=0.5		,    width=0.5       )
		lowercase       =    Rectangle(height=0.25		,    width=0.25       )
		lowercase       =    Rectangle(height=0.46		,    width=0.46       ).set_stroke(width=0       )

		lowercase       =    [mem.copy() for i in range(6       )]
		lowercase       =    [mem.copy() for i in range(6       )]
		lowercase       =    VGroup(*snake_case       ).arrange(snake_case		,    buff=0       )
		lowercase       =    VGroup(*snake_case       ).arrange(snake_case		,    buff=0       )
		lowercase       =    VGroup(snake_case		,    snake_case       ).arrange(snake_case		,    buff=0       )
		lowercase       =    Text('CPU'		,    font_size=24       )
		lowercase       =    Group(snake_case		,    snake_case       ).arrange(snake_case		,    buff=0.5		,    aligned_edge=snake_case       )
		cpu.move_to([-2.5, -0.5, 0]       )
		self.add(snake_case       )

		lowercase       =    [mem.copy() for i in range(4       )]
		lowercase       =    VGroup(*snake_case       ).arrange(snake_case		,    buff=0       )
		lowercase       =    Text('GPU'		,    font_size=24       )
		lowercase       =    Group(snake_case		,    snake_case       ).arrange(snake_case		,    buff=0.5		,    aligned_edge=snake_case       )
		gpu.move_to([-1, -1, 0]       )
		self.add(snake_case       )

		lowercase       =    [mem.copy() for i in range(6       )]
		lowercase       =    VGroup(*snake_case       ).arrange(snake_case		,    buff=0       )

		lowercase       =    Text('Model'		,    font_size=24       )
		lowercase       =    Group(snake_case		,    snake_case       ).arrange(snake_case		,    buff=0.5		,    aligned_edge=snake_case       )
		model.move_to([3, -1.0, 0]       )
		self.add(snake_case       )

		lowercase       =    []
		lowercase       =    []
		lowercase       =    []

		for i, rect in enumerate(snake_case       ):
			rect.set_stroke(snake_case       )

			lowercase       =    Rectangle(height=0.46 / 4		,    width=0.46 / 3       ).set_stroke(width=0.0       ).set_fill(snake_case		,    opacity=0.7       )

			if i == 0:
				cpu_target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT       )		,    buff=0.02		,    direction=snake_case       )
				cpu_target.set_x(cpu_target.get_x() + 0.1       )
			elif i == 3:
				cpu_target.next_to(model_cpu_arr[0]		,    direction=snake_case		,    buff=0.0       )
			else:
				cpu_target.next_to(model_cpu_arr[i - 1]		,    direction=snake_case		,    buff=0.0       )
			self.add(snake_case       )
			model_cpu_arr.append(snake_case       )

		self.add(*snake_case		,    *snake_case		,    *snake_case       )

		lowercase       =    [mem.copy() for i in range(6       )]
		lowercase       =    VGroup(*snake_case       ).arrange(snake_case		,    buff=0       )

		lowercase       =    Text('Loaded Checkpoint'		,    font_size=24       )
		lowercase       =    Group(snake_case		,    snake_case       ).arrange(snake_case		,    buff=0.5		,    aligned_edge=snake_case       )
		checkpoint.move_to([3, 0.5, 0]       )

		self.add(snake_case       )

		lowercase       =    []
		lowercase       =    []

		for i, rect in enumerate(snake_case       ):
			lowercase       =    fill.copy().set_fill(snake_case		,    opacity=0.7       )
			target.move_to(snake_case       )
			ckpt_arr.append(snake_case       )

			lowercase       =    target.copy()
			if i < 5:
				cpu_target.move_to(cpu_left_col_base[i + 1]       )
			else:
				cpu_target.move_to(cpu_right_col_base[i - 5]       )
			ckpt_cpu_arr.append(snake_case       )
		self.add(*snake_case		,    *snake_case       )

		lowercase       =    Square(side_length=2.2       )
		key.move_to([-5, 2, 0]       )

		lowercase       =    MarkupText(
		    F'''<b>Key:</b>\n\n<span fgcolor=\'{YELLOW}\'>●</span> Empty Model'''		,    font_size=18		,    )

		key_text.move_to([-5, 2.4, 0]       )

		self.add(snake_case		,    snake_case       )

		lowercase       =    MarkupText(
		    F'''<span fgcolor=\'{BLUE}\'>●</span> Checkpoint'''		,    font_size=18		,    )

		blue_text.next_to(snake_case		,    DOWN * 2.4		,    aligned_edge=key_text.get_left()       )
		self.add(snake_case       )

		lowercase       =    MarkupText(
		    F'''Based on the passed in configuration, weights are stored in\na variety of np.memmaps on disk or to a particular device.'''		,    font_size=24		,    )
		step_a.move_to([2, 2, 0]       )

		lowercase       =    [meta_mem.copy() for i in range(6       )]
		lowercase       =    [meta_mem.copy() for i in range(6       )]
		lowercase       =    VGroup(*snake_case       ).arrange(snake_case		,    buff=0       )
		lowercase       =    VGroup(*snake_case       ).arrange(snake_case		,    buff=0       )
		lowercase       =    VGroup(snake_case		,    snake_case       ).arrange(snake_case		,    buff=0       )
		lowercase       =    Text('Disk'		,    font_size=24       )
		lowercase       =    Group(snake_case		,    snake_case       ).arrange(snake_case		,    buff=0.5		,    aligned_edge=snake_case       )
		disk.move_to([-4.0, -1.25, 0]       )
		self.play(Write(snake_case		,    run_time=3       )		,    Write(snake_case		,    run_time=1       )		,    Create(snake_case		,    run_time=1       )       )

		lowercase       =    []
		for i, rect in enumerate(snake_case       ):
			lowercase       =    rect.copy()
			target.generate_target()
			target.target.move_to(disk_left_col_base[i]       ).scale(0.5       )
			animations.append(MoveToTarget(snake_case		,    run_time=1.5       )       )
		self.play(*snake_case       )

		self.play(FadeOut(snake_case       )       )

		lowercase       =    MarkupText(F'''Then, the checkpoint is removed from memory\nthrough garbage collection.'''		,    font_size=24       )
		step_a.move_to([2, 2, 0]       )

		self.play(Write(snake_case		,    run_time=3       )       )

		self.play(
		    FadeOut(snake_case		,    snake_case		,    *snake_case		,    *snake_case       )		,    )

		self.wait()

