MNLI_CLASSIFY
The MNLI_CLASSIFY function utilizes a Zero-Shot Classification model to categorize text without requiring the model to be explicitly trained on those specific labels.
It leverages the Xenova/distilbert-base-uncased-mnli model via transformers.js, which allows for flexible classification into any set of candidate labels provided by the user. This is particularly useful for dynamic categorization tasks where the labels might change or are not known in advance.
The function returns an Excel Data Type containing the top predicted label and its confidence score. Users can access these properties using the . operator (e.g., =A1.Label, =A1.Score).
Excel Usage
=MNLI_CLASSIFY(text, labels)
text(list[list], required): Single text cell or 2D text range to classify.labels(list[list], required): 2D range of candidate labels for classification.
Returns (list[list]): 2D array of classification results (Excel Data Types).
Example 1: Positive product review
Inputs:
| text | labels | |
|---|---|---|
| I love this product! | positive | negative |
Excel formula:
=MNLI_CLASSIFY("I love this product!", {"positive","negative"})
Expected output:
{"type":"String","basicValue":"positive","properties":{"Label":{"type":"String","basicValue":"positive"},"Score":{"type":"Double","basicValue":0.999818}}}
Example 2: Multiple candidate labels
Inputs:
| text | labels | |||
|---|---|---|---|---|
| The movie was alright, but the ending was confusing. | happy | sad | neutral | confused |
Excel formula:
=MNLI_CLASSIFY("The movie was alright, but the ending was confusing.", {"happy","sad","neutral","confused"})
Expected output:
{"type":"String","basicValue":"confused","properties":{"Label":{"type":"String","basicValue":"confused"},"Score":{"type":"Double","basicValue":0.997679}}}
Example 3: Multi-cell text input
Inputs:
| text | labels | |
|---|---|---|
| Great stuff | positive | negative |
| Terrible experience |
Excel formula:
=MNLI_CLASSIFY({"Great stuff";"Terrible experience"}, {"positive","negative"})
Expected output:
| Result |
|---|
| [object Object] |
| [object Object] |
Example 4: Handles empty cells
Inputs:
| text | labels | |
|---|---|---|
| Amazing | positive | negative |
Excel formula:
=MNLI_CLASSIFY({"Amazing";""}, {"positive","negative"})
Expected output:
| Result |
|---|
| [object Object] |
Python Code
Show Code
from transformers_js_py import import_transformers_js
async def mnli_classify(text, labels):
"""
Classify text into provided categories using a zero-shot MNLI model.
See: https://huggingface.co/Xenova/distilbert-base-uncased-mnli
This example function is provided as-is without any representation of accuracy.
Args:
text (list[list]): Single text cell or 2D text range to classify.
labels (list[list]): 2D range of candidate labels for classification.
Returns:
list[list]: 2D array of classification results (Excel Data Types).
"""
try:
def to2d(value):
return [[value]] if not isinstance(value, list) else value
def validate_grid(value, name):
if not isinstance(value, list) or not value or not all(isinstance(row, list) for row in value):
return None, f"Error: {name} must be a non-empty 2D list"
if len({len(row) for row in value}) != 1:
return None, f"Error: {name} must be a rectangular 2D list"
return value, None
text_grid, error = validate_grid(to2d(text), "text")
if error: return error
label_grid, error = validate_grid(to2d(labels), "labels")
if error: return error
# Flatten and clean labels
flat_labels = []
for row in label_grid:
for cell in row:
if cell is not None:
s_cell = str(cell).strip()
if s_cell:
flat_labels.append(s_cell)
if not flat_labels:
return "Error: labels must contain at least one non-empty string"
transformers = await import_transformers_js()
pipeline = transformers.pipeline
pipe = await pipeline("zero-shot-classification", "Xenova/distilbert-base-uncased-mnli")
result = []
for row in text_grid:
out_row = []
for cell in row:
if cell is None or (isinstance(cell, str) and not cell.strip()):
out_row.append("")
continue
# Perform classification
# multi_label=False ensures we get one winning label
raw_res = await pipe(str(cell), flat_labels, multi_label=False)
# Convert JS object to Python dict if necessary
if hasattr(raw_res, "to_py"):
res = raw_res.to_py()
else:
res = raw_res
# pipe with single string input returns a single result object
# However, some versions/tasks might return a list of one object
if isinstance(res, list):
res = res[0]
top_label = str(res["labels"][0])
top_score = float(res["scores"][0])
out_row.append({
"type": "String",
"basicValue": top_label,
"properties": {
"Label": {"type": "String", "basicValue": top_label},
"Score": {"type": "Double", "basicValue": top_score}
}
})
result.append(out_row)
return result
except Exception as e:
return f"Error: {str(e)}"