Huggingface Transformers

Hugging Face Concepts

  • Leading platform for NLP, Image, Audio models.
  • Provides pre-trained models for various tasks that can be used in applications.
  • Supports tasks like text classification, generation, summarization, question answering, etc.
  • Models can be fine-tuned on custom datasets for specific use cases.
  • Sign up at Hugging Face to access models and resources.

Walking Through Website and Model Hub

  • Explore the Hugging Face Model Hub via Top bar.
  • Walk through task sections to see relevant models.
  • Search for models based on task and language.
  • Click on a model to see details, usage instructions, and example code.
  • Install the transformers library: pip install transformers==4.57.6 huggingface-hub==0.36.2

Using Models with High-Level Pipelines

  • Hugging Face provides high-level pipelines for common NLP tasks.
  • Pipelines abstract away model loading and preprocessing steps.
  • Example: Text Classification Pipeline
    1. from transformers import pipeline text_classifier = pipeline("text-classification", model="distilbert-base-uncased-finetuned-sst-2-english") response = text_classifier("I love using Hugging Face models!") print(response)

Summarization Pipeline Example

    Sample Code:
    1. from transformers import pipeline summarizer = pipeline("summarization", model="nyamuda/extractive-summarization") article = """The Hugging Face Transformers library provides a wide range of pre-trained models for natural language processing tasks. It supports tasks such as text classification, generation, summarization, and qa.""" summary = summarizer(article, max_length=50, min_length=25) print(summary[0]['summary_text'])

Question Answering Pipeline Example

    Sample Code:
    1. from transformers import pipeline qa_pipeline = pipeline("question-answering", model="distilbert-base-cased-distilled-squad") context = "content_of_the_policy_file" question = "How many volunteer days are offered annually?" response = qa_pipeline(question=question, context=context) print(response)