


'''simple docstring'''


import argparse

import fairseq
import torch

from transformers import UniSpeechSatConfig, UniSpeechSatForCTC, UniSpeechSatForPreTraining, logging


logging.set_verbosity_info()
lowerCamelCase__					=   logging.get_logger(__name__)

lowerCamelCase__					=   {
    'post_extract_proj': 'feature_projection.projection',
    'encoder.pos_conv.0': 'encoder.pos_conv_embed.conv',
    'self_attn.k_proj': 'encoder.layers.*.attention.k_proj',
    'self_attn.v_proj': 'encoder.layers.*.attention.v_proj',
    'self_attn.q_proj': 'encoder.layers.*.attention.q_proj',
    'self_attn.out_proj': 'encoder.layers.*.attention.out_proj',
    'self_attn_layer_norm': 'encoder.layers.*.layer_norm',
    'fc1': 'encoder.layers.*.feed_forward.intermediate_dense',
    'fc2': 'encoder.layers.*.feed_forward.output_dense',
    'final_layer_norm': 'encoder.layers.*.final_layer_norm',
    'encoder.layer_norm': 'encoder.layer_norm',
    'encoder.layer_norm_for_extract': 'layer_norm_for_extract',
    'w2v_model.layer_norm': 'feature_projection.layer_norm',
    'quantizer.weight_proj': 'quantizer.weight_proj',
    'quantizer.vars': 'quantizer.codevectors',
    'project_q': 'project_q',
    'final_proj': 'project_hid',
    'w2v_encoder.proj': 'lm_head',
    'label_embs_concat': 'label_embeddings_concat',
    'mask_emb': 'masked_spec_embed',
    'spk_proj': 'speaker_proj',
}
lowerCamelCase__					=   [
    'lm_head',
    'quantizer.weight_proj',
    'quantizer.codevectors',
    'project_q',
    'project_hid',
    'label_embeddings_concat',
    'speaker_proj',
    'layer_norm_for_extract',
]






def        __lowerCAmelCase       (__lowerCAmelCase					,       __lowerCAmelCase					,       __lowerCAmelCase					,       __lowerCAmelCase					,       __lowerCAmelCase      ):
		for attribute in key.split("."      ):
				_UpperCAmelCase						:				Union[str, Any]	    =					getattr(__lowerCAmelCase					,       __lowerCAmelCase      )

		if weight_type is not None:
				_UpperCAmelCase						:				Optional[int]	    =					getattr(__lowerCAmelCase					,       __lowerCAmelCase      ).shape
		else:
				_UpperCAmelCase						:				List[str]	    =					hf_pointer.shape

		if hf_shape != value.shape:
				raise ValueError(
				    F"""Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be"""
				    F""" {value.shape} for {full_name}"""      )

		if weight_type == "weight":
				_UpperCAmelCase						:				int	    =					value
		elif weight_type == "weight_g":
				_UpperCAmelCase						:				List[Any]	    =					value
		elif weight_type == "weight_v":
				_UpperCAmelCase						:				str	    =					value
		elif weight_type == "bias":
				_UpperCAmelCase						:				Tuple	    =					value
		else:
				_UpperCAmelCase						:				Optional[int]	    =					value

		logger.info(F"""{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}."""      )






def        __lowerCAmelCase       (__lowerCAmelCase					,       __lowerCAmelCase      ):
		_UpperCAmelCase						:				Optional[int]	    =					[]
		_UpperCAmelCase						:				str	    =					fairseq_model.state_dict()

		_UpperCAmelCase						:				List[str]	    =					hf_model.unispeech_sat.feature_extractor

		for name, value in fairseq_dict.items():
				_UpperCAmelCase						:				Tuple	    =					False
				if "conv_layers" in name:
						load_conv_layer(
						    __lowerCAmelCase					,       __lowerCAmelCase					,       __lowerCAmelCase					,       __lowerCAmelCase					,       hf_model.config.feat_extract_norm == "group"					,       )
						_UpperCAmelCase						:				Optional[int]	    =					True
				else:
						for key, mapped_key in MAPPING.items():
								_UpperCAmelCase						:				Optional[Any]	    =					"unispeech_sat." + mapped_key if mapped_key not in TOP_LEVEL_KEYS else mapped_key
								if key in name or key.split("w2v_model."      )[-1] == name.split("."      )[0]:
										if "layer_norm_for_extract" in name and (".".join(name.split("."      )[:-1]      ) != key):
												# special case since naming is very similar
												continue
										_UpperCAmelCase						:				Union[str, Any]	    =					True
										if "*" in mapped_key:
												_UpperCAmelCase						:				Dict	    =					name.split(__lowerCAmelCase      )[0].split("."      )[-2]
												_UpperCAmelCase						:				Union[str, Any]	    =					mapped_key.replace("*"					,       __lowerCAmelCase      )
										if "weight_g" in name:
												_UpperCAmelCase						:				Dict	    =					"weight_g"
										elif "weight_v" in name:
												_UpperCAmelCase						:				int	    =					"weight_v"
										elif "bias" in name:
												_UpperCAmelCase						:				Union[str, Any]	    =					"bias"
										elif "weight" in name:
												# TODO: don't match quantizer.weight_proj
												_UpperCAmelCase						:				Optional[int]	    =					"weight"
										else:
												_UpperCAmelCase						:				Tuple	    =					None
										set_recursively(__lowerCAmelCase					,       __lowerCAmelCase					,       __lowerCAmelCase					,       __lowerCAmelCase					,       __lowerCAmelCase      )
								continue
				if not is_used:
						unused_weights.append(__lowerCAmelCase      )

		logger.warning(F"""Unused weights: {unused_weights}"""      )






