# 配置 import os import re import threading import subprocess import time install_path = '/home/xlab-app-center' webui_repo = 'AUTOMATIC1111/stable-diffusion-webui --branch v1.9.4' rename_repo = 'stable-diffusion-webui' webui_port = 7860 api_auth = 'Echoflare:Tt25faj8' download_tool = 'aria2c --console-log-level=error -c -x 16 -s 16 -k 1M' webui_args = [ '--api', '--xformers', '--no-hashing', '--disable-nan-check', '--disable-console-progressbars', '--enable-console-prompts', '--no-half-vae', '--skip-torch-cuda-test', '--allow-code' ] extensions = [ 'https://gitcode.com/zanllp/sd-webui-infinite-image-browsing', 'https://gitcode.com/dtlnor/stable-diffusion-webui-localization-zh_CN', # 汉化 'https://gitcode.com/DominikDoom/a1111-sd-webui-tagcomplete', # 提示词提示器 'https://kkgithub.com/Mikubill/sd-webui-controlnet', # ControlNet "https://gitcode.net/overbill1683/stable-diffusion-webui-localization-zh_Hans", "https://gitcode.net/ranting8323/sd-webui-inpaint-anything", "https://openi.pcl.ac.cn/2575044704/sd-extension-system-info", "https://openi.pcl.ac.cn/2575044704/batchlinks-webui", 'https://openi.pcl.ac.cn/2575044704/stable-diffusion-webui-localization-zh_CN', 'https://openi.pcl.ac.cn/2575044704/sd-webui-lora-block-weight', 'https://openi.pcl.ac.cn/2575044704/sd-skin-extension', "https://kkgithub.com/continue-revolution/sd-webui-animatediff.git", "https://kkgithub.com/Iyashinouta/sd-model-downloader.git", "https://kkgithub.com/fkunn1326/openpose-editor.git", "https://kkgithub.com/zero01101/openOutpaint-webUI-extension.git", "https://kkgithub.com/LonicaMewinsky/gif2gif.git", "https://openi.pcl.ac.cn/2575044704/sd-webui-agent-scheduler", "https://openi.pcl.ac.cn/Echoflare/letest", "https://github.com/Physton/sd-webui-prompt-all-in-one", ] sd_models = [] lora_models = [] vae_models = [ "https://hf-mirror.com/stabilityai/sd-vae-ft-mse-original/resolve/main/vae-ft-mse-840000-ema-pruned.safetensors", ] ControlNet = False embedding_models = [] hypernetwork_models = [] esrgan_models = [ "https://hf-mirror.com/mihirkothari0702/RealESRGAN/resolve/main/RealESRGAN_x4plus.pth", "https://hf-mirror.com/com-adm/RealESRGAN_x4plus_anime_6B/resolve/main/RealESRGAN_x4plus_anime_6B.pth", ] custom_commands = [ f'rm -rf {install_path}/{rename_repo}/config.json', f'rm -rf {install_path}/{rename_repo}/ui-config.json', f'rm -rf {install_path}/{rename_repo}/modules/ui_settings.py', f"{download_tool} https://hf-mirror.com/datasets/ACCA225/openxlab/resolve/main/config-pub.json -d {install_path}/{rename_repo} -o config.json --allow-overwrite=true", f"{download_tool} https://hf-mirror.com/datasets/ACCA225/openxlab/resolve/main/ui-config-pub3.json -d {install_path}/{rename_repo} -o ui-config.json --allow-overwrite=true", f"{download_tool} https://hf-mirror.com/datasets/Mira-LeafTown/sd-webui-openxlab/raw/main/ui_settings.py -d {install_path}/{rename_repo}/modules -o ui_settings.py --allow-overwrite=true", ] def redirect_github(url): return url.replace("github.com", "kkgithub.com") # 网页UI部署 def monitor_gpu(): import pynvml as nvidia_smi import wandb def download_extensions(extensions): os.chdir(f'{install_path}/{rename_repo}/extensions') for extension in extensions: extension = redirect_github(extension) os.system(f'git clone {extension}') def model_download(models, type_w): for model in models: download_files(model, type_w) def remove_restart(): os.chdir(f"{install_path}/{rename_repo}/html") os.system("rm ./footer.html && wget -O footer.html https://hf-mirror.com/datasets/ACCA225/openxlab/resolve/main/footer.html") def download_files(url, source): url = redirect_github(url) if '@' in url and not url.startswith(('http://', 'https://')): parts = url.split('@', 1) name, url = parts rename = f"-o '{name}'" else: if any(x in url for x in ['huggingface.co', 'hf-mirror.com', 'huggingface.sukaka.top']): url = url.replace("huggingface.co", "hf-mirror.com") match_name = re.search(r'/([^/?]+)(?:\?download=true)?$', url).group(1) rename = f"-o '{match_name}'" if match_name else '' else: rename = '' source_dir = f'{install_path}/{rename_repo}/{source}' os.makedirs(source_dir, exist_ok=True) os.chdir(source_dir) os.system(f"{download_tool} '{url}' {rename}") def run_webui(): os.system("pip install nvidia-ml-py3 wandb") monitor_thread = threading.Thread(target=monitor_gpu) monitor_thread.start() os.chdir(install_path) if not os.path.exists(f'{install_path}/{rename_repo}'): os.system(f"git clone https://kkgithub.com/AUTOMATIC1111/stable-diffusion-webui {install_path}/{rename_repo}") remove_restart() os.system(f"{download_tool} https://hf-mirror.com/datasets/ACCC1380/private-model/resolve/main/kaggle/input/museum/131-half.safetensors -d {install_path}/{rename_repo}/models/Stable-diffusion -o [萌二次元]131-half.safetensors") if not os.path.exists(f'{install_path}/{rename_repo}'): print(f'在克隆 https://kkgithub.com/{webui_repo} 时出错') run_webui() download_extensions(extensions) model_download(sd_models, 'models/Stable-diffusion') model_download(lora_models, 'models/Lora') model_download(vae_models, 'models/VAE') if ControlNet: model_download(controlnet_models, 'extensions/sd-webui-controlnet/models') model_download(hypernetwork_models, 'models/hypernetworks') model_download(embedding_models, 'embeddings') model_download(esrgan_models, 'models/ESRGAN') os.chdir(f"{install_path}/{rename_repo}") package_envs = [ {"env": "STABLE_DIFFUSION_XL_REPO", "url": os.environ.get('STABLE_DIFFUSION_XL_REPO', "https://gitcode.net/overbill1683/generative-models")}, {"env": "K_DIFFUSION_REPO", "url": os.environ.get('K_DIFFUSION_REPO', "https://gitcode.net/overbill1683/k-diffusion")}, {"env": "CODEFORMER_REPO", "url": os.environ.get('CODEFORMER_REPO', "https://gitcode.net/overbill1683/CodeFormer")}, {"env": "BLIP_REPO", "url": os.environ.get('BLIP_REPO', "https://gitcode.net/overbill1683/BLIP")}, {"env": "CLIP_REPO", "url": os.environ.get('CLIP_REPO', "https://kkgithub.com/openai/CLIP")}, ] os.environ["PIP_INDEX_URL"] = "https://mirrors.aliyun.com/pypi/simple/" for package_env in package_envs: os.environ[package_env["env"]] = package_env["url"] os.chdir(install_path) for custom_command in custom_commands: os.system(custom_command) os.chdir(f"{install_path}/{rename_repo}") os.system(f"python launch.py {' '.join(webui_args)} --port {webui_port}") # 实例保活 def session_saver(): try: import cupy as cp except ImportError: print("cupy模块未安装,正在安装...") import pip pip.main(['install', 'cupy']) import cupy as cp while True: time.sleep(60)