Run Huggingface Models Locally For Free

If you’ve been following AI advancements in the open-source space, you’ve likely heard of Hugging Face. This French-American company is dedicated to open-source machine learning tools, particularly focusing on natural language processing. Hugging Face is named after a cute emoji.

Hugging Face’s Core Offerings

Hugging Face provides several key tools to empower developers and researchers in the AI community:

  • Transformers Library: This is a collection of pre-trained models designed for various tasks such as text classification, translation, summarization, and more.
  • Datasets Library: A repository offering ready-to-use datasets for training and evaluating machine learning models.
  • Hugging Face Hub: A central platform that functions like the GitHub of AI, allowing users to share and explore models, datasets, and machine learning applications. It facilitates collaboration among machine learning enthusiasts and experts, enabling them to learn from each other’s work and experience.

Understanding Key Hugging Face Terminologies

To maximize your experience with Hugging Face, it’s helpful to understand some core terms:

Pre-trained Model

A pre-trained model is a machine learning model that has already been taught using a large amount of data for a specific task, such as recognizing images or understanding language. This means you can use it immediately without needing to train it from scratch.

Inference

Inference is the process where a trained model makes predictions or draws conclusions about new, previously unseen data, based on patterns it learned during its training phase.

Training

Training is the initial phase of the machine learning process where the machine learns by being exposed to numerous examples. For instance, if you’re teaching it to recognize cars, you provide it with many labeled pictures of different cars, allowing the machine to build its understanding. Once trained, the machine uses this acquired knowledge during inference to recognize new things, like a car it hasn’t seen before.

Transformers

Transformers are a type of model specifically designed to handle text-based tasks, including translation, summarization, and text generation. Their unique architecture uses “attention mechanisms” to identify and capture the relationships between words and sentences.

Tokenizer

A tokenizer is a process that breaks down text into smaller units known as tokens. These tokens are typically words or subwords and are essential for natural language processing tasks.

Getting Started with Hugging Face

To begin using Hugging Face, you’ll need to set up an account and install the necessary libraries and dependencies.

Account Setup

Signing up as a community individual contributor is free. You can create a free account by visiting the Hugging Face website and clicking “sign up”. You may need to complete a quick human verification, then enter your email and password, complete your profile, and generate an avatar. After creating the account, you’ll receive a verification link in your email that you must click to verify your address. Once verified, you are officially a Hugging Face member. For more features or organizational needs, pro or customized plans are available.

Environment Setup: Python, Pip, and Virtual Environments

Before using the Hugging Face Hub programmatically, you must set up your environment:

  1. Install Python and Pip: Ensure Python 3.8 or higher is installed. Pip, Python’s package manager, is included with Python and doesn’t need separate installation.
  2. Create a Virtual Environment: It’s recommended to create a virtual environment to isolate project dependencies from system-wide packages, leading to a cleaner and more manageable setup. Common options include Venv (included with Python 3.3+) and Conda (installed via Anaconda or Miniconda), which is popular for data science and ML projects. You can activate your environment using the conda activate command.
  3. Choose a Code Editor/IDE: For development, you can use any preferred code editor or IDE, such as Jupyter Notebook, PyCharm, or Visual Studio Code.

Installing Hugging Face Libraries

Hugging Face offers two main libraries for accessing pre-trained models:

  • Transformers: This library handles text-based tasks like translation, summarization, and text generation. Install it using pip install transformers.
  • Diffusers: This library manages image-based tasks, including image synthesis, image editing, and image captioning.

To enable the pipeline function (discussed next) to load models, you’ll also need a model backend like PyTorch or TensorFlow. To install TensorFlow, run pip install TensorFlow.

Using Pre-trained Models with Hugging Face

The Pipeline Function

The pipeline function, imported from the Hugging Face Transformers library, is a high-level helper function that simplifies the use of pre-trained models for common tasks. It automates processes like loading the appropriate model, tokenizing input, running the model, and formatting output, requiring only a few lines of code.

Example: Sentiment Analysis with DistilBERT

Here’s how you can use the pipeline function for sentiment analysis:

  1. Import the pipeline: from transformers import pipeline.
  2. Load the pre-trained model: Use pipeline with the sentiment-analysis parameter and specify a model like "distilbert-base-uncased-finetuned-sst2-english".
    • DistilBERT is a smaller, faster version of the BERT model.
    • base uncased means it processes lowercase text and disregards capitalization.
    • fine-tuned on SST2 indicates it was specifically trained for sentiment classification using the SST2 dataset.

    This setup creates a ready-to-use tool to determine if a sentence’s sentiment is positive or negative.

  3. Analyze text: Store your desired text in a variable (e.g., inputtext), then call the pipeline with this variable and print the output.
  4. Run the code: Ensure the correct Conda environment is active. If using VS Code, select the appropriate Python interpreter via the command palette (Shift+Command+P). Execute the Python file from the terminal (e.g., python your_file_name.py).

The model will return the sentiment analysis. For example, it identified one text as negative with 99.96% confidence and another positive sentence confidently as positive. The process demonstrates how straightforward it is to get a pre-trained model running using Hugging Face libraries. A dependency conflict (Keras) was resolved by installing the recommended version during one execution.

Discovering and Exploring Models

Finding the right pre-trained model for a specific task is simple on the Hugging Face website. You can browse models and filter them by task, library, language, and other criteria. Models and datasets can also be searched by keyword and sorted by trending, most likes, most downloads, or recent updates.

Every model on the Hugging Face Hub has a “model card” containing important information such as model details, usage examples, links to files, and community interaction features. You can also view “Spaces” that use a particular model and explore them further or even clone a Space to your local machine to run it yourself.