def        __lowerCAmelCase       (__lowerCAmelCase					,       __lowerCAmelCase					,       __lowerCAmelCase					,       __lowerCAmelCase					,       __lowerCAmelCase      ):
		_UpperCAmelCase						:				List[Any]	    =					full_name.split("conv_layers."      )[-1]
		_UpperCAmelCase						:				Dict	    =					name.split("."      )
		_UpperCAmelCase						:				Optional[int]	    =					int(items[0]      )
		_UpperCAmelCase						:				Union[str, Any]	    =					int(items[1]      )

		if type_id == 0:
				if "bias" in name:
						if value.shape != feature_extractor.conv_layers[layer_id].conv.bias.data.shape:
								raise ValueError(
								    F"""{full_name} has size {value.shape}, but"""
								    F""" {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found."""      )
						_UpperCAmelCase						:				List[str]	    =					value
						logger.info(F"""Feat extract conv layer {layer_id} was initialized from {full_name}."""      )
				elif "weight" in name:
						if value.shape != feature_extractor.conv_layers[layer_id].conv.weight.data.shape:
								raise ValueError(
								    F"""{full_name} has size {value.shape}, but"""
								    F""" {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found."""      )
						_UpperCAmelCase						:				Tuple	    =					value
						logger.info(F"""Feat extract conv layer {layer_id} was initialized from {full_name}."""      )
		elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm):
				if "bias" in name:
						if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape:
								raise ValueError(
								    F"""{full_name} has size {value.shape}, but"""
								    F""" {feature_extractor[layer_id].layer_norm.bias.data.shape} was found."""      )
						_UpperCAmelCase						:				str	    =					value
						logger.info(F"""Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}."""      )
				elif "weight" in name:
						if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape:
								raise ValueError(
								    F"""{full_name} has size {value.shape}, but"""
								    F""" {feature_extractor[layer_id].layer_norm.weight.data.shape} was found."""      )
						_UpperCAmelCase						:				List[Any]	    =					value
						logger.info(F"""Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}."""      )
		else:
				unused_weights.append(__lowerCAmelCase      )






@torch.no_grad()
def        __lowerCAmelCase       (__lowerCAmelCase					,       __lowerCAmelCase					,       __lowerCAmelCase=None					,       __lowerCAmelCase=None					,       __lowerCAmelCase=True      ):
		if config_path is not None:
				_UpperCAmelCase						:				Optional[Any]	    =					UniSpeechSatConfig.from_pretrained(__lowerCAmelCase      )
		else:
				_UpperCAmelCase						:				List[Any]	    =					UniSpeechSatConfig()

		_UpperCAmelCase						:				Optional[int]	    =					""

		if is_finetuned:
				_UpperCAmelCase						:				List[Any]	    =					UniSpeechSatForCTC(__lowerCAmelCase      )
		else:
				_UpperCAmelCase						:				Optional[Any]	    =					UniSpeechSatForPreTraining(__lowerCAmelCase      )

		_UpperCAmelCase    ,			_UpperCAmelCase    ,			_UpperCAmelCase						:				List[str]	    =					fairseq.checkpoint_utils.load_model_ensemble_and_task(
		    [checkpoint_path]					,       arg_overrides={"data": "/".join(dict_path.split("/"      )[:-1]      )}      )
		_UpperCAmelCase						:				Dict	    =					model[0].eval()

		recursively_load_weights(__lowerCAmelCase					,       __lowerCAmelCase      )

		hf_wavavec.save_pretrained(__lowerCAmelCase      )


if __name__ == "__main__":
						lowerCamelCase__					=   argparse.ArgumentParser()
						parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.')
						parser.add_argument('--checkpoint_path', default=None, type=str, help='Path to fairseq checkpoint')
						parser.add_argument('--dict_path', default=None, type=str, help='Path to dict of fine-tuned model')
						parser.add_argument('--config_path', default=None, type=str, help='Path to hf config.json of model to convert')
						parser.add_argument(
						    '--not_finetuned', action='store_true', help='Whether the model to convert is a fine-tuned model or not'
						)
						lowerCamelCase__					=   parser.parse_args()
						convert_unispeech_sat_checkpoint(
						    args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, not args.not_finetuned
						)







'''simple docstring'''


from __future__ import annotations

import json

import requests
from bsa import BeautifulSoup
from fake_useragent import UserAgent

