-
Notifications
You must be signed in to change notification settings - Fork 694
Expand file tree
/
Copy pathllms.txt
More file actions
3745 lines (2632 loc) · 124 KB
/
Copy pathllms.txt
File metadata and controls
3745 lines (2632 loc) · 124 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Cog: Containers for machine learning
Cog is an open-source tool that lets you package machine learning models in a standard, production-ready container.
You can deploy your packaged model to your own infrastructure, or to [Replicate](https://replicate.com/).
## Highlights
- 📦 **Docker containers without the pain.** Writing your own `Dockerfile` can be a bewildering process. With Cog, you define your environment with a [simple configuration file](#how-it-works) and it generates a Docker image with all the best practices: Nvidia base images, efficient caching of dependencies, installing specific Python versions, sensible environment variable defaults, and so on.
- 🤬️ **No more CUDA hell.** Cog knows which CUDA/cuDNN/PyTorch/Tensorflow/Python combos are compatible and will set it all up correctly for you.
- ✅ **Define the inputs and outputs for your model with standard Python.** Then, Cog generates an OpenAPI schema and validates the inputs and outputs.
- 🎁 **Automatic HTTP inference server**: Your model's types are used to dynamically generate a RESTful HTTP API using a high-performance Rust/Axum server.
- 🚀 **Ready for production.** Deploy your model anywhere that Docker images run. Your own infrastructure, or [Replicate](https://replicate.com).
## How it works
Define the Docker environment your model runs in with `cog.yaml`:
```yaml
build:
gpu: true
system_packages:
- "libgl1"
- "libglib2.0-0"
python_version: "3.13"
python_requirements: requirements.txt
run: "run.py:Runner"
```
Define how your model runs with `run.py`:
```python
from cog import BaseRunner, Input, Path
import torch
class Runner(BaseRunner):
def setup(self):
"""Load the model into memory to make running multiple inferences efficient"""
self.model = torch.load("./weights.pth")
# The arguments and types the model takes as input
def run(self,
image: Path = Input(description="Grayscale input image")
) -> Path:
"""Run the model"""
processed_image = preprocess(image)
output = self.model(processed_image)
return postprocess(output)
```
In the above we accept a path to the image as an input, and return a path to our transformed image after running it through our model.
Now, you can run the model:
```console
$ cog run -i image=@input.jpg
--> Building Docker image...
--> Running...
--> Output written to output.jpg
```
Or, build a Docker image for deployment:
```console
$ cog build -t my-classification-model
--> Building Docker image...
--> Built my-classification-model:latest
$ docker run -d -p 5000:5000 --gpus all my-classification-model
$ curl http://localhost:5000/predictions -X POST \
-H 'Content-Type: application/json' \
-d '{"input": {"image": "https://.../input.jpg"}}'
```
Or, combine build and run via the `serve` command:
```console
$ cog serve -p 8080
$ curl http://localhost:8080/predictions -X POST \
-H 'Content-Type: application/json' \
-d '{"input": {"image": "https://.../input.jpg"}}'
```
<!-- NOTE (bfirsh): Development environment instructions intentionally left out of readme for now, so as not to confuse the "ship a model to production" message.
In development, you can also run arbitrary commands inside the Docker environment:
```console
$ cog exec python train.py
...
```
Or, [spin up a Jupyter notebook](docs/notebooks.md):
```console
$ cog exec -p 8888 jupyter notebook --allow-root --ip=0.0.0.0
```
-->
## Why are we building this?
It's really hard for researchers to ship machine learning models to production.
Part of the solution is Docker, but it is so complex to get it to work: Dockerfiles, pre-/post-processing, Flask servers, CUDA versions. More often than not the researcher has to sit down with an engineer to get the damn thing deployed.
[Andreas](https://github.com/andreasjansson) and [Ben](https://github.com/bfirsh) created Cog. Andreas used to work at Spotify, where he built tools for building and deploying ML models with Docker. Ben worked at Docker, where he created [Docker Compose](https://github.com/docker/compose).
We realized that, in addition to Spotify, other companies were also using Docker to build and deploy machine learning models. [Uber](https://eng.uber.com/michelangelo-pyml/) and others have built similar systems. So, we're making an open source version so other people can do this too.
Hit us up if you're interested in using it or want to collaborate with us. [We're on Discord](https://discord.gg/replicate) or email us at [team@replicate.com](mailto:team@replicate.com).
## Prerequisites
- **macOS, Linux or Windows 11**. Cog works on macOS, Linux and Windows 11 with [WSL 2](docs/wsl2/wsl2.md)
- **Docker**. Cog uses Docker to create a container for your model. You'll need to [install Docker](https://docs.docker.com/get-docker/) before you can run Cog. If you install Docker Engine instead of Docker Desktop, you will need to [install Buildx](https://docs.docker.com/build/architecture/#buildx) as well.
## Install
Choose your platform for installation instructions.
<details>
<summary>macOS</summary>
The easiest way to install Cog on macOS is with Homebrew:
```console
brew install replicate/tap/cog
```
You can also use the install script:
```sh
# bash, zsh, and other shells
sh <(curl -fsSL https://cog.run/install.sh)
# fish shell
sh (curl -fsSL https://cog.run/install.sh | psub)
# download with wget and run in a separate command
wget -qO- https://cog.run/install.sh
sh ./install.sh
```
Or install manually:
```console
sudo curl -o /usr/local/bin/cog -L "https://github.com/replicate/cog/releases/latest/download/cog_$(uname -s)_$(uname -m | sed 's/aarch64/arm64/')"
sudo chmod +x /usr/local/bin/cog
sudo xattr -d com.apple.quarantine /usr/local/bin/cog 2>/dev/null || true
```
If you see a Gatekeeper warning saying the binary "cannot be opened because the developer cannot be verified", run:
```console
sudo xattr -d com.apple.quarantine /usr/local/bin/cog
```
</details>
<details>
<summary>Linux</summary>
You can install Cog using the install script:
```sh
# bash, zsh, and other shells
sh <(curl -fsSL https://cog.run/install.sh)
# fish shell
sh (curl -fsSL https://cog.run/install.sh | psub)
# download with wget and run in a separate command
wget -qO- https://cog.run/install.sh
sh ./install.sh
```
Or install manually:
```console
sudo curl -o /usr/local/bin/cog -L "https://github.com/replicate/cog/releases/latest/download/cog_$(uname -s)_$(uname -m | sed 's/aarch64/arm64/')"
sudo chmod +x /usr/local/bin/cog
```
</details>
<details markdown>
<summary>Windows</summary>
Cog does not natively support Windows, but you can run it on Windows 11 using [WSL 2](docs/wsl2/wsl2.md). Once WSL 2 is set up, follow the Linux installation instructions above.
</details>
<details>
<summary>Docker</summary>
To install Cog inside a Docker image:
```dockerfile
RUN sh -c "INSTALL_DIR=\"/usr/local/bin\" SUDO=\"\" $(curl -fsSL https://cog.run/install.sh)"
```
</details>
## Upgrade
If you're using macOS and you previously installed Cog with Homebrew, run the following:
```console
brew upgrade replicate/tap/cog
```
Otherwise, you can upgrade to the latest version by running the same commands you used to install it.
## Development
See [CONTRIBUTING.md](CONTRIBUTING.md) for how to set up a development environment and build from source.
## Next steps
- [Get started with an example model](docs/getting-started.md)
- [Get started with your own model](docs/getting-started-own-model.md)
- [Using Cog with notebooks](docs/notebooks.md)
- [Using Cog with Windows 11](docs/wsl2/wsl2.md)
- [Browse the example models in this repo](docs/examples.md)
- [Deploy models with Cog](docs/deploy.md)
- [`cog.yaml` reference](docs/yaml.md) to learn how to define your model's environment
- [Run interface reference](docs/python.md) to learn how the `Runner` interface works
- [Training interface reference](docs/training.md) to learn how to add a fine-tuning API to your model
- [HTTP API reference](docs/http.md) to learn how to use the HTTP API that models serve
## Need help?
[Join us in #cog on Discord.](https://discord.gg/replicate)
[](https://deepwiki.com/replicate/cog)
---
# CLI reference
<!-- This file is auto-generated. Do not edit manually. -->
## `cog`
Containers for machine learning.
To get started, take a look at the documentation:
https://github.com/replicate/cog
**Examples**
```
To execute a command inside a Docker environment defined with Cog:
$ cog exec echo hello world
```
**Options**
```
--debug Show debugging output
-h, --help help for cog
--no-color Disable colored output
--version Show version of Cog
```
## `cog build`
Build a Docker image from the cog.yaml in the current directory.
The generated image contains your model code, dependencies, and the Cog
runtime. It can be run locally with 'cog run' or pushed to a registry
with 'cog push'.
```
cog build [flags]
```
**Examples**
```
# Build with default settings
cog build
# Build and tag the image
cog build -t my-model:latest
# Build without using the cache
cog build --no-cache
# Build with model weights in a separate layer
cog build --separate-weights -t my-model:v1
```
**Options**
```
-f, --file string The name of the config file. (default "cog.yaml")
-h, --help help for build
--no-cache Do not use cache when building the image
--openapi-schema string Load OpenAPI schema from a file
--progress string Set type of build progress output, 'auto' (default), 'tty', 'plain', or 'quiet' (default "auto")
--secret stringArray Secrets to pass to the build environment in the form 'id=foo,src=/path/to/file'
--separate-weights Separate model weights from code in image layers
-t, --tag string A name for the built image in the form 'repository:tag'
--use-cog-base-image Use pre-built Cog base image for faster cold boots (default true)
--use-cuda-base-image string Use Nvidia CUDA base image, 'true' (default) or 'false' (use python base image). False results in a smaller image but may cause problems for non-torch projects (default "auto")
```
## `cog doctor`
Diagnose and fix common issues in your Cog project.
NOTE: cog doctor is experimental. Behavior and checks may change in future versions.
By default, cog doctor reports problems without modifying any files.
Pass --fix to automatically apply safe fixes.
```
cog doctor [flags]
```
**Options**
```
-f, --file string The name of the config file. (default "cog.yaml")
--fix Automatically apply fixes
-h, --help help for doctor
```
## `cog exec`
Execute a command inside a Docker environment defined by cog.yaml.
Cog builds a temporary image from your cog.yaml configuration and runs the
given command inside it. This is useful for debugging, running scripts, or
exploring the environment your model will run in.
```
cog exec <command> [arg...] [flags]
```
**Examples**
```
# Open a Python interpreter inside the model environment
cog exec python
# Run a script
cog exec python train.py
# Run with environment variables
cog exec -e HUGGING_FACE_HUB_TOKEN=abc123 python download.py
# Expose a port (e.g. for Jupyter)
cog exec -p 8888 jupyter notebook
```
**Options**
```
-e, --env stringArray Environment variables, in the form name=value
-f, --file string The name of the config file. (default "cog.yaml")
--gpus docker run --gpus GPU devices to add to the container, in the same format as docker run --gpus.
-h, --help help for exec
--progress string Set type of build progress output, 'auto' (default), 'tty', 'plain', or 'quiet' (default "auto")
-p, --publish stringArray Publish a container's port to the host, e.g. -p 8000
--use-cog-base-image Use pre-built Cog base image for faster cold boots (default true)
--use-cuda-base-image string Use Nvidia CUDA base image, 'true' (default) or 'false' (use python base image). False results in a smaller image but may cause problems for non-torch projects (default "auto")
```
## `cog init`
Create a cog.yaml and run.py in the current directory.
These files provide a starting template for defining your model's environment
and run interface. Edit them to match your model's requirements.
```
cog init [flags]
```
**Examples**
```
# Set up a new Cog project in the current directory
cog init
```
**Options**
```
-h, --help help for init
```
## `cog login`
Log in to a container registry.
For Replicate's registry (r8.im), this command handles authentication
through Replicate's token-based flow.
For other registries, this command prompts for username and password,
then stores credentials using Docker's credential system.
```
cog login [flags]
```
**Options**
```
-h, --help help for login
--token-stdin Pass login token on stdin instead of opening a browser. You can find your Replicate login token at https://replicate.com/auth/token
```
## `cog playground`
Open a browser playground for talking to a running model.
Starts a local web server that serves a schema-driven UI (a Postman-like tool
for Cog models). Point it at any running Cog HTTP API -- for example one started
with 'cog serve' -- and the playground reflects that model's inputs and outputs
from its OpenAPI schema in real time.
Requests are reverse-proxied through this server, so the target API does not
need to set CORS headers. The server also hosts a webhook sink so async
predictions can be observed in the browser.
Async/webhook testing against a containerized model requires the webhook URL to
be reachable from inside the container. On Docker Desktop the default
'host.docker.internal' works once the server listens on a reachable interface
(e.g. --host 0.0.0.0).
```
cog playground [flags]
```
**Examples**
```
# Start a model API in one terminal
cog serve -p 8393
# Open the playground pointing at it
cog playground --target http://localhost:8393
```
**Options**
```
-h, --help help for playground
--host string Address to bind (use 0.0.0.0 to receive webhooks from containers) (default "127.0.0.1")
--no-open Do not open the browser automatically
-p, --port int Port to listen on (0 picks a free port)
--target string Default target model API URL (default "http://localhost:8393")
--webhook-host string Hostname the model uses to reach this server for webhooks (default "host.docker.internal")
```
## `cog push`
Build a Docker image from cog.yaml and push it to a container registry.
Cog can push to any OCI-compliant registry. When pushing to Replicate's
registry (r8.im), run 'cog login' first to authenticate.
```
cog push [IMAGE] [flags]
```
**Examples**
```
# Push to Replicate
cog push r8.im/your-username/my-model
# Push to any OCI registry
cog push registry.example.com/your-username/model-name
# Push with model weights in a separate layer (Replicate only)
cog push r8.im/your-username/my-model --separate-weights
```
**Options**
```
-f, --file string The name of the config file. (default "cog.yaml")
-h, --help help for push
--no-cache Do not use cache when building the image
--openapi-schema string Load OpenAPI schema from a file
--progress string Set type of build progress output, 'auto' (default), 'tty', 'plain', or 'quiet' (default "auto")
--secret stringArray Secrets to pass to the build environment in the form 'id=foo,src=/path/to/file'
--separate-weights Separate model weights from code in image layers
--use-cog-base-image Use pre-built Cog base image for faster cold boots (default true)
--use-cuda-base-image string Use Nvidia CUDA base image, 'true' (default) or 'false' (use python base image). False results in a smaller image but may cause problems for non-torch projects (default "auto")
```
## `cog run`
Run the model.
If 'image' is passed, it will run the model on that Docker image.
It must be an image that has been built by Cog.
Otherwise, it will build the model in the current directory and run
it.
```
cog run [image] [flags]
```
**Examples**
```
# Run the model with named inputs
cog run -i prompt="a photo of a cat"
# Pass a file as input
cog run -i image=@photo.jpg
# Save output to a file
cog run -i image=@input.jpg -o output.png
# Pass multiple inputs
cog run -i prompt="sunset" -i width=1024 -i height=768
# Run against a pre-built image
cog run r8.im/your-username/my-model -i prompt="hello"
# Pass inputs as JSON
echo '{"prompt": "a cat"}' | cog run --json @-
```
**Options**
```
-e, --env stringArray Environment variables, in the form name=value
-f, --file string The name of the config file. (default "cog.yaml")
--gpus docker run --gpus GPU devices to add to the container, in the same format as docker run --gpus.
-h, --help help for run
-i, --input stringArray Inputs, in the form name=value. if value is prefixed with @, then it is read from a file on disk. E.g. -i path=@image.jpg
--json string Pass inputs as JSON object, read from file (@inputs.json) or via stdin (@-)
-o, --output string Output path
--progress string Set type of build progress output, 'auto' (default), 'tty', 'plain', or 'quiet' (default "auto")
--setup-timeout uint32 The timeout for a container to setup (in seconds). (default 300)
--use-cog-base-image Use pre-built Cog base image for faster cold boots (default true)
--use-cuda-base-image string Use Nvidia CUDA base image, 'true' (default) or 'false' (use python base image). False results in a smaller image but may cause problems for non-torch projects (default "auto")
--use-replicate-token Pass REPLICATE_API_TOKEN from local environment into the model context
```
## `cog serve`
Run an HTTP server.
Builds the model and starts an HTTP server that exposes the model's inputs
and outputs as a REST API. Compatible with the Cog HTTP protocol.
```
cog serve [flags]
```
**Examples**
```
# Start the server on the default port (8393)
cog serve
# Start on a custom port
cog serve -p 5000
# Test the server
curl http://localhost:8393/predictions \
-X POST \
-H 'Content-Type: application/json' \
-d '{"input": {"prompt": "a cat"}}'
```
**Options**
```
-f, --file string The name of the config file. (default "cog.yaml")
--gpus docker run --gpus GPU devices to add to the container, in the same format as docker run --gpus.
-h, --help help for serve
-p, --port int Port on which to listen (default 8393)
--progress string Set type of build progress output, 'auto' (default), 'tty', 'plain', or 'quiet' (default "auto")
--upload-url string Upload URL for file outputs (e.g. https://example.com/upload/)
--use-cog-base-image Use pre-built Cog base image for faster cold boots (default true)
--use-cuda-base-image string Use Nvidia CUDA base image, 'true' (default) or 'false' (use python base image). False results in a smaller image but may cause problems for non-torch projects (default "auto")
```
---
# Deploy models with Cog
Cog containers are Docker containers that serve an HTTP server
for running your model.
You can deploy them anywhere that Docker containers run.
The server inside Cog containers is **coglet**, a Rust-based inference server
that handles HTTP requests, worker process management, and run execution.
This guide assumes you have a model packaged with Cog.
If you don't, [follow our getting started guide](getting-started-own-model.md),
or start from one of the [examples in the Cog repository](examples.md).
## Build a Docker image
Build your model into a Docker image:
```console
cog build -t my-model
```
The image contains your model code, dependencies, the Cog runtime, and everything in between.
It serves an HTTP server on port 5000 when run.
## Run the model
You have several options for running a built image.
### Docker
Run the image directly with Docker.
This is the approach you'd use for production deployment.
```shell
# If your model uses a CPU:
docker run -d -p 5001:5000 my-model
# If your model uses a GPU:
docker run -d -p 5001:5000 --gpus all my-model
```
The server listens on port 5000 inside the container (mapped to 5001 above).
### cog serve
For local development, `cog serve` builds the image and starts the server
with your project directory mounted in:
```console
cog serve
```
By default the server runs on port 8393.
Use `-p` to choose a different port:
```console
cog serve -p 5000
```
## Make a prediction
Once the server is running, make predictions by sending a POST request
to the `/predictions` endpoint.
Inputs go inside an `"input"` object in the JSON body.
> [!NOTE]
> The examples below use `localhost:5001`, matching the Docker command above
> (`-p 5001:5000`). If you used `cog serve`, use `localhost:8393` by default,
> or the port you passed with `-p`.
```console
curl http://localhost:5001/predictions -X POST \
-H "Content-Type: application/json" \
-d '{"input": {"prompt": "a photo of a cat", "steps": 50}}'
```
```json
{
"status": "succeeded",
"output": "data:image/png;base64,...",
"metrics": {
"predict_time": 4.52
}
}
```
> [!IMPORTANT]
> Inputs **must** be wrapped in an `"input"` object.
> `{"input": {"scale": 2.0}}` is correct; `{"scale": 2.0}` is not.
To discover what inputs your model accepts,
view the OpenAPI schema:
```console
curl http://localhost:5001/openapi.json
```
### Passing file inputs
File inputs (`cog.Path` or `cog.File` types) are passed as strings
inside the `"input"` object.
There are two ways to do this:
**1. HTTP/HTTPS URLs**
Pass a URL to a publicly accessible file.
The server downloads it inside the container:
```console
curl http://localhost:5001/predictions -X POST \
-H "Content-Type: application/json" \
-d '{"input": {"image": "https://example.com/photo.jpg"}}'
```
**2. Data URLs (base64)**
To pass a local file, encode it as a [data URL](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URLs):
```bash
# Construct a data URL from a local file
DATA_URL=$(python3 -c "
import base64, mimetypes
with open('input.jpg', 'rb') as f:
data = base64.b64encode(f.read()).decode()
mime = mimetypes.guess_type('input.jpg')[0] or 'application/octet-stream'
print(f'data:{mime};base64,{data}')
")
curl http://localhost:5001/predictions -X POST \
-H "Content-Type: application/json" \
-d "{\"input\": {\"image\": \"$DATA_URL\"}}"
```
> [!NOTE]
> The HTTP API only accepts JSON (`application/json`).
> Multipart form uploads are not supported.
> When you use `cog run -i image=@photo.jpg`,
> the CLI handles the base64 encoding for you automatically.
### Getting output files
When a model returns a file output (`cog.Path` or `cog.File`),
the response contains a base64-encoded data URL by default:
```json
{
"status": "succeeded",
"output": "data:image/png;base64,iVBORw0KGgo..."
}
```
To have the server upload output files to external storage instead,
start the server with the `--upload-url` flag. The server then uploads each
file output to that URL prefix and returns the resulting URL in the response.
With `cog serve`:
```console
cog serve --upload-url https://example.com/upload/
```
When running the image directly with Docker, override the command to start the
server with `--upload-url`:
```shell
docker run -d -p 5001:5000 my-model \
python -m cog.server.http --upload-url https://example.com/upload/
```
With an upload URL configured, file outputs are uploaded and the response
contains the uploaded URL instead of a data URL:
```json
{
"status": "succeeded",
"output": "https://example.com/upload/image.png"
}
```
## Run a one-off prediction
The Docker and `cog serve` commands above leave an HTTP server running.
If you instead want to run a single prediction and exit — without starting a
server — use `cog run`:
```console
cog run my-model -i image=@input.jpg
```
This starts the container, runs one prediction, prints the result, and exits.
File inputs are passed with the `@` prefix (e.g. `-i image=@photo.jpg`),
and the CLI handles base64 encoding for you.
## Health checks
The server exposes a `GET /health-check` endpoint that returns the current status of the model container. Use this for readiness probes in orchestration systems like Kubernetes.
```console
curl http://localhost:5001/health-check
```
The response includes a `status` field with values like `STARTING`, `READY`, `BUSY`, `SETUP_FAILED`, or `DEFUNCT`. See the [HTTP API reference](http.md#get-health-check) for full details.
## Stop the server
If you started the container with `docker run -d`, stop it with:
```console
docker kill <container-id>
```
If you used `cog serve`, press `Ctrl+C` in the terminal.
(`cog run` exits on its own once the prediction finishes, so there's nothing
to stop.)
## Concurrency
By default, the server processes one run at a time. To enable concurrent runs, set the `concurrency.max` option in `cog.yaml`:
```yaml
concurrency:
max: 4
```
See the [`cog.yaml` reference](yaml.md#concurrency) for more details.
## Environment variables
You can configure runtime behavior with environment variables:
- `COG_SETUP_TIMEOUT`: Maximum time in seconds for the `setup()` method (default: no timeout).
- `COG_MAX_CONCURRENCY`: Number of concurrent prediction slots (default: 1).
See the [environment variables reference](environment.md) for the full list.
## Next steps
- [HTTP API reference](http.md) for full endpoint documentation
- [Private registries](private-package-registry.md) for using private Python package registries
- [`cog.yaml` reference](yaml.md) for configuration options
---
# Environment variables
This reference lists the public Cog-specific environment variables that change how Cog behaves.
## Build-time variables
### `COG_SDK_WHEEL`
Controls which Cog Python SDK wheel is installed in the Docker image during `cog build`. Takes precedence over `build.sdk_version` in `cog.yaml`.
**Supported values:**
| Value | Description |
| -------------------- | ---------------------------------------------------- |
| `pypi` | Install latest version from PyPI |
| `pypi:0.12.0` | Install specific version from PyPI |
| `dist` | Use wheel from `dist/` directory (requires git repo) |
| `https://...` | Install from URL |
| `/path/to/wheel.whl` | Install from local file path |
**Default behaviour:**
- Release builds install the latest Cog SDK from PyPI.
- Development builds auto-detect a wheel in `dist/`, then fall back to the latest Cog SDK from PyPI.
```console
$ COG_SDK_WHEEL=pypi:0.11.0 cog build
$ COG_SDK_WHEEL=dist cog build
$ COG_SDK_WHEEL=https://example.com/cog-0.12.0-py3-none-any.whl cog build
```
The `dist` option searches for wheels in:
1. `./dist/` (current directory)
2. `$REPO_ROOT/dist/` (if `REPO_ROOT` is set)
3. `<git-repo-root>/dist/` (via `git rev-parse`, useful when running from subdirectories)
### `COGLET_WHEEL`
Controls which coglet wheel is installed in the Docker image. Coglet is the Rust-based inference server.
**Supported values:** Same as `COG_SDK_WHEEL`.
**Default behaviour:** For development builds, auto-detects a wheel in `dist/`. For release builds, installs the latest version from PyPI.
```console
$ COGLET_WHEEL=dist cog build
$ COGLET_WHEEL=pypi:0.1.0 cog build
```
### `COG_CA_CERT`
Injects a custom CA certificate into the Docker image during `cog build`. This is useful when building behind a corporate proxy or VPN that uses custom certificate authorities (for example, Cloudflare WARP).
**Supported values:**
| Value | Description |
| -------------------------------- | ----------------------------------------------------------- |
| `/path/to/cert.crt` | Path to a single PEM certificate file |
| `/path/to/certs/` | Directory of `.crt` and `.pem` files (all are concatenated) |
| `-----BEGIN CERTIFICATE-----...` | Inline PEM certificate |
| `LS0tLS1CRUdJTi...` | Base64-encoded PEM certificate |
The certificate is installed into the system CA store and the `SSL_CERT_FILE` and `REQUESTS_CA_BUNDLE` environment variables are set automatically in the built image.
```console
$ COG_CA_CERT=/usr/local/share/ca-certificates/corporate-ca.crt cog build
$ COG_CA_CERT=/etc/custom-certs/ cog build
$ COG_CA_CERT="$(cat /path/to/cert.pem)" cog build
```
### `COG_OPENAPI_SCHEMA`
Uses a pre-built OpenAPI schema instead of generating one from the configured predict or train reference.
The value must be a path to a JSON schema file. Cog reads that file during schema generation and embeds it in the built image.
```console
$ COG_OPENAPI_SCHEMA=./openapi.json cog build
```
## CLI and local cache variables
### `COG_NO_UPDATE_CHECK`
Disables Cog's automatic update check. Set it to any non-empty value.
```console
$ COG_NO_UPDATE_CHECK=1 cog build
```
### `COG_NO_COLOR`
Disables coloured CLI output. Set it to any non-empty value.
Cog also honours the standard `NO_COLOR` environment variable.
```console
$ COG_NO_COLOR=1 cog predict -i prompt="hello"
```
### `COG_SKIP_DOCKER_CHECK`
Skips the `cog doctor` Docker environment check. Set it to any non-empty value.
```console
$ COG_SKIP_DOCKER_CHECK=1 cog doctor
```
### `COG_CACHE_DIR`
Overrides Cog's local cache root.
Cog currently uses this cache for the content-addressed weights store. If unset, Cog uses `$XDG_CACHE_HOME/cog` when `XDG_CACHE_HOME` is set, otherwise `$HOME/.cache/cog`.
```console
$ COG_CACHE_DIR=/mnt/fast-cache cog weights pull
```
## Model reference and registry variables
### `COG_MODEL`
Overrides the full model reference used by commands that need a model destination, such as `cog push` and weights commands.
The value is parsed as a complete model reference (`registry/repo`, `registry/repo:tag`, or `registry/repo@digest`). If no tag is supplied, Cog generates a timestamp tag.
When `COG_MODEL` is set, it takes precedence over `COG_MODEL_REGISTRY`, `COG_MODEL_REPO`, and `COG_MODEL_TAG`.
```console
$ COG_MODEL=r8.im/acme/my-model:v1 cog push
```
### `COG_MODEL_REGISTRY`
Overrides only the registry host of the model reference.
```console
$ COG_MODEL_REGISTRY=registry.example.com cog push
```
### `COG_MODEL_REPO`
Overrides only the repository path of the model reference. The value must not include a registry host, tag, or digest.
```console
$ COG_MODEL_REPO=acme/my-model cog push
```
### `COG_MODEL_TAG`
Overrides only the tag of the model reference.
Tags starting with `cog-` are reserved for tags that Cog generates internally and are rejected.
```console