Skip to content

Hugging Face

Introduction to Hugging Face

Overview

Hugging Face is a leading platform in natural language processing (NLP) and machine learning (ML), providing tools, libraries, and models for developers and researchers. It is widely known for its open-source libraries and community contributions, facilitating the use of pre-trained models and accelerating ML workflows.

Applications of Hugging Face:

  • Sentiment Analysis
  • Text Summarization
  • Machine Translation
  • Chatbots and Virtual Assistants
  • Image Captioning (via VLMs)
  • Healthcare, legal, and financial domain-specific NLP solutions

Why Hugging Face Matters:

Hugging Face democratizes access to advanced AI tools, fostering innovation and collaboration. With its open-source ethos, it has become a go-to resource for researchers and developers alike, empowering them to tackle complex challenges in AI and ML effectively.

Hugging Face can be used with both TensorFlow and PyTorch.

Hugging Face AutoClasses

Hugging Face AutoClasses are an abstraction that simplifies the use of pre-trained models for various tasks, such as text classification, translation, and summarization. They automatically select the appropriate architecture and configuration for a given pre-trained model from the Hugging Face Model Hub.

Commonly Used AutoClasses:

1. AutoModel
  • For loading generic pre-trained models.
  • Use case: Extracting hidden states or embeddings.
from transformers import AutoModel
model = AutoModel.from_pretrained("bert-base-uncased")
2. AutoModelForSequenceClassification
  • For text classification tasks.
  • Use case: Sentiment analysis, spam detection, etc.
from transformers import AutoModelForSequenceClassification
model = AutoModelForSequenceClassification.from_pretrained("bert-base-uncased")
3. AutoTokenizer
  • Automatically loads the appropriate tokenizer for the specified model.
  • Handles tokenization, encoding, and decoding.
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
4. AutoModelForQuestionAnswering
  • For question-answering tasks.
  • Use case: Extracting answers from context.
from transformers import AutoModelForQuestionAnswering
model = AutoModelForQuestionAnswering.from_pretrained("bert-large-uncased-whole-word-masking-finetuned-squad")
5. AutoModelForSeq2SeqLM
  • For sequence-to-sequence tasks like translation or summarization.
from transformers import AutoModelForSeq2SeqLM
model = AutoModelForSeq2SeqLM.from_pretrained("t5-small")
6. AutoModelForTokenClassification
  • For tasks like Named Entity Recognition (NER) or Part-of-Speech (POS) tagging.
from transformers import AutoModelForTokenClassification
model = AutoModelForTokenClassification.from_pretrained("dbmdz/bert-large-cased-finetuned-conll03-english")
7. AutoModelForCausalLM
  • For language modeling tasks that generate text.
from transformers import AutoModelForCausalLM
model = AutoModelForCausalLM.from_pretrained("gpt2")
8. AutoProcessor (for Multimodal Models)
  • Loads processors for tasks involving images, text, or both.
  • Example: Vision-Language Models (e.g., CLIP).
from transformers import AutoProcessor
processor = AutoProcessor.from_pretrained("openai/clip-vit-base-patch32")

Use Cases in Projects:

  • VLMs: Use AutoProcessor and AutoModel for image-text embedding or image captioning tasks.
  • Healthcare: Use AutoModelForSequenceClassification for text classification tasks like predicting medical conditions based on clinical notes.

Why use Transformers?

Traditionally to process text we RNNS but as the window size increases we see the problem of vanishing gradients. Additionally, they are slow. Transformers are able to address these concerns.