Cog is an open-source tool for packaging machine learning models into standard Docker containers. It allows you to define a Python `Predictor` class with `setup` and `predict` methods, which Cog then uses to build a Docker image for local testing or deployment to platforms like Replicate. The current version is 0.17.2, and it receives frequent minor updates with occasional major releases introducing significant architectural changes.
pip install cogVerified import paths — ran on the pinned version, not inferred.
Define a `Predictor` class inheriting from `BasePredictor`. Implement `setup()` to load your model and `predict()` to handle inference. `Input` specifies prediction inputs with types, descriptions, and defaults. `Path` is used for file-based inputs/outputs.
Prefix `def` with `async` (e.g., `async def setup(self):`) for asynchronous operations. Ensure your code properly `await`s async calls within these methods.
Either provide a default value to `Input()` (e.g., `Input(default=...)`) or explicitly handle `None` checks in your `predict` method for any input that might be optional.
Install Docker Desktop (for Windows/macOS) or Docker Engine (for Linux) and ensure the Docker daemon is running before using Cog commands.
Always initialize `cog.yaml` using `cog init` and carefully specify `python_version`, `build.python_packages`, `build.system_packages`, and `build.python_version` as needed for your model. Double-check paths to `requirements.txt` and other build files.
Remove `extra_model_headers` from your `cog.yaml` file. If you relied on this for custom headers, you may need to implement a custom HTTP handler or find an alternative approach.
Ensure the `cog` CLI tool is correctly installed in your environment. When running `cog build`, Cog will automatically install its Python SDK wheel into the Docker image it creates. If you're running local Python scripts that import `cog`'s types outside of the `cog build` process, you might need to explicitly install the `cog` Python package in your local environment using `pip install cog`.
Add a `python_version` field under the `build` section in your `cog.yaml` file, specifying a supported Python version (e.g., '3.10', '3.11', '3.12', or '3.13'). Example: ```yaml build: python_version: "3.10" # ... other build configurations ```
1. **Install and configure Docker Buildx:** Ensure Docker Buildx is properly set up by running `docker buildx install` and `docker buildx create --use`. 2. **Adjust for architecture:** If you are on an ARM-based machine (e.g., `aarch64`) and do not require GPU, set `gpu: false` in your `cog.yaml` to use a CPU-only base image, which has broader platform support. If you need GPU support on ARM, ensure you are using a base image specifically built for that architecture and a compatible `cuda` version. 3. **Buildx driver:** In CI/CD environments, ensure the `buildx` driver is correctly configured, sometimes explicitly setting it to `docker` instead of `docker-container` can resolve issues: `docker buildx use default` or similar configuration in your CI script.
Replace all instances of `cog.File` with `cog.Path` in your `predict.py` file and any other related model code. Example: ```python # Before (deprecated) from cog import BasePredictor, File # After (correct) from cog import BasePredictor, Path ```