|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 2, |
| 6 | + "metadata": { |
| 7 | + "colab": { |
| 8 | + "base_uri": "https://localhost:8080/" |
| 9 | + }, |
| 10 | + "id": "h-TO-Q8HnNSI", |
| 11 | + "outputId": "cf9f170f-b8af-4f3d-877f-da9641ca7ff7" |
| 12 | + }, |
| 13 | + "outputs": [ |
| 14 | + { |
| 15 | + "name": "stdout", |
| 16 | + "output_type": "stream", |
| 17 | + "text": [ |
| 18 | + "Requirement already satisfied: transformers in /usr/local/lib/python3.10/dist-packages (4.44.2)\n", |
| 19 | + "Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from transformers) (3.16.1)\n", |
| 20 | + "Requirement already satisfied: huggingface-hub<1.0,>=0.23.2 in /usr/local/lib/python3.10/dist-packages (from transformers) (0.24.7)\n", |
| 21 | + "Requirement already satisfied: numpy>=1.17 in /usr/local/lib/python3.10/dist-packages (from transformers) (1.26.4)\n", |
| 22 | + "Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.10/dist-packages (from transformers) (24.1)\n", |
| 23 | + "Requirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.10/dist-packages (from transformers) (6.0.2)\n", |
| 24 | + "Requirement already satisfied: regex!=2019.12.17 in /usr/local/lib/python3.10/dist-packages (from transformers) (2024.9.11)\n", |
| 25 | + "Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from transformers) (2.32.3)\n", |
| 26 | + "Requirement already satisfied: safetensors>=0.4.1 in /usr/local/lib/python3.10/dist-packages (from transformers) (0.4.5)\n", |
| 27 | + "Requirement already satisfied: tokenizers<0.20,>=0.19 in /usr/local/lib/python3.10/dist-packages (from transformers) (0.19.1)\n", |
| 28 | + "Requirement already satisfied: tqdm>=4.27 in /usr/local/lib/python3.10/dist-packages (from transformers) (4.66.5)\n", |
| 29 | + "Requirement already satisfied: fsspec>=2023.5.0 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub<1.0,>=0.23.2->transformers) (2024.6.1)\n", |
| 30 | + "Requirement already satisfied: typing-extensions>=3.7.4.3 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub<1.0,>=0.23.2->transformers) (4.12.2)\n", |
| 31 | + "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->transformers) (3.4.0)\n", |
| 32 | + "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->transformers) (3.10)\n", |
| 33 | + "Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests->transformers) (2.2.3)\n", |
| 34 | + "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests->transformers) (2024.8.30)\n" |
| 35 | + ] |
| 36 | + } |
| 37 | + ], |
| 38 | + "source": [ |
| 39 | + "# Import necessary libraries\n", |
| 40 | + "!pip install transformers\n", |
| 41 | + "\n", |
| 42 | + "from transformers import pipeline\n", |
| 43 | + "\n", |
| 44 | + "# Initialize the zero-shot classifier pipeline\n", |
| 45 | + "classifier = pipeline(\"zero-shot-classification\", model=\"facebook/bart-large-mnli\")\n", |
| 46 | + "\n", |
| 47 | + "# Define the labels/categories for classification\n", |
| 48 | + "candidate_labels = [\n", |
| 49 | + " \"sports\", \"education\", \"entertainment\", \"harassment\", \"violence\",\n", |
| 50 | + " \"kids\", \"politics\", \"technology\", \"history\", \"geography\", \"economics\",\n", |
| 51 | + " \"health\", \"science\", \"environment\", \"culture\"\n", |
| 52 | + "]" |
| 53 | + ] |
| 54 | + }, |
| 55 | + { |
| 56 | + "cell_type": "code", |
| 57 | + "execution_count": 3, |
| 58 | + "metadata": { |
| 59 | + "colab": { |
| 60 | + "base_uri": "https://localhost:8080/" |
| 61 | + }, |
| 62 | + "id": "pt9YEVe5nu9_", |
| 63 | + "outputId": "6e5f63d3-652d-46bb-d4fb-8f6033afec33" |
| 64 | + }, |
| 65 | + "outputs": [ |
| 66 | + { |
| 67 | + "name": "stdout", |
| 68 | + "output_type": "stream", |
| 69 | + "text": [ |
| 70 | + "politics: 0.59\n", |
| 71 | + "history: 0.15\n", |
| 72 | + "culture: 0.05\n", |
| 73 | + "technology: 0.03\n", |
| 74 | + "entertainment: 0.03\n", |
| 75 | + "environment: 0.02\n", |
| 76 | + "violence: 0.02\n", |
| 77 | + "kids: 0.02\n", |
| 78 | + "geography: 0.02\n", |
| 79 | + "health: 0.02\n", |
| 80 | + "economics: 0.02\n", |
| 81 | + "science: 0.01\n", |
| 82 | + "harassment: 0.01\n", |
| 83 | + "sports: 0.01\n", |
| 84 | + "education: 0.01\n" |
| 85 | + ] |
| 86 | + } |
| 87 | + ], |
| 88 | + "source": [ |
| 89 | + "# Example text for classification\n", |
| 90 | + "text = \"The member won the presidency election by 75 seats.\"\n", |
| 91 | + "\n", |
| 92 | + "# Perform zero-shot classification\n", |
| 93 | + "result = classifier(text, candidate_labels)\n", |
| 94 | + "\n", |
| 95 | + "# Display the classification result\n", |
| 96 | + "for label, score in zip(result['labels'], result['scores']):\n", |
| 97 | + " print(f\"{label}: {score:.2f}\")\n" |
| 98 | + ] |
| 99 | + } |
| 100 | + ], |
| 101 | + "metadata": { |
| 102 | + "colab": { |
| 103 | + "provenance": [] |
| 104 | + }, |
| 105 | + "kernelspec": { |
| 106 | + "display_name": "Python 3", |
| 107 | + "name": "python3" |
| 108 | + }, |
| 109 | + "language_info": { |
| 110 | + "name": "python" |
| 111 | + } |
| 112 | + }, |
| 113 | + "nbformat": 4, |
| 114 | + "nbformat_minor": 0 |
| 115 | +} |
0 commit comments