|
import os |
|
import sys |
|
import shutil |
|
import argparse |
|
import importlib |
|
import subprocess |
|
|
|
def parse_args(): |
|
parser = argparse.ArgumentParser(description='Script configuration') |
|
parser.add_argument('--lang', type=str, default='en', help='Код языка, по умолчанию "en"') |
|
parser.add_argument('--repo', type=str, required=True, help='Repository Name') |
|
return parser.parse_args() |
|
|
|
def detect_environment(): |
|
free_plan = (os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES') / (1024 ** 3) <= 20) |
|
environments = { |
|
'COLAB_GPU': ('Google Colab', "/root" if free_plan else "/content"), |
|
'KAGGLE_URL_BASE': ('Kaggle', "/kaggle/working/content") |
|
} |
|
for env_var, (environment, path) in environments.items(): |
|
if env_var in os.environ: |
|
return environment, path, free_plan |
|
print("\033[31mError: an unsupported runtime environment was detected.\n\033[34mSupported environments:\033[0m Google Colab, Kaggle") |
|
return None, None, None |
|
|
|
def setup_module_folder(root_path): |
|
modules_folder = os.path.join(root_path, "modules") |
|
os.makedirs(modules_folder, exist_ok=True) |
|
if modules_folder not in sys.path: |
|
sys.path.append(modules_folder) |
|
|
|
def clear_module_cache(modules_folder): |
|
for module_name in list(sys.modules.keys()): |
|
module = sys.modules[module_name] |
|
if hasattr(module, '__file__') and module.__file__ and module.__file__.startswith(modules_folder): |
|
del sys.modules[module_name] |
|
|
|
importlib.invalidate_caches() |
|
|
|
def download_files(root_path, lang, repo): |
|
print("正在下载文件... 👀", end='', flush=True) |
|
files_dict = { |
|
'CSS': {'CSS': ['main_widgets.css', 'auto_cleaner.css', 'dl_display_result.css']}, |
|
'file_cell': {f'files_cells/python/{lang}': [f'widgets_{lang}.py', f'downloading_{lang}.py', f'launch_{lang}.py', f'auto_cleaner_{lang}.py']}, |
|
'file_cell/special': {f'special': ['dl_display_results.py']}, |
|
'modules': {f'modules': ['models_data.py', 'directory_setup.py']} |
|
} |
|
for folder, contents in files_dict.items(): |
|
folder_path = os.path.join(root_path, folder) |
|
if os.path.exists(folder_path): |
|
shutil.rmtree(folder_path) |
|
os.makedirs(folder_path) |
|
for path_url, files in contents.items(): |
|
for file in files: |
|
file_url = f"https://huggingface.co/NagisaNao/{repo}/resolve/main/{path_url}/{file}" |
|
file_path = os.path.join(folder_path, file) |
|
os.system(f'wget -q {file_url} -O {file_path}') |
|
print("\r完成!现在你可以运行下面的代码单元格了。 ☄️" + " "*30) |
|
|
|
def main(): |
|
args = parse_args() |
|
lang = args.lang |
|
repo = args.repo |
|
|
|
env, root_path, free_plan = detect_environment() |
|
|
|
if env and root_path: |
|
webui_path = f"{root_path}/sdw" |
|
download_files(root_path, lang, repo) |
|
clear_module_cache(os.path.join(root_path, "modules")) |
|
setup_module_folder(root_path) |
|
|
|
|
|
os.environ['ENV_NAME'] = env |
|
os.environ['ROOT_PATH'] = root_path |
|
os.environ['WEBUI_PATH'] = webui_path |
|
os.environ['FREE_PLAN'] = '付费计划' if free_plan else '免费计划' |
|
|
|
print(f"运行环境:\033[33m{env}\033[0m") |
|
if env == "Google Colab": |
|
print(f"Colab Pro 订阅:\033[34m{not free_plan}\033[0m") |
|
print(f"文件位置:\033[32m{root_path}\033[0m") |
|
|
|
if repo != 'fast_repo': |
|
print('\n\033[31mWARNING: Test mode is used, there may be errors in use!\033[0m') |
|
|
|
if __name__ == "__main__": |
|
main() |