{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "FlUZEghpZy-v" }, "source": [ "### In this section, we will learn how to finetune LLMs for Q/A tasks using HuggingFace 🤗\n", "### First, let's install HuggingFace libraries\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "2GZnvNB5KJ_c", "outputId": "044e6445-e655-4c3e-8425-4cc1f5ed6665" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: transformers in /usr/local/lib/python3.11/dist-packages (4.48.3)\n", "Requirement already satisfied: datasets in /usr/local/lib/python3.11/dist-packages (3.3.0)\n", "Requirement already satisfied: torch in /usr/local/lib/python3.11/dist-packages (2.5.1+cu124)\n", "Requirement already satisfied: filelock in /usr/local/lib/python3.11/dist-packages (from transformers) (3.17.0)\n", "Requirement already satisfied: huggingface-hub<1.0,>=0.24.0 in /usr/local/lib/python3.11/dist-packages (from transformers) (0.28.1)\n", "Requirement already satisfied: numpy>=1.17 in /usr/local/lib/python3.11/dist-packages (from transformers) (1.26.4)\n", "Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.11/dist-packages (from transformers) (24.2)\n", "Requirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.11/dist-packages (from transformers) (6.0.2)\n", "Requirement already satisfied: regex!=2019.12.17 in /usr/local/lib/python3.11/dist-packages (from transformers) (2024.11.6)\n", "Requirement already satisfied: requests in /usr/local/lib/python3.11/dist-packages (from transformers) (2.32.3)\n", "Requirement already satisfied: tokenizers<0.22,>=0.21 in /usr/local/lib/python3.11/dist-packages (from transformers) (0.21.0)\n", "Requirement already satisfied: safetensors>=0.4.1 in /usr/local/lib/python3.11/dist-packages (from transformers) (0.5.2)\n", "Requirement already satisfied: tqdm>=4.27 in /usr/local/lib/python3.11/dist-packages (from transformers) (4.67.1)\n", "Requirement already satisfied: pyarrow>=15.0.0 in /usr/local/lib/python3.11/dist-packages (from datasets) (17.0.0)\n", "Requirement already satisfied: dill<0.3.9,>=0.3.0 in /usr/local/lib/python3.11/dist-packages (from datasets) (0.3.8)\n", "Requirement already satisfied: pandas in /usr/local/lib/python3.11/dist-packages (from datasets) (2.2.2)\n", "Requirement already satisfied: xxhash in /usr/local/lib/python3.11/dist-packages (from datasets) (3.5.0)\n", "Requirement already satisfied: multiprocess<0.70.17 in /usr/local/lib/python3.11/dist-packages (from datasets) (0.70.16)\n", "Requirement already satisfied: fsspec<=2024.12.0,>=2023.1.0 in /usr/local/lib/python3.11/dist-packages (from fsspec[http]<=2024.12.0,>=2023.1.0->datasets) (2024.10.0)\n", "Requirement already satisfied: aiohttp in /usr/local/lib/python3.11/dist-packages (from datasets) (3.11.12)\n", "Requirement already satisfied: typing-extensions>=4.8.0 in /usr/local/lib/python3.11/dist-packages (from torch) (4.12.2)\n", "Requirement already satisfied: networkx in /usr/local/lib/python3.11/dist-packages (from torch) (3.4.2)\n", "Requirement already satisfied: jinja2 in /usr/local/lib/python3.11/dist-packages (from torch) (3.1.5)\n", "Requirement already satisfied: nvidia-cuda-nvrtc-cu12==12.4.127 in /usr/local/lib/python3.11/dist-packages (from torch) (12.4.127)\n", "Requirement already satisfied: nvidia-cuda-runtime-cu12==12.4.127 in /usr/local/lib/python3.11/dist-packages (from torch) (12.4.127)\n", "Requirement already satisfied: nvidia-cuda-cupti-cu12==12.4.127 in /usr/local/lib/python3.11/dist-packages (from torch) (12.4.127)\n", "Requirement already satisfied: nvidia-cudnn-cu12==9.1.0.70 in /usr/local/lib/python3.11/dist-packages (from torch) (9.1.0.70)\n", "Requirement already satisfied: nvidia-cublas-cu12==12.4.5.8 in /usr/local/lib/python3.11/dist-packages (from torch) (12.4.5.8)\n", "Requirement already satisfied: nvidia-cufft-cu12==11.2.1.3 in /usr/local/lib/python3.11/dist-packages (from torch) (11.2.1.3)\n", "Requirement already satisfied: nvidia-curand-cu12==10.3.5.147 in /usr/local/lib/python3.11/dist-packages (from torch) (10.3.5.147)\n", "Requirement already satisfied: nvidia-cusolver-cu12==11.6.1.9 in /usr/local/lib/python3.11/dist-packages (from torch) (11.6.1.9)\n", "Requirement already satisfied: nvidia-cusparse-cu12==12.3.1.170 in /usr/local/lib/python3.11/dist-packages (from torch) (12.3.1.170)\n", "Requirement already satisfied: nvidia-nccl-cu12==2.21.5 in /usr/local/lib/python3.11/dist-packages (from torch) (2.21.5)\n", "Requirement already satisfied: nvidia-nvtx-cu12==12.4.127 in /usr/local/lib/python3.11/dist-packages (from torch) (12.4.127)\n", "Requirement already satisfied: nvidia-nvjitlink-cu12==12.4.127 in /usr/local/lib/python3.11/dist-packages (from torch) (12.4.127)\n", "Requirement already satisfied: triton==3.1.0 in /usr/local/lib/python3.11/dist-packages (from torch) (3.1.0)\n", "Requirement already satisfied: sympy==1.13.1 in /usr/local/lib/python3.11/dist-packages (from torch) (1.13.1)\n", "Requirement already satisfied: mpmath<1.4,>=1.1.0 in /usr/local/lib/python3.11/dist-packages (from sympy==1.13.1->torch) (1.3.0)\n", "Requirement already satisfied: aiohappyeyeballs>=2.3.0 in /usr/local/lib/python3.11/dist-packages (from aiohttp->datasets) (2.4.6)\n", "Requirement already satisfied: aiosignal>=1.1.2 in /usr/local/lib/python3.11/dist-packages (from aiohttp->datasets) (1.3.2)\n", "Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.11/dist-packages (from aiohttp->datasets) (25.1.0)\n", "Requirement already satisfied: frozenlist>=1.1.1 in /usr/local/lib/python3.11/dist-packages (from aiohttp->datasets) (1.5.0)\n", "Requirement already satisfied: multidict<7.0,>=4.5 in /usr/local/lib/python3.11/dist-packages (from aiohttp->datasets) (6.1.0)\n", "Requirement already satisfied: propcache>=0.2.0 in /usr/local/lib/python3.11/dist-packages (from aiohttp->datasets) (0.2.1)\n", "Requirement already satisfied: yarl<2.0,>=1.17.0 in /usr/local/lib/python3.11/dist-packages (from aiohttp->datasets) (1.18.3)\n", "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.11/dist-packages (from requests->transformers) (3.4.1)\n", "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.11/dist-packages (from requests->transformers) (3.10)\n", "Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.11/dist-packages (from requests->transformers) (2.3.0)\n", "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.11/dist-packages (from requests->transformers) (2025.1.31)\n", "Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.11/dist-packages (from jinja2->torch) (3.0.2)\n", "Requirement already satisfied: python-dateutil>=2.8.2 in /usr/local/lib/python3.11/dist-packages (from pandas->datasets) (2.8.2)\n", "Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.11/dist-packages (from pandas->datasets) (2025.1)\n", "Requirement already satisfied: tzdata>=2022.7 in /usr/local/lib/python3.11/dist-packages (from pandas->datasets) (2025.1)\n", "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.11/dist-packages (from python-dateutil>=2.8.2->pandas->datasets) (1.17.0)\n" ] } ], "source": [ "!pip install transformers datasets torch" ] }, { "cell_type": "markdown", "metadata": { "id": "T7TODCfO5_fh" }, "source": [ "### Steps in finetuning LLMs model for Q/A task\n", "\n", "- Datasets\n", "- Model choosing\n", "- Vocabulary, Vectorizer\n", "- Training loop\n", "- Evaluation on the test data" ] }, { "cell_type": "markdown", "metadata": { "id": "ArIsaSOr7MFL" }, "source": [ "### Datasets\n", "\n", "We'll be choosing a simple Vietnamese Q/A dataset to finetune our LLM." ] }, { "cell_type": "markdown", "metadata": { "id": "ZeqWNfxoQR22" }, "source": [ "In this case, we'll be using the **UIT-ViQuAD** dataset.\n", "\n", "In Hugging Face, this dataset is available under the identifier ``taidng/UIT-ViQuAD2.0``. This version includes over 23,000 question-answer pairs from the original dataset, along with an additional 12,000 unanswerable questions" ] }, { "cell_type": "markdown", "metadata": { "id": "D_AXPu57bzUZ" }, "source": [ "### Choosing model to finetune." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "TlvkajEzNsug" }, "outputs": [], "source": [ "from datasets import load_dataset\n", "\n", "dataset = load_dataset(\"taidng/UIT-ViQuAD2.0\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "7-ZebmC-O3kg", "outputId": "5268fe12-2f71-4027-b151-9ae2b404ab22" }, "outputs": [ { "data": { "text/plain": [ "DatasetDict({\n", " train: Dataset({\n", " features: ['id', 'uit_id', 'title', 'context', 'question', 'answers', 'is_impossible', 'plausible_answers'],\n", " num_rows: 28454\n", " })\n", " validation: Dataset({\n", " features: ['id', 'uit_id', 'title', 'context', 'question', 'answers', 'is_impossible', 'plausible_answers'],\n", " num_rows: 3814\n", " })\n", " test: Dataset({\n", " features: ['id', 'uit_id', 'title', 'context', 'question', 'answers', 'is_impossible', 'plausible_answers'],\n", " num_rows: 7301\n", " })\n", "})" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dataset" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "kQllZpTKO9mh", "outputId": "96f815a3-b1dd-42d8-8b10-1c0c9cf54489" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "uit_000001\n", "Phạm Văn Đồng (1 tháng 3 năm 1906 – 29 tháng 4 năm 2000) là Thủ tướng đầu tiên của nước Cộng hòa Xã hội chủ nghĩa Việt Nam từ năm 1976 (từ năm 1981 gọi là Chủ tịch Hội đồng Bộ trưởng) cho đến khi nghỉ hưu năm 1987. Trước đó ông từng giữ chức vụ Thủ tướng Chính phủ Việt Nam Dân chủ Cộng hòa từ năm 1955 đến năm 1976. Ông là vị Thủ tướng Việt Nam tại vị lâu nhất (1955–1987). Ông là học trò, cộng sự của Chủ tịch Hồ Chí Minh. Ông có tên gọi thân mật là Tô, đây từng là bí danh của ông. Ông còn có tên gọi là Lâm Bá Kiệt khi làm Phó chủ nhiệm cơ quan Biện sự xứ tại Quế Lâm (Chủ nhiệm là Hồ Học Lãm).\n", "Tên gọi nào được Phạm Văn Đồng sử dụng khi làm Phó chủ nhiệm cơ quan Biện sự xứ tại Quế Lâm?\n", "{'text': ['Lâm Bá Kiệt'], 'answer_start': [507]}\n", "False\n", "None\n", "\n", "uit_000002\n", "Phạm Văn Đồng (1 tháng 3 năm 1906 – 29 tháng 4 năm 2000) là Thủ tướng đầu tiên của nước Cộng hòa Xã hội chủ nghĩa Việt Nam từ năm 1976 (từ năm 1981 gọi là Chủ tịch Hội đồng Bộ trưởng) cho đến khi nghỉ hưu năm 1987. Trước đó ông từng giữ chức vụ Thủ tướng Chính phủ Việt Nam Dân chủ Cộng hòa từ năm 1955 đến năm 1976. Ông là vị Thủ tướng Việt Nam tại vị lâu nhất (1955–1987). Ông là học trò, cộng sự của Chủ tịch Hồ Chí Minh. Ông có tên gọi thân mật là Tô, đây từng là bí danh của ông. Ông còn có tên gọi là Lâm Bá Kiệt khi làm Phó chủ nhiệm cơ quan Biện sự xứ tại Quế Lâm (Chủ nhiệm là Hồ Học Lãm).\n", "Phạm Văn Đồng giữ chức vụ gì trong bộ máy Nhà nước Cộng hòa Xã hội chủ nghĩa Việt Nam?\n", "{'text': ['Thủ tướng'], 'answer_start': [60]}\n", "False\n", "None\n", "\n", "uit_000003\n", "Phạm Văn Đồng (1 tháng 3 năm 1906 – 29 tháng 4 năm 2000) là Thủ tướng đầu tiên của nước Cộng hòa Xã hội chủ nghĩa Việt Nam từ năm 1976 (từ năm 1981 gọi là Chủ tịch Hội đồng Bộ trưởng) cho đến khi nghỉ hưu năm 1987. Trước đó ông từng giữ chức vụ Thủ tướng Chính phủ Việt Nam Dân chủ Cộng hòa từ năm 1955 đến năm 1976. Ông là vị Thủ tướng Việt Nam tại vị lâu nhất (1955–1987). Ông là học trò, cộng sự của Chủ tịch Hồ Chí Minh. Ông có tên gọi thân mật là Tô, đây từng là bí danh của ông. Ông còn có tên gọi là Lâm Bá Kiệt khi làm Phó chủ nhiệm cơ quan Biện sự xứ tại Quế Lâm (Chủ nhiệm là Hồ Học Lãm).\n", "Giai đoạn năm 1955-1976, Phạm Văn Đồng nắm giữ chức vụ gì?\n", "{'text': ['Thủ tướng Chính phủ Việt Nam Dân chủ Cộng hòa'], 'answer_start': [245]}\n", "False\n", "None\n", "\n", "uit_000004\n", "Phạm Văn Đồng (1 tháng 3 năm 1906 – 29 tháng 4 năm 2000) là Thủ tướng đầu tiên của nước Cộng hòa Xã hội chủ nghĩa Việt Nam từ năm 1976 (từ năm 1981 gọi là Chủ tịch Hội đồng Bộ trưởng) cho đến khi nghỉ hưu năm 1987. Trước đó ông từng giữ chức vụ Thủ tướng Chính phủ Việt Nam Dân chủ Cộng hòa từ năm 1955 đến năm 1976. Ông là vị Thủ tướng Việt Nam tại vị lâu nhất (1955–1987). Ông là học trò, cộng sự của Chủ tịch Hồ Chí Minh. Ông có tên gọi thân mật là Tô, đây từng là bí danh của ông. Ông còn có tên gọi là Lâm Bá Kiệt khi làm Phó chủ nhiệm cơ quan Biện sự xứ tại Quế Lâm (Chủ nhiệm là Hồ Học Lãm).\n", "Tên gọi nào được Phạm Văn Đồng sử dụng trước khi làm Phó chủ nhiệm cơ quan Biện sự xứ tại Quế Lâm?\n", "{'text': [], 'answer_start': []}\n", "True\n", "{'text': ['Lâm Bá Kiệt'], 'answer_start': [507]}\n", "\n", "uit_000005\n", "Phạm Văn Đồng (1 tháng 3 năm 1906 – 29 tháng 4 năm 2000) là Thủ tướng đầu tiên của nước Cộng hòa Xã hội chủ nghĩa Việt Nam từ năm 1976 (từ năm 1981 gọi là Chủ tịch Hội đồng Bộ trưởng) cho đến khi nghỉ hưu năm 1987. Trước đó ông từng giữ chức vụ Thủ tướng Chính phủ Việt Nam Dân chủ Cộng hòa từ năm 1955 đến năm 1976. Ông là vị Thủ tướng Việt Nam tại vị lâu nhất (1955–1987). Ông là học trò, cộng sự của Chủ tịch Hồ Chí Minh. Ông có tên gọi thân mật là Tô, đây từng là bí danh của ông. Ông còn có tên gọi là Lâm Bá Kiệt khi làm Phó chủ nhiệm cơ quan Biện sự xứ tại Quế Lâm (Chủ nhiệm là Hồ Học Lãm).\n", "Hồ Học Lãm giữ chức vụ gì trong bộ máy Nhà nước Cộng hòa Xã hội chủ nghĩa Việt Nam?\n", "{'text': [], 'answer_start': []}\n", "True\n", "{'text': ['Thủ tướng'], 'answer_start': [60]}\n", "\n", "uit_000006\n", "Phạm Văn Đồng (1 tháng 3 năm 1906 – 29 tháng 4 năm 2000) là Thủ tướng đầu tiên của nước Cộng hòa Xã hội chủ nghĩa Việt Nam từ năm 1976 (từ năm 1981 gọi là Chủ tịch Hội đồng Bộ trưởng) cho đến khi nghỉ hưu năm 1987. Trước đó ông từng giữ chức vụ Thủ tướng Chính phủ Việt Nam Dân chủ Cộng hòa từ năm 1955 đến năm 1976. Ông là vị Thủ tướng Việt Nam tại vị lâu nhất (1955–1987). Ông là học trò, cộng sự của Chủ tịch Hồ Chí Minh. Ông có tên gọi thân mật là Tô, đây từng là bí danh của ông. Ông còn có tên gọi là Lâm Bá Kiệt khi làm Phó chủ nhiệm cơ quan Biện sự xứ tại Quế Lâm (Chủ nhiệm là Hồ Học Lãm).\n", "Giai đoạn năm 1987-2000, Phạm Văn Đồng nắm giữ chức vụ gì?\n", "{'text': [], 'answer_start': []}\n", "True\n", "{'text': ['Thủ tướng Chính phủ Việt Nam Dân chủ Cộng hòa'], 'answer_start': [245]}\n", "\n", "uit_000007\n", "Phạm Văn Đồng (1 tháng 3 năm 1906 – 29 tháng 4 năm 2000) là Thủ tướng đầu tiên của nước Cộng hòa Xã hội chủ nghĩa Việt Nam từ năm 1976 (từ năm 1981 gọi là Chủ tịch Hội đồng Bộ trưởng) cho đến khi nghỉ hưu năm 1987. Trước đó ông từng giữ chức vụ Thủ tướng Chính phủ Việt Nam Dân chủ Cộng hòa từ năm 1955 đến năm 1976. Ông là vị Thủ tướng Việt Nam tại vị lâu nhất (1955–1987). Ông là học trò, cộng sự của Chủ tịch Hồ Chí Minh. Ông có tên gọi thân mật là Tô, đây từng là bí danh của ông. Ông còn có tên gọi là Lâm Bá Kiệt khi làm Phó chủ nhiệm cơ quan Biện sự xứ tại Quế Lâm (Chủ nhiệm là Hồ Học Lãm).\n", "Chủ tịch Hội đồng Bộ trưởng đầu tiên của nước Cộng hòa xã hội chủ nghĩa Việt Nam là ai?\n", "{'text': ['Phạm Văn Đồng'], 'answer_start': [0]}\n", "False\n", "None\n", "\n", "uit_000008\n", "Năm 1954, ông được giao nhiệm vụ Trưởng phái đoàn Chính phủ dự Hội nghị Genève về Đông Dương. Những đóng góp của đoàn Việt Nam do ông đứng đầu là vô cùng quan trọng, tạo ra những đột phá đưa Hội nghị tới thành công. Trải qua 8 phiên họp toàn thể và 23 phiên họp rất căng thẳng và phức tạp, với tinh thần chủ động và cố gắng của phái đoàn Việt Nam, ngày 20/7/1954, bản Hiệp định đình chỉ chiến sự ở Việt Nam, Campuchia và Lào đã được ký kết thừa nhận tôn trọng độc lập, chủ quyền, của nước Việt Nam, Lào và Campuchia.\n", "Sự kiện quan trọng nào đã diễn ra vào ngày 20/7/1954?\n", "{'text': ['bản Hiệp định đình chỉ chiến sự ở Việt Nam, Campuchia và Lào đã được ký kết thừa nhận tôn trọng độc lập, chủ quyền, của nước Việt Nam, Lào và Campuchia'], 'answer_start': [364]}\n", "False\n", "None\n", "\n", "uit_000009\n", "Năm 1954, ông được giao nhiệm vụ Trưởng phái đoàn Chính phủ dự Hội nghị Genève về Đông Dương. Những đóng góp của đoàn Việt Nam do ông đứng đầu là vô cùng quan trọng, tạo ra những đột phá đưa Hội nghị tới thành công. Trải qua 8 phiên họp toàn thể và 23 phiên họp rất căng thẳng và phức tạp, với tinh thần chủ động và cố gắng của phái đoàn Việt Nam, ngày 20/7/1954, bản Hiệp định đình chỉ chiến sự ở Việt Nam, Campuchia và Lào đã được ký kết thừa nhận tôn trọng độc lập, chủ quyền, của nước Việt Nam, Lào và Campuchia.\n", "Chức vụ mà Phạm Văn Đồng đảm nhiệm tại Hội nghị Genève về Đông Dương?\n", "{'text': ['Trưởng phái đoàn Chính phủ'], 'answer_start': [33]}\n", "False\n", "None\n", "\n", "uit_000010\n", "Năm 1954, ông được giao nhiệm vụ Trưởng phái đoàn Chính phủ dự Hội nghị Genève về Đông Dương. Những đóng góp của đoàn Việt Nam do ông đứng đầu là vô cùng quan trọng, tạo ra những đột phá đưa Hội nghị tới thành công. Trải qua 8 phiên họp toàn thể và 23 phiên họp rất căng thẳng và phức tạp, với tinh thần chủ động và cố gắng của phái đoàn Việt Nam, ngày 20/7/1954, bản Hiệp định đình chỉ chiến sự ở Việt Nam, Campuchia và Lào đã được ký kết thừa nhận tôn trọng độc lập, chủ quyền, của nước Việt Nam, Lào và Campuchia.\n", "Hội nghị Genève về Đông Dương có tính chất như thế nào?\n", "{'text': ['rất căng thẳng và phức tạp'], 'answer_start': [262]}\n", "False\n", "None\n", "\n" ] } ], "source": [ "for i in range(10):\n", " print(dataset['train']['uit_id'][i])\n", " print(dataset['train']['context'][i])\n", " print(dataset['train']['question'][i])\n", " print(dataset['train']['answers'][i])\n", " print(dataset['train']['is_impossible'][i])\n", " print(dataset['train']['plausible_answers'][i])\n", " print('')" ] }, { "cell_type": "markdown", "metadata": { "id": "yMlcBOoRbApT" }, "source": [ "### Model choosing\n", "\n", "Using colab's free plan, we'll get a T4 GPU with ~16GB of VRAM. That is optimized for inference. There are several pretrain models that we can choose from HuggingFace for limited resources in this case.\n", "\n", "For some candidates for example:\n", "- **phobert-base** which has about 134M params\n", "- **bloomz-1b7** which is a multilingual model that pre-trained on 46 languages including Vietnamese that 1.7B parameters - small enough to run on T4 with optimizations\n", "- **xlm-roberta-large-vi-qa**, a fine-tuned version of XLM-RoBERTa large model for extractive question answering in Vietnamese with ~550M params.\n", "\n", "Let's try out the **xlm-roberta-large-vi-qa**.\n", "\n", "How do we do this with HuggingFace?\n", "- In HuggingFace, they provided a way to get pretrain models right off their libraries. Depending on the task you want to use, you can use for example: **AutoModel** to get your desired checkpoint.\n", "- If you already know which model you want to use for example, you can also specify it, for example: ``from transformer import BertModel, BertConfig`` to build your brandnew BERT model." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "fcmuN9-sWbrB" }, "outputs": [], "source": [ "from transformers import AutoModelForQuestionAnswering\n", "\n", "model_name = \"ancs21/xlm-roberta-large-vi-qa\"\n", "model = AutoModelForQuestionAnswering.from_pretrained(model_name)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "h8AMav1y9tRd", "outputId": "c31b47fe-a31f-46a9-93e7-184d936f1140" }, "outputs": [ { "data": { "text/plain": [ "XLMRobertaForQuestionAnswering(\n", " (roberta): XLMRobertaModel(\n", " (embeddings): XLMRobertaEmbeddings(\n", " (word_embeddings): Embedding(250002, 1024, padding_idx=1)\n", " (position_embeddings): Embedding(514, 1024, padding_idx=1)\n", " (token_type_embeddings): Embedding(1, 1024)\n", " (LayerNorm): LayerNorm((1024,), eps=1e-05, elementwise_affine=True)\n", " (dropout): Dropout(p=0.1, inplace=False)\n", " )\n", " (encoder): XLMRobertaEncoder(\n", " (layer): ModuleList(\n", " (0-23): 24 x XLMRobertaLayer(\n", " (attention): XLMRobertaAttention(\n", " (self): XLMRobertaSdpaSelfAttention(\n", " (query): Linear(in_features=1024, out_features=1024, bias=True)\n", " (key): Linear(in_features=1024, out_features=1024, bias=True)\n", " (value): Linear(in_features=1024, out_features=1024, bias=True)\n", " (dropout): Dropout(p=0.1, inplace=False)\n", " )\n", " (output): XLMRobertaSelfOutput(\n", " (dense): Linear(in_features=1024, out_features=1024, bias=True)\n", " (LayerNorm): LayerNorm((1024,), eps=1e-05, elementwise_affine=True)\n", " (dropout): Dropout(p=0.1, inplace=False)\n", " )\n", " )\n", " (intermediate): XLMRobertaIntermediate(\n", " (dense): Linear(in_features=1024, out_features=4096, bias=True)\n", " (intermediate_act_fn): GELUActivation()\n", " )\n", " (output): XLMRobertaOutput(\n", " (dense): Linear(in_features=4096, out_features=1024, bias=True)\n", " (LayerNorm): LayerNorm((1024,), eps=1e-05, elementwise_affine=True)\n", " (dropout): Dropout(p=0.1, inplace=False)\n", " )\n", " )\n", " )\n", " )\n", " )\n", " (qa_outputs): Linear(in_features=1024, out_features=2, bias=True)\n", ")" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model" ] }, { "cell_type": "markdown", "metadata": { "id": "b_5c3PDs7EKt" }, "source": [ "### Vocabulary, Vectorizer\n", "\n", "From previous lectures, you know that we need a vocabulary and something to vectorize text into a form that the model could use to calculate (for the machine to understand)\n", "\n", "With HuggingFace similarly, you can use an **AutoTokenizer** to get the vocabulary of your choosen model's. Then you can use this to tokenize (vectorize) the text in your dataset." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "hHh8_ehgtoQI", "outputId": "4ea06da9-eec9-4d10-dd22-738a74f4736b" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['▁Tôi', '▁thật', '▁đẹp', '▁trai']\n" ] } ], "source": [ "from transformers import AutoTokenizer, DataCollatorForLanguageModeling\n", "\n", "# Example of tokens in a sentence\n", "tokenizer = AutoTokenizer.from_pretrained(model_name)\n", "tokens = tokenizer.tokenize(\"Tôi thật đẹp trai\")\n", "\n", "# data_collator = DataCollatorForLanguageModeling(\n", "# tokenizer=tokenizer,\n", "# mlm=True, # Set to True for masked language modeling\n", "# mlm_probability=0.10 # Masking probability\n", "# )\n", "\n", "print(tokens)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "DYjqMTbbuqYV", "outputId": "514fa5e6-af58-4a89-e744-1a6f0c4a0fc9" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'input_ids': [0, 14343, 10435, 9301, 16071, 2], 'attention_mask': [1, 1, 1, 1, 1, 1]}\n", " Tôi thật đẹp trai\n" ] } ], "source": [ "# Translating into machine vocabs\n", "encoded_string = tokenizer(\"Tôi thật đẹp trai\")\n", "print(encoded_string)\n", "decoded_string = tokenizer.decode(encoded_string['input_ids'])\n", "print(decoded_string)" ] }, { "cell_type": "markdown", "metadata": { "id": "tjpOHw1atmng" }, "source": [ "Now let's use this for our dataset." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "id": "_1bZo_q5IBza", "outputId": "14ef106d-cc0f-4dac-e687-e031ba1f1935" }, "outputs": [ { "data": { "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" }, "text/plain": [ "'\\nSince the context length for this model is 512, some context-question exceeds this.\\nThis code handles without truncation for the extra tokens.\\n'" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"\"\"\n", "Since the context length for this model is 512, some context-question exceeds this.\n", "This code handles without truncation for the extra tokens.\n", "\"\"\"\n", "\n", "# from transformers import AutoTokenizer\n", "# from datasets import DatasetDict\n", "\n", "# def preprocess_function(examples):\n", "# # Tokenize context and questions\n", "# questions = [q.strip() for q in examples[\"question\"]]\n", "# contexts = [c.strip() for c in examples[\"context\"]]\n", "\n", "# # Tokenize both questions and contexts\n", "# tokenized_examples = tokenizer(\n", "# questions,\n", "# contexts,\n", "# truncation=False,\n", "# return_offsets_mapping=True,\n", "# padding=True,\n", "# )\n", "\n", "# # Get the offset mapping\n", "# offset_mapping = tokenized_examples.pop(\"offset_mapping\")\n", "\n", "# # Let's label those examples\n", "# tokenized_examples[\"start_positions\"] = []\n", "# tokenized_examples[\"end_positions\"] = []\n", "\n", "# for i, offsets in enumerate(offset_mapping):\n", "# input_ids = tokenized_examples[\"input_ids\"][i]\n", "# cls_index = input_ids.index(tokenizer.cls_token_id)\n", "\n", "# # One example can give several spans\n", "# answers = examples[\"answers\"][i]\n", "\n", "# # If no answers are given, set cls_index as answer\n", "# if len(answers[\"answer_start\"]) == 0:\n", "# tokenized_examples[\"start_positions\"].append(cls_index)\n", "# tokenized_examples[\"end_positions\"].append(cls_index)\n", "# else:\n", "# # Start/end character index of the answer in the text\n", "# start_char = answers[\"answer_start\"][0]\n", "# end_char = start_char + len(answers[\"text\"][0])\n", "\n", "# # Find start and end token indices\n", "# start_token = None\n", "# end_token = None\n", "\n", "# for idx, (start, end) in enumerate(offsets):\n", "# if start <= start_char <= end:\n", "# start_token = idx\n", "# if start <= end_char <= end:\n", "# end_token = idx\n", "\n", "# # If answer span is detected\n", "# if start_token is not None and end_token is not None:\n", "# tokenized_examples[\"start_positions\"].append(start_token)\n", "# tokenized_examples[\"end_positions\"].append(end_token)\n", "# else:\n", "# # If answer cannot be mapped, use cls_index\n", "# tokenized_examples[\"start_positions\"].append(cls_index)\n", "# tokenized_examples[\"end_positions\"].append(cls_index)\n", "\n", "# return tokenized_examples\n", "\n", "\n", "# # Apply tokenization to all splits\n", "# tokenized_dataset = DatasetDict({\n", "# 'train': dataset['train'].map(\n", "# preprocess_function,\n", "# batched=True,\n", "# remove_columns=dataset[\"train\"].column_names\n", "# ),\n", "# 'validation': dataset['validation'].map(\n", "# preprocess_function,\n", "# batched=True,\n", "# remove_columns=dataset[\"validation\"].column_names\n", "# )\n", "# })" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 81, "referenced_widgets": [ "16e5a2bc321d4f3785298dc80016ad0c", "54d5ffd9d5fe4e6a8f758cd3e8254e53", "20eb1b0ece9941ff83772341ac91c8f2", "f9e764d4f8bd45ca97b6a32e5bb82e60", "b417b6191a114677b24c6a84a7747d3d", "d8506e3e8c4b44669b50f4ddaef15933", "ac883d2ac46543b2b047d17c02ac6c44", "bb23be450976482d8dabe7c0148bbecd", "0a102ce98a794c219e846d232e230957", "62e1e59258f44748b2c99cdeeb4e14f7", "9af364bd64ac40359e1acc0613d3f048", "06f47bc5acb0482e8e8290e5ae82796d", "fe9bff88a0be4822b44424f416e2fd4a", "50f4ed55d75f42cda3bc8cbadb9cffd3", "6bdd4ee6b9c44fd08a9c5e4d74cb0a16", "3bcbebf2ac1f4eeb9f210ead0d18874b", "8fa528633f6c4e439f7a7056fc8be0ad", "8a18883701cb4513b21a2100dc906b29", "96e3a8662b55498c9e5b80028ede779e", "7841bd99237e46598a8f21b0ca6f0f01", "d7aaa4b238d84a89999299ce9a060b7a", "7c25ee09dd7c4756ab2b959d696c9850" ] }, "id": "fQO0-fYHBvmY", "outputId": "a4a895f4-df2d-4836-9182-33754c271b08" }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "16e5a2bc321d4f3785298dc80016ad0c", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Map: 0%| | 0/28454 [00:00= end_char):\n", " tokenized_examples[\"start_positions\"].append(cls_index)\n", " tokenized_examples[\"end_positions\"].append(cls_index)\n", " else:\n", " # Otherwise move the token_start_index and token_end_index to the two ends of the answer\n", " while token_start_index < len(offsets) and \\\n", " offsets[token_start_index][0] <= start_char:\n", " token_start_index += 1\n", " tokenized_examples[\"start_positions\"].append(token_start_index - 1)\n", "\n", " while offsets[token_end_index][1] >= end_char:\n", " token_end_index -= 1\n", " tokenized_examples[\"end_positions\"].append(token_end_index + 1)\n", "\n", " return tokenized_examples\n", "\n", "# Apply tokenization to all splits\n", "tokenized_dataset = DatasetDict({\n", " 'train': dataset['train'].map(\n", " preprocess_function,\n", " batched=True,\n", " remove_columns=dataset[\"train\"].column_names\n", " ),\n", " 'validation': dataset['validation'].map(\n", " preprocess_function,\n", " batched=True,\n", " remove_columns=dataset[\"validation\"].column_names\n", " )\n", "})" ] }, { "cell_type": "markdown", "metadata": { "id": "JYLxS9fLisjK" }, "source": [ "To test if we tokenize them correctly, let's decode some examples" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "9-AIdS90itHK", "outputId": "7dc81aca-dd93-4f13-f02f-c05fd132e2ed" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " Tên gọi nào được Phạm Văn Đồng sử dụng khi làm Phó chủ nhiệm cơ quan Biện sự xứ tại Quế Lâm? Phạm Văn Đồng (1 tháng 3 năm 1906 – 29 tháng 4 năm 2000) là Thủ tướng đầu tiên của nước Cộng hòa Xã hội chủ nghĩa Việt Nam từ năm 1976 (từ năm 1981 gọi là Chủ tịch Hội đồng Bộ trưởng) cho đến khi nghỉ hưu năm 1987. Trước đó ông từng giữ chức vụ Thủ tướng Chính phủ Việt Nam Dân chủ Cộng hòa từ năm 1955 đến năm 1976. Ông là vị Thủ tướng Việt Nam tại vị lâu nhất (1955–1987). Ông là học trò, cộng sự của Chủ tịch Hồ Chí Minh. Ông có tên gọi thân mật là Tô, đây từng là bí danh của ông. Ông còn có tên gọi là Lâm Bá Kiệt khi làm Phó chủ nhiệm cơ quan Biện sự xứ tại Quế Lâm (Chủ nhiệm là Hồ Học Lãm).\n", "Lâm Bá Kiệt\n", "\n", " Phạm Văn Đồng giữ chức vụ gì trong bộ máy Nhà nước Cộng hòa Xã hội chủ nghĩa Việt Nam? Phạm Văn Đồng (1 tháng 3 năm 1906 – 29 tháng 4 năm 2000) là Thủ tướng đầu tiên của nước Cộng hòa Xã hội chủ nghĩa Việt Nam từ năm 1976 (từ năm 1981 gọi là Chủ tịch Hội đồng Bộ trưởng) cho đến khi nghỉ hưu năm 1987. Trước đó ông từng giữ chức vụ Thủ tướng Chính phủ Việt Nam Dân chủ Cộng hòa từ năm 1955 đến năm 1976. Ông là vị Thủ tướng Việt Nam tại vị lâu nhất (1955–1987). Ông là học trò, cộng sự của Chủ tịch Hồ Chí Minh. Ông có tên gọi thân mật là Tô, đây từng là bí danh của ông. Ông còn có tên gọi là Lâm Bá Kiệt khi làm Phó chủ nhiệm cơ quan Biện sự xứ tại Quế Lâm (Chủ nhiệm là Hồ Học Lãm).\n", "Thủ tướng\n", "\n", " Giai đoạn năm 1955-1976, Phạm Văn Đồng nắm giữ chức vụ gì? Phạm Văn Đồng (1 tháng 3 năm 1906 – 29 tháng 4 năm 2000) là Thủ tướng đầu tiên của nước Cộng hòa Xã hội chủ nghĩa Việt Nam từ năm 1976 (từ năm 1981 gọi là Chủ tịch Hội đồng Bộ trưởng) cho đến khi nghỉ hưu năm 1987. Trước đó ông từng giữ chức vụ Thủ tướng Chính phủ Việt Nam Dân chủ Cộng hòa từ năm 1955 đến năm 1976. Ông là vị Thủ tướng Việt Nam tại vị lâu nhất (1955–1987). Ông là học trò, cộng sự của Chủ tịch Hồ Chí Minh. Ông có tên gọi thân mật là Tô, đây từng là bí danh của ông. Ông còn có tên gọi là Lâm Bá Kiệt khi làm Phó chủ nhiệm cơ quan Biện sự xứ tại Quế Lâm (Chủ nhiệm là Hồ Học Lãm).\n", "Thủ tướng Chính phủ Việt Nam Dân chủ Cộng hòa\n", "\n", " Tên gọi nào được Phạm Văn Đồng sử dụng trước khi làm Phó chủ nhiệm cơ quan Biện sự xứ tại Quế Lâm? Phạm Văn Đồng (1 tháng 3 năm 1906 – 29 tháng 4 năm 2000) là Thủ tướng đầu tiên của nước Cộng hòa Xã hội chủ nghĩa Việt Nam từ năm 1976 (từ năm 1981 gọi là Chủ tịch Hội đồng Bộ trưởng) cho đến khi nghỉ hưu năm 1987. Trước đó ông từng giữ chức vụ Thủ tướng Chính phủ Việt Nam Dân chủ Cộng hòa từ năm 1955 đến năm 1976. Ông là vị Thủ tướng Việt Nam tại vị lâu nhất (1955–1987). Ông là học trò, cộng sự của Chủ tịch Hồ Chí Minh. Ông có tên gọi thân mật là Tô, đây từng là bí danh của ông. Ông còn có tên gọi là Lâm Bá Kiệt khi làm Phó chủ nhiệm cơ quan Biện sự xứ tại Quế Lâm (Chủ nhiệm là Hồ Học Lãm).\n", "\n", "\n", " Hồ Học Lãm giữ chức vụ gì trong bộ máy Nhà nước Cộng hòa Xã hội chủ nghĩa Việt Nam? Phạm Văn Đồng (1 tháng 3 năm 1906 – 29 tháng 4 năm 2000) là Thủ tướng đầu tiên của nước Cộng hòa Xã hội chủ nghĩa Việt Nam từ năm 1976 (từ năm 1981 gọi là Chủ tịch Hội đồng Bộ trưởng) cho đến khi nghỉ hưu năm 1987. Trước đó ông từng giữ chức vụ Thủ tướng Chính phủ Việt Nam Dân chủ Cộng hòa từ năm 1955 đến năm 1976. Ông là vị Thủ tướng Việt Nam tại vị lâu nhất (1955–1987). Ông là học trò, cộng sự của Chủ tịch Hồ Chí Minh. Ông có tên gọi thân mật là Tô, đây từng là bí danh của ông. Ông còn có tên gọi là Lâm Bá Kiệt khi làm Phó chủ nhiệm cơ quan Biện sự xứ tại Quế Lâm (Chủ nhiệm là Hồ Học Lãm).\n", "\n", "\n" ] } ], "source": [ "for i in range(5):\n", " print(tokenizer.decode(tokenized_dataset['train'][i]['input_ids']))\n", " start = tokenized_dataset['train'][i]['start_positions']\n", " end = tokenized_dataset['train'][i]['end_positions']\n", " print(tokenizer.decode(tokenized_dataset['train'][i]['input_ids'][start:end+1]))\n", " print(\"\")" ] }, { "cell_type": "markdown", "metadata": { "id": "93wfqRcaDGXE" }, "source": [ "Now, we need a way to load our data into the model for each batch.\n", "\n", "In this case, we'll use ``DataLoader`` from torch" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "o1gIIAHDinFY", "outputId": "af21c589-b079-4186-baeb-d227f24821b8" }, "outputs": [ { "data": { "text/plain": [ "{'input_ids': torch.Size([8, 512]),\n", " 'attention_mask': torch.Size([8, 512]),\n", " 'start_positions': torch.Size([8]),\n", " 'end_positions': torch.Size([8])}" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import torch\n", "from torch.utils.data import DataLoader\n", "from transformers import default_data_collator\n", "\n", "# First, let's set the format of our datasets to PyTorch\n", "tokenized_dataset.set_format('torch')\n", "\n", "# Define batch size\n", "batch_size = 8\n", "\n", "# Create dataloaders for each split\n", "train_dataloader = DataLoader(\n", " tokenized_dataset[\"train\"],\n", " shuffle=True,\n", " batch_size=batch_size,\n", " collate_fn=default_data_collator\n", ")\n", "\n", "eval_dataloader = DataLoader(\n", " tokenized_dataset[\"validation\"],\n", " shuffle=False,\n", " batch_size=batch_size,\n", " collate_fn=default_data_collator\n", ")\n", "\n", "for batch in train_dataloader:\n", " break\n", "\n", "{k: v.shape for k, v in batch.items()}" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 303 }, "id": "95sNx4lZOlW0", "outputId": "168277c5-c5a2-4aff-82dc-0538f0e57d58" }, "outputs": [ { "data": { "text/plain": [ "QuestionAnsweringModelOutput(loss=tensor(1.9861, grad_fn=), start_logits=tensor([[ -4.4823, -7.0031, -7.7921, ..., -10.5709, -10.5709, -10.5709],\n", " [ -4.7611, -7.0290, -8.1530, ..., -10.6116, -10.6116, -10.6116],\n", " [ -4.7986, -7.5903, -8.7621, ..., -10.6024, -10.6024, -10.6024],\n", " ...,\n", " [ -4.9443, -3.5931, -7.4082, ..., -10.4332, -10.4332, -10.4332],\n", " [ -5.1866, -8.0363, -8.3495, ..., -10.5487, -10.5487, -10.5487],\n", " [ -4.3495, -7.4726, -8.5240, ..., -10.7268, -10.7268, -10.7268]],\n", " grad_fn=), end_logits=tensor([[ -5.9101, -12.0865, -12.2975, ..., -12.4889, -12.4889, -12.4889],\n", " [ -7.0197, -12.3621, -12.2130, ..., -12.9142, -12.9142, -12.9142],\n", " [ -6.1025, -12.2340, -11.8061, ..., -12.6984, -12.6984, -12.6984],\n", " ...,\n", " [ -6.1289, -10.4349, -7.7572, ..., -11.8855, -11.8855, -11.8855],\n", " [ -6.4094, -11.8059, -11.7186, ..., -12.8557, -12.8557, -12.8557],\n", " [ -6.2921, -11.5546, -11.3684, ..., -12.7439, -12.7439, -12.7439]],\n", " grad_fn=), hidden_states=None, attentions=None)" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "outputs = model(**batch)\n", "outputs" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "bTIHHCFPLfqF", "outputId": "c8dfc71a-d712-4641-b663-3e6fa1d8df84" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tensor(1.9861, grad_fn=) torch.Size([8, 512]) torch.Size([8, 512])\n" ] } ], "source": [ "print(outputs.loss, outputs.start_logits.shape, outputs.end_logits.shape)" ] }, { "cell_type": "markdown", "metadata": { "id": "jsKcY2jcTdk6" }, "source": [ "### Now let's try to finetune our model\n" ] }, { "cell_type": "markdown", "metadata": { "id": "5Axk7Z-zlFXf" }, "source": [ "To test if the model really training, one fast way to check is trying to overfit a single batch. We'll try using the AdamW optimizer." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 998, "referenced_widgets": [ "75b475db12eb4e3387941188306f93b7", "4cce0316a56a4dbe8e4e4b3fc44df00e", "301bc6d611e049a0b647fec894498a8f", "b924c764009e4c5285bec912291dbd0f", "824498828e3349b1a26947eb76904417", "25684507e15a478da69d56d566567706", "f3ea65d57a3643d6b92947d916254669", "e141508449d94c5a8bb5c2a5fca17ed7", "db92d33a020845149669dc316406f372", "273391fe0e78489d96f9fc3d8190a762", "d4855087508f4ea59982792c318ca482" ] }, "id": "nvrfVQp2lUiA", "outputId": "ba14ef11-5708-43dc-fc20-9010d04dc503" }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/usr/local/lib/python3.11/dist-packages/transformers/optimization.py:591: FutureWarning: This implementation of AdamW is deprecated and will be removed in a future version. Use the PyTorch implementation torch.optim.AdamW instead, or set `no_deprecation_warning=True` to disable this warning\n", " warnings.warn(\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "75b475db12eb4e3387941188306f93b7", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/50 [00:00=2.0.0 in /usr/local/lib/python3.11/dist-packages (from evaluate) (3.3.0)\n", "Requirement already satisfied: numpy>=1.17 in /usr/local/lib/python3.11/dist-packages (from evaluate) (1.26.4)\n", "Requirement already satisfied: dill in /usr/local/lib/python3.11/dist-packages (from evaluate) (0.3.8)\n", "Requirement already satisfied: pandas in /usr/local/lib/python3.11/dist-packages (from evaluate) (2.2.2)\n", "Requirement already satisfied: requests>=2.19.0 in /usr/local/lib/python3.11/dist-packages (from evaluate) (2.32.3)\n", "Requirement already satisfied: tqdm>=4.62.1 in /usr/local/lib/python3.11/dist-packages (from evaluate) (4.67.1)\n", "Requirement already satisfied: xxhash in /usr/local/lib/python3.11/dist-packages (from evaluate) (3.5.0)\n", "Requirement already satisfied: multiprocess in /usr/local/lib/python3.11/dist-packages (from evaluate) (0.70.16)\n", "Requirement already satisfied: fsspec>=2021.05.0 in /usr/local/lib/python3.11/dist-packages (from fsspec[http]>=2021.05.0->evaluate) (2024.10.0)\n", "Requirement already satisfied: huggingface-hub>=0.7.0 in /usr/local/lib/python3.11/dist-packages (from evaluate) (0.28.1)\n", "Requirement already satisfied: packaging in /usr/local/lib/python3.11/dist-packages (from evaluate) (24.2)\n", "Requirement already satisfied: filelock in /usr/local/lib/python3.11/dist-packages (from datasets>=2.0.0->evaluate) (3.17.0)\n", "Requirement already satisfied: pyarrow>=15.0.0 in /usr/local/lib/python3.11/dist-packages (from datasets>=2.0.0->evaluate) (17.0.0)\n", "Requirement already satisfied: aiohttp in /usr/local/lib/python3.11/dist-packages (from datasets>=2.0.0->evaluate) (3.11.12)\n", "Requirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.11/dist-packages (from datasets>=2.0.0->evaluate) (6.0.2)\n", "Requirement already satisfied: typing-extensions>=3.7.4.3 in /usr/local/lib/python3.11/dist-packages (from huggingface-hub>=0.7.0->evaluate) (4.12.2)\n", "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.11/dist-packages (from requests>=2.19.0->evaluate) (3.4.1)\n", "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.11/dist-packages (from requests>=2.19.0->evaluate) (3.10)\n", "Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.11/dist-packages (from requests>=2.19.0->evaluate) (2.3.0)\n", "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.11/dist-packages (from requests>=2.19.0->evaluate) (2025.1.31)\n", "Requirement already satisfied: python-dateutil>=2.8.2 in /usr/local/lib/python3.11/dist-packages (from pandas->evaluate) (2.8.2)\n", "Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.11/dist-packages (from pandas->evaluate) (2025.1)\n", "Requirement already satisfied: tzdata>=2022.7 in /usr/local/lib/python3.11/dist-packages (from pandas->evaluate) (2025.1)\n", "Requirement already satisfied: aiohappyeyeballs>=2.3.0 in /usr/local/lib/python3.11/dist-packages (from aiohttp->datasets>=2.0.0->evaluate) (2.4.6)\n", "Requirement already satisfied: aiosignal>=1.1.2 in /usr/local/lib/python3.11/dist-packages (from aiohttp->datasets>=2.0.0->evaluate) (1.3.2)\n", "Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.11/dist-packages (from aiohttp->datasets>=2.0.0->evaluate) (25.1.0)\n", "Requirement already satisfied: frozenlist>=1.1.1 in /usr/local/lib/python3.11/dist-packages (from aiohttp->datasets>=2.0.0->evaluate) (1.5.0)\n", "Requirement already satisfied: multidict<7.0,>=4.5 in /usr/local/lib/python3.11/dist-packages (from aiohttp->datasets>=2.0.0->evaluate) (6.1.0)\n", "Requirement already satisfied: propcache>=0.2.0 in /usr/local/lib/python3.11/dist-packages (from aiohttp->datasets>=2.0.0->evaluate) (0.2.1)\n", "Requirement already satisfied: yarl<2.0,>=1.17.0 in /usr/local/lib/python3.11/dist-packages (from aiohttp->datasets>=2.0.0->evaluate) (1.18.3)\n", "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.11/dist-packages (from python-dateutil>=2.8.2->pandas->evaluate) (1.17.0)\n", "Downloading evaluate-0.4.3-py3-none-any.whl (84 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m84.0/84.0 kB\u001b[0m \u001b[31m3.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hInstalling collected packages: evaluate\n", "Successfully installed evaluate-0.4.3\n" ] } ], "source": [ "!pip install evaluate" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "background_save": true, "base_uri": "https://localhost:8080/", "height": 81, "referenced_widgets": [ "bc12f7e280294867b0d0c2ccd47605f9", "ac7cecb4d2f041e8b101825e3a121d69", "e9af5c2035dc48039a8fbd3332562a12", "b782dca127ef49e0ba6cb96a7cce2902", "1d860e97198a449aa598b18f62a9e2d7", "4ab79471592c4f479764d0902b69c632", "fbf5fb38636543f9a34bc1dd77d166be", "f4f8af22e3e54f00a138cd60eafca65c", "06e351483e89408f83cfad0df0324884", "60304cf0d8674646869ba23e4b143edc", "f3e03d01450246388ed05e825267f071", "a6b7c6f12505431184ad202bf341e06a", "caf2dc291eaf4b8fb998448128f976b1", "cc088783ab354d3aa9ad9629a64b1f6f", "d2ff693c912c4c4294c54d330a5c738e", "16d844dac745455c959fb56f8828dd01", "9c12e92e4a8447b5a0344aaa6bee360f", "56f6eb6ed19f405cbe0c0e1c904ef716", "3d4e4a214e224444b815eac8cec5d4f8", "d6da6cec5f6643fb9fc5b91fbf217e0d", "0f1207ccb5f34df3838ff8b298018b68", "919a10d9bbf84272bde6e09955fe8f1a" ] }, "id": "_u5Y4Kqsx4ER", "outputId": "57ef4469-5f23-478d-8a5d-548b1b79f00c" }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "bc12f7e280294867b0d0c2ccd47605f9", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Downloading builder script: 0%| | 0.00/4.53k [00:00