Transformers_text_example.ipynb Open in SWAN Download

Transformers for text processing

Transformers provides APIs and tools to easily download and train state-of-the-art pretrained models. Credits: Huggingface documentation

Text generation

In [1]:
from transformers import pipeline

# Create the text-generation pipeline with GPU
generator = pipeline("text-generation", device=0)  # Specify the GPU device index
No model was supplied, defaulted to gpt2 and revision 6c0e608 (https://huggingface.co/gpt2).
Using a pipeline without specifying a model name and revision in production is not recommended.
In [2]:
# Generate text from a prompt
generator("In this notebook, we show you how to")
/cvmfs/sft-nightlies.cern.ch/lcg/views/dev4cuda/Mon/x86_64-centos7-gcc11-opt/lib/python3.9/site-packages/transformers/generation/utils.py:1219: UserWarning: You have modified the pretrained model configuration to control generation. This is a deprecated strategy to control generation and will be removed soon, in a future version. Please use a generation configuration file (see https://huggingface.co/docs/transformers/main_classes/text_generation)
  warnings.warn(
Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.
/cvmfs/sft-nightlies.cern.ch/lcg/views/dev4cuda/Mon/x86_64-centos7-gcc11-opt/lib/python3.9/site-packages/transformers/generation/utils.py:1313: UserWarning: Using `max_length`'s default (50) to control the generation length. This behaviour is deprecated and will be removed from the config in v5 of Transformers -- we recommend using `max_new_tokens` to control the maximum length of the generation.
  warnings.warn(
Out[2]:
[{'generated_text': 'In this notebook, we show you how to run and deploy custom Docker builds (Rockskin Docker Compile script). We also show how to add a new project for the current directory in a bash script.\n\nIn an example, suppose'}]

Summarization

In [3]:
# Create the summarization pipeline with GPU
summarizer = pipeline("summarization", device=0)  # Specify the GPU device index
No model was supplied, defaulted to sshleifer/distilbart-cnn-12-6 and revision a4f8f3e (https://huggingface.co/sshleifer/distilbart-cnn-12-6).
Using a pipeline without specifying a model name and revision in production is not recommended.
In [4]:
# text from the CERN website
summarizer(
"""
What is the Higgs boson?
Animation to show that a particle can also be thought of as a wave in a field
In quantum field theory, particles can be described as waves in a field (Image: Piotr Traczyk/CERN)

To answer this question needs an exploration into the quantum world and how particles interact…
The particle that we now call the Higgs boson first appeared in a scientific paper written by Peter Higgs in 1964. At that time, physicists were working on describing the weak force – one of the four fundamental forces of Nature – using a framework called quantum field theory.

Particle, wave or both?
Quantum field theory describes the microscopic world of particles very differently to everyday life. Fundamental “quantum fields” fill the universe and dictate what nature can and cannot do. In this description, every particle can be represented by a wave in a “field”, similar to a ripple on the surface of a vast ocean. One example is the photon, the particle of light, which is a wave in the electromagnetic field. 

Force carriers
When particles interact with one another, they exchange “force carriers”. These force carriers are particles and can also be described as waves in their respective fields. For example, when two electrons interact, they do so by exchanging photons – photons are the force carriers of the electromagnetic interaction.

Symmetry
Another important component of this picture is symmetry. Just like a shape can be called symmetrical if it doesn’t change when rotated or flipped, similar requirements are placed on the laws of Nature.
For example, the electrical force between particles with an electrical charge of one will always be the same, irrespective of whether the particle is an electron, muon or proton. Such symmetries form the basis and define the structure of the theory.
"""
)
Out[4]:
[{'summary_text': ' In quantum field theory, particles can be described as waves in a field . Fundamental ‘quantum fields’ fill the universe and dictate what nature can and cannot do . The Higgs boson first appeared in a scientific paper written by Peter Higgs in 1964 .'}]

Translation

In [ ]:
# sentencepiece is needed for this example
# pip install **and restart the notebook**

!pip install sentencepiece
In [1]:
from transformers import pipeline

# Create the translator pipeline with GPU
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-fr", device=0)
#translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-fr")
/cvmfs/sft-nightlies.cern.ch/lcg/views/dev4cuda/Mon/x86_64-centos7-gcc11-opt/lib/python3.9/site-packages/transformers/models/marian/tokenization_marian.py:194: UserWarning: Recommended: pip install sacremoses.
  warnings.warn("Recommended: pip install sacremoses.")
In [4]:
# text from the CERN website

translator(
"""
The accelerator complex at CERN is a succession of machines that accelerate particles to increasingly higher energies. 
Each machine boosts the energy of a beam of particles before injecting it into the next machine in the sequence. 
In the Large Hadron Collider (LHC) – the last element in this chain – particle beams are accelerated up to the record energy of 6.5 TeV per beam.
""")
Out[4]:
[{'translation_text': "Le complexe d'accélérateur du CERN est une succession de machines qui accélèrent les particules à des énergies de plus en plus élevées. Chaque machine stimule l'énergie d'un faisceau de particules avant de l'injecter dans la machine suivante dans la séquence. Dans le Grand Collisionneur de Hadron (LHC) – le dernier élément de cette chaîne – les faisceaux de particules sont accélérés jusqu'à l'énergie record de 6,5 TeV par faisceau."}]
In [ ]: