← Master Index
Vol. 22 Module 22.5 Lecture

Hugging Face (Transformers, Datasets, Spaces, Inference API, Model Hub)

AI Development Platforms

How This Lesson Fits the Module & Volume

Module 22.4 ended with media vendors (Udio). Module 22.5 is where engineers find, run, and host models. Hugging Face is the default open-ML platform: Model Hub, transformers, datasets, Spaces, and Inference APIs/Endpoints. Vol. 11.5 open-source models taught families; this page teaches the distribution and inference surface you actually type into a terminal.

Peers: Replicate (Cog predictions), Together / Groq / Cerebras (fast LLM hosts), OpenRouter (gateway). SDK wrap remains Vol. 18 OpenAI SDK / FastAPI—HF Inference is often OpenAI-compatible now, but the Hub is not “an SDK.” Speech models on the Hub still follow Vol. 16 Whisper / TTS eval.

Learning Objectives

By the end of this lesson, students should be able to:

  • Name the five HF surfaces: Hub, Transformers, Datasets, Spaces, Inference (serverless + dedicated endpoints).
  • Load a Hub model with transformers locally vs call Inference API remotely.
  • Contrast HF vs Replicate vs Together vs self-host on qualitative price, control, and ops.
  • Read model cards, licenses, and gated-repo rules before shipping weights.
  • Sketch wrapping HF Inference behind FastAPI like any Vol. 18 vendor.
  • Know when Spaces is a demo, not a multi-tenant product.
Definition

Hugging Face is an open machine-learning platform and company. The Model Hub hosts weights, configs, and model cards. Transformers is the Python library that loads many of those checkpoints. Datasets loads and streams training/eval corpora. Spaces hosts Gradio/Streamlit (and Docker) demos. Inference is HF’s hosted runtimes: serverless Inference API and dedicated Inference Endpoints (plus related Inference Providers). Hugging Face is not a single model and not a replacement for your product API.

Five Surfaces, One Account

Students collapse “Hugging Face” into “that chatbot.” In production you pick a surface: download weights (Hub + Transformers + your GPU), call a metered endpoint (Inference), or show a prototype (Spaces). Mixing them without intent causes surprise bills and surprise licenses.

Hub + Transformers

  • You operate GPU/RAM
  • Full control, license still binds
  • Vol. 18.3/18.4 hardware tiers

Inference API / Endpoints

  • HF or partner GPUs
  • HTTP / OpenAI-compatible clients
  • Scale-to-zero vs dedicated

Datasets + Spaces

  • Datasets: train/eval corpora
  • Spaces: public demos
  • Not your SLA production API

Pricing / Strengths / Weaknesses (Qualitative)

DimensionHugging FaceReplicateTogether / Groq / CerebrasSelf-host (vLLM etc.)
Pricing postureFree Hub/Transformers (you pay compute); Spaces CPU/GPU tiers; Inference billed per request or dedicated instance—confirm live metersPay-per-prediction / GPU-secondToken-based LLM inference (hardware specialists)Your cloud/GPU bill + eng time
StrengthsLargest open catalog; library + cards + community; multimodal (LLM, vision, speech); endpoints + Spaces; ecosystem gravityTurnkey Cog models, especially image/videoHigh-throughput LLM servingData residency, custom kernels, no per-token vendor margin
WeaknessesServerless cold starts; license/gated-model footguns; Spaces ≠ prod; you still own eval; not the fastest LLM chipLess “research Hub”; you depend on someone’s CogNarrower non-LLM catalogOps burden (Vol. 18.4)

Python: Transformers Local vs Inference Remote

Local pipeline is for labs and Vol. 18.4 boxes. Production often uses InferenceClient or an OpenAI-compatible base URL. Pin revision hashes for reproducibility. Honor gated models’ user access tokens and licenses.

