





import importlib
import inspect
import json
import os
import re
import shutil
import sys
from pathlib import Path
from typing import Dict, Optional, Union
from urllib import request

from huggingface_hub import HfFolder, cached_download, hf_hub_download, model_info
from packaging import version

from .. import __version__
from . import DIFFUSERS_DYNAMIC_MODULE_NAME, HF_MODULES_CACHE, logging


lowercase_          =		(
    """https://raw.githubusercontent.com/huggingface/diffusers/{revision}/examples/community/{pipeline}.py"""
)


lowercase_          =		logging.get_logger(__name__)  # pylint: disable=invalid-name




def       __UpperCamelCase  ()    ->       Union[str, Any]:
  lowercase__           =  'https://pypi.org/pypi/diffusers/json'
  lowercase__           =  json.loads(request.urlopen(_SCREAMING_SNAKE_CASE		).read()		)['releases'].keys()
  return sorted(_SCREAMING_SNAKE_CASE		,						key=lambda _SCREAMING_SNAKE_CASE		: version.Version(_SCREAMING_SNAKE_CASE		)		)




def       __UpperCamelCase  ()    ->       Union[str, Any]:
  # This function has already been executed if HF_MODULES_CACHE already is in the Python path.
  if HF_MODULES_CACHE in sys.path:
    return

  sys.path.append(_SCREAMING_SNAKE_CASE		)
  os.makedirs(_SCREAMING_SNAKE_CASE		,						exist_ok=_SCREAMING_SNAKE_CASE		)
  lowercase__           =  Path(_SCREAMING_SNAKE_CASE		) / '__init__.py'
  if not init_path.exists():
    init_path.touch()




def       __UpperCamelCase  (_SCREAMING_SNAKE_CASE		)    ->       Tuple:
  init_hf_modules()
  lowercase__           =  Path(_SCREAMING_SNAKE_CASE		) / name
  # If the parent module does not exist yet, recursively create it.
  if not dynamic_module_path.parent.exists():
    create_dynamic_module(dynamic_module_path.parent		)
  os.makedirs(_SCREAMING_SNAKE_CASE		,						exist_ok=_SCREAMING_SNAKE_CASE		)
  lowercase__           =  dynamic_module_path / '__init__.py'
  if not init_path.exists():
    init_path.touch()




def       __UpperCamelCase  (_SCREAMING_SNAKE_CASE		)    ->       Dict:
  with open(_SCREAMING_SNAKE_CASE		,						'r'		,						encoding='utf-8'		) as f:
    lowercase__           =  f.read()

  # Imports of the form `import .xxx`
  lowercase__           =  re.findall('^\s*import\s+\.(\S+)\s*$'		,						_SCREAMING_SNAKE_CASE		,						flags=re.MULTILINE		)
  # Imports of the form `from .xxx import yyy`
  relative_imports += re.findall('^\s*from\s+\.(\S+)\s+import'		,						_SCREAMING_SNAKE_CASE		,						flags=re.MULTILINE		)
  # Unique-ify
  return list(set(_SCREAMING_SNAKE_CASE		)		)




def       __UpperCamelCase  (_SCREAMING_SNAKE_CASE		)    ->       Union[str, Any]:
  lowercase__           =  False
  lowercase__           =  [module_file]
  lowercase__           =  []

  # Let's recurse through all relative imports
  while not no_change:
    lowercase__           =  []
    for f in files_to_check:
      new_imports.extend(get_relative_imports(_SCREAMING_SNAKE_CASE		)		)

    lowercase__           =  Path(_SCREAMING_SNAKE_CASE		).parent
    lowercase__           =  [str(module_path / m		) for m in new_imports]
    lowercase__           =  [f for f in new_import_files if f not in all_relative_imports]
    lowercase__           =  [F"""{f}.py""" for f in new_import_files]

    lowercase__           =  len(_SCREAMING_SNAKE_CASE		) == 0
    all_relative_imports.extend(_SCREAMING_SNAKE_CASE		)

  return all_relative_imports




