






import argparse
import collections
from pathlib import Path

import torch
from torch.serialization import default_restore_location

from transformers import BertConfig, DPRConfig, DPRContextEncoder, DPRQuestionEncoder, DPRReader


_UpperCAmelCase				:	List[Any]    						=    collections.namedtuple(
    'CheckpointState', ['model_dict', 'optimizer_dict', 'scheduler_dict', 'offset', 'epoch', 'encoder_params']
)






def    UpperCAmelCase__	(						lowerCamelCase  : str						):
   print(f'''Reading saved model from {model_file}'''						)
   lowerCamelCase											=torch.load(lowercase_ ,map_location=lambda lowerCamelCase ,lowerCamelCase						: default_restore_location(lowercase_ ,"""cpu"""						)						)
   return CheckpointState(**lowercase_						)


class 		lowerCAmelCase_							:





     def __init__(  self       ,   _UpperCAmelCase     )		->							Tuple:
        lowerCamelCase											=src_file





     def 	lowerCamelCase       (  self     )		->							Optional[int]:
        raise NotImplementedError





     @staticmethod
     def 	lowerCamelCase       (  _UpperCAmelCase       ,   *_UpperCAmelCase       ,   **_UpperCAmelCase     )		->							"DPRState":
        if comp_type.startswith("""c"""     ):
           return DPRContextEncoderState(*_UpperCAmelCase       ,   **_UpperCAmelCase     )
        if comp_type.startswith("""q"""     ):
           return DPRQuestionEncoderState(*_UpperCAmelCase       ,   **_UpperCAmelCase     )
        if comp_type.startswith("""r"""     ):
           return DPRReaderState(*_UpperCAmelCase       ,   **_UpperCAmelCase     )
        else:
           raise ValueError("""Component type must be either \'ctx_encoder\', \'question_encoder\' or \'reader\'."""     )




class 		lowerCAmelCase_							(     a_   ):





     def 	lowerCamelCase       (  self     )		->							Optional[Any]:
        lowerCamelCase											=DPRContextEncoder(DPRConfig(**BertConfig.get_config_dict("""bert-base-uncased"""     )[0]     )     )
        print(F'''Loading DPR biencoder from {self.src_file}'''     )
        lowerCamelCase											=load_states_from_checkpoint(self.src_file     )
        lowerCamelCase											=model.ctx_encoder, 'ctx_model.'
        # Fix changes from https://github.com/huggingface/transformers/commit/614fef1691edb806de976756d4948ecbcd0c0ca3
        lowerCamelCase											={'bert_model.embeddings.position_ids': model.ctx_encoder.bert_model.embeddings.position_ids}
        for key, value in saved_state.model_dict.items():
           if key.startswith(_UpperCAmelCase     ):
              lowerCamelCase											=key[len(_UpperCAmelCase     ) :]
              if not key.startswith("""encode_proj."""     ):
                 lowerCamelCase											='bert_model.' + key
              lowerCamelCase											=value
        encoder.load_state_dict(_UpperCAmelCase     )
        return model




class 		lowerCAmelCase_							(     a_   ):





     def 	lowerCamelCase       (  self     )		->							Optional[int]:
        lowerCamelCase											=DPRQuestionEncoder(DPRConfig(**BertConfig.get_config_dict("""bert-base-uncased"""     )[0]     )     )
        print(F'''Loading DPR biencoder from {self.src_file}'''     )
        lowerCamelCase											=load_states_from_checkpoint(self.src_file     )
        lowerCamelCase											=model.question_encoder, 'question_model.'
        # Fix changes from https://github.com/huggingface/transformers/commit/614fef1691edb806de976756d4948ecbcd0c0ca3
        lowerCamelCase											={'bert_model.embeddings.position_ids': model.question_encoder.bert_model.embeddings.position_ids}
        for key, value in saved_state.model_dict.items():
           if key.startswith(_UpperCAmelCase     ):
              lowerCamelCase											=key[len(_UpperCAmelCase     ) :]
              if not key.startswith("""encode_proj."""     ):
                 lowerCamelCase											='bert_model.' + key
              lowerCamelCase											=value
        encoder.load_state_dict(_UpperCAmelCase     )
        return model




