Integrating Hugging Face’s open-source AI Models into Google Sheet: Handle your data like in Excel spreadsheet with formula and simple drag

Share this post:

Views: 24


Introduction

If you’re in the humanities and have ever felt a bit intimidated by the idea of coding, no worries – sometimes, the thought of diving into tech can be overwhelming, especially when all you want to do is work with your data. There’s a way to make it easier! You can incorporate AI models into Google Sheets, allowing you to manage your data similarly to how you would in Excel, utilizing custom formulas and dragging cells for batch tasks.

In this article, we’ll introduce you what is Hugging Face and show you how easily you could use the models on the Hugging Face platform in Google Sheets. Let’s follow us to bring AI magic into your spreadsheets!

What is Hugging Face?

As introduced in the Hugging Face’s documentation: Hugging Face (https://huggingface.co/) is a platform “with over 900k models, 200k datasets, and 300k demo apps (Spaces), all open source and publicly available, in an online platform where people can easily collaborate and build ML together.” In other words, there are a bunch of free AI models for you to use beyond just the recent most popular large language model (LLM) chatbots. There are diverse models and applications in areas such as computer vision, audio processing, text analysis and more in the Hugging Face platform.

How to use the models on Hugging Face in Google Sheet?

Please see the screenshots on slides 7-9 below for a step-by-step guide on how to use Hugging Face models into Google Sheets. In short, the steps are:

  1. Create an account on Hugging Face (https://huggingface.co/).
  2. Get your Hugging Face token here: https://huggingface.co/settings/tokens
  3. In Google Sheet, go to the “Extension” → “Apps Script” to create a custom formula which integrated the Hugging Face models you find useful (you may copy the code in the next section of this article).
  4. You can now use your custom formula in the spreadsheet!

Sample Code for putting in the Google Sheet “Apps Script”

The sample code below showcases how to apply a zero-shot classification model provided by Facebook on the Hugging Face platform (https://huggingface.co/facebook/bart-large-mnli). In this case, we named the formula as “classifyOne” but you can rename it to whatever you like. After that, you can use this formula like an Excel formula in your Google Sheet spreadsheet.

function classifyOne(input, labels, repo_id="facebook/bart-large-mnli") {

  endpoint = "https://api-inference.huggingface.co/models/" + repo_id;

  const payload = JSON.stringify({
    "inputs": input,
    "parameters": {"candidate_labels": labels}});

  const options = {
      "headers":  {"Authorization": "Bearer <your_huggingface_token>"}, // input your huggingface token here
      "wait_for_model": true,
      "use_gpu": false,
      "method" : "POST",
      "contentType" : "application/json",
      "payload" : payload,
      
  };

  const response = UrlFetchApp.fetch(endpoint, options);
  const data = JSON.parse(response.getContentText());
  return data['labels'][0];
}

Since the input and output formats vary among different models, you may need to make some adjustments to the apps script code if you decide to use other models. Many people generously share their code online, so a quick Google search is very helpful. For example, this article by Mr Kunjal Chawhan provides code for using the cardiffNLP/twitter-roberta-base-sentiment-latest model for sentiment analysis. Below, I renamed the formula as “emotion” – let’s see the gif below the sample code to see how it works!

function emotion(input) {

  const API_URL = "https://api-inference.huggingface.co/models/cardiffnlp/twitter-roberta-base-sentiment-latest";
  const API_TOKEN = "<your_huggingface_token>"; // input your huggingface token here

  const payload = {
    inputs: input
  };

  const options = {
    method: "POST",
    contentType: "application/json",
    headers: {
      "Authorization": "Bearer " + API_TOKEN
    },
    payload: JSON.stringify(payload)
  };

  try {
    // Make the API request
    const response = UrlFetchApp.fetch(API_URL, options);
    const result = JSON.parse(response.getContentText());

    // Process the result
    if (result && result[0] && result[0].length > 0) {
      const sentiments = result[0].map(item => ({ label: item.label, score: item.score }));
      sentiments.sort((a, b) => b.score - a.score);

      // Return the sentiment with the highest score
      return sentiments[0].label;
    } else {
      return "Error: Unable to determine sentiment";
    }
  } catch (error) {
    return "Error: " + error.toString();
  }
}
Result on using the custom formula integrated with AI model functionality in Google Sheet

Conclusion

Both Python and Google Sheet Apps Script can integrate Hugging Face models for use, providing you with flexible alternatives. Feel free to experiment with each approach and see which one you find more comfortable to use. Whether you prefer coding in Python or using Google Sheets for easy access, there’s a solution waiting for you!

Enjoy your hunt on Hugging Face! There are a wide range of models that might be just right for your needs.

References

Chawhan, Kunjal. (2024, October 5). Sentiment classification on Google Sheets using HuggingFace API. Decode Digital Market. https://www.decodedigitalmarket.com/hugging-face-api-sentiment-classification-google-sheets-appscript/

Daudens, Florent. (2024, July 19). Bringing open-source models to spreadsheets. Hugging Face. https://huggingface.co/blog/fdaudens/hugging-face-on-sheets

JournalistsonHF. (n.d.). Hugging Face Sheets add-on. Hugging Face. https://huggingface.co/spaces/JournalistsonHF/huggingface-on-sheets

Soma, Jonathan. [@jsoma]. (2024, April 25). AI, Hugging Face and non-chatbot models (practical AI for journalism, session 4) [Video]. YouTube. https://www.youtube.com/live/ZsVlvsxfnXw

 

– By Holly Chan, Library

December 19, 2024

You may also be interested in…