'''simple docstring'''




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


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


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






def __lowerCAmelCase			(							)     ->   Optional[Any]:
    __lowerCamelCase          =  '''https://pypi.org/pypi/diffusers/json'''
    __lowerCamelCase          =  json.loads(request.urlopen(UpperCamelCase__    ).read()    )['''releases'''].keys()
    return sorted(UpperCamelCase__    ,  key=lambda UpperCamelCase__    : version.Version(UpperCamelCase__    )    )






def __lowerCAmelCase			(							)     ->   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(UpperCamelCase__    )
    os.makedirs(UpperCamelCase__    ,  exist_ok=UpperCamelCase__    )
    __lowerCamelCase          =  Path(UpperCamelCase__    ) / '''__init__.py'''
    if not init_path.exists():
        init_path.touch()






def __lowerCAmelCase			(							UpperCamelCase__    )     ->   Tuple:
    init_hf_modules()
    __lowerCamelCase          =  Path(UpperCamelCase__    ) / 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(UpperCamelCase__    ,  exist_ok=UpperCamelCase__    )
    __lowerCamelCase          =  dynamic_module_path / '''__init__.py'''
    if not init_path.exists():
        init_path.touch()






def __lowerCAmelCase			(							UpperCamelCase__    )     ->   List[str]:
    with open(UpperCamelCase__    ,  '''r'''    ,  encoding='''utf-8'''    ) as f:
        __lowerCamelCase          =  f.read()

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






def __lowerCAmelCase			(							UpperCamelCase__    )     ->   str:
    __lowerCamelCase          =  False
    __lowerCamelCase          =  [module_file]
    __lowerCamelCase          =  []

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

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

        __lowerCamelCase          =  len(UpperCamelCase__    ) == 0
        all_relative_imports.extend(UpperCamelCase__    )

    return all_relative_imports






def __lowerCAmelCase			(							UpperCamelCase__    )     ->   List[str]:
    with open(UpperCamelCase__    ,  '''r'''    ,  encoding='''utf-8'''    ) as f:
        __lowerCamelCase          =  f.read()

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

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

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

    return get_relative_imports(UpperCamelCase__    )






def __lowerCAmelCase			(							UpperCamelCase__    ,  UpperCamelCase__    )     ->   Optional[int]:
    __lowerCamelCase          =  module_path.replace(os.path.sep    ,  '''.'''    )
    __lowerCamelCase          =  importlib.import_module(UpperCamelCase__    )

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






