diff --git a/nlp/0_1_LLM.ipynb b/nlp/0_1_LLM.ipynb new file mode 100644 index 0000000..75833f2 --- /dev/null +++ b/nlp/0_1_LLM.ipynb @@ -0,0 +1,742 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "skip" + } + }, + "source": [ + "![](images/EscUpmPolit_p.gif \"UPM\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "skip" + } + }, + "source": [ + "# Course Notes for Learning Intelligent Systems" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "skip" + } + }, + "source": [ + "Department of Telematic Engineering Systems, Universidad Politécnica de Madrid, © Carlos A. Iglesias" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "# Table of Contents\n", + "* [Objectives](#Objectives)\n", + "* [Transformers](#Transformers)\n", + "* [Use cases: how to use pipelines](#Use-cases:-how-to-usepipelines)\n", + " * Sentiment Analysis\n", + " * Translation\n", + " * Masked Word Completion\n", + " * Text generation\n", + " * Question Answering\n", + " * Text to Speech\n", + "* [References](#References)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Objectives" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "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/miniconda3/lib/python3.12/site-packages (2.7.0)\n", + "Requirement already satisfied: transformers in /home/cif/miniconda3/lib/python3.12/site-packages (4.51.3)\n", + "Requirement already satisfied: filelock in /home/cif/miniconda3/lib/python3.12/site-packages (from torch) (3.18.0)\n", + "Requirement already satisfied: typing-extensions>=4.10.0 in /home/cif/miniconda3/lib/python3.12/site-packages (from torch) (4.12.2)\n", + "Requirement already satisfied: setuptools in /home/cif/miniconda3/lib/python3.12/site-packages (from torch) (75.8.0)\n", + "Requirement already satisfied: sympy>=1.13.3 in /home/cif/miniconda3/lib/python3.12/site-packages (from torch) (1.13.3)\n", + "Requirement already satisfied: networkx in /home/cif/miniconda3/lib/python3.12/site-packages (from torch) (3.4.2)\n", + "Requirement already satisfied: jinja2 in /home/cif/miniconda3/lib/python3.12/site-packages (from torch) (3.1.6)\n", + "Requirement already satisfied: fsspec in /home/cif/miniconda3/lib/python3.12/site-packages (from torch) (2025.3.2)\n", + "Requirement already satisfied: nvidia-cuda-nvrtc-cu12==12.6.77 in /home/cif/miniconda3/lib/python3.12/site-packages (from torch) (12.6.77)\n", + "Requirement already satisfied: nvidia-cuda-runtime-cu12==12.6.77 in /home/cif/miniconda3/lib/python3.12/site-packages (from torch) (12.6.77)\n", + "Requirement already satisfied: nvidia-cuda-cupti-cu12==12.6.80 in /home/cif/miniconda3/lib/python3.12/site-packages (from torch) (12.6.80)\n", + "Requirement already satisfied: nvidia-cudnn-cu12==9.5.1.17 in /home/cif/miniconda3/lib/python3.12/site-packages (from torch) (9.5.1.17)\n", + "Requirement already satisfied: nvidia-cublas-cu12==12.6.4.1 in /home/cif/miniconda3/lib/python3.12/site-packages (from torch) (12.6.4.1)\n", + "Requirement already satisfied: nvidia-cufft-cu12==11.3.0.4 in /home/cif/miniconda3/lib/python3.12/site-packages (from torch) (11.3.0.4)\n", + "Requirement already satisfied: nvidia-curand-cu12==10.3.7.77 in /home/cif/miniconda3/lib/python3.12/site-packages (from torch) (10.3.7.77)\n", + "Requirement already satisfied: nvidia-cusolver-cu12==11.7.1.2 in /home/cif/miniconda3/lib/python3.12/site-packages (from torch) (11.7.1.2)\n", + "Requirement already satisfied: nvidia-cusparse-cu12==12.5.4.2 in /home/cif/miniconda3/lib/python3.12/site-packages (from torch) (12.5.4.2)\n", + "Requirement already satisfied: nvidia-cusparselt-cu12==0.6.3 in /home/cif/miniconda3/lib/python3.12/site-packages (from torch) (0.6.3)\n", + "Requirement already satisfied: nvidia-nccl-cu12==2.26.2 in /home/cif/miniconda3/lib/python3.12/site-packages (from torch) (2.26.2)\n", + "Requirement already satisfied: nvidia-nvtx-cu12==12.6.77 in /home/cif/miniconda3/lib/python3.12/site-packages (from torch) (12.6.77)\n", + "Requirement already satisfied: nvidia-nvjitlink-cu12==12.6.85 in /home/cif/miniconda3/lib/python3.12/site-packages (from torch) (12.6.85)\n", + "Requirement already satisfied: nvidia-cufile-cu12==1.11.1.6 in /home/cif/miniconda3/lib/python3.12/site-packages (from torch) (1.11.1.6)\n", + "Requirement already satisfied: triton==3.3.0 in /home/cif/miniconda3/lib/python3.12/site-packages (from torch) (3.3.0)\n", + "Requirement already satisfied: huggingface-hub<1.0,>=0.30.0 in /home/cif/miniconda3/lib/python3.12/site-packages (from transformers) (0.30.2)\n", + "Requirement already satisfied: numpy>=1.17 in /home/cif/miniconda3/lib/python3.12/site-packages (from transformers) (2.2.4)\n", + "Requirement already satisfied: packaging>=20.0 in /home/cif/miniconda3/lib/python3.12/site-packages (from transformers) (24.2)\n", + "Requirement already satisfied: pyyaml>=5.1 in /home/cif/miniconda3/lib/python3.12/site-packages (from transformers) (6.0.2)\n", + "Requirement already satisfied: regex!=2019.12.17 in /home/cif/miniconda3/lib/python3.12/site-packages (from transformers) (2024.11.6)\n", + "Requirement already satisfied: requests in /home/cif/miniconda3/lib/python3.12/site-packages (from transformers) (2.32.3)\n", + "Requirement already satisfied: tokenizers<0.22,>=0.21 in /home/cif/miniconda3/lib/python3.12/site-packages (from transformers) (0.21.1)\n", + "Requirement already satisfied: safetensors>=0.4.3 in /home/cif/miniconda3/lib/python3.12/site-packages (from transformers) (0.5.3)\n", + "Requirement already satisfied: tqdm>=4.27 in /home/cif/miniconda3/lib/python3.12/site-packages (from transformers) (4.67.1)\n", + "Requirement already satisfied: mpmath<1.4,>=1.1.0 in /home/cif/miniconda3/lib/python3.12/site-packages (from sympy>=1.13.3->torch) (1.3.0)\n", + "Requirement already satisfied: MarkupSafe>=2.0 in /home/cif/miniconda3/lib/python3.12/site-packages (from jinja2->torch) (3.0.2)\n", + "Requirement already satisfied: charset-normalizer<4,>=2 in /home/cif/miniconda3/lib/python3.12/site-packages (from requests->transformers) (3.3.2)\n", + "Requirement already satisfied: idna<4,>=2.5 in /home/cif/miniconda3/lib/python3.12/site-packages (from requests->transformers) (3.7)\n", + "Requirement already satisfied: urllib3<3,>=1.21.1 in /home/cif/miniconda3/lib/python3.12/site-packages (from requests->transformers) (2.3.0)\n", + "Requirement already satisfied: certifi>=2017.4.17 in /home/cif/miniconda3/lib/python3.12/site-packages (from requests->transformers) (2025.1.31)\n" + ] + } + ], + "source": [ + "!pip install torch transformers" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Requirement already satisfied: huggingface_hub[hf_xet] in /home/cif/miniconda3/lib/python3.12/site-packages (0.30.2)\n", + "Requirement already satisfied: filelock in /home/cif/miniconda3/lib/python3.12/site-packages (from huggingface_hub[hf_xet]) (3.18.0)\n", + "Requirement already satisfied: fsspec>=2023.5.0 in /home/cif/miniconda3/lib/python3.12/site-packages (from huggingface_hub[hf_xet]) (2025.3.2)\n", + "Requirement already satisfied: packaging>=20.9 in /home/cif/miniconda3/lib/python3.12/site-packages (from huggingface_hub[hf_xet]) (24.2)\n", + "Requirement already satisfied: pyyaml>=5.1 in /home/cif/miniconda3/lib/python3.12/site-packages (from huggingface_hub[hf_xet]) (6.0.2)\n", + "Requirement already satisfied: requests in /home/cif/miniconda3/lib/python3.12/site-packages (from huggingface_hub[hf_xet]) (2.32.3)\n", + "Requirement already satisfied: tqdm>=4.42.1 in /home/cif/miniconda3/lib/python3.12/site-packages (from huggingface_hub[hf_xet]) (4.67.1)\n", + "Requirement already satisfied: typing-extensions>=3.7.4.3 in /home/cif/miniconda3/lib/python3.12/site-packages (from huggingface_hub[hf_xet]) (4.12.2)\n", + "Requirement already satisfied: hf-xet>=0.1.4 in /home/cif/miniconda3/lib/python3.12/site-packages (from huggingface_hub[hf_xet]) (1.1.0)\n", + "Requirement already satisfied: charset-normalizer<4,>=2 in /home/cif/miniconda3/lib/python3.12/site-packages (from requests->huggingface_hub[hf_xet]) (3.3.2)\n", + "Requirement already satisfied: idna<4,>=2.5 in /home/cif/miniconda3/lib/python3.12/site-packages (from requests->huggingface_hub[hf_xet]) (3.7)\n", + "Requirement already satisfied: urllib3<3,>=1.21.1 in /home/cif/miniconda3/lib/python3.12/site-packages (from requests->huggingface_hub[hf_xet]) (2.3.0)\n", + "Requirement already satisfied: certifi>=2017.4.17 in /home/cif/miniconda3/lib/python3.12/site-packages (from requests->huggingface_hub[hf_xet]) (2025.1.31)\n" + ] + } + ], + "source": [ + "!pip install huggingface_hub[hf_xet]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Use cases: how to use pipelines" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Sentiment Analysis\n", + "Let's classify sentiments" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[{'label': 'positive', 'score': 0.974217414855957}]\n", + "[{'label': 'negative', 'score': 0.9310991168022156}, {'label': 'neutral', 'score': 0.5152539610862732}]\n" + ] + } + ], + "source": [ + "from transformers import pipeline\n", + "\n", + "from transformers import logging\n", + "\n", + "logging.set_verbosity_error()\n", + "#logging.set_verbosity_warning()\n", + "\n", + "model_sentiment = \"cardiffnlp/twitter-roberta-base-sentiment-latest\"\n", + "\n", + "sentiment_pipe = pipeline(\"sentiment-analysis\", model=model_sentiment)\n", + "\n", + "print(sentiment_pipe(\"I love LLMs.\"))\n", + "\n", + "#We pan \n", + "print(sentiment_pipe([\"I hate LLMs.\", \"I don't care about LLMs\"]))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Translation\n", + "Let's translate a sentence" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[{'translation_text': 'Il s’agit du cours de traitement des langues naturelles'}]\n" + ] + } + ], + "source": [ + "from transformers import pipeline\n", + "\n", + "#if no model is specified, it uses google-t5\n", + "translator_en_fr = pipeline(\"translation_en_to_fr\")\n", + "\n", + "print(translator_en_fr(\"This is the course of Natural Language Processing\", max_length=40))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Masked word completion\n", + "Generate words for a mask" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[{'score': 0.324342280626297,\n", + " 'token': 2795,\n", + " 'token_str': 'table',\n", + " 'sequence': 'hello, im am eating at a table.'},\n", + " {'score': 0.3150157630443573,\n", + " 'token': 4825,\n", + " 'token_str': 'restaurant',\n", + " 'sequence': 'hello, im am eating at a restaurant.'},\n", + " {'score': 0.07178673893213272,\n", + " 'token': 3347,\n", + " 'token_str': 'bar',\n", + " 'sequence': 'hello, im am eating at a bar.'},\n", + " {'score': 0.042759791016578674,\n", + " 'token': 15736,\n", + " 'token_str': 'diner',\n", + " 'sequence': 'hello, im am eating at a diner.'},\n", + " {'score': 0.032276567071676254,\n", + " 'token': 28305,\n", + " 'token_str': 'buffet',\n", + " 'sequence': 'hello, im am eating at a buffet.'}]" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from transformers import pipeline\n", + "unmasker = pipeline('fill-mask', model='bert-base-uncased')\n", + "unmasker(\"Hello, Im am eating at a [MASK].\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Ner\n", + "Let's detect NER" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[{'entity_group': 'PER',\n", + " 'score': np.float32(0.9992756),\n", + " 'word': 'Peter',\n", + " 'start': 0,\n", + " 'end': 5},\n", + " {'entity_group': 'ORG',\n", + " 'score': np.float32(0.98043567),\n", + " 'word': 'Universidad Politécnica de Madrid',\n", + " 'start': 21,\n", + " 'end': 54},\n", + " {'entity_group': 'LOC',\n", + " 'score': np.float32(0.9985493),\n", + " 'word': 'Madrid',\n", + " 'start': 58,\n", + " 'end': 64},\n", + " {'entity_group': 'LOC',\n", + " 'score': np.float32(0.99971014),\n", + " 'word': 'Spain',\n", + " 'start': 66,\n", + " 'end': 71}]" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from transformers import pipeline\n", + "\n", + "ner = pipeline(\"ner\", aggregation_strategy=\"simple\")\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": 8, + "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" + ] + } + ], + "source": [ + "from transformers import pipeline\n", + "\n", + "summarizer = pipeline(\"summarization\", model=\"facebook/bart-large-cnn\")\n", + "\n", + "article = \"\"\"\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", + "\"\"\"\n", + "print(summarizer(article, max_length=130, min_length=30, do_sample=False)) " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Zero-shot classification\n", + "Classification without examples!" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'sequence': 'one day I will see the world',\n", + " 'labels': ['travel', 'cooking', 'dancing'],\n", + " 'scores': [0.9799639582633972, 0.010604996234178543, 0.009431015700101852]}" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from transformers import pipeline\n", + "classifier = pipeline('zero-shot-classification', model='roberta-large-mnli')\n", + "\n", + "sequence_to_classify = \"one day I will see the world\"\n", + "candidate_labels = ['travel', 'cooking', 'dancing']\n", + "classifier(sequence_to_classify, candidate_labels)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'sequence': 'The CEO had a strong handshake.',\n", + " 'labels': ['male', 'female'],\n", + " 'scores': [0.8384836912155151, 0.16151626408100128]}" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sequence_to_classify = \"The CEO had a strong handshake.\"\n", + "candidate_labels = ['male', 'female']\n", + "hypothesis_template = \"This text speaks about a {} profession.\"\n", + "classifier(sequence_to_classify, candidate_labels, hypothesis_template=hypothesis_template)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[{'sequence': 'Nadal has won the last match',\n", + " 'labels': ['sport', 'culture', 'economics', 'politics'],\n", + " 'scores': [0.8608437180519104,\n", + " 0.07932601124048233,\n", + " 0.03197354078292847,\n", + " 0.027856681495904922]},\n", + " {'sequence': 'There is an election in Bulgaria',\n", + " 'labels': ['politics', 'culture', 'economics', 'sport'],\n", + " 'scores': [0.962326169013977,\n", + " 0.015147211961448193,\n", + " 0.012851407751441002,\n", + " 0.009675218723714352]},\n", + " {'sequence': 'The oil price is very high',\n", + " 'labels': ['economics', 'culture', 'politics', 'sport'],\n", + " 'scores': [0.846241295337677,\n", + " 0.06119693070650101,\n", + " 0.046528540551662445,\n", + " 0.04603324830532074]},\n", + " {'sequence': 'The new film by Almodovar has been just released',\n", + " 'labels': ['culture', 'politics', 'sport', 'economics'],\n", + " 'scores': [0.7116525769233704,\n", + " 0.12886522710323334,\n", + " 0.10017038881778717,\n", + " 0.05931178107857704]}]" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "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", + "candidate_labels = ['sport', 'politics', 'culture', 'economics']\n", + "classifier(sentences, candidate_labels)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Text generation\n", + "Let's generate" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[{'generated_text': \"This articles aims evaluating transformers' capabilities, its performance or its usefulness when applied to specific cases in which these transformers can be useful.\\n\\nA typical transformers in Java are given in the article Transformers. Since Java has many different classes\"}]" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from transformers import pipeline\n", + "\n", + "generation = pipeline(\"text-generation\")\n", + "\n", + "generation(\"This articles aims evaluating transformers' capabilities\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Question-Answering\n", + "Let's create a QA!" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'score': 0.8235689401626587,\n", + " 'start': 52,\n", + " 'end': 77,\n", + " 'answer': 'Alcobendas, Madrid, Spain'}" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from transformers import pipeline\n", + "\n", + "qa = pipeline(\"question-answering\")\n", + "\n", + "qa(\n", + " question = \"Where was born Penelope Cruz?\",\n", + " context = '''\n", + " Cruz was born on April 28, 1974, in Alcobendas, Madrid, Spain. \n", + " In July 2010, Cruz married her Vicky Cristina Barcelona co-star, \n", + " Spanish actor Javier Bardem. The couple had begun dating early into filming, in 2007.\n", + " '''\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'score': 0.4068848490715027,\n", + " 'start': 126,\n", + " 'end': 202,\n", + " 'answer': 'Vicky Cristina Barcelona co-star,\\n Spanish actor Javier Bardem'}" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "qa(\n", + " question = \"Who is Penelope Cruz' husband?\",\n", + " context = '''\n", + " Cruz was born on April 28, 1974, in Alcobendas, Madrid, Spain. \n", + " In July 2010, Cruz married her Vicky Cristina Barcelona co-star,\n", + " Spanish actor Javier Bardem. The couple had begun dating early into filming, in 2007.\n", + " '''\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Text-to-Speech" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "\n", + " \n", + " " + ], + "text/plain": [ + "" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from transformers import pipeline\n", + "\n", + "pipe = pipeline(\"text-to-speech\", model=\"suno/bark-small\")\n", + "text = \"[clears throat] This is a test ... and I just took a long pause.\"\n", + "output = pipe(text)\n", + "\n", + "from IPython.display import Audio \n", + "\n", + "Audio(output[\"audio\"], rate=output[\"sampling_rate\"])" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "skip" + } + }, + "source": [ + "## References\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "skip" + } + }, + "source": [ + "* [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", + "\n", + "© Carlos A. Iglesias, Universidad Politécnica de Madrid." + ] + } + ], + "metadata": { + "celltoolbar": "Slideshow", + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.2" + }, + "latex_envs": { + "LaTeX_envs_menu_present": true, + "autocomplete": true, + "bibliofile": "biblio.bib", + "cite_by": "apalike", + "current_citInitial": 1, + "eqLabelWithNumbers": true, + "eqNumInitial": 1, + "hotkeys": { + "equation": "Ctrl-E", + "itemize": "Ctrl-I" + }, + "labels_anchors": false, + "latex_user_defs": false, + "report_style_numbering": false, + "user_envs_cfg": false + } + }, + "nbformat": 4, + "nbformat_minor": 4 +}