class 		lowerCAmelCase_							(     a_   ):





     def 	lowerCamelCase       (  self     )		->							Optional[Any]:
        lowerCamelCase											=DPRReader(DPRConfig(**BertConfig.get_config_dict("""bert-base-uncased"""     )[0]     )     )
        print(F'''Loading DPR reader from {self.src_file}'''     )
        lowerCamelCase											=load_states_from_checkpoint(self.src_file     )
        # Fix changes from https://github.com/huggingface/transformers/commit/614fef1691edb806de976756d4948ecbcd0c0ca3
        lowerCamelCase											={
            'encoder.bert_model.embeddings.position_ids': model.span_predictor.encoder.bert_model.embeddings.position_ids
        }
        for key, value in saved_state.model_dict.items():
           if key.startswith("""encoder."""     ) and not key.startswith("""encoder.encode_proj"""     ):
              lowerCamelCase											='encoder.bert_model.' + key[len("""encoder."""     ) :]
           lowerCamelCase											=value
        model.span_predictor.load_state_dict(_UpperCAmelCase     )
        return model






def    UpperCAmelCase__	(						lowerCamelCase  : str ,lowerCamelCase  : Path ,lowerCamelCase  : Path						):
   lowerCamelCase											=Path(lowercase_						)
   dest_dir.mkdir(exist_ok=lowercase_						)

   lowerCamelCase											=DPRState.from_type(lowercase_ ,src_file=lowercase_						)
   lowerCamelCase											=dpr_state.load_dpr_model()
   model.save_pretrained(lowercase_						)
   model.from_pretrained(lowercase_						)  # sanity check


if __name__ == "__main__":
      _UpperCAmelCase				:	Optional[Any]    						=    argparse.ArgumentParser()
      # Required parameters
      parser.add_argument(
          '--type', type=str, help='Type of the component to convert: \'ctx_encoder\', \'question_encoder\' or \'reader\'.'
      )
      parser.add_argument(
          '--src',
          type=str,
          help=(
              'Path to the dpr checkpoint file. They can be downloaded from the official DPR repo'
              ' https://github.com/facebookresearch/DPR. Note that in the official repo, both encoders are stored in the'
              ' \'retriever\' checkpoints.'
          ),
      )
      parser.add_argument('--dest', type=str, default=None, help='Path to the output PyTorch model directory.')
      _UpperCAmelCase				:	Dict    						=    parser.parse_args()

      _UpperCAmelCase				:	Optional[int]    						=    Path(args.src)
      _UpperCAmelCase				:	str    						=    f"converted-{src_file.name}" if args.dest is None else args.dest
      _UpperCAmelCase				:	Dict    						=    Path(dest_dir)
      assert src_file.exists()
      assert (
          args.type is not None
      ), "Please specify the component type of the DPR model to convert: 'ctx_encoder', 'question_encoder' or 'reader'."
      convert(args.type, src_file, dest_dir)







import subprocess
from typing import Union

import numpy as np
import requests

from ..utils import add_end_docstrings, is_torch_available, is_torchaudio_available, logging
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_torch_available():
   from ..models.auto.modeling_auto import MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING

_UpperCAmelCase				:	Optional[int]    						=    logging.get_logger(__name__)






def    UpperCAmelCase__	(						lowerCamelCase  : bytes ,lowerCamelCase  : int						):
   lowerCamelCase											=f'''{sampling_rate}'''
   lowerCamelCase											="""1"""
   lowerCamelCase											="""f32le"""
   lowerCamelCase											=[
       """ffmpeg""",
       """-i""",
       """pipe:0""",
       """-ac""",
       ac,
       """-ar""",
       ar,
       """-f""",
       format_for_conversion,
       """-hide_banner""",
       """-loglevel""",
       """quiet""",
       """pipe:1""",
   ]

   try:
      lowerCamelCase											=subprocess.Popen(lowerCamelCase ,stdin=subprocess.PIPE ,stdout=subprocess.PIPE						)
   except FileNotFoundError:
      raise ValueError("""ffmpeg was not found but is required to load audio files from filename"""						)
   lowerCamelCase											=ffmpeg_process.communicate(lowerCamelCase						)
   lowerCamelCase											=output_stream[0]

   lowerCamelCase											=np.frombuffer(lowerCamelCase ,np.floataa						)
   if audio.shape[0] == 0:
      raise ValueError("""Malformed soundfile"""						)
   return audio







