Natural Language Processing Python

NLP with Hugging Face Transformer

If we want state of the art transformer package for Natural Language Processing (NLP), then I would highly recommend Hugging Face Transformer
The Hugging Face Transformer is a State-of-the-art Natural Language Processing for Jax, Pytorch and TensorFlow
The Hugging Face Transformers (formerly known as pytorch-transformers and pytorch-pretrained-bert) provides general-purpose architectures (BERT, GPT-2, RoBERTa, XLM, DistilBert, XLNet…) for Natural Language Understanding (NLU) and Natural Language Generation (NLG) with over 32+ pretrained models in 100+ languages and deep interoperability between Jax, PyTorch and TensorFlow.

You can simple install the Hugging Face Transformer Python package using pip

pip install transformers

Here is a simple demo of using Hugging Face transformer for Question-Answers

from transformers import pipeline

question_answerer = pipeline("question-answering")

context = r"""
... Extractive Question Answering is the task of extracting an answer from a text given a question. An example of a
... question answering dataset is the SQuAD dataset, which is entirely based on that task. If you would like to fine-tune
... a model on a SQuAD task, you may leverage the examples/pytorch/question-answering/run_squad.py script.
... """

After executing this, you can ask questions like below and get the answers

>>> result = question_answerer(question="What is extractive question answering?", context=context)
>>> print(f"Answer: '{result['answer']}', score: {round(result['score'], 4)}, start: {result['start']}, end: {result['end']}")
Answer: 'the task of extracting an answer from a text given a question', score: 0.6177, start: 34, end: 95

>>> result = question_answerer(question="What is a good example of a question answering dataset?", context=context)
>>> print(f"Answer: '{result['answer']}', score: {round(result['score'], 4)}, start: {result['start']}, end: {result['end']}")
Answer: 'SQuAD dataset', score: 0.5152, start: 147, end: 160

Another demo of Hugging Face is on text generation.

from transformers import pipeline

text_generator = pipeline("text-generation")
print(text_generator("As far as I am concerned, I will", max_length=50, do_sample=False))

Output:

[{'generated_text': 'As far as I am concerned, I will be the first to admit that I am not a fan of the idea of a
"free market." I think that the idea of a free market is a bit of a stretch. I think that the idea'}]

Next we re-train the model as follows:

References:

Relevant Courses

October 6, 2021