lang03383 commited on
Commit
101ef15
·
verified ·
1 Parent(s): 82c5721

Create setuup_en.py

Browse files
Files changed (1) hide show
  1. sd_yun/setuup_en.py +88 -0
sd_yun/setuup_en.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+ import shutil
4
+ import argparse
5
+ import importlib
6
+ import subprocess
7
+
8
+ def parse_args():
9
+ parser = argparse.ArgumentParser(description='Script configuration')
10
+ parser.add_argument('--lang', type=str, default='en', help='Код языка, по умолчанию "en"')
11
+ parser.add_argument('--repo', type=str, required=True, help='Repository Name')
12
+ return parser.parse_args()
13
+
14
+ def detect_environment():
15
+ free_plan = (os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES') / (1024 ** 3) <= 20)
16
+ environments = {
17
+ 'COLAB_GPU': ('Google Colab', "/root" if free_plan else "/content"),
18
+ 'KAGGLE_URL_BASE': ('Kaggle', "/kaggle/working/content")
19
+ }
20
+ for env_var, (environment, path) in environments.items():
21
+ if env_var in os.environ:
22
+ return environment, path, free_plan
23
+ print("\033[31mError: an unsupported runtime environment was detected.\n\033[34mSupported environments:\033[0m Google Colab, Kaggle")
24
+ return None, None, None
25
+
26
+ def setup_module_folder(root_path):
27
+ modules_folder = os.path.join(root_path, "modules")
28
+ os.makedirs(modules_folder, exist_ok=True)
29
+ if modules_folder not in sys.path:
30
+ sys.path.append(modules_folder)
31
+
32
+ def clear_module_cache(modules_folder):
33
+ for module_name in list(sys.modules.keys()):
34
+ module = sys.modules[module_name]
35
+ if hasattr(module, '__file__') and module.__file__ and module.__file__.startswith(modules_folder):
36
+ del sys.modules[module_name]
37
+
38
+ importlib.invalidate_caches()
39
+
40
+ def download_files(root_path, lang, repo):
41
+ print("正在下载文件... 👀", end='', flush=True)
42
+ files_dict = { # save folder name | url folder path | files list
43
+ 'CSS': {'CSS': ['main_widgets.css', 'auto_cleaner.css', 'dl_display_result.css']},
44
+ 'file_cell': {f'files_cells/python/{lang}': [f'widgets_{lang}.py', f'downloading_{lang}.py', f'launch_{lang}.py', f'auto_cleaner_{lang}.py']},
45
+ 'file_cell/special': {f'special': ['dl_display_results.py']},
46
+ 'modules': {f'modules': ['models_data.py', 'directory_setup.py']}
47
+ }
48
+ for folder, contents in files_dict.items():
49
+ folder_path = os.path.join(root_path, folder)
50
+ if os.path.exists(folder_path):
51
+ shutil.rmtree(folder_path)
52
+ os.makedirs(folder_path)
53
+ for path_url, files in contents.items():
54
+ for file in files:
55
+ file_url = f"https://huggingface.co/NagisaNao/{repo}/resolve/main/{path_url}/{file}"
56
+ file_path = os.path.join(folder_path, file)
57
+ os.system(f'wget -q {file_url} -O {file_path}')
58
+ print("\r完成!现在你可以运行下面的代码单元格了。 ☄️" + " "*30)
59
+
60
+ def main():
61
+ args = parse_args()
62
+ lang = args.lang
63
+ repo = args.repo
64
+
65
+ env, root_path, free_plan = detect_environment()
66
+
67
+ if env and root_path:
68
+ webui_path = f"{root_path}/sdw"
69
+ download_files(root_path, lang, repo)
70
+ clear_module_cache(os.path.join(root_path, "modules"))
71
+ setup_module_folder(root_path)
72
+
73
+ # Set global environment variable
74
+ os.environ['ENV_NAME'] = env
75
+ os.environ['ROOT_PATH'] = root_path
76
+ os.environ['WEBUI_PATH'] = webui_path
77
+ os.environ['FREE_PLAN'] = 'True' if free_plan else 'False'
78
+
79
+ print(f"运行环境:\033[33m{env}\033[0m")
80
+ if env == "Google Colab":
81
+ print(f"Colab Pro 订阅:\033[34m{not free_plan}\033[0m")
82
+ print(f"文件位置:\033[32m{root_path}\033[0m")
83
+
84
+ if repo != 'fast_repo':
85
+ print('\n\033[31mWARNING: Test mode is used, there may be errors in use!\033[0m')
86
+
87
+ if __name__ == "__main__":
88
+ main()