Core Concepts
- Transformers Pipeline:
pipeline("text-classification")abstracts tokenize-infer-decode - Model Hub: 500k+ pretrained models hosted at huggingface.co/models
- Fine-tuning:
TrainerAPI withTrainingArgumentsfor supervised fine-tuning - Datasets Library:
load_dataset("dataset_name")with streaming, sharding, mapping - Tokenizers: Fast tokenizers (Rust-backed) with
encode/decode/batch_encode - Inference API: Serverless or dedicated endpoints via
huggingface_hub.InferenceClient
Common Patterns
pipeline("text-generation", model="mistralai/Mistral-7B-v0.1")for inferenceAutoModelForSequenceClassification.from_pretrained("model")withTrainerfor fine-tuningload_dataset("imdb", split="train").select(range(100))for dataset slicingAutoTokenizer.from_pretrained("model")withpadding=True, truncation=True- Push to hub:
model.push_to_hub("my-finetuned-model", use_auth_token=True) - Spaces:
gradio.Interface(fn, inputs, outputs).launch()deployed via HF Spaces
Best Practices
- Use
safetensorsformat for faster and safer model loading - Enable
device_map="auto"for multi-GPU inference - Set
load_in_4bit=Truewith BitsAndBytesConfig for memory-efficient fine-tuning - Pin
transformersversion in production to avoid breaking API changes - Cache models in persistent volume to avoid repeated downloads