{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Try_mrm8488_xquad_finetuned_model.ipynb",
      "provenance": [],
      "authorship_tag": "ABX9TyPJzFXZJGLuPSisCMajUaU3",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/mrm8488/shared_colab_notebooks/blob/master/Try_mrm8488_xquad_finetuned_model.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "RTFfiD2JdN5B",
        "colab_type": "text"
      },
      "source": [
        "# Colab to try the model [mrm8488/bert-multi-cased-finetuned-xquadv1](https://huggingface.co/mrm8488/bert-multi-cased-finetuned-xquadv1)\n",
        "\n",
        "## A fined tuned version of BERT base **multilingual** cased on [XQUAD](https://github.com/deepmind/xquad/blob/master/README.md) dataset (```multilingual Q&A```)\n",
        "\n",
        "Supported Languages:\n",
        "```\n",
        "- Arabic: `ar`\n",
        "- German: `de`\n",
        "- Greek: `el`\n",
        "- English: `en`\n",
        "- Spanish: `es`\n",
        "- Hindi: `hi`\n",
        "- Russian: `ru`\n",
        "- Thai: `th`\n",
        "- Turkish: `tr`\n",
        "- Vietnamese: `vi`\n",
        "- Chinese: `zh`\n",
        "```"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "70UYUF6Ob7vI",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "!pip install -q transformers"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "NCoInXk-cFTY",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "from transformers import pipeline\n",
        "\n",
        "qa_pipeline = pipeline(\n",
        "    \"question-answering\",\n",
        "    model=\"mrm8488/bert-multi-cased-finetuned-xquadv1\",\n",
        "    tokenizer=\"mrm8488/bert-multi-cased-finetuned-xquadv1\"\n",
        ")"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "RyObybtDcbF0",
        "colab_type": "text"
      },
      "source": [
        "### Spanish"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "0zBho6MOccb-",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 70
        },
        "outputId": "1a6fce23-0efe-447b-b9c2-85525843b53b"
      },
      "source": [
        "qa_pipeline({\n",
        "    'context': \"Manuel Romero ha estado colaborando activamente últimamente en el repositorio de hugginface/transformers\",\n",
        "    'question': \"¿Quién ha estado colaborando de forma activa en hugginface/transformers?\"\n",
        "    \n",
        "})"
      ],
      "execution_count": 3,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "convert squad examples to features: 100%|██████████| 1/1 [00:00<00:00, 159.81it/s]\n",
            "add example index and unique id: 100%|██████████| 1/1 [00:00<00:00, 3155.98it/s]\n"
          ],
          "name": "stderr"
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "{'answer': 'Manuel Romero', 'end': 13, 'score': 0.7157343813066319, 'start': 0}"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 3
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "jyRFfgFRch4H",
        "colab_type": "text"
      },
      "source": [
        "### English"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "IVtxQNB1ceiv",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 70
        },
        "outputId": "ccf57f6a-5af5-493e-d0fb-aaca16f61f86"
      },
      "source": [
        "qa_pipeline({\n",
        "    'context': \"Manuel Romero has been working hardly in the repository hugginface/transformers lately\",\n",
        "    'question': \"Who has been working hard for hugginface/transformers lately?\"\n",
        "    \n",
        "})"
      ],
      "execution_count": 4,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "convert squad examples to features: 100%|██████████| 1/1 [00:00<00:00, 184.37it/s]\n",
            "add example index and unique id: 100%|██████████| 1/1 [00:00<00:00, 6000.43it/s]\n"
          ],
          "name": "stderr"
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "{'answer': 'Manuel Romero', 'end': 13, 'score': 0.7254485993702389, 'start': 0}"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 4
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "fMj7oF5Dcmbu",
        "colab_type": "text"
      },
      "source": [
        "### German"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "HbgyuzsNclEF",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 124
        },
        "outputId": "4a2ce368-71a4-4ad2-e5ce-049f73fb069f"
      },
      "source": [
        "qa_pipeline({\n",
        "    'context': \"Manuel Romero hat in letzter Zeit kaum im Hugginface / Transformers-Repository gearbeitet\",\n",
        "    'question': \"Für welches Repository hat Manuel Romero in letzter Zeit gearbeitet?\"\n",
        "    \n",
        "})"
      ],
      "execution_count": 5,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "convert squad examples to features: 100%|██████████| 1/1 [00:00<00:00, 200.93it/s]\n",
            "add example index and unique id: 100%|██████████| 1/1 [00:00<00:00, 4315.13it/s]\n"
          ],
          "name": "stderr"
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "{'answer': 'Hugginface / Transformers-Repository',\n",
              " 'end': 78,\n",
              " 'score': 0.6885512433484031,\n",
              " 'start': 42}"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 5
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "3veaGBmJcunH",
        "colab_type": "text"
      },
      "source": [
        "### French"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "vHJVI7KQctcl",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 124
        },
        "outputId": "e34c2754-79a5-4e8f-ffb0-1abb68b90bd0"
      },
      "source": [
        "qa_pipeline({\n",
        "    'context': \"Manuel Romero a travaillé à peine dans le référentiel hugginface / transformers ces derniers temps\",\n",
        "    'question': \"Pour quel référentiel a travaillé Manuel Romero récemment?\"\n",
        "    \n",
        "})"
      ],
      "execution_count": 6,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "convert squad examples to features: 100%|██████████| 1/1 [00:00<00:00, 889.75it/s]\n",
            "add example index and unique id: 100%|██████████| 1/1 [00:00<00:00, 503.70it/s]\n"
          ],
          "name": "stderr"
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "{'answer': 'hugginface / transformers',\n",
              " 'end': 79,\n",
              " 'score': 0.6482061613915384,\n",
              " 'start': 54}"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 6
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "o8sNsjNtc3Il",
        "colab_type": "text"
      },
      "source": [
        "### Russian"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "vVn_8yhdc1jb",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 124
        },
        "outputId": "c533573e-15ea-409f-8764-a1d7ad22a3da"
      },
      "source": [
        "qa_pipeline({\n",
        "    'context': \"Мануэль Ромеро в последнее время почти не работал в репозитории hugginface / transformers\",\n",
        "    'question': \"Кто в последнее время усердно работал над обнимашками / трансформерами?\"\n",
        "    \n",
        "})"
      ],
      "execution_count": 7,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "convert squad examples to features: 100%|██████████| 1/1 [00:00<00:00, 185.34it/s]\n",
            "add example index and unique id: 100%|██████████| 1/1 [00:00<00:00, 936.02it/s]\n"
          ],
          "name": "stderr"
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "{'answer': 'Мануэль Ромеро',\n",
              " 'end': 14,\n",
              " 'score': 0.7240765577438069,\n",
              " 'start': 0}"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 7
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "fz6FS8nAc-Dv",
        "colab_type": "text"
      },
      "source": [
        "### Hindi"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Sd84mWGcc8cU",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 70
        },
        "outputId": "944f68d9-ac43-450a-990f-a5e84b27bc90"
      },
      "source": [
        "# context: Coronavirus is seeding panic in the West because it expands so fast.\n",
        "# question: Where is seeding panic Coronavirus?\n",
        "qa_pipeline({\n",
        "    'context': \"कोरोनावायरस पश्चिम में आतंक बो रहा है क्योंकि यह इतनी तेजी से फैलता है।\",\n",
        "    'question': \"कोरोनावायरस घबराहट कहां है?\"\n",
        "    \n",
        "})\n",
        "# output: West"
      ],
      "execution_count": 8,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "convert squad examples to features: 100%|██████████| 1/1 [00:00<00:00, 2474.52it/s]\n",
            "add example index and unique id: 100%|██████████| 1/1 [00:00<00:00, 483.44it/s]\n"
          ],
          "name": "stderr"
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "{'answer': 'पश्चिम', 'end': 18, 'score': 0.7037217439689059, 'start': 12}"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 8
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "cui4YwhudD0n",
        "colab_type": "text"
      },
      "source": [
        "### Chinese"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ZpLzs_NHdCzD",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 124
        },
        "outputId": "e230f30b-90c6-46b1-8ea0-4f913ac5457f"
      },
      "source": [
        "# context: coronavirus is seeding the confusion in Europe because it spreads so fast. A lot of public events are being suspended.\n",
        "# question: Where does coronavirus cause confusion?\n",
        "qa_pipeline({\n",
        "    'context': \"冠状病毒在欧洲引起了混乱，因为它的传播速度如此之快。 许多公共活动都被暂停。\",\n",
        "    'question': \"冠状病毒在哪里引起混乱？\"\n",
        "    \n",
        "})\n",
        "# output: Coronavirus has caused confusion in Europe because it spreads so fast."
      ],
      "execution_count": 9,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "convert squad examples to features: 100%|██████████| 1/1 [00:00<00:00, 299.36it/s]\n",
            "add example index and unique id: 100%|██████████| 1/1 [00:00<00:00, 596.54it/s]\n"
          ],
          "name": "stderr"
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "{'answer': '冠状病毒在欧洲引起了混乱，因为它的传播速度如此之快。',\n",
              " 'end': 26,\n",
              " 'score': 0.7080380565056927,\n",
              " 'start': 0}"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 9
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "-c8u9XxOe5UU",
        "colab_type": "text"
      },
      "source": [
        "## Bonus: do it in a FORM"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "WGA6yPiFe-JW",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 124
        },
        "outputId": "3b610ae6-1887-4907-a2f3-4ffbc35e071e"
      },
      "source": [
        "#@title Q&A Form\n",
        "\n",
        "context = 'Վերջերս Մանուել Ռոմերոն ակտիվորեն համագործակցում է hugginface / transformers պահոցում' #@param {type:\"string\"}\n",
        "question = 'Ո՞վ է ակտիվորեն համագործակցում hugginface / տրանսֆորմատորներում' #@param {type:\"string\"}\n",
        "\n",
        "qa_pipeline({\n",
        "    'context': context,\n",
        "    'question': question\n",
        "    \n",
        "})"
      ],
      "execution_count": 10,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "convert squad examples to features: 100%|██████████| 1/1 [00:00<00:00, 160.69it/s]\n",
            "add example index and unique id: 100%|██████████| 1/1 [00:00<00:00, 6743.25it/s]\n"
          ],
          "name": "stderr"
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "{'answer': 'Վերջերս Մանուել Ռոմերոն',\n",
              " 'end': 23,\n",
              " 'score': 0.6982514187280131,\n",
              " 'start': 0}"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 10
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "BFvmXLP1fHsS",
        "colab_type": "text"
      },
      "source": [
        "> Created by [Manuel Romero/@mrm8488](https://twitter.com/mrm8488)"
      ]
    }
  ]
}