def       __UpperCamelCase  (_SCREAMING_SNAKE_CASE		)    ->       Dict:
  with open(_SCREAMING_SNAKE_CASE		,						'r'		,						encoding='utf-8'		) as f:
    lowercase__           =  f.read()

  # Imports of the form `import xxx`
  lowercase__           =  re.findall('^\s*import\s+(\S+)\s*$'		,						_SCREAMING_SNAKE_CASE		,						flags=re.MULTILINE		)
  # Imports of the form `from xxx import yyy`
  imports += re.findall('^\s*from\s+(\S+)\s+import'		,						_SCREAMING_SNAKE_CASE		,						flags=re.MULTILINE		)
  # Only keep the top-level module
  lowercase__           =  [imp.split('.'		)[0] for imp in imports if not imp.startswith('.'		)]

  # Unique-ify and test we got them all
  lowercase__           =  list(set(_SCREAMING_SNAKE_CASE		)		)
  lowercase__           =  []
  for imp in imports:
    try:
      importlib.import_module(_SCREAMING_SNAKE_CASE		)
    except ImportError:
      missing_packages.append(_SCREAMING_SNAKE_CASE		)

  if len(_SCREAMING_SNAKE_CASE		) > 0:
    raise ImportError(
        'This modeling file requires the following packages that were not found in your environment: '
        F"""{", ".join(_SCREAMING_SNAKE_CASE		)}. Run `pip install {" ".join(_SCREAMING_SNAKE_CASE		)}`"""		)

  return get_relative_imports(_SCREAMING_SNAKE_CASE		)




def       __UpperCamelCase  (_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE		)    ->       Tuple:
  lowercase__           =  module_path.replace(os.path.sep		,						'.'		)
  lowercase__           =  importlib.import_module(_SCREAMING_SNAKE_CASE		)

  if class_name is None:
    return find_pipeline_class(_SCREAMING_SNAKE_CASE		)
  return getattr(_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE		)




def       __UpperCamelCase  (_SCREAMING_SNAKE_CASE		)    ->       Any:
  from ..pipelines import DiffusionPipeline

  lowercase__           =  dict(inspect.getmembers(_SCREAMING_SNAKE_CASE		,						inspect.isclass		)		)

  lowercase__           =  None
  for cls_name, cls in cls_members.items():
    if (
        cls_name != DiffusionPipeline.__name__
        and issubclass(cls		,						_SCREAMING_SNAKE_CASE		)
        and cls.__module__.split('.'		)[0] != "diffusers"
    ):
      if pipeline_class is not None:
        raise ValueError(
            F"""Multiple classes that inherit from {DiffusionPipeline.__name__} have been found:"""
            F""" {pipeline_class.__name__}, and {cls_name}. Please make sure to define only one in"""
            F""" {loaded_module}."""		)
      lowercase__           =  cls

  return pipeline_class




