其他
在 Replicate 上发布或更新 Cog…
当你已有可用的 Cog 项目,需要把模型推送到 Replicate、发布新版本,或搭建模型发布 CI/CD 时使用。
🧪 尚未被验证(还没有小呱复用过的记录,不代表不好)
GDI C 35/100
进化评分(GDI)
综合排序依据 —— 四个维度的加权结果,让你看出「为什么这条排在前面」
内在质量 55/100 ×0.35
使用度 0/100 ×0.30
口碑 50/100 ×0.20
新鲜度 38/100 ×0.15
适用场景
在 Replicate 上发布或更新 C特定
解决思路
- 用 `cog push r8.im/owner/my-model` 构建并上传新版本。
- 或在 `cog.yaml` 中设置 `image: r8.im/owner/my-model`,然后运行裸 `cog push`。
- 模型权重超过 1GB 时,用 `--separate-weights` 将权重存到单独层,加快冷启动和推送。
- 迭代时用 `--x-fast` 加快推送,但会跳过部分验证。
- 需要构建期密钥时用 `--secret id=hf,src=$HOME/.hf_token`,避免密钥写入镜像历史。
- 安装 `cog-safe-push`:`pip install git+https://github.com/replicate/cog-safe-push.git`。
- 设置 `REPLICATE_API_TOKEN`;设置 `ANTHROPIC_API_KEY`,用于 Claude 判定随机模型输出相似度。
- 基本用法:`cog-safe-push --test-hardware=gpu-l40s owner/my-model`。
- 让 `cog-safe-push` 先用 ruff lint `predict.py`。
- 让它在缺失时创建私有测试模型 `owner/my-model-test`。
- 让它把本地 Cog 模型推送到测试模型。
- 让它 lint schema,包括描述、默认值等。
- 让它对照线上 `owner/my-model` 版本检查 schema 兼容性。
- 让它在线版本与测试版本之间运行预测对比。
- 让它用 AI 生成输入对测试模型做 fuzz。
- 若全部通过,让它推送到 `owner/my-model`。
- 在项目根目录放置 `cog-safe-push.yaml`;多模型仓库中放到 `cog-safe-push-configs/<variant>.yaml`。
- 每个测试用例从五种 checker 中只选一种:`match_prompt` 用于 Claude AI 判定,适合图像/视频;`match_url` 用于二进制/图像匹配;`error_contains` 用于负向测试;`jq_query` 用于 JSON 输出;`exact_string` 用于精确字符串匹配。
- 随机模型设置 `compare_outputs: false`;默认 `true` 对扩散模型/LLM 易脆。
- 配置 `predict` 块:`predict_timeout`、`test_cases`;配置 `fuzz` 块:`fixed_inputs.seed`、`disabled_inputs`、`iterations`、`prompt`。
- 如果模型有 trainer,配置 `train` 块:`destination`、`destination_hardware`、`train_timeout`、`test_cases`。
- 配置 `deployment` 块:`name`、`owner`、`hardware`,每次 push 自动创建或更新 Replicate deployment。
- 按需设置 `parallel`、`fast_push`、`ignore_schema_compatibility`、`official_model`。
- 自建 GitHub Actions 时创建 `.github/workflows/push.yaml`,使用 `workflow_dispatch` 输入 `no_push`。
- 构建 job 使用 `runs-on: ubuntu-latest-4-cores`,因为构建需要磁盘和 CPU。
- 在 job 中使用 `actions/checkout@v4`。
- 使用 `jlumbroso/free-disk-space@v1.3.1`,设置 `tool-cache: false`、`docker-images: false` 释放空间。
- 使用 `replicate/setup-cog@v2`,传入 `token: ${{ secrets.REPLICATE_API_TOKEN }}`。
- 运行 `pip install git+https://github.com/replicate/cog-safe-push.git`。
- 在设置 `ANTHROPIC_API_KEY` 和 `REPLICATE_API_TOKEN` 环境变量后运行 `cog-safe-push -vv ${{ inputs.no_push && '--no-push' || '' }}`。
- 添加 `concurrency` 块:`group: ${{ github.workflow }}-${{ github.ref }}`,`cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}`,让 PR 构建互相取消、main 分支推送排队。
- 使用 `replicate/model-ci-template` 的可复用工作流:创建 `.github/workflows/ci.yaml`,在 `pull_request`、`push` 到 `main`、`workflow_dispatch` 时触发,输入 `models`、`ignore_schema_checks`、`cog_version`、`test_only`。
- 在 job 中 `uses: replicate/model-ci-template/.github/workflows/template.yaml@main`,传入 `trigger_type`、`models`、`ignore_schema_checks`、`cog_version`、`test_only`,并 `secrets: inherit`。
- 可复用工作流要求 `cog-safe-push-configs/<model>.yaml`,每个模型变体一个。
- 可复用工作流要求 `script/select-model` bash 文件,用 `if/elif [[ "$MODEL" == "..." ]]` 块列出有效模型名。
- 可复用工作流需要 secrets:`COG_TOKEN`、`REPLICATE_API_TOKEN`、`ANTHROPIC_API_KEY`。
- 多模型矩阵推送:一个仓库 N 个变体,并行推送。
- 创建 `prepare` job,输出 `matrix`:当 `inputs.models` 为 `all` 时生成包含如 `schnell`、`dev`、`krea-dev` 的 JSON;否则用 `jq` 按逗号切分 `inputs.models` 并写入 `GITHUB_OUTPUT`。
- 创建 `push` job,`needs: prepare`,`runs-on: ubuntu-latest-4-cores`,`strategy.fail-fast: false`,`matrix: ${{ fromJson(needs.prepare.outputs.matrix) }}`。
- 在 push job 中 checkout,然后运行 `./script/select.sh ${{ matrix.model }}` 从模板生成 `cog.yaml`。
- 然后运行 `cog-safe-push --config cog-safe-push-configs/${{ matrix.model }}.yaml -vv`。
- 代理/官方模型两段推送:先推私有 wrapper,再更新公开 official model card。
- 运行 `./script/write-api-key` 将 API key 写入 config。
- 运行 `cog-safe-push --config cog-safe-push-configs/${MODEL}.yaml -vv`。
- 运行 `./script/delete-api-key` 去掉 key。
- 运行 `cog-safe-push --push-official-model --config cog-safe-push-configs/${MODEL}.yaml -vv`。
- 在 config 中设置 `official_model: owner/name`,让 `--push-official-model` 知道发布目标。
- 在 `cog-safe-push.yaml` 中添加 `deployment` 块,每次 push 自动创建或更新 Replicate deployment。
- 需要调整扩缩容时,通过 API 或 web UI 手动调整;默认 CPU deployment 1-20 实例,GPU deployment 0-2。
- 运行每小时 canary 检测 registry 路径:创建 Hourly cog push check workflow,`schedule` 使用 `cron: 0 * * * *`,并支持 `workflow_dispatch`。
- canary job 运行 `./script/canary.sh`:生成带唯一 uuid 的小模型,推送,按 digest 运行 prediction,任何失败大声报错。
- 对生产关键模型执行 canary,尤其当收入依赖 registry 可用性时。
- 不要无意破坏 schema 兼容性;`cog-safe-push` 会捕获;用 `--ignore-schema-compatibility` 显式退出检查。
- 固定 `test_hardware`,使测试推送可复现。
- PR CI 中用 `--no-push` 做 dry run;合并到 main 或打版本 tag 时执行完整 push。
- 一旦有用户,从 CI 推送而不是从笔记本推送。
- 随机模型用 `compare_outputs: false`;图像/视频输出用 `match_prompt` 做 VLM 判定,自己控制的二进制输出用 `match_url`,JSON 用 `jq_query`,负向测试用 `error_contains`。
- 绝不提交 `REPLICATE_API_TOKEN` 或 `ANTHROPIC_API_KEY`;使用 repo secrets。
- 权重超过 1GB 的模型用 `--separate-weights` 推送。
适用边界 · 注意事项
- 尚未有可用的 Cog 项目时,应先参考 build-models 打包,而不是用本流程发布。
- 不发布到 Replicate、不需要版本兼容性检查和 CI/CD 的本地模型开发流程不适用。
- 没有 Replicate 账号/API token,或无法提供 ANTHROPIC_API_KEY 来运行 cog-safe-push 的 AI 判定与 fuzz 时,不应依赖完整 cog-safe-push 流程。