Metadata-Version: 2.5
Name: kainguru-cli
Version: 0.1.0
Summary: Build and test a cog.yaml model on your machine exactly as Kainguru will, then publish it.
Author: Kainguru
License: MIT
Requires-Python: >=3.10
Requires-Dist: pyyaml>=6
Provides-Extra: dev
Requires-Dist: pytest>=8.0; extra == 'dev'
Description-Content-Type: text/markdown

# kainguru CLI

Build and test a `cog.yaml` model on your own machine exactly as
Kainguru will build it, so a broken model is caught here in minutes instead of on a GPU.

```
kainguru build            # the image the platform builds, built here
kainguru test -i text=hi  # start it and make one prediction, as the platform does
kainguru login            # sign in with an API key from your profile page
kainguru push --name MY_MODEL --category text-generation --banner banner.png --readme README.md
kainguru status ID --wait # follow the registration
```

`push` uploads your project's source, not your image, and starts the platform's build of it: the
platform rebuilds the source with this same recipe and makes one GPU test prediction before the model
goes live (docs/AUTOMATED_REGISTRATION_PLAN.md, section 6b).

## Install

Needs Python 3.10+, Docker, and Linux or macOS (on Windows, WSL 2).

```
pipx install ./kg_cli        # or: pip install ./kg_cli
```

The first build downloads the pinned cog CLI (v0.22.0, the version CI uses) into
`~/.cache/kainguru/cog/` and checks it against the release checksum.

## `kainguru build [DIR] [-t TAG]`

Runs the build job of `.github/workflows/kg-cog-build.yml`, step for step:

1. **Checks the project without Docker**, in well under a second: `cog.yaml` parses, the
   predictor file exists and has no syntax error, it defines the class `predict:` names, and
   `python_requirements` stays inside the project.
2. **Generates the Dockerfile** with the pinned cog on a public `python:<version>-slim` base,
   never a third party's, with the cog runtime pinned to the same version.
3. **Generates the input schema** from your predictor's source without running it, and
   refuses an input whose default cog cannot read (write defaults as literals:
   `Input(default=4)`, not `Input(default=SOME_CONSTANT)`).
4. **Builds the image** for `linux/amd64`, what the platform's nodes run. On Apple silicon
   that means emulation, which is slower.
5. **Checks the image**: the schema is baked in and nothing but your project is in `/src`.

The whole directory lands in the image, as with `COPY . /src`, except `.git` and `.cog`.
Keep weights and virtualenvs out of it, or list them in `.dockerignore`.

The image is tagged `kainguru-local/<directory name>:latest` unless you pass `-t`.

## `kainguru test [DIR] [-t TAG] [-i NAME=VALUE ...]`

Starts the image, waits for `setup()` (up to 900 s), and makes one prediction with your
inputs (up to 1800 s), the same calls and limits the platform uses. Inputs are typed by the
model's schema; `-i image=@photo.png` sends a file, and repeating a name fills a list. File
outputs are written to `output.<ext>` in the current directory.

A **GPU model** is only tested where Docker can give it an NVIDIA GPU. On a machine without
one the test is skipped, not failed: cog would quietly fall back to the CPU, which proves
nothing about the GPU path. The platform makes the GPU test prediction after push.

Exit status: 0 when the prediction succeeded or a GPU test was skipped, 1 otherwise.

## `kainguru login [--token KEY] [--api URL]` and `kainguru logout`