def __lowerCAmelCase			(							UpperCamelCase__    )     ->   Tuple:
    from ..pipelines import DiffusionPipeline

    __lowerCamelCase          =  dict(inspect.getmembers(UpperCamelCase__    ,  inspect.isclass    )    )

    __lowerCamelCase          =  None
    for cls_name, cls in cls_members.items():
        if (
            cls_name != DiffusionPipeline.__name__
            and issubclass(cls    ,  UpperCamelCase__    )
            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}."""    )
            __lowerCamelCase          =  cls

    return pipeline_class






def __lowerCAmelCase			(							UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__ = None    ,  UpperCamelCase__ = False    ,  UpperCamelCase__ = False    ,  UpperCamelCase__ = None    ,  UpperCamelCase__ = None    ,  UpperCamelCase__ = None    ,  UpperCamelCase__ = False    ,  )     ->   List[Any]:
    __lowerCamelCase          =  str(UpperCamelCase__    )

    __lowerCamelCase          =  os.path.join(UpperCamelCase__    ,  UpperCamelCase__    )

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

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

    # Now we move the module inside our cached dynamic modules.
    __lowerCamelCase          =  DIFFUSERS_DYNAMIC_MODULE_NAME + os.path.sep + submodule
    create_dynamic_module(UpperCamelCase__    )
    __lowerCamelCase          =  Path(UpperCamelCase__    ) / 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(UpperCamelCase__    ,  submodule_path / module_file    )
        for module_needed in modules_needed:
            __lowerCamelCase          =  f"""{module_needed}.py"""
            shutil.copy(os.path.join(UpperCamelCase__    ,  UpperCamelCase__    )    ,  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(UpperCamelCase__    ,  UpperCamelCase__    ):
            __lowerCamelCase          =  use_auth_token
        elif use_auth_token is True:
            __lowerCamelCase          =  HfFolder.get_token()
        else:
            __lowerCamelCase          =  None

        __lowerCamelCase          =  model_info(UpperCamelCase__    ,  revision=UpperCamelCase__    ,  token=UpperCamelCase__    ).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.
        __lowerCamelCase          =  submodule_path / commit_hash
        __lowerCamelCase          =  full_submodule + os.path.sep + commit_hash
        create_dynamic_module(UpperCamelCase__    )

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






def __lowerCAmelCase			(							UpperCamelCase__    ,  UpperCamelCase__    ,  UpperCamelCase__ = None    ,  UpperCamelCase__ = None    ,  UpperCamelCase__ = False    ,  UpperCamelCase__ = False    ,  UpperCamelCase__ = None    ,  UpperCamelCase__ = None    ,  UpperCamelCase__ = None    ,  UpperCamelCase__ = False    ,  **UpperCamelCase__    ,  )     ->   List[Any]:
    __lowerCamelCase          =  get_cached_module_file(
        UpperCamelCase__    ,  UpperCamelCase__    ,  cache_dir=UpperCamelCase__    ,  force_download=UpperCamelCase__    ,  resume_download=UpperCamelCase__    ,  proxies=UpperCamelCase__    ,  use_auth_token=UpperCamelCase__    ,  revision=UpperCamelCase__    ,  local_files_only=UpperCamelCase__    ,  )
    return get_class_in_module(UpperCamelCase__    ,  final_module.replace('''.py'''    ,  ''''''    )    )



'''simple docstring'''




# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from ..models.auto import AutoModelForSeqaSeqLM, AutoTokenizer
from .base import PipelineTool


__UpperCAmelCase												={
    "Acehnese Arabic": "ace_Arab",
    "Acehnese Latin": "ace_Latn",
    "Mesopotamian Arabic": "acm_Arab",
    "Ta'izzi-Adeni Arabic": "acq_Arab",
    "Tunisian Arabic": "aeb_Arab",
    "Afrikaans": "afr_Latn",
    "South Levantine Arabic": "ajp_Arab",
    "Akan": "aka_Latn",
    "Amharic": "amh_Ethi",
    "North Levantine Arabic": "apc_Arab",
    "Modern Standard Arabic": "arb_Arab",
    "Modern Standard Arabic Romanized": "arb_Latn",
    "Najdi Arabic": "ars_Arab",
    "Moroccan Arabic": "ary_Arab",
    "Egyptian Arabic": "arz_Arab",
    "Assamese": "asm_Beng",
    "Asturian": "ast_Latn",
    "Awadhi": "awa_Deva",
    "Central Aymara": "ayr_Latn",
    "South Azerbaijani": "azb_Arab",
    "North Azerbaijani": "azj_Latn",
    "Bashkir": "bak_Cyrl",
    "Bambara": "bam_Latn",
    "Balinese": "ban_Latn",
    "Belarusian": "bel_Cyrl",
    "Bemba": "bem_Latn",
    "Bengali": "ben_Beng",
    "Bhojpuri": "bho_Deva",
    "Banjar Arabic": "bjn_Arab",
    "Banjar Latin": "bjn_Latn",
    "Standard Tibetan": "bod_Tibt",
    "Bosnian": "bos_Latn",
    "Buginese": "bug_Latn",
    "Bulgarian": "bul_Cyrl",
    "Catalan": "cat_Latn",
    "Cebuano": "ceb_Latn",
    "Czech": "ces_Latn",
    "Chokwe": "cjk_Latn",
    "Central Kurdish": "ckb_Arab",
    "Crimean Tatar": "crh_Latn",
    "Welsh": "cym_Latn",
    "Danish": "dan_Latn",
    "German": "deu_Latn",
    "Southwestern Dinka": "dik_Latn",
    "Dyula": "dyu_Latn",
    "Dzongkha": "dzo_Tibt",
    "Greek": "ell_Grek",
    "English": "eng_Latn",
    "Esperanto": "epo_Latn",
    "Estonian": "est_Latn",
    "Basque": "eus_Latn",
    "Ewe": "ewe_Latn",
    "Faroese": "fao_Latn",
    "Fijian": "fij_Latn",
    "Finnish": "fin_Latn",
    "Fon": "fon_Latn",
    "French": "fra_Latn",
    "Friulian": "fur_Latn",
    "Nigerian Fulfulde": "fuv_Latn",
    "Scottish Gaelic": "gla_Latn",
    "Irish": "gle_Latn",
    "Galician": "glg_Latn",
    "Guarani": "grn_Latn",
    "Gujarati": "guj_Gujr",
    "Haitian Creole": "hat_Latn",
    "Hausa": "hau_Latn",
    "Hebrew": "heb_Hebr",
    "Hindi": "hin_Deva",
    "Chhattisgarhi": "hne_Deva",
    "Croatian": "hrv_Latn",
    "Hungarian": "hun_Latn",
    "Armenian": "hye_Armn",
    "Igbo": "ibo_Latn",
    "Ilocano": "ilo_Latn",
    "Indonesian": "ind_Latn",
    "Icelandic": "isl_Latn",
    "Italian": "ita_Latn",
    "Javanese": "jav_Latn",
    "Japanese": "jpn_Jpan",
    "Kabyle": "kab_Latn",
    "Jingpho": "kac_Latn",
    "Kamba": "kam_Latn",
    "Kannada": "kan_Knda",
    "Kashmiri Arabic": "kas_Arab",
    "Kashmiri Devanagari": "kas_Deva",
    "Georgian": "kat_Geor",
    "Central Kanuri Arabic": "knc_Arab",
    "Central Kanuri Latin": "knc_Latn",
    "Kazakh": "kaz_Cyrl",
    "Kabiyè": "kbp_Latn",
    "Kabuverdianu": "kea_Latn",
    "Khmer": "khm_Khmr",
    "Kikuyu": "kik_Latn",
    "Kinyarwanda": "kin_Latn",
    "Kyrgyz": "kir_Cyrl",
    "Kimbundu": "kmb_Latn",
    "Northern Kurdish": "kmr_Latn",
    "Kikongo": "kon_Latn",
    "Korean": "kor_Hang",
    "Lao": "lao_Laoo",
    "Ligurian": "lij_Latn",
    "Limburgish": "lim_Latn",
    "Lingala": "lin_Latn",
    "Lithuanian": "lit_Latn",
    "Lombard": "lmo_Latn",
    "Latgalian": "ltg_Latn",
    "Luxembourgish": "ltz_Latn",
    "Luba-Kasai": "lua_Latn",
    "Ganda": "lug_Latn",
    "Luo": "luo_Latn",
    "Mizo": "lus_Latn",
    "Standard Latvian": "lvs_Latn",
    "Magahi": "mag_Deva",
    "Maithili": "mai_Deva",
    "Malayalam": "mal_Mlym",
    "Marathi": "mar_Deva",
    "Minangkabau Arabic ": "min_Arab",
    "Minangkabau Latin": "min_Latn",
    "Macedonian": "mkd_Cyrl",
    "Plateau Malagasy": "plt_Latn",
    "Maltese": "mlt_Latn",
    "Meitei Bengali": "mni_Beng",
    "Halh Mongolian": "khk_Cyrl",
    "Mossi": "mos_Latn",
    "Maori": "mri_Latn",
    "Burmese": "mya_Mymr",
    "Dutch": "nld_Latn",
    "Norwegian Nynorsk": "nno_Latn",
    "Norwegian Bokmål": "nob_Latn",
    "Nepali": "npi_Deva",
    "Northern Sotho": "nso_Latn",
    "Nuer": "nus_Latn",
    "Nyanja": "nya_Latn",
    "Occitan": "oci_Latn",
    "West Central Oromo": "gaz_Latn",
    "Odia": "ory_Orya",
    "Pangasinan": "pag_Latn",
    "Eastern Panjabi": "pan_Guru",
    "Papiamento": "pap_Latn",
    "Western Persian": "pes_Arab",
    "Polish": "pol_Latn",
    "Portuguese": "por_Latn",
    "Dari": "prs_Arab",
    "Southern Pashto": "pbt_Arab",
    "Ayacucho Quechua": "quy_Latn",
    "Romanian": "ron_Latn",
    "Rundi": "run_Latn",
    "Russian": "rus_Cyrl",
    "Sango": "sag_Latn",
    "Sanskrit": "san_Deva",
    "Santali": "sat_Olck",
    "Sicilian": "scn_Latn",
    "Shan": "shn_Mymr",
    "Sinhala": "sin_Sinh",
    "Slovak": "slk_Latn",
    "Slovenian": "slv_Latn",
    "Samoan": "smo_Latn",
    "Shona": "sna_Latn",
    "Sindhi": "snd_Arab",
    "Somali": "som_Latn",
    "Southern Sotho": "sot_Latn",
    "Spanish": "spa_Latn",
    "Tosk Albanian": "als_Latn",
    "Sardinian": "srd_Latn",
    "Serbian": "srp_Cyrl",
    "Swati": "ssw_Latn",
    "Sundanese": "sun_Latn",
    "Swedish": "swe_Latn",
    "Swahili": "swh_Latn",
    "Silesian": "szl_Latn",
    "Tamil": "tam_Taml",
    "Tatar": "tat_Cyrl",
    "Telugu": "tel_Telu",
    "Tajik": "tgk_Cyrl",
    "Tagalog": "tgl_Latn",
    "Thai": "tha_Thai",
    "Tigrinya": "tir_Ethi",
    "Tamasheq Latin": "taq_Latn",
    "Tamasheq Tifinagh": "taq_Tfng",
    "Tok Pisin": "tpi_Latn",
    "Tswana": "tsn_Latn",
    "Tsonga": "tso_Latn",
    "Turkmen": "tuk_Latn",
    "Tumbuka": "tum_Latn",
    "Turkish": "tur_Latn",
    "Twi": "twi_Latn",
    "Central Atlas Tamazight": "tzm_Tfng",
    "Uyghur": "uig_Arab",
    "Ukrainian": "ukr_Cyrl",
    "Umbundu": "umb_Latn",
    "Urdu": "urd_Arab",
    "Northern Uzbek": "uzn_Latn",
    "Venetian": "vec_Latn",
    "Vietnamese": "vie_Latn",
    "Waray": "war_Latn",
    "Wolof": "wol_Latn",
    "Xhosa": "xho_Latn",
    "Eastern Yiddish": "ydd_Hebr",
    "Yoruba": "yor_Latn",
    "Yue Chinese": "yue_Hant",
    "Chinese Simplified": "zho_Hans",
    "Chinese Traditional": "zho_Hant",
    "Standard Malay": "zsm_Latn",
    "Zulu": "zul_Latn",
}




class    a__				(       UpperCAmelCase__			):
     lowerCamelCase  :					Any							="facebook/nllb-200-distilled-600M"
     lowerCamelCase  :					Optional[Any]							=(
         "This is a tool that translates text from a language to another. It takes three inputs: `text`, which should "
         "be the text to translate, `src_lang`, which should be the language of the text to translate and `tgt_lang`, "
         "which should be the language for the desired ouput language. Both `src_lang` and `tgt_lang` are written in "
         "plain English, such as 'Romanian', or 'Albanian'. It returns the text translated in `tgt_lang`."
     )
     lowerCamelCase  :					Tuple							="translator"
     lowerCamelCase  :					Any							=AutoTokenizer
     lowerCamelCase  :					Dict							=AutoModelForSeqaSeqLM
     lowerCamelCase  :					Union[str, Any]							=LANGUAGE_CODES

     lowerCamelCase  :					Any							=["text", "text", "text"]
     lowerCamelCase  :					Union[str, Any]							=["text"]






     def        SCREAMING_SNAKE_CASE__       (			self		:		Optional[int]  ,    a		:		Any  ,    a		:		List[str]  ,    a		:		Any   ):





         """simple docstring"""



         if src_lang not in self.lang_to_code:
             raise ValueError(f"""{src_lang} is not a supported language."""   )
         if tgt_lang not in self.lang_to_code:
             raise ValueError(f"""{tgt_lang} is not a supported language."""   )
         __lowerCamelCase          =  self.lang_to_code[src_lang]
         __lowerCamelCase          =  self.lang_to_code[tgt_lang]
         return self.pre_processor._build_translation_inputs(
             a  ,    return_tensors='''pt'''  ,    src_lang=a  ,    tgt_lang=a   )






     def        SCREAMING_SNAKE_CASE__       (			self		:		str  ,    a		:		Optional[Any]   ):





         """simple docstring"""



         return self.model.generate(**a   )






     def        SCREAMING_SNAKE_CASE__       (			self		:		Optional[int]  ,    a		:		str   ):





         """simple docstring"""



         return self.post_processor.decode(outputs[0].tolist()  ,    skip_special_tokens=a   )
