Aan de slagGa gratis aan de slag

Dynamic category assignment

Dynamic category assignment enables a model to classify text into predefined categories, even without prior training for those categories.

Using Hugging Face’s pipeline() for the zero-shot-classification task, provide the text and predefined categories to identify the best match.

Build a classifier to predict the label for the input text, which is a news headline already loaded for you.

The pipelines from the transformers library is preloaded for your convenience.

Note: We are using a customized version of the pipeline to help you learn how to use these functions without needing to download the model.

Deze oefening maakt deel uit van de cursus

Working with Hugging Face

Cursus bekijken

Oefeninstructies

  • Build the pipeline and save as classifier.
  • Create a list of the labels - "politics", "science", "sports" - and save as categories.
  • Predict the label of text using the classifier and predefined categories.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

text = "AI-powered robots assist in complex brain surgeries with precision."

# Create the pipeline
____ = pipeline(____="zero-shot-classification", ____="facebook/bart-large-mnli")

# Create the categories list
categories = ["politics", "____", "____"]

# Predict the output
output = ____(____, ____)

# Print the top label and its score
print(f"Top Label: {output['labels'][0]} with score: {output['scores'][0]}")
Code bewerken en uitvoeren