# hf_two_paths.py — local Transformers vs hosted Inference import os from transformers import pipeline from huggingface_hub import InferenceClient # A) Local (you bring the GPU). Pin revision in real apps. clf = pipeline( "sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english", revision="main", # prefer a git SHA in production ) print(clf("Vol. 22 is a vendor catalog, not a benchmark sheet.")[0]) # B) Hosted Inference (token from HF settings; never commit) client = InferenceClient(token=os.environ["HF_TOKEN"]) out = client.chat.completions.create( model=os.environ.get("HF_CHAT_MODEL", "meta-llama/Llama-3.1-8B-Instruct"), messages=[{"role": "user", "content": "One sentence: what is the Model Hub?"}], max_tokens=128, ) print(out.choices[0].message.content)

Do

  • Read the model card + license before deploy
  • Pin revision; log model id in traces
  • Treat Spaces as prototypes
  • Wrap Inference behind FastAPI + auth

Don’t

  • Assume Hub weights are Apache-2.0
  • Ship a public Space as the only backend
  • Ignore gated-repo terms
  • Confuse HF Chat UI with your SLA

Related Lectures

LectureRole
Open-source models / Llama familyWhat weights mean
OpenAI SDK / FastAPI / Tier 1 API-onlyHow you ship calls
Whisper / TTSSpeech checkpoints on the Hub
Replicate · Together · OpenRouterAlternative runtimes / gateways
CopyrightWeight + dataset licenses
Common Misconception

“Hugging Face is one API like OpenAI.” Hub, library, Spaces, and Inference are different SLAs. Second: downloading Llama from the Hub ignores the license/gate. Third: a viral Space is production. Fourth: Inference serverless has no cold start. Fifth: Datasets streaming means you own the corpus copyright. Sixth: Vol. 19 perplexity (metric) is the Perplexity company (last lecture in this module).

Knowledge Check

  1. Short Answer: Name three Hugging Face surfaces. Answer: Hub, Transformers, Datasets, Spaces, Inference (any three).
  2. True/False: Spaces is the recommended multi-tenant production API. Answer: False.
  3. Multiple Choice: Before shipping Hub weights you should: (a) read card + license + gate rules, (b) assume MIT, (c) only check likes. Answer: (a).
  4. Short Answer: What is the difference between Transformers local and Inference remote? Answer: Local runs on your GPU/CPU; Inference is hosted HTTP/metered runtime.
  5. True/False: You should invent HF Inference dollar rates from memory. Answer: False.
  6. Multiple Choice: Pin production checkpoints by: (a) git revision/SHA, (b) “latest,” (c) Space likes. Answer: (a).
  7. Short Answer: Which Vol. 18 lecture wraps vendor HTTP into your product? Answer: FastAPI (or OpenAI SDK / API lecture).
  8. True/False: Replicate is the same product as the Model Hub. Answer: False.
  9. Multiple Choice: Speech models on the Hub still need: (a) Vol. 16 WER/MOS eval, (b) no eval, (c) only Suno credits. Answer: (a).
  10. Short Answer: Which Vol. 11.5 lecture catalogs open weights conceptually? Answer: Open-source models (or a family lecture such as Llama).

Key Takeaways

  • HF = Hub + Transformers + Datasets + Spaces + Inference—pick the surface on purpose.
  • Licenses, gates, and revisions are part of the vendor decision.
  • Serverless Inference ≠ dedicated Endpoint ≠ self-host.
  • Wrap calls with Vol. 18; eval speech/vision with Vol. 16/19.
  • Next: Replicate—Cog predictions as a productized runtime.
Trainer’s Guide

Lab: Students pick one Hub text model (small) and (1) run pipeline locally or on CPU, (2) sketch the same call via InferenceClient with HF_TOKEN from env, (3) paste the model card license clause. Grade: revision noted, no committed token, explicit statement whether Spaces would be allowed in prod (answer: no).

Recap: Hugging Face is the open-ML platform of record. Use the Hub to choose weights; use Inference or your GPU to run them. Next runtime: Replicate.