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
- 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
- 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
.
- 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 *
- 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/
- 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for cog_hf_template-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bea71d4df03839d186d03519a922c65361ec0684de504548214c32a8271e50ec |
|
MD5 | 964dbab401aef89e31ebc5cb17d949c6 |
|
BLAKE2b-256 | 5e299461ccddd33eb8c5ca9203608f35f6f02d5a873e579ff12c79ea391905ca |