def       __UpperCamelCase  (_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE = None		,						_SCREAMING_SNAKE_CASE = False		,						_SCREAMING_SNAKE_CASE = False		,						_SCREAMING_SNAKE_CASE = None		,						_SCREAMING_SNAKE_CASE = None		,						_SCREAMING_SNAKE_CASE = None		,						_SCREAMING_SNAKE_CASE = False		,						)    ->       Optional[Any]:
  lowercase__           =  str(_SCREAMING_SNAKE_CASE		)

  lowercase__           =  os.path.join(_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE		)

  if os.path.isfile(_SCREAMING_SNAKE_CASE		):
    lowercase__           =  module_file_or_url
    lowercase__           =  'local'
  elif pretrained_model_name_or_path.count('/'		) == 0:
    lowercase__           =  get_diffusers_versions()
    # cut ".dev0"
    lowercase__           =  'v' + '.'.join(__version__.split('.'		)[:3]		)

    # retrieve github version that matches
    if revision is None:
      lowercase__           =  latest_version if latest_version[1:] in available_versions else 'main'
      logger.info(F"""Defaulting to latest_version: {revision}."""		)
    elif revision in available_versions:
      lowercase__           =  F"""v{revision}"""
    elif revision == "main":
      lowercase__           =  revision
    else:
      raise ValueError(
          F"""`custom_revision`: {revision} does not exist. Please make sure to choose one of"""
          F""" {", ".join(available_versions + ["main"]		)}."""		)

    # community pipeline on GitHub
    lowercase__           =  COMMUNITY_PIPELINES_URL.format(revision=_SCREAMING_SNAKE_CASE		,						pipeline=_SCREAMING_SNAKE_CASE		)
    try:
      lowercase__           =  cached_download(
          _SCREAMING_SNAKE_CASE		,						cache_dir=_SCREAMING_SNAKE_CASE		,						force_download=_SCREAMING_SNAKE_CASE		,						proxies=_SCREAMING_SNAKE_CASE		,						resume_download=_SCREAMING_SNAKE_CASE		,						local_files_only=_SCREAMING_SNAKE_CASE		,						use_auth_token=_SCREAMING_SNAKE_CASE		,						)
      lowercase__           =  'git'
      lowercase__           =  pretrained_model_name_or_path + '.py'
    except EnvironmentError:
      logger.error(F"""Could not locate the {module_file} inside {pretrained_model_name_or_path}."""		)
      raise
  else:
    try:
      # Load from URL or cache if already cached
      lowercase__           =  hf_hub_download(
          _SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE		,						cache_dir=_SCREAMING_SNAKE_CASE		,						force_download=_SCREAMING_SNAKE_CASE		,						proxies=_SCREAMING_SNAKE_CASE		,						resume_download=_SCREAMING_SNAKE_CASE		,						local_files_only=_SCREAMING_SNAKE_CASE		,						use_auth_token=_SCREAMING_SNAKE_CASE		,						)
      lowercase__           =  os.path.join('local'		,						'--'.join(pretrained_model_name_or_path.split('/'		)		)		)
    except EnvironmentError:
      logger.error(F"""Could not locate the {module_file} inside {pretrained_model_name_or_path}."""		)
      raise

    # Check we have all the requirements in our environment
  lowercase__           =  check_imports(_SCREAMING_SNAKE_CASE		)

  # Now we move the module inside our cached dynamic modules.
  lowercase__           =  DIFFUSERS_DYNAMIC_MODULE_NAME + os.path.sep + submodule
  create_dynamic_module(_SCREAMING_SNAKE_CASE		)
  lowercase__           =  Path(_SCREAMING_SNAKE_CASE		) / full_submodule
  if submodule == "local" or submodule == "git":
    # We always copy local files (we could hash the file to see if there was a change, and give them the name of
    # that hash, to only copy when there is a modification but it seems overkill for now).
    # The only reason we do the copy is to avoid putting too many folders in sys.path.
    shutil.copy(_SCREAMING_SNAKE_CASE		,						submodule_path / module_file		)
    for module_needed in modules_needed:
      lowercase__           =  F"""{module_needed}.py"""
      shutil.copy(os.path.join(_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE		)		,						submodule_path / module_needed		)
  else:
    # Get the commit hash
    # TODO: we will get this info in the etag soon, so retrieve it from there and not here.
    if isinstance(_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE		):
      lowercase__           =  use_auth_token
    elif use_auth_token is True:
      lowercase__           =  HfFolder.get_token()
    else:
      lowercase__           =  None

    lowercase__           =  model_info(_SCREAMING_SNAKE_CASE		,						revision=_SCREAMING_SNAKE_CASE		,						token=_SCREAMING_SNAKE_CASE		).sha

    # The module file will end up being placed in a subfolder with the git hash of the repo. This way we get the
    # benefit of versioning.
    lowercase__           =  submodule_path / commit_hash
    lowercase__           =  full_submodule + os.path.sep + commit_hash
    create_dynamic_module(_SCREAMING_SNAKE_CASE		)

    if not (submodule_path / module_file).exists():
      shutil.copy(_SCREAMING_SNAKE_CASE		,						submodule_path / module_file		)
    # Make sure we also have every file with relative
    for module_needed in modules_needed:
      if not (submodule_path / module_needed).exists():
        get_cached_module_file(
            _SCREAMING_SNAKE_CASE		,						F"""{module_needed}.py"""		,						cache_dir=_SCREAMING_SNAKE_CASE		,						force_download=_SCREAMING_SNAKE_CASE		,						resume_download=_SCREAMING_SNAKE_CASE		,						proxies=_SCREAMING_SNAKE_CASE		,						use_auth_token=_SCREAMING_SNAKE_CASE		,						revision=_SCREAMING_SNAKE_CASE		,						local_files_only=_SCREAMING_SNAKE_CASE		,						)
  return os.path.join(_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE		)




def       __UpperCamelCase  (_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE = None		,						_SCREAMING_SNAKE_CASE = None		,						_SCREAMING_SNAKE_CASE = False		,						_SCREAMING_SNAKE_CASE = False		,						_SCREAMING_SNAKE_CASE = None		,						_SCREAMING_SNAKE_CASE = None		,						_SCREAMING_SNAKE_CASE = None		,						_SCREAMING_SNAKE_CASE = False		,						**_SCREAMING_SNAKE_CASE		,						)    ->       Dict:
  lowercase__           =  get_cached_module_file(
      _SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE		,						cache_dir=_SCREAMING_SNAKE_CASE		,						force_download=_SCREAMING_SNAKE_CASE		,						resume_download=_SCREAMING_SNAKE_CASE		,						proxies=_SCREAMING_SNAKE_CASE		,						use_auth_token=_SCREAMING_SNAKE_CASE		,						revision=_SCREAMING_SNAKE_CASE		,						local_files_only=_SCREAMING_SNAKE_CASE		,						)
  return get_class_in_module(_SCREAMING_SNAKE_CASE		,						final_module.replace('.py'		,						''		)		)









import argparse
import json
import re
from pathlib import Path

import requests
import torch
from huggingface_hub import hf_hub_download
from PIL import Image

from transformers import (
    MobileNetVaConfig,
    MobileNetVaForImageClassification,
    MobileNetVaImageProcessor,
    load_tf_weights_in_mobilenet_va,
)
from transformers.utils import logging


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




