Skip to content
/CreatorBox/images/jms_logo.png

扩展能力 📤

您可以根据实际需求灵活定制启动方式与运行参数,灵活适配自身需求场景,快捷启动

扩展预览

启动器

1. 定义

launcher/stable_diffusion_webui2.py
python
from src.utils.cblauncher import LauncherProvider
from src.utils import cbfile, cbruntime

class StableDiffusion2Launcher(LauncherProvider):

    def __init__(self, project, **kwargs):
        super().__init__(project, **kwargs)
        self.host = kwargs.get("host") or "0.0.0.0"
        self.port = kwargs.get("port") or 8002

    # 安装
    def install(self):
        # 拉取代码
        # checker = cbruntime.check_git()
        # if checker.get_version() == "None":
        #     logger.error("git is not available. please install git first.")
        #     return
        # cbruntime.subprocess_popen(["git", "clone", "https://github.com/AUTOMATIC1111/stable-diffusion-webui.git", self.project_dir]).wait()
        # 开始安装(优先级 requirements.txt -> pyproject.toml)
        super().install()
        # 运行环境
        os.environ["VENV_DIR"] = f"{self.project_dir}\.venv"
        os.environ["SD_WEBUI_RESTARTING"] = "1"
        # 绕过 AWS 凭证检查(模型下载需要)
        os.environ["AWS_ACCESS_KEY_ID"] = "dummy"
        os.environ["AWS_SECRET_ACCESS_KEY"] = "dummy"
        os.environ["AWS_SESSION_TOKEN"] = "dummy"
        # 下载模型
        cbruntime.process_aria2("https://hf-mirror.com/runwayml/stable-diffusion-v1-5/resolve/main/v1-5-pruned-emaonly.safetensors", os.path.join(self.project_dir, "models/Stable-diffusion"))

    # 启动
    def start(self):
        cmd = [
            self.python_exe,
            "launch.py",
            f"--port {str(self.port)}",
            "--api",
            "--loglevel debug",
            "--use-cpu all",
            "--precision full",
            "--no-half",
            "--skip-torch-cuda-test",
            "--autolaunch",
        ]
        if self.share:
            cmd.append("--share")
        # 独立子进程(主程序退出也不影响)
        process = cbruntime.subprocess_popen(
            cbruntime.split_cmd(cmd),
            cwd=self.project_dir,
            log=self.log_file,
        )
        # 写入 PID 文件
        cbfile.write(self.pid_file, str(process.pid))
        logger.info(f"✅ {self.project} 已启动(PID: {process.pid})")
        return process

2. 注册

src/app/launcher.py
python
from launcher import StableDiffusion2Launcher
from src.utils.cblauncher import CosyVoiceLauncher, GptSovitsLauncher, IndexTTSLauncher, IOPaintLauncher, LauncherProvider, StableDiffusionLauncher

class Launcher:

    @staticmethod
    def _get_launcher_map():
        return {
            "iopaint": IOPaintLauncher,
            "stable_diffusion_webui": StableDiffusionLauncher,
            "cosyvoice": CosyVoiceLauncher,
            "index_tts": IndexTTSLauncher,
            "gpt_sovits": GptSovitsLauncher,
            "voxcpm": VoxCPMLauncher,
        }

    ...

if __name__ == "__main__":
    project = "stable_diffusion_webui2"
    python = "3.10"
    kwargs = {
        "index": "https://mirrors.aliyun.com/pypi/simple",
        "pyv": python,
        "host": "0.0.0.0",
        "port": 9000,
        "share": False,
    }
    # 创建启动器
    launcher = Launcher.builder(project, **kwargs)
    # 执行流程
    launcher.create()
    launcher.install()
    launcher.start().wait()

3. 启动

  • 链接启动
http://127.0.0.1:8000/launcher?project=stable_diffusion_webui2
  • 命令启动
bash
uv run cli.py install -n stable_diffusion_webui2 -p 9000 --start
  • 原生启动
bash
uv run launch.py --port 9000 --api --loglevel debug --use-cpu all --precision full --no-half --skip-torch-cuda-test --autolaunch