Registering a model needs an API key. Create one on your profile page (Applications, then the
application's API keys; the key is shown once), then run `kainguru login`. It asks for the key
(it is not echoed), checks it with the API and only then stores it. `--token KEY` skips the
question, but a key typed on the command line stays in your shell history.

The key is stored in `~/.config/kainguru/credentials.json`, or in `credentials.json` under
`$KAINGURU_HOME` or `$XDG_CONFIG_HOME/kainguru` when either is set, readable by you alone. The CLI
refuses to read the file if others can read it, and never prints the key or a signed link.

| Environment | Effect |
|---|---|
| `KAINGURU_API_KEY` | used instead of the stored key, so CI and servers have nothing to log in to |
| `KAINGURU_API` | used instead of the stored address, and as the default of `login --api` |

The address is `https://api.kainguru.com/api` unless `login --api URL` (or `KAINGURU_API`) says
otherwise. It must be `https://`, except for this machine, because the key is sent with every call.

`kainguru logout` deletes the stored key. The key itself works until you revoke it on your profile
page. Registering also needs the `MODEL_REGISTRAR` role on your account; without it the API
answers 403, and `push` says so.

## `kainguru push [DIR] --name NAME --category CATEGORY --banner IMAGE --readme README.md`

Registers the model, uploads the project's source and starts the platform's build of it (default
project: the current directory). It

1. checks the project as `build` does, and the options, before anything is sent;
2. packs the project: a gzipped tarball of the directory's contents, with `cog.yaml` at the top;
3. uploads the banner and the README as public files (anyone can read them), registers the model
   with them, asks the API for a signed link and uploads the tarball to it;
4. tells the API the source is there (`POST /v1/models/ID/build` with the tarball's SHA-256), which
   starts the build and prints `Build started`.

It then prints the model's id. The model starts `PENDING`, is `RUNNING` while the platform rebuilds
and tests it (`kainguru status` calls that `building`), and ends `REGISTERED` or `FAILED`;
`kainguru status ID --wait` follows it.

| Option | |
|---|---|
| `--name NAME` | upper-case letters, digits and single underscores, starting with a letter, at most 64 characters: `MY_MODEL_V2` |
| `--category CATEGORY` | the category's slug or name (`text-generation`), or its id |
| `--banner IMAGE` | a PNG, JPEG or WebP image of at most 20 MiB, told by its content |
| `--readme README.md` | the model's page in the catalog, markdown, at most 20 MiB |
| `--description TEXT` | one line, at most 255 characters |
| `--private` | only you can see and run the model (otherwise public once it is registered) |
| `--tag TAG` | a catalog tag; repeat for more |
| `--gpu-class CLASS` | required when `cog.yaml` has `build.gpu: true`: `mig1g`, `mig2g` or `mig3g` (an A100 slice of 10, 20 or 40 GB). A CPU model needs none |
| `--resume ID` | see below |

`--banner` and `--readme` are paths from the current directory. A name in use cannot be registered
again (a `FAILED` model gives its name back), and only one registration can be in progress, `PENDING`
or `RUNNING`, at a time.

**What is packed.** Everything in the project directory except `.git` and `__pycache__` at any
depth; `.cog`, `.kgctx` and `Dockerfile.kainguru` at the top, which the build writes itself; any
directory that holds a `pyvenv.cfg`, which is a virtualenv; and what `.kainguruignore` lists. A
symlink is packed as a symlink, and refused if it points outside the project. The packed source
may be at most 100 MiB, so keep weights out of the project and download them in `setup()`. Everything
else in the directory is uploaded, `.env` files and keys included: list those in `.kainguruignore`.

`.kainguruignore` sits in the project directory, one pattern per line, `#` for a comment:

```
*.ckpt          a name, at any depth
weights/        a directory, at any depth
/notes.txt      a path from the project's top: a pattern with a / in it, other than at its end
docs/**/*.pdf   ** stands for any number of directories
```

`*`, `?` and `[abc]` match within one name and never across a `/`. `!` patterns are not supported.
Only this file decides what is uploaded (`.dockerignore` still applies to the image build). `push`
refuses a `.kainguruignore` that leaves out `cog.yaml`, the predictor file or the requirements file.

**If the push fails after the model was registered** (a dropped connection, storage refusing the
file, the platform unable to start the build), the model exists, and `push` says whether it waits for
its source or has it but no build. Run the same command with `--resume ID`: it uploads the source
again, starts the build and registers nothing. When the platform cannot start builds, the API says so
in its own words and the model keeps its status: a `503` `build dispatch is not configured` means the
platform has no way to start builds yet (nothing you can fix), and a `502` that its build service
refused or could not be reached (try again in a minute). The source you uploaded is kept in both cases.

The same command builds a model whose build `FAILED`, once you have fixed the project: `--resume ID`
uploads the new source and starts the build again. A failed model gave its name back, so it takes the
name again, and is refused if someone registered it meanwhile or if you have another registration in progress.

## `kainguru status ID [--wait]` and `kainguru models`

`status` shows a model's status: `PENDING`, `building` (`RUNNING` in the API), then `REGISTERED` or
`FAILED`. With `--wait` it prints each change until the model is registered, and exits 1 if it ends
any other way. When the
model fails, the reason is shown if the API gives one. `models` lists your own models, newest first,
in every status.

An answer of 401 means the API did not accept the key (run `kainguru login`), 403 that the account
is not a `MODEL_REGISTRAR` yet, 404 that there is no such model or category; any other refusal is
printed as the API words it.

## Development

```
pip install -e './kg_cli[dev]'
cd kg_cli && pytest                             # unit tests, no Docker; the API commands run against a fake API on localhost
KAINGURU_DOCKER_TESTS=1 pytest -k hello_world   # builds and tests cog_models/hello-world
```
