




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


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


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





def lowerCamelCase__       (      ):

     '''simple docstring'''


     UpperCAmelCase_      :							str       =      '''https://pypi.org/pypi/diffusers/json'''
     UpperCAmelCase_      :							Any       =      json.loads(request.urlopen(_lowercase				).read()				)['''releases'''].keys()
     return sorted(_lowercase			,						key=lambda _lowercase				: version.Version(_lowercase				)				)





def lowerCamelCase__       (      ):

     '''simple docstring'''


     if HF_MODULES_CACHE in sys.path:
          return

     sys.path.append(_lowercase				)
     os.makedirs(_lowercase			,						exist_ok=_lowercase				)
     UpperCAmelCase_      :							Optional[int]       =      Path(_lowercase				) / '''__init__.py'''
     if not init_path.exists():
          init_path.touch()





def lowerCamelCase__       (      _lowercase				):

     '''simple docstring'''


     init_hf_modules()
     UpperCAmelCase_      :							Optional[int]       =      Path(_lowercase				) / 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(_lowercase			,						exist_ok=_lowercase				)
     UpperCAmelCase_      :							Optional[Any]       =      dynamic_module_path / '''__init__.py'''
     if not init_path.exists():
          init_path.touch()





def lowerCamelCase__       (      _lowercase				):

     '''simple docstring'''


     with open(_lowercase			,						'''r'''			,						encoding='''utf-8'''				) as f:
          UpperCAmelCase_      :							int       =      f.read()

     # Imports of the form `import .xxx`
     UpperCAmelCase_      :							Union[str, Any]       =      re.findall('''^\s*import\s+\.(\S+)\s*$'''			,						_lowercase			,						flags=re.MULTILINE				)
     # Imports of the form `from .xxx import yyy`
     relative_imports += re.findall('''^\s*from\s+\.(\S+)\s+import'''			,						_lowercase			,						flags=re.MULTILINE				)
     # Unique-ify
     return list(set(_lowercase				)				)





def lowerCamelCase__       (      _lowercase				):

     '''simple docstring'''


     UpperCAmelCase_      :							Any       =      False
     UpperCAmelCase_      :							Optional[Any]       =      [module_file]
     UpperCAmelCase_      :							Union[str, Any]       =      []

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

          UpperCAmelCase_      :							str       =      Path(_lowercase				).parent
          UpperCAmelCase_      :							Tuple       =      [str(module_path / m				) for m in new_imports]
          UpperCAmelCase_      :							Tuple       =      [f for f in new_import_files if f not in all_relative_imports]
          UpperCAmelCase_      :							Dict       =      [f'''{f}.py''' for f in new_import_files]

          UpperCAmelCase_      :							Optional[int]       =      len(_lowercase				) == 0
          all_relative_imports.extend(_lowercase				)

     return all_relative_imports





def lowerCamelCase__       (      _lowercase				):

     '''simple docstring'''


     with open(_lowercase			,						'''r'''			,						encoding='''utf-8'''				) as f:
          UpperCAmelCase_      :							int       =      f.read()

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

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

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

     return get_relative_imports(_lowercase				)





def lowerCamelCase__       (      _lowercase			,						_lowercase				):

     '''simple docstring'''


     UpperCAmelCase_      :							Union[str, Any]       =      module_path.replace(os.path.sep			,						'''.'''				)
     UpperCAmelCase_      :							int       =      importlib.import_module(_lowercase				)

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





