Skip to main content

cog-huggingface-template

Tools to help deploy Hugging Face models to replicate.

Requirements

  • GPU environment
  • Cog

Installation

PyPI Install

pip install cog_hf_template

Dev Install

Clone the repo:

git clone https://github.com/replicate/cog-huggingface-template
cd cog-huggingface-template
pip install -e .

Run tests

pip install pytest
pytest -vv tests

Usage

NOTE (Oct 19) - examples are out of date. Will update soon.

For now, see the examples directory for a working example.

Mirroring Weights from HF to Replicate Storage

Here's a quick tip on how to mirror weights from Hugging Face to GCP

You can use a minimal install of this package to avoid the extra dependencies...

# required deps for mirroring weights/CLI
pip install --no-cache huggingface-hub==0.18.0 google-cloud-storage==2.12.0 hf-transfer==0.1.3 fire==0.5.0

# Minimal version of the package
pip install --no-deps --no-cache --upgrade cog-hf-template

You will also need pget installed

curl -o /usr/local/bin/pget -L "https://github.com/replicate/pget/releases/download/v0.0.2/pget" && chmod +x /usr/local/bin/pget

Then you can run the following command to mirror the weights:

cog-hf-template mirror-weights \
    openai/whisper-large-v3 \
    your-bucket \
    --gcp-project-name your-project \
    --config_save_path whisper_large_v3_config.json

See cog-hf-template mirror-weights --help for more details.

In the example above, we mirror openai/whisper-large-v3 to the bucket your-bucket in the project your-project. We also save the model config to whisper_large_v3_config.json so that we can use it later to load the model (as you'll need to know the files to download).

The config looks like this:

{
  "hf_model_id": "openai/whisper-large-v3",
  "task": "automatic-speech-recognition",
  "gcp_bucket_weights": "gs://your-bucket/openai/whisper-large-v3/1940b900b8d4d8fdf72a31be85a0d223b9488e00",
  "trust_remote_code": true,
  "remote_filenames": [
    ".gitattributes",
    "README.md",
    "added_tokens.json",
    "config.json",
    "flax_model.msgpack",
    "generation_config.json",
    "merges.txt",
    "model.fp32-00001-of-00002.safetensors",
    "model.fp32-00002-of-00002.safetensors",
    "model.safetensors",
    "model.safetensors.index.fp32.json",
    "preprocessor_config.json",
    "pytorch_model.bin",
    "pytorch_model.bin.index.fp32.json",
    "pytorch_model.fp32-00001-of-00002.bin",
    "pytorch_model.fp32-00002-of-00002.bin",
    "special_tokens_map.json",
    "tokenizer_config.json",
    "vocab.json"
  ]
}

Note the files are accessible through the delivery URLs, and will the gs://your-bucket prefix will be replaced by the appropriate prefix before being downloaded.

import json
from pathlib import Path

from cog_hf_template.download_utils import maybe_pget_weights

config = json.loads(Path("whisper_large_v3_config.json").read_text())
maybe_pget_weights(
    path="whisper_large_v3",
    remote_path=config["gcp_bucket_weights"],
    remote_filenames=config["remote_filenames"],
)

Metadata

Release files for cog-hf-template 0.0.13

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cog-hf-template 0.0.13
File Size Uploaded
cog_hf_template-0.0.13.tar.gz 17.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cog-hf-template 0.0.13
File Interpreter ABI Platform
cog_hf_template-0.0.13-py3-none-any.whl Python 3 none any Details

Total release size: 34.0 kB

Release files / cog_hf_template-0.0.13.tar.gz

Download URL cog_hf_template-0.0.13.tar.gz
Size 17.2 kB
Tags Source
SHA-256 checksum
How to use checksums
9de1fb914f654de852a8e784ec03900685f2cb005c36178ebb0706d1f68a7037
BLAKE2b-256 checksum
How to use checksums
a4142bf7008096a0c556c84184c0b2ff7e3e8cfd25ec377c6d19de3b1a884101
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.12.1

Release files / cog_hf_template-0.0.13-py3-none-any.whl

Download URL cog_hf_template-0.0.13-py3-none-any.whl
Size 16.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4ed417c3beaa17550c2c83b953c040412806ffd14d2630823b8855887691654c
BLAKE2b-256 checksum
How to use checksums
319b73c0a178494817ed940694fa71867dd8f2106be6b050747fc7a2a8726899
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.12.1

Release history Release notifications | RSS feed

This release

0.0.13 This release

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page