def       __UpperCamelCase  (_SCREAMING_SNAKE_CASE		)    ->       Any:
  lowercase__           =  MobileNetVaConfig(layer_norm_eps=0.0_0_1		)

  if "_quant" in model_name:
    raise ValueError('Quantized models are not supported.'		)

  lowercase__           =  re.match(R'^mobilenet_v1_([^_]*)_([^_]*)$'		,						_SCREAMING_SNAKE_CASE		)
  if matches:
    lowercase__           =  float(matches[1]		)
    lowercase__           =  int(matches[2]		)

  # The TensorFlow version of MobileNetV1 predicts 1001 classes instead of
  # the usual 1000. The first class (index 0) is "background".
  lowercase__           =  1001
  lowercase__           =  'imagenet-1k-id2label.json'
  lowercase__           =  'huggingface/label-files'
  lowercase__           =  json.load(open(hf_hub_download(_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE		,						repo_type='dataset'		)		,						'r'		)		)
  lowercase__           =  {int(_SCREAMING_SNAKE_CASE		) + 1: v for k, v in idalabel.items()}
  lowercase__           =  'background'
  lowercase__           =  idalabel
  lowercase__           =  {v: k for k, v in idalabel.items()}

  return config




def       __UpperCamelCase  ()    ->       int:
  lowercase__           =  'http://images.cocodataset.org/val2017/000000039769.jpg'
  lowercase__           =  Image.open(requests.get(_SCREAMING_SNAKE_CASE		,						stream=_SCREAMING_SNAKE_CASE		).raw		)
  return im




@torch.no_grad()
def       __UpperCamelCase  (_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE=False		)    ->       Union[str, Any]:
  lowercase__           =  get_mobilenet_va_config(_SCREAMING_SNAKE_CASE		)

  # Load 🤗 model
  lowercase__           =  MobileNetVaForImageClassification(_SCREAMING_SNAKE_CASE		).eval()

  # Load weights from TensorFlow checkpoint
  load_tf_weights_in_mobilenet_va(_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE		)

  # Check outputs on an image, prepared by MobileNetV1ImageProcessor
  lowercase__           =  MobileNetVaImageProcessor(
      crop_size={'width': config.image_size, 'height': config.image_size}		,						size={'shortest_edge': config.image_size + 32}		,						)
  lowercase__           =  image_processor(images=prepare_img()		,						return_tensors='pt'		)
  lowercase__           =  model(**_SCREAMING_SNAKE_CASE		)
  lowercase__           =  outputs.logits

  assert logits.shape == (1, 1001)

  if model_name == "mobilenet_v1_1.0_224":
    lowercase__           =  torch.tensor([-4.1_7_3_9, -1.1_2_3_3, 3.1_2_0_5]		)
  elif model_name == "mobilenet_v1_0.75_192":
    lowercase__           =  torch.tensor([-3.9_4_4_0, -2.3_1_4_1, -0.3_3_3_3]		)
  else:
    lowercase__           =  None

  if expected_logits is not None:
    assert torch.allclose(logits[0, :3]		,						_SCREAMING_SNAKE_CASE		,						atol=1E-4		)

  Path(_SCREAMING_SNAKE_CASE		).mkdir(exist_ok=_SCREAMING_SNAKE_CASE		)
  print(F"""Saving model {model_name} to {pytorch_dump_folder_path}"""		)
  model.save_pretrained(_SCREAMING_SNAKE_CASE		)
  print(F"""Saving image processor to {pytorch_dump_folder_path}"""		)
  image_processor.save_pretrained(_SCREAMING_SNAKE_CASE		)

  if push_to_hub:
    print('Pushing to the hub...'		)
    lowercase__           =  'google/' + model_name
    image_processor.push_to_hub(_SCREAMING_SNAKE_CASE		)
    model.push_to_hub(_SCREAMING_SNAKE_CASE		)


if __name__ == "__main__":
      lowercase_          =		argparse.ArgumentParser()
      # Required parameters
      parser.add_argument(
          """--model_name""",
          default="""mobilenet_v1_1.0_224""",
          type=str,
          help="""Name of the MobileNetV1 model you'd like to convert. Should in the form 'mobilenet_v1_<depth>_<size>'.""",
      )
      parser.add_argument(
          """--checkpoint_path""", required=True, type=str, help="""Path to the original TensorFlow checkpoint (.ckpt file)."""
      )
      parser.add_argument(
          """--pytorch_dump_folder_path""", required=True, type=str, help="""Path to the output PyTorch model directory."""
      )
      parser.add_argument(
          """--push_to_hub""", action="""store_true""", help="""Whether or not to push the converted model to the 🤗 hub."""
      )

      lowercase_          =		parser.parse_args()
      convert_movilevit_checkpoint(
          args.model_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub
      )


