Hugging Face’s CEO on why companies are done renting their AI
Back to Tutorials
aiTutorialintermediate

Hugging Face’s CEO on why companies are done renting their AI

July 10, 202611 views5 min read

Learn how to work with Hugging Face's open-source AI ecosystem by downloading, fine-tuning, and deploying pre-trained models using the Transformers library.

Introduction

In the rapidly evolving world of artificial intelligence, companies are shifting from renting AI services to building their own proprietary solutions. Hugging Face's CEO, Clem Delangue, has observed this trend firsthand, with open-source AI platforms becoming the backbone of innovation. In this tutorial, you'll learn how to leverage Hugging Face's powerful ecosystem to work with open-source AI models, specifically focusing on downloading, fine-tuning, and deploying a pre-trained model using their Transformers library.

This hands-on guide will teach you how to access and utilize the open-source AI tools that are revolutionizing how companies approach AI development, giving you the skills to build your own AI applications using the same technology powering Fortune 500 companies.

Prerequisites

  • Basic Python programming knowledge
  • Intermediate understanding of machine learning concepts
  • Installed Python 3.7 or higher
  • Basic familiarity with Jupyter Notebook or any Python IDE
  • Internet connection for downloading models and datasets

Step-by-Step Instructions

1. Install Required Libraries

Before working with Hugging Face models, you need to install the essential packages. The Transformers library is the primary tool for working with pre-trained models, while datasets and tokenizers are essential companions.

pip install transformers datasets tokenizers torch

Why this step? Installing these libraries provides you with the core tools needed to download, process, and utilize pre-trained AI models. The Transformers library is specifically designed to make working with state-of-the-art models accessible to developers of all levels.

2. Import Libraries and Set Up Environment

Once installed, import the necessary modules to begin working with models and datasets.

from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
from datasets import load_dataset
import torch

Why this step? Importing these modules gives you access to the core functionality of Hugging Face's ecosystem. The pipeline function is particularly powerful as it provides a simple interface for running inference on various tasks without needing to write complex code.

3. Load a Pre-trained Model

For this tutorial, we'll use a sentiment analysis model. Hugging Face provides many pre-trained models that can be easily loaded using the Auto classes.

# Load a pre-trained sentiment analysis model
model_name = "cardiffnlp/twitter-roberta-base-sentiment-latest"

# Load tokenizer and model
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)

Why this step? Using pre-trained models saves significant time and computational resources. These models have already been trained on massive datasets and can be fine-tuned or used directly for various tasks, demonstrating the power of open-source AI collaboration.

4. Create a Sentiment Analysis Pipeline

Transformers provides a convenient pipeline interface that combines tokenization, model inference, and output processing.

# Create a pipeline for sentiment analysis
sentiment_pipeline = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)

Why this step? The pipeline abstraction simplifies the process of running inference, allowing you to focus on the problem you're solving rather than the technical details of model execution.

5. Test the Model with Sample Data

Now let's test our sentiment analysis model with some sample tweets.

# Sample tweets for testing
sample_tweets = [
    "I love this new AI technology!",
    "This is terrible, I hate it.",
    "The weather is okay today.",
    "Hugging Face is amazing for open-source AI!"
]

# Run sentiment analysis
results = sentiment_pipeline(sample_tweets)

# Display results
for tweet, result in zip(sample_tweets, results):
    print(f"Tweet: {tweet}")
    print(f"Sentiment: {result['label']}, Confidence: {result['score']:.4f}")
    print("---")

Why this step? Testing with sample data helps you verify that your model is working correctly and gives you insight into how it interprets different inputs. This step demonstrates the practical application of open-source models in real-world scenarios.

6. Fine-tune the Model on Custom Data

To make the model more specific to your use case, you can fine-tune it on your own dataset. First, load a dataset from Hugging Face's datasets library.

# Load a sentiment dataset
dataset = load_dataset("tweet_eval", "sentiment")

# Take a small subset for demonstration
train_dataset = dataset["train"].select(range(100))

# Prepare the dataset for training
def tokenize_function(examples):
    return tokenizer(examples["text"], truncation=True, padding=True)

# Tokenize the dataset
tokenized_dataset = train_dataset.map(tokenize_function, batched=True)

Why this step? Fine-tuning allows you to adapt pre-trained models to specific domains or tasks, which is crucial for achieving better performance on specialized applications. This demonstrates how companies can leverage open-source models while customizing them for their specific needs.

7. Train the Model

With your dataset prepared, you can now train the model using Hugging Face's Trainer API.

# Import training components
from transformers import TrainingArguments, Trainer

# Define training arguments
training_args = TrainingArguments(
    output_dir="./sentiment_model",
    num_train_epochs=1,
    per_device_train_batch_size=8,
    per_device_eval_batch_size=8,
    warmup_steps=500,
    weight_decay=0.01,
    logging_dir="./logs",
)

# Create the trainer
trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=tokenized_dataset,
    eval_dataset=tokenized_dataset,
)

# Start training
trainer.train()

Why this step? Training demonstrates how companies can build upon existing open-source work to create specialized solutions. This approach is more efficient than training models from scratch and represents the core philosophy of open-source AI collaboration.

8. Save and Deploy Your Model

After training, save your model so you can use it later or share it with others.

# Save the fine-tuned model
model.save_pretrained("./fine_tuned_sentiment_model")
tokenizer.save_pretrained("./fine_tuned_sentiment_model")

# Load the model for future use
loaded_model = AutoModelForSequenceClassification.from_pretrained("./fine_tuned_sentiment_model")
loaded_tokenizer = AutoTokenizer.from_pretrained("./fine_tuned_sentiment_model")

Why this step? Saving and deploying your model ensures that your custom work can be reused, shared, or integrated into production systems. This represents the final stage of the open-source AI workflow that companies are adopting.

Summary

This tutorial demonstrated how to work with Hugging Face's open-source AI ecosystem. You learned to download pre-trained models, create pipelines for quick inference, fine-tune models on custom datasets, and save your trained models for deployment. These techniques represent the shift from renting AI services to building proprietary solutions that companies are embracing, as highlighted by Hugging Face's CEO.

The power of open-source AI lies in its collaborative nature, where developers can build upon each other's work to create more sophisticated applications. This approach is more cost-effective and faster than developing everything from scratch, which explains why companies are moving away from renting AI services toward building their own.

Related Articles