@add_end_docstrings(UpperCamelCase   )
class 		lowerCAmelCase_							(     UpperCamelCase   ):





     def __init__(  self       ,   *_UpperCAmelCase       ,   **_UpperCAmelCase     )		->							Dict:
        # Default, might be overriden by the model.config.
        lowerCamelCase											=5
        super().__init__(*_UpperCAmelCase       ,   **_UpperCAmelCase     )

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

        self.check_model_type(_UpperCAmelCase     )





     def __call__(  self       ,   _UpperCAmelCase       ,   **_UpperCAmelCase       ,   )		->							Union[str, Any]:
        return super().__call__(_UpperCAmelCase       ,   **_UpperCAmelCase     )





     def 	lowerCamelCase       (  self       ,   _UpperCAmelCase=None       ,   **_UpperCAmelCase     )		->							Optional[int]:
        # No parameters on this pipeline right now
        lowerCamelCase											={}
        if top_k is not None:
           if top_k > self.model.config.num_labels:
              lowerCamelCase											=self.model.config.num_labels
           lowerCamelCase											=top_k
        return {}, {}, postprocess_params





     def 	lowerCamelCase       (  self       ,   _UpperCAmelCase     )		->							Dict:
        if isinstance(_UpperCAmelCase       ,   _UpperCAmelCase     ):
           if inputs.startswith("""http://"""     ) or inputs.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											=requests.get(_UpperCAmelCase     ).content
           else:
              with open(_UpperCAmelCase       ,   """rb"""     ) as f:
                 lowerCamelCase											=f.read()

        if isinstance(_UpperCAmelCase       ,   _UpperCAmelCase     ):
           lowerCamelCase											=ffmpeg_read(_UpperCAmelCase       ,   self.feature_extractor.sampling_rate     )

        if isinstance(_UpperCAmelCase       ,   _UpperCAmelCase     ):
           # Accepting `"array"` which is the key defined in `datasets` for
           # better integration
           if not ("sampling_rate" in inputs and ("raw" in inputs or "array" in inputs)):
              raise ValueError(
                  """When passing a dictionary to AudioClassificationPipeline, the dict needs to contain a """
                  """\"raw\" key containing the numpy array representing the audio and a \"sampling_rate\" key, """
                  """containing the sampling_rate associated with that array"""     )

           lowerCamelCase											=inputs.pop("""raw"""       ,   _UpperCAmelCase     )
           if _inputs is None:
              # Remove path which will not be used from `datasets`.
              inputs.pop("""path"""       ,   _UpperCAmelCase     )
              lowerCamelCase											=inputs.pop("""array"""       ,   _UpperCAmelCase     )
           lowerCamelCase											=inputs.pop("""sampling_rate"""     )
           lowerCamelCase											=_inputs
           if in_sampling_rate != self.feature_extractor.sampling_rate:
              import torch

              if is_torchaudio_available():
                 from torchaudio import functional as F
              else:
                 raise ImportError(
                     """torchaudio is required to resample audio samples in AudioClassificationPipeline. """
                     """The torchaudio package can be installed through: `pip install torchaudio`."""     )

              lowerCamelCase											=F.resample(
                  torch.from_numpy(_UpperCAmelCase     )       ,   _UpperCAmelCase       ,   self.feature_extractor.sampling_rate     ).numpy()

        if not isinstance(_UpperCAmelCase       ,   np.ndarray     ):
           raise ValueError("""We expect a numpy ndarray as input"""     )
        if len(inputs.shape     ) != 1:
           raise ValueError("""We expect a single channel audio input for AudioClassificationPipeline"""     )

        lowerCamelCase											=self.feature_extractor(
            _UpperCAmelCase       ,   sampling_rate=self.feature_extractor.sampling_rate       ,   return_tensors="""pt"""     )
        return processed





     def 	lowerCamelCase       (  self       ,   _UpperCAmelCase     )		->							int:
        lowerCamelCase											=self.model(**_UpperCAmelCase     )
        return model_outputs





     def 	lowerCamelCase       (  self       ,   _UpperCAmelCase       ,   _UpperCAmelCase=5     )		->							Tuple:
        lowerCamelCase											=model_outputs.logits[0].softmax(-1     )
        lowerCamelCase							,					lowerCamelCase											=probs.topk(_UpperCAmelCase     )

        lowerCamelCase											=scores.tolist()
        lowerCamelCase											=ids.tolist()

        lowerCamelCase											=[{"""score""": score, """label""": self.model.config.idalabel[_id]} for score, _id in zip(_UpperCAmelCase       ,   _UpperCAmelCase     )]

        return labels