Using LLMs for Automated Data Categorization in Google Sheets

Using LLMs for Automated Data Categorization in Google Sheets

This post unveils a well-kept secret unknown to many: using externally hosted Large Language Models (LLMs) in Google Sheets to perform NLP tasks on text data, such as categorizing text “in your own terms”. The good news is that it only takes a few minutes to get this enthralling and useful feature up and running in your spreadsheet. Here’s how to integrate Hugging Face LLMs in Google Sheets.

Add Hugging Face’s script to analyze text in Google Sheets

The key requirement to be able to invoke Hugging Face pre-trained LLMs is to incorporate in Google Sheets a custom function that handles the process of calling it for performing language tasks like text categorization.

In Google Sheets, go to the Extensions menu and select Apps Script to open the script editor (note that in older versions of Google Sheets, the script editor was located on the ‘Tools’ menu).

Accessing the scripts editor in Google Sheets

The main body of this editor (which may have been opened in a new tab on your browser) is where we will add the custom function we need to categorize text using a pre-trained Hugging Face LLM. This function is already implemented and publicly available on this GitHub repository. All you need to do is access the link, copy the code, and paste it into your script editor.

Once pasted in Google Sheets’ script editor, the script code should look like this:

Script for analyzing and categorizing Google Sheets content using a Hugging Face pre-trained model
Script for analyzing and categorizing Google Sheets content using a Hugging Face pre-trained model | Source: gist.github.com

Don’t worry, there’s very little work to do here. First, on the left-hand side pane of the editor, give any name to the script file (just make sure it has “.gs” extension).

Now let’s take a quick look at the above code. The script defines a custom function to be utilized in Google Sheets to categorize a piece of text by using an existing LLM in Hugging Face hub. The function name is ANALYZE and it takes three arguments:

  • input: the input text to categorize.
  • labels: a set of labels separated by commas, representing the class categories to be used by the LLM for the classification task.
  • repo_id: a Hugging Face LLM identifier used in the script to create an endpoint URL from where Google Sheets will invoke the model for inference. By default, the script uses the "facebook/bart-large-mnli", a BART model trained for multi-class text classification.

In a nutshell, the code inside the ANALYZE function takes the necessary steps to gather the input and labels, create an endpoint to invoke the Hugging Face LLM, pass in the collected input and labels, and receive the output prediction.

So, there is no need to apply any modifications to the entire code … except for one little thing. Did you notice the following line of code has been highlighted in the above screenshot?

"headers":  {"Authorization": "Bearer <YOUR HUGGINGFACE API KEY>"}

That wasn’t a mere coincidence. The script will not run on Google Sheets unless you paste here your own Hugging Face API Key (now renamed as access token), so that LLMs hosted in Hugging Face hub are accessed using your own Hugging Face user account. The next quick step to take is -take a wild guess- getting your token.

Obtain Hugging Face Access Token

First, go to Hugging Face website and log in or register on the platform. Once logged in, go to Settings by clicking on your user icon in the upper-right corner, then go to Access Tokens.

Access tokens in Hugging Face

Here, we’ll create a “read-only” access token. We don’t need write permissions in this use case since we will just load pre-trained LLMs for inference in Google Sheets. All we need to do is click on the + Create new token button, on the next screen switch to the “Read” tab, give a name of your choice to the token, and click on Create token to generate it.

Creating a read-only access token in Hugging Face
 
Obtaining your Hugging Face Access Token

That’s it! Now copy and carefully paste the token in your script, replacing the ‘<YOUR HUGGINGFACE API KEY>‘ part with your token, save the script, and go back to Google Sheets to start playing!

Using the LLM in Google Sheets to Categorize Text Data

If everything went well, now you’ll be able to use the ANALYZE in Google Sheets, just like any other spreadsheet function. Refresh your browser tab to make sure the changes applied in the script editor become effective. Then we can start adding some example text and a label set as follows:

Example set of labels and text to categorize

This example aims at categorizing text descriptions of modern landmarks located in Tokyo, Japan. Cell A4 contains the comma-separated categories (landmark types), and cells A7 to A9 contain three example inputs to categorize.

Let’s go to cell B7 (the active cell in the above image) and call our function to categorize text using an LLM. The syntax is quite simple: since a default LLM was specified in the previous script, we only need to specify two arguments in the following order: (1) the cell containing the text to categorize, and (2) the cell containing the labels to be used by the LLM in the inference process. Here is the formula:

=analyze($A7,$A$4)

By extending the same formula for the other two examples, this is the result:

Automated categorization of text using the analyze() function

To finalize, here is the icing on the cake: you can simultaneously categorize the same text in multiple ways, using different sets of categories. All you need to do is add extra lists of categories in another cell and adapt our formulae to use them. For example, what happens if we use an additional column to categorize the sentiment of the text into positive, negative, or neutral?

Automated categorization of text using the analyze() function

Now we have a double categorization of our data. Notice I slightly tweaked the third example text (the one about Pokemon Cafe) so that not all examples were categorized as positive. This is particularly interesting: if you modify any parts of the input text, its categorization re-calculating by automatically invoking the endpoint LLM again!

References

The following video posted by Hugging Face on their Youtube channel provides another similar illustrative example to the ones seen in this tutorial.

Leave a Reply

Your email address will not be published. Required fields are marked *