来源:examples/cog
示例:Cog + SkyPilot#
使用 SkyPilot 自助托管任何 Cog 打包的项目。
这是来自 replicate/cog-examples 的“Blur”示例。
使用单个实例进行服务#
sky launch -c cog ./sky.yaml
IP=$(sky status --ip cog)
curl http://$IP:5000/predictions -X POST \
-H 'Content-Type: application/json' \
-d '{"input": {"image": "https://blog.skypilot.org.cn/introducing-sky-serve/images/sky-serve-thumbnail.png"}}' \
| jq -r '.output | split(",")[1]' | base64 --decode > output.png
使用 SkyServe 扩展部署#
我们可以使用 SkyServe (sky serve
) 将部署扩展到多个实例,同时享受负载均衡、自动扩缩容以及其他 SkyServe 功能。
sky serve up -n cog ./sky.yaml
请注意,唯一的更改是从 sky launch
变为 sky serve up
。可以使用相同的 YAML 文件,无需更改。
服务启动后,使用以下方式访问部署
ENDPOINT=$(sky serve status --endpoint cog)
curl http://$ENDPOINT/predictions -X POST \
-H 'Content-Type: application/json' \
-d '{"input": {"image": "https://blog.skypilot.org.cn/introducing-sky-serve/images/sky-serve-thumbnail.png"}}' \
| jq -r '.output | split(",")[1]' | base64 --decode > output.png
包含的文件#
cog.yaml
build:
python_version: "3.8"
python_packages:
- "pillow==8.2.0"
system_packages:
- "libpng-dev"
- "libjpeg-dev"
predict: "predict.py:Predictor"
import tempfile
import cog
from PIL import Image
from PIL import ImageFilter
class Predictor(cog.BasePredictor):
def predict(
self,
image: cog.Path = cog.Input(description='Input image'),
blur: float = cog.Input(description='Blur radius', default=5),
) -> cog.Path:
if blur == 0:
return input
im = Image.open(str(image))
im = im.filter(ImageFilter.BoxBlur(blur))
out_path = cog.Path(tempfile.mkdtemp()) / 'out.png'
im.save(str(out_path))
return out_path
sky.yaml
# Example: Cog + SkyPilot.
#
# This is the "Blur" example from https://github.com/replicate/cog-examples/blob/main/blur/README.md
#
# Usage (1 serving instance):
#
# sky launch -c cog ./sky.yaml
#
# IP=$(sky status --ip cog)
# curl http://$IP:5000/predictions -X POST \
# -H 'Content-Type: application/json' \
# -d '{"input": {"image": "https://blog.skypilot.org.cn/introducing-sky-serve/images/sky-serve-thumbnail.png"}}' \
# | jq -r '.output | split(",")[1]' | base64 --decode > output.png
#
# Usage (SkyServe): See README.md
service:
readiness_probe:
path: /predictions
post_data:
input: {"image": "https://blog.skypilot.org.cn/introducing-sky-serve/images/sky-serve-thumbnail.png"}
replicas: 2
resources:
accelerators: {L4, T4, A10G}
ports:
- 5000
workdir: .
setup: |
set -e
sudo curl -o /usr/local/bin/cog -L "https://github.com/replicate/cog/releases/latest/download/cog_$(uname -s)_$(uname -m)"
sudo chmod +x /usr/local/bin/cog
cog build -t my-model
run: |
docker run -d -p 5000:5000 --gpus all my-model