Skip to main content

Cog template for Hugging Face.

Project description

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 Replicate Storage on 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

You'll need to make sure you have access to the bucket. You can do this by running gcloud auth login and following the instructions, assuming you've been granted access to the weights-access@replicate.com group on GCP.

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

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

In the example above, we mirror openai/whisper-large-v3 to the bucket replicate-hf-weights in the project replicate-production. 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://replicate-hf-weights/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://replicate-hf-weights 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"],
)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cog_hf_template-0.0.12.tar.gz (16.7 kB view details)

Uploaded Source

Built Distribution

cog_hf_template-0.0.12-py3-none-any.whl (16.4 kB view details)

Uploaded Python 3

File details

Details for the file cog_hf_template-0.0.12.tar.gz.

File metadata

  • Download URL: cog_hf_template-0.0.12.tar.gz
  • Upload date:
  • Size: 16.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.0

File hashes

Hashes for cog_hf_template-0.0.12.tar.gz
Algorithm Hash digest
SHA256 95529636dd5df54856b3f885e3597e7509e90163a3923fda72af020c150b04dc
MD5 3896cc0311edc16ace71ad7f8cd64879
BLAKE2b-256 882c0b7e6b46c658979e7bb384a3969f72848a420f71212393704489441334ef

See more details on using hashes here.

File details

Details for the file cog_hf_template-0.0.12-py3-none-any.whl.

File metadata

File hashes

Hashes for cog_hf_template-0.0.12-py3-none-any.whl
Algorithm Hash digest
SHA256 69bc9415a987a12d04563e4e5a1a74cd014585581b7da413c61e331e8d7babab
MD5 e12f3bf91865f84329a7b7d3b87ec33d
BLAKE2b-256 5873cad703cbcc73fa5880ef66a4c870a93761b77039adb543323410c8d67a31

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page