# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="4Pjmz-RORV8E"
# # Part 5: Labeling with zero-shot classification
#
# This notebook shows how zero-shot classification can be used to perform text classification/labeling. txtai provides a light-weight wrapper around the zero-shot-classification pipeline in Hugging Face Transformers. This method works impressively well out of the box. Kudos to the Hugging Face team for the phenomenal work on zero-shot classification!
#
# The examples in this notebook pick the best matching label using a list of labels for a snippet of text.
#
# [tldrstory](https://github.com/neuml/tldrstory) has full-stack implementation of a zero-shot classification system using Streamlit, FastAPI and Hugging Face Transformers. There is also a [Medium article describing tldrstory](https://towardsdatascience.com/tldrstory-ai-powered-understanding-of-headlines-and-story-text-fc86abd702fc) and zero-shot classification. 
#

# + [markdown] id="Dk31rbYjSTYm"
# # Install dependencies
#
# Install txtai and all dependencies

# + id="XMQuuun2R06J"
# %%capture
# !pip install git+https://github.com/neuml/txtai

# + [markdown] id="PNPJ95cdTKSS"
# # Create a Labels instance
#
# The Labels instance is the main entrypoint for zero-shot classification. This is a light-weight wrapper around the zero-shot-classification pipeline in Hugging Face Transformers.
#
# In addition to the default model, additional models can be found on the [Hugging Face model hub](https://huggingface.co/models?search=mnli).
#

# + id="nTDwXOUeTH2-"
# %%capture

from txtai.pipeline import Labels

# Create labels model
labels = Labels()

# Alternate models can be used via passing the model path as shown below
# labels = Labels("roberta-large-mnli")

# + [markdown] id="-vGR_piwZZO6"
# # Applying labels to text
#
# The example below shows how a zero-shot classifier can be applied to arbitary text. The default model for the zero-shot classification pipeline is *bart-large-mnli*. 
#
# Look at the results below. It's nothing short of amazing✨ how well it performs. These aren't all simple even for a human. For example, intercepted was purposely picked as that is more common in football than basketball. The amount of knowledge stored in larger Transformer models continues to impress me. 

# + colab={"base_uri": "https://localhost:8080/"} id="-K2YJJzsVtfq" outputId="ce29f32c-49da-4178-ae27-7c444df62340"
sections = ["Dodgers lose again, give up 3 HRs in a loss to the Giants",
            "Giants 5 Cardinals 4 final in extra innings",
            "Dodgers drop Game 2 against the Giants, 5-4",
            "Flyers 4 Lightning 1 final. 45 saves for the Lightning.",
            "Slashing, penalty, 2 minute power play coming up",
            "What a stick save!",
            "Leads the NFL in sacks with 9.5",
            "UCF 38 Temple 13",
            "With the 30 yard completion, down to the 10 yard line",
            "Drains the 3pt shot!!, 0:15 remaining in the game",
            "Intercepted! Drives down the court and shoots for the win",
            "Massive dunk!!! they are now up by 15 with 2 minutes to go"]

# List of labels
tags = ["Baseball", "Football", "Hockey", "Basketball"]

print("%-75s %s" % ("Text", "Label"))

for text in sections:
    print("%-75s %s" % (text, labels(text, tags)[0][0]))

# + [markdown] id="t-tGAzCxsHLy"
# # Let's try emoji 😀
#
# Does the model have knowledge of emoji? Check out the run below, sure looks like it does! Notice the labels are applied based on the perspective from which the information is presented. 

# + colab={"base_uri": "https://localhost:8080/"} id="uIf064M9pbjn" outputId="6cf15ade-286f-4167-d7d6-449849bacd16"
tags = ["😀", "😡"]

for text in sections:
    print("%-75s %s" % (text, labels(text, tags)[0][0]))

StarcoderdataJupyterScriptsDedupFiltered