My Journey to a serverless transformers pipeline on Google Cloud
A Hugging Face transformers sentiment-analysis pipeline (DistilBERT fine-tuned on SST-2) was deployed as a low-traffic microservice on Google Cloud Run after AI Platform Prediction (beta instability, checkpoint not a pure TensorFlow model) and App Engine (TensorFlow install failure, PyTorch handling only 2 requests per instance) fell short. The final setup is a Flask app served by gunicorn with one worker and one thread in a Docker image, with Cloud Run memory raised to 4 GB because the model needs most of it. Requests take under five seconds including model loading, plus roughly 10 seconds on cold start; loading the model at startup instead of per request would cut both latency and memory. The image is about 1 GB (PyTorch ~700 MB, model ~250 MB), so storage costs roughly €0.10 per GB per month on top of request costs.
Related posts
- Why we’re switching to Hugging Face Inference Endpoints, and maybe you should too
Hugging Face mediumAdeptA team moved CPU-based transformer inference off a self-built FastAPI + Docker + AWS ECS/Fargate stack onto Hugging Face Inference Endpoints, collapsing deployment to train, push to Hub, deploy. In ab benchmarks with a RoBERTa text classifier, a 4-vCPU endpoint answered in 80±30 ms versus ~200 ms on their ECS container, and an 8-vCPU endpoint in 43 ms. Cost runs 24–50% higher than Fargate (about $60/month more for a large instance), which they judge worth it without a dedicated MLOps team, though not at hundreds of services. The main gap they note is the lack of a Terraform provider; they deploy with their own hugie CLI.
- Deploying 🤗 ViT on Vertex AI
Hugging Face mediumAdeptA TensorFlow ViT B/16 SavedModel from 🤗 Transformers, with base64 image preprocessing and label postprocessing baked into its signature, can be served on Google Cloud Vertex AI in four SDK steps: upload to the Model Registry, create an Endpoint, deploy with machine/accelerator specs and traffic split, then send prediction requests. Code covers ModelServiceClient, EndpointServiceClient and PredictionServiceClient usage and the required {"b64": ...} request payload format. A Locust load test on an n1-standard-8 with one T4 GPU averaged 646 ms latency over about 17k requests, and hourly pricing is roughly $0.44 for the machine plus $0.40 for the T4.
- Deploying Speech-to-Speech on Hugging Face
Hugging Face mediumAdeptHugging Face's Speech-to-Speech pipeline (VAD, speech-to-text, LLM, text-to-speech) can be deployed to Inference Endpoints by forking the huggingface-inference-toolkit into a custom Docker image. Code and data ship as git submodules and requirements are installed at build time, which cuts endpoint startup time. The endpoint runs on an AWS L4 GPU at about $0.80/hour and is configured through either the GUI or the huggingface_hub API, with container port 80 and an HF_TOKEN secret for gated models. A small Starlette WebSocket webservice keeps latency low, and a Python client records audio, streams it to the server and plays back the queued responses.
- 1 Billion Classifications
Hugging Face mediumAdeptProcessing 1B classifications or embeddings on a single-GPU baseline costs about $254 for DistilBERT sentiment classification and $409 for ModernBERT-based gte embeddings on an NVIDIA L4 ($0.8/hr), but roughly $44,500 for ColQwen2 vision embeddings. The method serves models with Infinity on Hugging Face Inference Endpoints and load-tests them with k6's shared-iterations executor. It sweeps batch size and virtual users using an exponential-then-binary search with a 2% improvement threshold, then scales cost horizontally by adding replicas. The best configurations were batch 64 / 448 VUs for classification, 32 / 256 for embedding and 4 / 4 for vision. The cheap T4 lost to the L4, partly because Flash Attention 2 requires newer GPUs.
- Rocket Money x Hugging Face: Scaling Volatile ML Models in Production
Hugging Face easyNoviceRocket Money replaced a regex-and-decision-table merchant detector with a BERT-family transformer classifier spanning 4000+ classes, served through Hugging Face's hosted Inference API rather than an in-house MLOps stack. They picked it over AWS SageMaker and a hand-rolled host after a three-month evaluation with gradual traffic ramps and worst-case load tests. An A/B split of new users showed better paid retention, and the rollout reached 100% over two months. A pre-inference cache cut costs, hitting about 85% hit rate in production against a theoretical 93%, as volume grew past a billion transactions per month. The team hit an outage when expanding classes and a stale-model caching bug on the vendor side, and so far has found LLMs slower and costlier than specialized classifiers.