lowerCamelCase__					=   {'UserAgent': UserAgent().random}






def        __lowerCAmelCase       (__lowerCAmelCase      ):
		_UpperCAmelCase						:				List[str]	    =					script.contents[0]
		_UpperCAmelCase						:				Any	    =					json.loads(data[data.find("{\"config\""      ) : -1]      )
		return info["entry_data"]["ProfilePage"][0]["graphql"]["user"]






class 		lowerCAmelCase__	:




			def __init__(						self     :   str		,     lowerCamelCase__     :   List[Any]       )      ->List[str]:



					'''simple docstring'''







					_UpperCAmelCase						:				List[Any]	    =					F"""https://www.instagram.com/{username}/"""
					_UpperCAmelCase						:				Dict	    =					self.get_json()




			def 							lowerCAmelCase__			(						self     :   Any       )      ->dict:



					'''simple docstring'''







					_UpperCAmelCase						:				Optional[int]	    =					requests.get(self.url		,     headers=lowerCamelCase__       ).text
					_UpperCAmelCase						:				Any	    =					BeautifulSoup(lowerCamelCase__		,     "html.parser"       ).find_all("script"       )
					try:
							return extract_user_profile(scripts[4]       )
					except (json.decoder.JSONDecodeError, KeyError):
							return extract_user_profile(scripts[3]       )




			def __repr__(						self     :   Optional[Any]       )      ->str:



					'''simple docstring'''







					return F"""{self.__class__.__name__}('{self.username}')"""




			def __str__(						self     :   List[Any]       )      ->str:



					'''simple docstring'''







					return F"""{self.fullname} ({self.username}) is {self.biography}"""




			@property
			def 							lowerCAmelCase__			(						self     :   Optional[int]       )      ->str:



					'''simple docstring'''







					return self.user_data["username"]




			@property
			def 							lowerCAmelCase__			(						self     :   Optional[int]       )      ->str:



					'''simple docstring'''







					return self.user_data["full_name"]




			@property
			def 							lowerCAmelCase__			(						self     :   Optional[int]       )      ->str:



					'''simple docstring'''







					return self.user_data["biography"]




			@property
			def 							lowerCAmelCase__			(						self     :   int       )      ->str:



					'''simple docstring'''







					return self.user_data["business_email"]




			@property
			def 							lowerCAmelCase__			(						self     :   str       )      ->str:



					'''simple docstring'''







					return self.user_data["external_url"]




			@property
			def 							lowerCAmelCase__			(						self     :   Tuple       )      ->int:



					'''simple docstring'''







					return self.user_data["edge_followed_by"]["count"]




			@property
			def 							lowerCAmelCase__			(						self     :   str       )      ->int:



					'''simple docstring'''







					return self.user_data["edge_follow"]["count"]




			@property
			def 							lowerCAmelCase__			(						self     :   Any       )      ->int:



					'''simple docstring'''







					return self.user_data["edge_owner_to_timeline_media"]["count"]




			@property
			def 							lowerCAmelCase__			(						self     :   List[Any]       )      ->str:



					'''simple docstring'''







					return self.user_data["profile_pic_url_hd"]




			@property
			def 							lowerCAmelCase__			(						self     :   Optional[Any]       )      ->bool:



					'''simple docstring'''







					return self.user_data["is_verified"]






			@property
			def 							lowerCAmelCase__			(						self     :   int       )      ->bool:



					'''simple docstring'''







					return self.user_data["is_private"]






def        __lowerCAmelCase       (__lowerCAmelCase = "github"      ):
		import os

		if os.environ.get("CI"      ):
				return  # test failing on GitHub Actions
		_UpperCAmelCase						:				Dict	    =					InstagramUser(__lowerCAmelCase      )
		assert instagram_user.user_data
		assert isinstance(instagram_user.user_data					,       __lowerCAmelCase      )
		assert instagram_user.username == username
		if username != "github":
				return
		assert instagram_user.fullname == "GitHub"
		assert instagram_user.biography == "Built for developers."
		assert instagram_user.number_of_posts > 150
		assert instagram_user.number_of_followers > 120_000
		assert instagram_user.number_of_followings > 15
		assert instagram_user.email == "support@github.com"
		assert instagram_user.website == "https://github.com/readme"
		assert instagram_user.profile_picture_url.startswith("https://instagram."      )
		assert instagram_user.is_verified is True
		assert instagram_user.is_private is False


if __name__ == "__main__":
						import doctest

						doctest.testmod()
						lowerCamelCase__					=   InstagramUser('github')
						print(instagram_user)
						print(F'''{instagram_user.number_of_posts = }''')
						print(F'''{instagram_user.number_of_followers = }''')
						print(F'''{instagram_user.number_of_followings = }''')
						print(F'''{instagram_user.email = }''')
						print(F'''{instagram_user.website = }''')
						print(F'''{instagram_user.profile_picture_url = }''')
						print(F'''{instagram_user.is_verified = }''')
						print(F'''{instagram_user.is_private = }''')



