Skip to main content
Create a cog.yaml and predict.py in the current directory. These files provide a starting template for defining your model’s environment and prediction interface.

Usage

cog init

What It Creates

When you run cog init, Cog creates two files in the current directory:

cog.yaml

A configuration file that defines your model’s environment:
cog.yaml
build:
  python_version: "3.12"
  python_requirements: requirements.txt
predict: "predict.py:Predictor"

predict.py

A template prediction interface:
predict.py
from cog import BasePredictor, Path, Input
import torch

class Predictor(BasePredictor):
    def setup(self):
        """Load the model into memory to make running multiple predictions efficient"""
        self.net = torch.load("weights.pth")

    def predict(self,
            image: Path = Input(description="Image to enlarge"),
            scale: float = Input(description="Factor to scale image by", default=1.5)
    ) -> Path:
        """Run a single prediction on the model"""
        # ... pre-processing ...
        output = self.net(input)
        # ... post-processing ...
        return output

Examples

Initialize a new project

# Navigate to your project directory
cd my-model

# Initialize Cog
cog init
Expected output:
Setting up the current directory for use with Cog...

Created cog.yaml
Created predict.py

Next steps:
  1. Edit cog.yaml to add your dependencies
  2. Edit predict.py to define your prediction interface
  3. Run 'cog predict' to test your model

Start from scratch

# Create a new directory
mkdir my-new-model
cd my-new-model

# Initialize with Cog
cog init

# Add your model code and dependencies
cog init will not overwrite existing cog.yaml or predict.py files. If these files already exist, the command will exit without making changes.

Next Steps

After running cog init, you’ll want to:
  1. Edit cog.yaml to specify your Python version, system packages, and dependencies
  2. Edit predict.py to implement your model’s prediction logic
  3. Test your model with cog predict

cog.yaml reference

Learn about all configuration options

Python API

Define your prediction interface

cog predict

Test your model locally

Your own model guide

Complete setup guide

Options

FlagDescription
-h, --helpShow help for the init command

Build docs developers (and LLMs) love