






import logging
import random

import ray

from transformers import RagConfig, RagRetriever, RagTokenizer
from transformers.models.rag.retrieval_rag import CustomHFIndex


SCREAMING_SNAKE_CASE 							=						logging.getLogger(__name__)
class 							A_    :






 '''simple docstring'''




 def __init__(	self) ->		Optional[int]:

     """simple docstring"""



     _UpperCAmelCase				:      Dict			  =				False


 def  snake_case__				(	self							,  _A							,  _A							,  _A							,  _A) ->		Optional[int]:

     """simple docstring"""



     if not self.initialized:
         _UpperCAmelCase				:      str			  =				RagRetriever(
             _A							,  question_encoder_tokenizer=_A							,  generator_tokenizer=_A							,  index=_A							,  init_retrieval=_A							,  )
         _UpperCAmelCase				:      str			  =				True


 def  snake_case__				(	self) ->		Optional[int]:

     """simple docstring"""



     self.retriever.index.init_index()


 def  snake_case__				(	self							,  _A							,  _A) ->		Tuple:

     """simple docstring"""



     _UpperCAmelCase  , _UpperCAmelCase				:      Tuple			  =				self.retriever._main_retrieve(_A							,  _A)
     return doc_ids, retrieved_doc_embeds



class 							A_    (						__lowercase			):






 '''simple docstring'''




 def __init__(	self							,  _A							,  _A							,  _A							,  _A							,  _A=None) ->		Tuple:

     """simple docstring"""



     if index is not None and index.is_initialized() and len(_A) > 0:
         raise ValueError(
             '''When using Ray for distributed fine-tuning, '''
             '''you\'ll need to provide the paths instead, '''
             '''as the dataset and the index are loaded '''
             '''separately. More info in examples/rag/use_own_knowledge_dataset.py ''')
     super().__init__(
         _A							,  question_encoder_tokenizer=_A							,  generator_tokenizer=_A							,  index=_A							,  init_retrieval=_A							,  )
     _UpperCAmelCase				:      List[str]			  =				retrieval_workers
     if len(self.retrieval_workers) > 0:
         ray.get(
             [
                 worker.create_rag_retriever.remote(_A							,  _A							,  _A							,  _A)
                 for worker in self.retrieval_workers
             ])


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

     """simple docstring"""



     logger.info('''initializing retrieval''')

     if len(self.retrieval_workers) > 0:
         ray.get([worker.init_retrieval.remote() for worker in self.retrieval_workers])
     else:
         # Non-distributed training. Load index into this same process.
         self.index.init_index()


 def  snake_case__				(	self							,  _A							,  _A) ->		int:

     """simple docstring"""



     if len(self.retrieval_workers) > 0:
         # Select a random retrieval actor.
         _UpperCAmelCase				:      Any			  =				self.retrieval_workers[random.randint(0							,  len(self.retrieval_workers) - 1)]
         _UpperCAmelCase  , _UpperCAmelCase				:      Union[str, Any]			  =				ray.get(random_worker.retrieve.remote(_A							,  _A))
     else:
         _UpperCAmelCase  , _UpperCAmelCase				:      Optional[Any]			  =				self._main_retrieve(_A							,  _A)
     return retrieved_doc_embeds, doc_ids, self.index.get_doc_dicts(_A)


 @classmethod
 def  snake_case__				(	cls							,  _A							,  _A=None							,  **_A) ->		str:

     """simple docstring"""



     return super(_A							,  cls).get_tokenizers(_A							,  _A							,  **_A)


 @classmethod
 def  snake_case__				(	cls							,  _A							,  _A							,  _A=None							,  **_A) ->		Union[str, Any]:

     """simple docstring"""



     _UpperCAmelCase				:      Optional[int]			  =				kwargs.pop('''config'''							,  _A) or RagConfig.from_pretrained(_A							,  **_A)
     _UpperCAmelCase				:      Dict			  =				RagTokenizer.from_pretrained(_A							,  config=_A)
     _UpperCAmelCase				:      List[str]			  =				rag_tokenizer.question_encoder
     _UpperCAmelCase				:      List[str]			  =				rag_tokenizer.generator
     if indexed_dataset is not None:
         _UpperCAmelCase				:      List[str]			  =				'''custom'''
         _UpperCAmelCase				:      Tuple			  =				CustomHFIndex(config.retrieval_vector_size							,  _A)
     else:
         _UpperCAmelCase				:      Optional[Any]			  =				cls._build_index(_A)
     return cls(
         _A							,  question_encoder_tokenizer=_A							,  generator_tokenizer=_A							,  retrieval_workers=_A							,  index=_A							,  )












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


SCREAMING_SNAKE_CASE 							=						logging.get_logger(__name__)
@add_end_docstrings(__lowercase			)
class 							A_    (						__lowercase			):






 '''simple docstring'''




 def __init__(	self							,  **_A) ->		int:

     """simple docstring"""



     super().__init__(**_A)

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


 def __call__(	self							,  _A							,  **_A) ->		List[str]:

     """simple docstring"""



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


 def  snake_case__				(	self							,  **_A) ->		List[str]:

     """simple docstring"""



     _UpperCAmelCase				:      Optional[int]			  =				{}
     if "candidate_labels" in kwargs:
         _UpperCAmelCase				:      Union[str, Any]			  =				kwargs['''candidate_labels''']
     if "hypothesis_template" in kwargs:
         _UpperCAmelCase				:      Dict			  =				kwargs['''hypothesis_template''']

     return preprocess_params, {}, {}


 def  snake_case__				(	self							,  _A							,  _A=None							,  _A="This is a sound of {}.") ->		Tuple:

     """simple docstring"""



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

     if isinstance(_A							,  _A):
         _UpperCAmelCase				:      List[Any]			  =				ffmpeg_read(_A							,  self.feature_extractor.sampling_rate)

     if not isinstance(_A							,  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				:      Optional[Any]			  =				self.feature_extractor(
         [audio]							,  sampling_rate=self.feature_extractor.sampling_rate							,  return_tensors='''pt''')
     _UpperCAmelCase				:      Dict			  =				candidate_labels
     _UpperCAmelCase				:      List[str]			  =				[hypothesis_template.format(_A) for x in candidate_labels]
     _UpperCAmelCase				:      Optional[Any]			  =				self.tokenizer(_A							,  return_tensors=self.framework							,  padding=_A)
     _UpperCAmelCase				:      str			  =				[text_inputs]
     return inputs


 def  snake_case__				(	self							,  _A) ->		List[str]:

     """simple docstring"""



     _UpperCAmelCase				:      Optional[int]			  =				model_inputs.pop('''candidate_labels''')
     _UpperCAmelCase				:      Union[str, Any]			  =				model_inputs.pop('''text_inputs''')
     if isinstance(text_inputs[0]							,  _A):
         _UpperCAmelCase				:      int			  =				text_inputs[0]
     else:
         # Batching case.
         _UpperCAmelCase				:      Dict			  =				text_inputs[0][0]

     _UpperCAmelCase				:      str			  =				self.model(**_A							,  **_A)

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


 def  snake_case__				(	self							,  _A) ->		List[Any]:

     """simple docstring"""



     _UpperCAmelCase				:      str			  =				model_outputs.pop('''candidate_labels''')
     _UpperCAmelCase				:      str			  =				model_outputs['''logits'''][0]

     if self.framework == "pt":
         _UpperCAmelCase				:      int			  =				logits.softmax(dim=0)
         _UpperCAmelCase				:      Optional[Any]			  =				probs.tolist()
     else:
         raise ValueError('''`tf` framework not supported.''')

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