def lowerCamelCase__       (      _lowercase				):

     '''simple docstring'''


     from ..pipelines import DiffusionPipeline

     UpperCAmelCase_      :							str       =      dict(inspect.getmembers(_lowercase			,						inspect.isclass				)				)

     UpperCAmelCase_      :							Optional[int]       =      None
     for cls_name, cls in cls_members.items():
          if (
              cls_name != DiffusionPipeline.__name__
              and issubclass(cls			,						_lowercase				)
              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}.'''				)
               UpperCAmelCase_      :							Optional[int]       =      cls

     return pipeline_class





def lowerCamelCase__       (      _lowercase			,						_lowercase			,						_lowercase = None			,						_lowercase = False			,						_lowercase = False			,						_lowercase = None			,						_lowercase = None			,						_lowercase = None			,						_lowercase = False			,						):

     '''simple docstring'''


     UpperCAmelCase_      :							Optional[int]       =      str(_lowercase				)

     UpperCAmelCase_      :							Optional[int]       =      os.path.join(_lowercase			,						_lowercase				)

     if os.path.isfile(_lowercase				):
          UpperCAmelCase_      :							Union[str, Any]       =      module_file_or_url
          UpperCAmelCase_      :							Optional[int]       =      '''local'''
     elif pretrained_model_name_or_path.count('''/'''				) == 0:
          UpperCAmelCase_      :							Any       =      get_diffusers_versions()
          # cut ".dev0"
          UpperCAmelCase_      :							str       =      '''v''' + '''.'''.join(__version__.split('''.'''				)[:3]				)

          # retrieve github version that matches
          if revision is None:
               UpperCAmelCase_      :							Optional[Any]       =      latest_version if latest_version[1:] in available_versions else '''main'''
               logger.info(f'''Defaulting to latest_version: {revision}.'''				)
          elif revision in available_versions:
               UpperCAmelCase_      :							Optional[int]       =      f'''v{revision}'''
          elif revision == "main":
               UpperCAmelCase_      :							Optional[Any]       =      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
          UpperCAmelCase_      :							Optional[int]       =      COMMUNITY_PIPELINES_URL.format(revision=_lowercase			,						pipeline=_lowercase				)
          try:
               UpperCAmelCase_      :							Union[str, Any]       =      cached_download(
                   _lowercase			,						cache_dir=_lowercase			,						force_download=_lowercase			,						proxies=_lowercase			,						resume_download=_lowercase			,						local_files_only=_lowercase			,						use_auth_token=_lowercase			,						)
               UpperCAmelCase_      :							List[Any]       =      '''git'''
               UpperCAmelCase_      :							List[Any]       =      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
               UpperCAmelCase_      :							Union[str, Any]       =      hf_hub_download(
                   _lowercase			,						_lowercase			,						cache_dir=_lowercase			,						force_download=_lowercase			,						proxies=_lowercase			,						resume_download=_lowercase			,						local_files_only=_lowercase			,						use_auth_token=_lowercase			,						)
               UpperCAmelCase_      :							Tuple       =      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
     UpperCAmelCase_      :							List[Any]       =      check_imports(_lowercase				)

     # Now we move the module inside our cached dynamic modules.
     UpperCAmelCase_      :							Optional[Any]       =      DIFFUSERS_DYNAMIC_MODULE_NAME + os.path.sep + submodule
     create_dynamic_module(_lowercase				)
     UpperCAmelCase_      :							Union[str, Any]       =      Path(_lowercase				) / 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(_lowercase			,						submodule_path / module_file				)
          for module_needed in modules_needed:
               UpperCAmelCase_      :							Tuple       =      f'''{module_needed}.py'''
               shutil.copy(os.path.join(_lowercase			,						_lowercase				)			,						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(_lowercase			,						_lowercase				):
               UpperCAmelCase_      :							str       =      use_auth_token
          elif use_auth_token is True:
               UpperCAmelCase_      :							Optional[int]       =      HfFolder.get_token()
          else:
               UpperCAmelCase_      :							Optional[int]       =      None

          UpperCAmelCase_      :							List[str]       =      model_info(_lowercase			,						revision=_lowercase			,						token=_lowercase				).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.
          UpperCAmelCase_      :							int       =      submodule_path / commit_hash
          UpperCAmelCase_      :							Any       =      full_submodule + os.path.sep + commit_hash
          create_dynamic_module(_lowercase				)

          if not (submodule_path / module_file).exists():
               shutil.copy(_lowercase			,						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(
                        _lowercase			,						f'''{module_needed}.py'''			,						cache_dir=_lowercase			,						force_download=_lowercase			,						resume_download=_lowercase			,						proxies=_lowercase			,						use_auth_token=_lowercase			,						revision=_lowercase			,						local_files_only=_lowercase			,						)
     return os.path.join(_lowercase			,						_lowercase				)





def lowerCamelCase__       (      _lowercase			,						_lowercase			,						_lowercase = None			,						_lowercase = None			,						_lowercase = False			,						_lowercase = False			,						_lowercase = None			,						_lowercase = None			,						_lowercase = None			,						_lowercase = False			,						**_lowercase			,						):

     '''simple docstring'''


     UpperCAmelCase_      :							Optional[int]       =      get_cached_module_file(
         _lowercase			,						_lowercase			,						cache_dir=_lowercase			,						force_download=_lowercase			,						resume_download=_lowercase			,						proxies=_lowercase			,						use_auth_token=_lowercase			,						revision=_lowercase			,						local_files_only=_lowercase			,						)
     return get_class_in_module(_lowercase			,						final_module.replace('''.py'''			,						''''''				)				)





from random import shuffle

import tensorflow as tf
from numpy import array







def lowerCamelCase__       (      _lowercase			,						_lowercase				):

     '''simple docstring'''


     UpperCAmelCase_      :							Union[str, Any]       =      int(_lowercase				)
     assert noofclusters < len(_lowercase				)

     # Find out the dimensionality
     UpperCAmelCase_      :							Optional[int]       =      len(vectors[0]				)

     # Will help select random centroids from among the available vectors
     UpperCAmelCase_      :							Optional[int]       =      list(range(len(_lowercase				)				)				)
     shuffle(_lowercase				)

     # GRAPH OF COMPUTATION
     # We initialize a new graph and set it as the default during each run
     # of this algorithm. This ensures that as this function is called
     # multiple times, the default graph doesn't keep getting crowded with
     # unused ops and Variables from previous function calls.

     UpperCAmelCase_      :							List[str]       =      tf.Graph()

     with graph.as_default():
          # SESSION OF COMPUTATION

          UpperCAmelCase_      :							Tuple       =      tf.Session()

          ##CONSTRUCTING THE ELEMENTS OF COMPUTATION

          ##First lets ensure we have a Variable vector for each centroid,
          ##initialized to one of the vectors from the available data points
          UpperCAmelCase_      :							str       =      [
              tf.Variable(vectors[vector_indices[i]]				) for i in range(_lowercase				)
          ]
          ##These nodes will assign the centroid Variables the appropriate
          ##values
          UpperCAmelCase_      :							List[Any]       =      tf.placeholder('''float64'''			,						[dim]				)
          UpperCAmelCase_      :							List[Any]       =      []
          for centroid in centroids:
               cent_assigns.append(tf.assign(_lowercase			,						_lowercase				)				)

          ##Variables for cluster assignments of individual vectors(initialized
          ##to 0 at first)
          UpperCAmelCase_      :							Optional[int]       =      [tf.Variable(0				) for i in range(len(_lowercase				)				)]
          ##These nodes will assign an assignment Variable the appropriate
          ##value
          UpperCAmelCase_      :							List[Any]       =      tf.placeholder('''int32'''				)
          UpperCAmelCase_      :							Union[str, Any]       =      []
          for assignment in assignments:
               cluster_assigns.append(tf.assign(_lowercase			,						_lowercase				)				)

          ##Now lets construct the node that will compute the mean
          # The placeholder for the input
          UpperCAmelCase_      :							Tuple       =      tf.placeholder('''float'''			,						[None, dim]				)
          # The Node/op takes the input and computes a mean along the 0th
          # dimension, i.e. the list of input vectors
          UpperCAmelCase_      :							str       =      tf.reduce_mean(_lowercase			,						0				)

          ##Node for computing Euclidean distances
          # Placeholders for input
          UpperCAmelCase_      :							List[Any]       =      tf.placeholder('''float'''			,						[dim]				)
          UpperCAmelCase_      :							Tuple       =      tf.placeholder('''float'''			,						[dim]				)
          UpperCAmelCase_      :							Optional[Any]       =      tf.sqrt(tf.reduce_sum(tf.pow(tf.sub(_lowercase			,						_lowercase				)			,						2				)				)				)

          ##This node will figure out which cluster to assign a vector to,
          ##based on Euclidean distances of the vector from the centroids.
          # Placeholder for input
          UpperCAmelCase_      :							int       =      tf.placeholder('''float'''			,						[noofclusters]				)
          UpperCAmelCase_      :							Any       =      tf.argmin(_lowercase			,						0				)

          ##INITIALIZING STATE VARIABLES

          ##This will help initialization of all Variables defined with respect
          ##to the graph. The Variable-initializer should be defined after
          ##all the Variables have been constructed, so that each of them
          ##will be included in the initialization.
          UpperCAmelCase_      :							List[Any]       =      tf.initialize_all_variables()

          # Initialize all variables
          sess.run(_lowercase				)

          ##CLUSTERING ITERATIONS

          # Now perform the Expectation-Maximization steps of K-Means clustering
          # iterations. To keep things simple, we will only do a set number of
          # iterations, instead of using a Stopping Criterion.
          UpperCAmelCase_      :							Tuple       =      100
          for _ in range(_lowercase				):
               ##EXPECTATION STEP
               ##Based on the centroid locations till last iteration, compute
               ##the _expected_ centroid assignments.
               # Iterate over each vector
               for vector_n in range(len(_lowercase				)				):
                    UpperCAmelCase_      :							Optional[int]       =      vectors[vector_n]
                    # Compute Euclidean distance between this vector and each
                    # centroid. Remember that this list cannot be named
                    #'centroid_distances', since that is the input to the
                    # cluster assignment node.
                    UpperCAmelCase_      :							Tuple       =      [
                        sess.run(_lowercase			,						feed_dict={va: vect, va: sess.run(_lowercase				)}				)
                        for centroid in centroids
                    ]
                    # Now use the cluster assignment node, with the distances
                    # as the input
                    UpperCAmelCase_      :							Dict       =      sess.run(
                        _lowercase			,						feed_dict={centroid_distances: distances}				)
                    # Now assign the value to the appropriate state variable
                    sess.run(
                        cluster_assigns[vector_n]			,						feed_dict={assignment_value: assignment}				)

               ##MAXIMIZATION STEP
               # Based on the expected state computed from the Expectation Step,
               # compute the locations of the centroids so as to maximize the
               # overall objective of minimizing within-cluster Sum-of-Squares
               for cluster_n in range(_lowercase				):
                    # Collect all the vectors assigned to this cluster
                    UpperCAmelCase_      :							List[str]       =      [
                        vectors[i]
                        for i in range(len(_lowercase				)				)
                        if sess.run(assignments[i]				) == cluster_n
                    ]
                    # Compute new centroid location
                    UpperCAmelCase_      :							Any       =      sess.run(
                        _lowercase			,						feed_dict={mean_input: array(_lowercase				)}				)
                    # Assign value to appropriate variable
                    sess.run(
                        cent_assigns[cluster_n]			,						feed_dict={centroid_value: new_location}				)

        # Return centroids and assignments
          UpperCAmelCase_      :							Optional[int]       =      sess.run(_lowercase				)
          UpperCAmelCase_      :							int       =      sess.run(_lowercase				)
          return centroids, assignments