from transformers import pipeline

Sentiment analysis

classifier = pipeline('sentiment-analysis')
classifier("Ihave waiting for a course my whole life.")
[{'label': 'POSITIVE', 'score': 0.9817631840705872}]
alist = ["Covid is good", "I love covid"]
classifier(alist)
[{'label': 'POSITIVE', 'score': 0.9998581409454346},
 {'label': 'POSITIVE', 'score': 0.999816358089447}]

Zero-shot classification

classifier = pipeline("zero-shot-classification")
classifier("This is a sensitive topic on transport and libarary", 
          candidate_labels=["education", "math", "business"])
{'sequence': 'This is a sensitive topic on transport and libarary',
 'labels': ['business', 'education', 'math'],
 'scores': [0.5921958684921265, 0.22866113483905792, 0.17914298176765442]}

Text generation

generator = pipeline("text-generation")
generator("In this notebook, we will")
Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.
[{'generated_text': 'In this notebook, we will create several templates to illustrate how to generate the JavaScript code. For simplicity, we used the Angular JS example from one of our previous posts.\n\nIn fact, we would not think you would not understand. The above'}]
gen_gpt2 = pipeline("text-generation", model="distilgpt2")
gen_gpt2("In this pandas notebook, we will", max_lenght=20)
Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.
[{'generated_text': "In this pandas notebook, we will be using the first part of the first part of the series on Pandas as a learning tool for students to learn from Pandas. In the next post, we'll look at how to use both learning tools"}]

Mask filling

unmasker = pipeline("fill-mask")
unmasker("This notebook will show <mask> direction", top_k=2)
[{'sequence': 'This notebook will show visual direction',
  'score': 0.05728420987725258,
  'token': 7133,
  'token_str': ' visual'},
 {'sequence': 'This notebook will show editorial direction',
  'score': 0.053508173674345016,
  'token': 8161,
  'token_str': ' editorial'}]

Named entity recognition

ner = pipeline("ner", grouped_entities=True)
ner("I am Dan P who carry out research at Monash Uni in Melbourne City")
/home/danph/.pyenv/versions/3.8.5/envs/.ml/lib/python3.8/site-packages/transformers/pipelines/token_classification.py:154: UserWarning: `grouped_entities` is deprecated and will be removed in version v5.0.0, defaulted to `aggregation_strategy="AggregationStrategy.SIMPLE"` instead.
  warnings.warn(
[{'entity_group': 'PER',
  'score': 0.9955268,
  'word': 'Dan P',
  'start': 5,
  'end': 10},
 {'entity_group': 'ORG',
  'score': 0.991117,
  'word': 'Monash Uni',
  'start': 37,
  'end': 47},
 {'entity_group': 'LOC',
  'score': 0.971781,
  'word': 'Melbourne City',
  'start': 51,
  'end': 65}]

Question answering

qa = pipeline("question-answering")
qa(question="Where to I work",
   context="I am Dan P who carry out research at Monash Uni in Melbourne City")
{'score': 0.8566566705703735, 'start': 37, 'end': 47, 'answer': 'Monash Uni'}

Summarization

summarizer = pipeline("summarization")
article = """

The ongoing discussions within the presidential palace in Kabul are “utterly extraordinary” after two decades of war, CNN International Security Editor Nick Paton Walsh reports.

“This has been a morning of stunning events and that looks like we are heading towards some sort of transitional government here,” Paton Walsh said. He said names are being floated around, though nothing is confirmed, and President Ashraf Ghani would need to agree to step aside to make way for a transitional administration.

Yesterday Ghani made a brief but sombre address to the nation in which he said he was consulting with elders and other leaders both inside and outside of the country. In the short speech, he told the Afghan people his "focus is to avoid further instability, aggression and displacement," but he did not resign.

As talks on Sunday continue, Paton Walsh said there hasn’t been evidence of Taliban fighters moving into the city. Earlier panic appeared to be a clash around a bank where people were trying to withdraw money.

“I've heard sporadic gunfire here but that seems to be traffic disputes. A quick drive around the city has shown traffic has dissipated until you get towards the airport, so utter chaos and panic here. The traffic in the skies we saw around the embassy appears to have quietened as well so perhaps that might suggest some of that operation is winding up,” he continued.
The apparently last-ditch diplomatic efforts would hopefully avoid the Taliban presumably moving to its next phase of slowly entering the city, which Paton Walsh said would “not be remotely pleasant for anybody living here.”

“There will be elements of resistance too so I think everybody would prefer to avoid that kind of situation,” he added.

"""
summarizer(article)
/home/danph/.pyenv/versions/3.8.5/envs/.ml/lib/python3.8/site-packages/torch/_tensor.py:575: UserWarning: floor_divide is deprecated, and will be removed in a future version of pytorch. It currently rounds toward 0 (like the 'trunc' function NOT 'floor'). This results in incorrect rounding for negative values.
To keep the current behavior, use torch.div(a, b, rounding_mode='trunc'), or for actual floor division, use torch.div(a, b, rounding_mode='floor'). (Triggered internally at  /pytorch/aten/src/ATen/native/BinaryOps.cpp:467.)
  return torch.floor_divide(self, other)
[{'summary_text': ' President Ashraf Ghani made a brief but sombre address to the nation in which he said he was consulting with elders and other leaders both inside and outside of the country . CNN International Security Editor Nick Paton Walsh said there hasn’t been evidence of Taliban fighters moving into the city .'}]

Translation

translator = pipeline("translation", model="t5-base")
/home/danph/.pyenv/versions/3.8.5/envs/.ml/lib/python3.8/site-packages/transformers/pipelines/__init__.py:497: UserWarning: "translation" task was used, instead of "translation_XX_to_YY", defaulting to "translation_en_to_de"
  warnings.warn(
translator("Hi, my name is Dan")
[{'translation_text': 'Hallo, mein Name ist Dan'}]