Skip to main content

Cog template for Hugging Face.

Project description

cog-huggingface-template

Tools to help deploy Hugging Face models to replicate. For now, just text generation models.

Requirements

  • GPU environment
  • Cog

Setup

Clone the repo:

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

Local install:

pip install -e .

Usage

For now, see predict.py for a working example.

Optional - Use GCP for Weights

  1. install pget
sudo curl -o /usr/local/bin/pget -L "https://github.com/replicate/pget/releases/download/v0.0.2/pget" && sudo chmod +x /usr/local/bin/pget
  1. Run predict.py to download local weights cache
python predict.py

Inside it should be something like:

from cog_hf_template import get_predictor

Predictor = get_predictor(
    task="image-classification",
    model_name_or_path="nateraw/rare-puppers",
    cache_dir="./weights-cache",
)

if __name__ == "__main__":
    predictor = Predictor()
    predictor.setup()

.setup() will download the weights from huggingface to ./weights-cache.

  1. Tar up weights cache

Now that you have the weights locally, you need to tar them up before pushing to GCP. Note that you have to tar from the weights-cache directory itself to avoid the weights-cache directory being included in the tar.

cd weights-cache
tar cvf weights-cache.tar *
  1. Push to GCP bucket

Take the weights-cache.tar you just created and push to GCP bucket.

gsutil cp weights-cache.tar gs://your-bucket/nateraw/rare-puppers/
  1. Specify bucket in predictor to download/extract to cache with pget

Before cog push-ing, make sure you have removed the weights-cache directory from your local or have ignored it in .cogignore so the weights-cache dir doesn't get included in the cog build.

# Under the hood it runs this to download and extract weights cache in same structure as you had it locally
# pget -x https://storage.googleapis.com/your-bucket/nateraw/rare-puppers/weights-cache.tar ./weights-cache
from cog_hf_template import get_predictor


Predictor = get_predictor(
    task="image-classification",
    model_name_or_path="nateraw/rare-puppers",
    cache_dir="./weights-cache",
    gcp_bucket_weights="gs://your-bucket/nateraw/rare-puppers/weights-cache.tar",
)

Notes

Optionally, if you run in interactive mode, you can play with the resulting predictor. In this example we are using a text-generation model...

generator = predictor.predict("Write me a recipe for chocolate chip cookies", 1024, 1.0, 1.0, 50)
for out in generator:
    print(out, end='')

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.1.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

cog_hf_template-0.0.1-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for cog_hf_template-0.0.1.tar.gz
Algorithm Hash digest
SHA256 212a40c8ddf225db465026c2093a3c6510d31876e4be198510aa11b9bb495047
MD5 077aae18cb3f7d9eb8b9b65d75059b05
BLAKE2b-256 763b79d2e15e2e9eae598b261985c1ba380a385b44699a0ca9c7d5ce2c0c27d4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cog_hf_template-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bea71d4df03839d186d03519a922c65361ec0684de504548214c32a8271e50ec
MD5 964dbab401aef89e31ebc5cb17d949c6
BLAKE2b-256 5e299461ccddd33eb8c5ca9203608f35f6f02d5a873e579ff12c79ea391905ca

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