"In this session we are going to learn the power of transformers."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Transformers\n",
"As we saw, transformers are an extremely powerful architecture capable of performing many popular NLP tasks.\n",
"\n",
"A well-known transformer model repository is available at https://huggingface.co/. \n",
"\n",
"Let's see how to use it. To go deeper, consult the Hugging tutorial (https://huggingface.co/learn/nlp-course/chapter1/1).\n",
"\n",
"The transformers package requires to have installed Pytorch or TensorFlow. Check the installation details if you want to configure your environment well. For learning purposes, we are going to install Pytorch.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First of all, you should install Hugging Face. Execute:\n",
"* pip install torch transformers"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: torch in /home/cif/anaconda3/lib/python3.11/site-packages (2.3.0)\n",
"Requirement already satisfied: transformers in /home/cif/anaconda3/lib/python3.11/site-packages (4.40.2)\n",
"Requirement already satisfied: filelock in /home/cif/anaconda3/lib/python3.11/site-packages (from torch) (3.13.4)\n",
"Requirement already satisfied: typing-extensions>=4.8.0 in /home/cif/anaconda3/lib/python3.11/site-packages (from torch) (4.11.0)\n",
"Requirement already satisfied: sympy in /home/cif/anaconda3/lib/python3.11/site-packages (from torch) (1.12)\n",
"Requirement already satisfied: networkx in /home/cif/anaconda3/lib/python3.11/site-packages (from torch) (3.3)\n",
"Requirement already satisfied: jinja2 in /home/cif/anaconda3/lib/python3.11/site-packages (from torch) (3.1.3)\n",
"Requirement already satisfied: fsspec in /home/cif/anaconda3/lib/python3.11/site-packages (from torch) (2023.10.0)\n",
"Requirement already satisfied: nvidia-cuda-nvrtc-cu12==12.1.105 in /home/cif/anaconda3/lib/python3.11/site-packages (from torch) (12.1.105)\n",
"Requirement already satisfied: nvidia-cuda-runtime-cu12==12.1.105 in /home/cif/anaconda3/lib/python3.11/site-packages (from torch) (12.1.105)\n",
"Requirement already satisfied: nvidia-cuda-cupti-cu12==12.1.105 in /home/cif/anaconda3/lib/python3.11/site-packages (from torch) (12.1.105)\n",
"Requirement already satisfied: nvidia-cudnn-cu12==8.9.2.26 in /home/cif/anaconda3/lib/python3.11/site-packages (from torch) (8.9.2.26)\n",
"Requirement already satisfied: nvidia-cublas-cu12==12.1.3.1 in /home/cif/anaconda3/lib/python3.11/site-packages (from torch) (12.1.3.1)\n",
"Requirement already satisfied: nvidia-cufft-cu12==11.0.2.54 in /home/cif/anaconda3/lib/python3.11/site-packages (from torch) (11.0.2.54)\n",
"Requirement already satisfied: nvidia-curand-cu12==10.3.2.106 in /home/cif/anaconda3/lib/python3.11/site-packages (from torch) (10.3.2.106)\n",
"Requirement already satisfied: nvidia-cusolver-cu12==11.4.5.107 in /home/cif/anaconda3/lib/python3.11/site-packages (from torch) (11.4.5.107)\n",
"Requirement already satisfied: nvidia-cusparse-cu12==12.1.0.106 in /home/cif/anaconda3/lib/python3.11/site-packages (from torch) (12.1.0.106)\n",
"Requirement already satisfied: nvidia-nccl-cu12==2.20.5 in /home/cif/anaconda3/lib/python3.11/site-packages (from torch) (2.20.5)\n",
"Requirement already satisfied: nvidia-nvtx-cu12==12.1.105 in /home/cif/anaconda3/lib/python3.11/site-packages (from torch) (12.1.105)\n",
"Requirement already satisfied: triton==2.3.0 in /home/cif/anaconda3/lib/python3.11/site-packages (from torch) (2.3.0)\n",
"Requirement already satisfied: nvidia-nvjitlink-cu12 in /home/cif/anaconda3/lib/python3.11/site-packages (from nvidia-cusolver-cu12==11.4.5.107->torch) (12.4.127)\n",
"Requirement already satisfied: huggingface-hub<1.0,>=0.19.3 in /home/cif/anaconda3/lib/python3.11/site-packages (from transformers) (0.23.0)\n",
"Requirement already satisfied: numpy>=1.17 in /home/cif/anaconda3/lib/python3.11/site-packages (from transformers) (1.26.4)\n",
"Requirement already satisfied: packaging>=20.0 in /home/cif/anaconda3/lib/python3.11/site-packages (from transformers) (24.0)\n",
"Requirement already satisfied: pyyaml>=5.1 in /home/cif/anaconda3/lib/python3.11/site-packages (from transformers) (6.0.1)\n",
"Requirement already satisfied: regex!=2019.12.17 in /home/cif/anaconda3/lib/python3.11/site-packages (from transformers) (2023.12.25)\n",
"Requirement already satisfied: requests in /home/cif/anaconda3/lib/python3.11/site-packages (from transformers) (2.31.0)\n",
"Requirement already satisfied: tokenizers<0.20,>=0.19 in /home/cif/anaconda3/lib/python3.11/site-packages (from transformers) (0.19.1)\n",
"Requirement already satisfied: safetensors>=0.4.1 in /home/cif/anaconda3/lib/python3.11/site-packages (from transformers) (0.4.3)\n",
"Requirement already satisfied: tqdm>=4.27 in /home/cif/anaconda3/lib/python3.11/site-packages (from transformers) (4.66.2)\n",
"Requirement already satisfied: MarkupSafe>=2.0 in /home/cif/anaconda3/lib/python3.11/site-packages (from jinja2->torch) (2.1.5)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in /home/cif/anaconda3/lib/python3.11/site-packages (from requests->transformers) (2.0.4)\n",
"Requirement already satisfied: idna<4,>=2.5 in /home/cif/anaconda3/lib/python3.11/site-packages (from requests->transformers) (3.7)\n",
"Requirement already satisfied: urllib3<3,>=1.21.1 in /home/cif/anaconda3/lib/python3.11/site-packages (from requests->transformers) (2.2.1)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /home/cif/anaconda3/lib/python3.11/site-packages (from requests->transformers) (2024.2.2)\n",
"Requirement already satisfied: mpmath>=0.19 in /home/cif/anaconda3/lib/python3.11/site-packages (from sympy->torch) (1.3.0)\n"
"ner(\"Peter has studied at Universidad Politécnica de Madrid in Madrid, Spain\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Summarization\n",
"Let's generate a summary."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[{'summary_text': 'Wopke Hoekstra, the EU commissioner for climate action, said Europe had no choice but to press ahead with strong measures to cut greenhouse gases. He said more attention was needed to help businesses thrive in a low-carbon world.'}]\n"
"Europe’s climate chief has warned against politicians trying to use the climate crisis as a wedge issue in the forthcoming EU parliament elections, calling instead for climate policy that will bring wider economic benefits.\n",
"Wopke Hoekstra, the EU commissioner for climate action, said Europe had no choice but to press ahead with strong measures to cut greenhouse gases, whoever was in power, but added that more attention was needed to help businesses thrive in a low-carbon world.\n",
"He said: “There is no alternative than to continue with climate action. We need to continue in the direction of travel we have set. We need to speed up our pace.”\n",
"Rightwing parties are forecast in polls to do well in the election, to be held from 6 to 9 June, largely at the expense of the Greens and socialist parties. Protests by farmers in EU capitals have attacked climate policies, and some rightwing parties have stepped up anti-green rhetoric.\n",
"sentences = [\"Nadal has won the last match\", \"There is an election in Bulgaria\", \"The oil price is very high\", \"The new film by Almodovar has been just released\"]\n",
"* [Hugging Face Tutorial](https://huggingface.co/learn/nlp-course/chapter1/1) "
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"## Licence"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"The notebook is freely licensed under under the [Creative Commons Attribution Share-Alike license](https://creativecommons.org/licenses/by/2.0/). \n",