AI Model Wiki

    Skip to main content
    45 min
    Lesson 1 of 5

    Pipeline API & Quick Start

    From the course Hugging Face & Transformers

    The Hugging Face Ecosystem

    Hugging Face is the GitHub of machine learning — hosting 500K+ models, 100K+ datasets, and providing tools to use them.

    Pipeline API

    The simplest way to use any model:

    from transformers import pipeline
    
    # Text generation
    generator = pipeline("text-generation", model="meta-llama/Llama-3.1-8B-Instruct")
    result = generator("The future of AI is", max_new_tokens=100)
    
    # Sentiment analysis
    classifier = pipeline("sentiment-analysis")
    result = classifier("I love this product!")
    # [{"label": "POSITIVE", "score": 0.9998}]
    
    # Summarization
    summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
    result = summarizer(long_text, max_length=130, min_length=30)
    

    Available Pipelines

    PipelineUse Case
    text-generationChat, completion, creative writing
    text-classificationSentiment, topic, intent
    summarizationDocument summarization
    translationLanguage translation
    question-answeringExtract answers from context
    image-classificationLabel images
    object-detectionFind objects in images
    automatic-speech-recognitionTranscribe audio

    Installation

    pip install transformers torch  # PyTorch backend
    pip install transformers[tf]     # TensorFlow backend
    

    Common Pitfall: Downloading Models at Import Time on Cold Start

    When you import a pipeline without specifying a local cache path, Hugging Face downloads the model to ~/.cache/huggingface/hub on the first call. In containerized deployments, this directory is ephemeral — the model re-downloads on every cold start, adding 30–120 seconds of latency. Bake model weights into the Docker image:

    RUN python -c "from transformers import pipeline; pipeline('sentiment-analysis')"
    

    Or set HF_HOME to a Docker volume that persists across restarts.

    Best Practice: Specify a Revision for Reproducibility

    Model weights on the Hub can be updated without changing the model name. Pin to a specific Git revision to ensure reproducible behavior:

    classifier = pipeline(
        "sentiment-analysis",
        model="distilbert/distilbert-base-uncased-finetuned-sst-2-english",
        revision="714eb0f",  # Pin to exact commit hash
    )
    

    Named Product Reference: Hugging Face Inference Endpoints

    Hugging Face's managed Inference Endpoints service lets you deploy any Hub model to AWS, GCP, or Azure with one click. Endpoints auto-scale to zero when idle (great for development) or maintain warm instances for production. For teams that want the quality of Hugging Face models without managing GPUs, Inference Endpoints is the fastest path to production.

    Key Takeaways

    • •The Pipeline API provides one-line access to any task and model
    • •Hugging Face hosts 500K+ models across all AI domains
    • •Pipelines handle tokenization, inference, and post-processing automatically
    • •Supports PyTorch and TensorFlow backends
    0 of 5 lessons marked complete

    Progress is kept in this browser only.