"""simple docstring"""




from io import BytesIO
from typing import List, Union

import requests

from ..utils import add_end_docstrings, is_decord_available, is_torch_available, logging, requires_backends
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_decord_available():
				import numpy as np
				from decord import VideoReader


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

__lowerCamelCase							 =     logging.get_logger(__name__)
@add_end_docstrings(__A       )
class 			UpperCamelCase__(	__A       ):





						def __init__(				self						,*__UpperCAmelCase						,**__UpperCAmelCase   )    -> Tuple:
													super().__init__(*__UpperCAmelCase						,**__UpperCAmelCase   )
													requires_backends(self						,'decord'   )
													self.check_model_type(__UpperCAmelCase   )





						def  snake_case__		(				self						,__UpperCAmelCase=None						,__UpperCAmelCase=None						,__UpperCAmelCase=None   )    -> int:
													A__		   =							{}
													if frame_sampling_rate is not None:
																				A__		   =							frame_sampling_rate
													if num_frames is not None:
																				A__		   =							num_frames

													A__		   =							{}
													if top_k is not None:
																				A__		   =							top_k
													return preprocess_params, {}, postprocess_params





						def __call__(				self						,__UpperCAmelCase						,**__UpperCAmelCase   )    -> Dict:
													return super().__call__(__UpperCAmelCase						,**__UpperCAmelCase   )





						def  snake_case__		(				self						,__UpperCAmelCase						,__UpperCAmelCase=None						,__UpperCAmelCase=1   )    -> Union[str, Any]:
													if num_frames is None:
																				A__		   =							self.model.config.num_frames

													if video.startswith('http://'   ) or video.startswith('https://'   ):
																				A__		   =							BytesIO(requests.get(__UpperCAmelCase   ).content   )

													A__		   =							VideoReader(__UpperCAmelCase   )
													videoreader.seek(0   )

													A__		   =							0
													A__		   =							num_frames * frame_sampling_rate - 1
													A__		   =							np.linspace(__UpperCAmelCase						,__UpperCAmelCase						,num=__UpperCAmelCase						,dtype=np.intaa   )

													A__		   =							videoreader.get_batch(__UpperCAmelCase   ).asnumpy()
													A__		   =							list(__UpperCAmelCase   )

													A__		   =							self.image_processor(__UpperCAmelCase						,return_tensors=self.framework   )
													return model_inputs





						def  snake_case__		(				self						,__UpperCAmelCase   )    -> Dict:
													A__		   =							self.model(**__UpperCAmelCase   )
													return model_outputs





						def  snake_case__		(				self						,__UpperCAmelCase						,__UpperCAmelCase=5   )    -> Union[str, Any]:
													if top_k > self.model.config.num_labels:
																				A__		   =							self.model.config.num_labels

													if self.framework == "pt":
																				A__		   =							model_outputs.logits.softmax(-1   )[0]
																				A__   ,     A__		   =							probs.topk(__UpperCAmelCase   )
													else:
																				raise ValueError(f'''Unsupported framework: {self.framework}'''   )

													A__		   =							scores.tolist()
													A__		   =							ids.tolist()
													return [{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(__UpperCAmelCase						,__UpperCAmelCase   )]






"""simple docstring"""




import argparse

import torch

from ...utils import logging
from . import AlbertConfig, AlbertForPreTraining, load_tf_weights_in_albert


logging.set_verbosity_info()






def      UpperCAmelCase    (		UpperCamelCase__				,			UpperCamelCase__				,			UpperCamelCase__ ):






							"""simple docstring"""





							A__		   =							AlbertConfig.from_json_file(UpperCamelCase__ )
							print(F'''Building PyTorch model from configuration: {config}''' )
							A__		   =							AlbertForPreTraining(UpperCamelCase__ )

							# Load weights from tf checkpoint
							load_tf_weights_in_albert(UpperCamelCase__				,			UpperCamelCase__				,			UpperCamelCase__ )

							# Save pytorch-model
							print(F'''Save PyTorch model to {pytorch_dump_path}''' )
							torch.save(model.state_dict()				,			UpperCamelCase__ )


if __name__ == "__main__":
				__lowerCamelCase							 =     argparse.ArgumentParser()
				# Required parameters
				parser.add_argument(
				    "--tf_checkpoint_path", default=None, type=str, required=True, help="Path to the TensorFlow checkpoint path."
				)
				parser.add_argument(
				    "--albert_config_file",
				    default=None,
				    type=str,
				    required=True,
				    help=(
				        "The config json file corresponding to the pre-trained ALBERT model. \n"
				        "This specifies the model architecture."
				    ),
				)
				parser.add_argument(
				    "--pytorch_dump_path", default=None, type=str, required=True, help="Path to the output PyTorch model."
				)
				__lowerCamelCase							 =     parser.parse_args()
				convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.albert_config_file, args.pytorch_dump_path)





