第 1 章 创建模型镜像并将其推送到容器镜像 registry
您可以创建一个 modelcar 镜像,其中包含您可以使用 Red Hat AI Inference Server 部署的语言模型。
要创建 modelcar 镜像,请从 Hugging Face 下载模型,然后将其打包到容器镜像中,并将 modelcar 容器推送到镜像 registry。
先决条件
- 已安装 Python 3.11 或更高版本。
- 已安装 Podman 或 Docker。
- 您可以访问互联网来从 Hugging Face 下载模型。
- 您已配置了可将镜像推送到并登录的容器镜像 registry。
流程
创建 Python 虚拟环境并安装
huggingface_hubPython 库:python3 -m venv venv && \ source venv/bin/activate && \ pip install --upgrade pip && \ pip install huggingface_hub创建模型下载程序 Python 脚本:
vi download_model.py在
download_model.py文件中添加以下内容,根据需要调整model_repo的值:from huggingface_hub import snapshot_download # Specify the Hugging Face repository containing the model model_repo = "ibm-granite/granite-3.1-2b-instruct" snapshot_download( repo_id=model_repo, local_dir="/models", allow_patterns=["*.safetensors", "*.json", "*.txt"], )为 modelcar 创建
Dockerfile:FROM registry.access.redhat.com/ubi9/python-311:latest as base USER root RUN pip install huggingface-hub # Download the model file from Hugging Face COPY download_model.py . RUN python download_model.py # Final image containing only the essential model files FROM registry.access.redhat.com/ubi9/ubi-micro:9.4 # Copy the model files from the base container COPY --from=base /models /models USER 1001构建 modelcar 镜像:
podman build . -t modelcar-example:latest --platform linux/amd64输出示例
Successfully tagged localhost/modelcar-example:latest将 modelcar 镜像推送到容器 registry。例如:
$ podman push modelcar-example:latest quay.io/<your_model_registry>/modelcar-example:latest输出示例
Getting image source signatures Copying blob b2ed7134f853 done Copying config 4afd393610 done Writing manifest to image destination Storing signatures