{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 4.54 s, sys: 975 ms, total: 5.52 s\n",
      "Wall time: 5.61 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "import malaya"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## get labels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['severe toxic',\n",
       " 'obscene',\n",
       " 'identity attack',\n",
       " 'insult',\n",
       " 'threat',\n",
       " 'asian',\n",
       " 'atheist',\n",
       " 'bisexual',\n",
       " 'buddhist',\n",
       " 'christian',\n",
       " 'female',\n",
       " 'heterosexual',\n",
       " 'indian',\n",
       " 'homosexual, gay or lesbian',\n",
       " 'intellectual or learning disability',\n",
       " 'male',\n",
       " 'muslim',\n",
       " 'other disability',\n",
       " 'other gender',\n",
       " 'other race or ethnicity',\n",
       " 'other religion',\n",
       " 'other sexual orientation',\n",
       " 'physical disability',\n",
       " 'psychiatric or mental illness',\n",
       " 'transgender',\n",
       " 'malay',\n",
       " 'chinese']"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "malaya.toxic.label"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "string = 'Benda yg SALAH ni, jgn lah didebatkan. Yg SALAH xkan jadi betul. Ingat tu. Mcm mana kesat sekalipun org sampaikan mesej, dan memang benda tu salah, diam je. Xyah nk tunjuk kau open sangat nk tegur cara org lain berdakwah. '\n",
    "another_string = 'melayu bodoh, dah la gay, sokong lgbt lagi, memang tak guna'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load multinomial model\n",
    "\n",
    "All model interface will follow sklearn interface started v3.4,\n",
    "\n",
    "```python\n",
    "model.predict(List[str])\n",
    "\n",
    "model.predict_proba(List[str])\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = malaya.toxic.multinomial()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'severe toxic': 0.9983866471486633,\n",
       "  'obscene': 0.9609727610993377,\n",
       "  'identity attack': 0.8695613508984636,\n",
       "  'insult': 0.5893315709933827,\n",
       "  'threat': 0.022178387416617994,\n",
       "  'asian': 0.020300810205187092,\n",
       "  'atheist': 0.011794932510638331,\n",
       "  'bisexual': 0.002584488616645158,\n",
       "  'buddhist': 0.004570410474229619,\n",
       "  'christian': 0.03405075979783316,\n",
       "  'female': 0.03787090649113612,\n",
       "  'heterosexual': 0.008360866566466152,\n",
       "  'indian': 0.9206507865140837,\n",
       "  'homosexual, gay or lesbian': 0.03492931132214706,\n",
       "  'intellectual or learning disability': 0.00158322379679834,\n",
       "  'male': 0.06432988855860852,\n",
       "  'muslim': 0.06722155678421161,\n",
       "  'other disability': 0.0,\n",
       "  'other gender': 0.0,\n",
       "  'other race or ethnicity': 0.0017973269863205566,\n",
       "  'other religion': 0.0017937047323945308,\n",
       "  'other sexual orientation': 0.0012965120040433268,\n",
       "  'physical disability': 0.001553693991766015,\n",
       "  'psychiatric or mental illness': 0.024938805254016427,\n",
       "  'transgender': 0.011663162911194878,\n",
       "  'malay': 0.9995238230425324,\n",
       "  'chinese': 0.9912614436972298}]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.predict_proba([string])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## List available Transformer models"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'bert': ['425.7 MB', 'accuracy: 0.814'],\n",
       " 'tiny-bert': ['57.4 MB', 'accuracy: 0.815'],\n",
       " 'albert': ['48.7 MB', 'accuracy: 0.812'],\n",
       " 'tiny-albert': ['22.4 MB', 'accuracy: 0.808'],\n",
       " 'xlnet': ['446.5 MB', 'accuracy: 0.807'],\n",
       " 'alxlnet': ['46.8 MB', 'accuracy: 0.817']}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "malaya.toxic.available_transformer_model()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load ALXLNET model\n",
    "\n",
    "All model interface will follow sklearn interface started v3.4,\n",
    "\n",
    "```python\n",
    "model.predict(List[str])\n",
    "\n",
    "model.predict_proba(List[str])\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = malaya.toxic.transformer(model = 'alxlnet')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Predict batch of strings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'severe toxic': 0.201493,\n",
       "  'obscene': 0.12493244,\n",
       "  'identity attack': 0.005829394,\n",
       "  'insult': 0.08384159,\n",
       "  'threat': 0.0010293126,\n",
       "  'asian': 0.0004298091,\n",
       "  'atheist': 0.0005042255,\n",
       "  'bisexual': 0.0007214546,\n",
       "  'buddhist': 0.00031352043,\n",
       "  'christian': 0.001463592,\n",
       "  'female': 0.095250845,\n",
       "  'heterosexual': 0.00018996,\n",
       "  'indian': 0.029991329,\n",
       "  'homosexual, gay or lesbian': 0.00020930171,\n",
       "  'intellectual or learning disability': 0.00018399954,\n",
       "  'male': 0.017134428,\n",
       "  'muslim': 0.0050880015,\n",
       "  'other disability': 0.000233531,\n",
       "  'other gender': 4.813075e-05,\n",
       "  'other race or ethnicity': 0.00010916591,\n",
       "  'other religion': 0.00031152368,\n",
       "  'other sexual orientation': 0.00026413798,\n",
       "  'physical disability': 0.000107735395,\n",
       "  'psychiatric or mental illness': 3.6627054e-05,\n",
       "  'transgender': 0.00016203523,\n",
       "  'malay': 0.08275634,\n",
       "  'chinese': 0.001092732},\n",
       " {'severe toxic': 0.9906007,\n",
       "  'obscene': 0.90202737,\n",
       "  'identity attack': 0.9795381,\n",
       "  'insult': 0.6345859,\n",
       "  'threat': 0.015953332,\n",
       "  'asian': 0.014682382,\n",
       "  'atheist': 0.0035497844,\n",
       "  'bisexual': 0.0326609,\n",
       "  'buddhist': 0.0101745725,\n",
       "  'christian': 0.025312841,\n",
       "  'female': 0.00968048,\n",
       "  'heterosexual': 0.029808193,\n",
       "  'indian': 0.011105597,\n",
       "  'homosexual, gay or lesbian': 0.13856784,\n",
       "  'intellectual or learning disability': 0.04939267,\n",
       "  'male': 0.014529228,\n",
       "  'muslim': 0.024640262,\n",
       "  'other disability': 0.0009796321,\n",
       "  'other gender': 0.037679344,\n",
       "  'other race or ethnicity': 0.033878565,\n",
       "  'other religion': 0.003752023,\n",
       "  'other sexual orientation': 0.103711344,\n",
       "  'physical disability': 0.00469586,\n",
       "  'psychiatric or mental illness': 0.001588594,\n",
       "  'transgender': 0.003436562,\n",
       "  'malay': 0.9901147,\n",
       "  'chinese': 0.1126565}]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.predict_proba([string,another_string])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Open toxicity visualization dashboard\n",
    "\n",
    "Default when you call `predict_words` it will open a browser with visualization dashboard, you can disable by `visualization=False`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.predict_words(another_string)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 335836 characters]\n",
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "metadata": {
      "image/png": {
       "width": 800
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from IPython.core.display import Image, display\n",
    "\n",
    "display(Image('toxicity-dashboard.png', width=800))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Stacking models\n",
    "\n",
    "More information, you can read at [https://malaya.readthedocs.io/en/latest/Stack.html](https://malaya.readthedocs.io/en/latest/Stack.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:loading sentence piece model\n"
     ]
    }
   ],
   "source": [
    "albert = malaya.toxic.transformer(model = 'albert')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'severe toxic': 0.9968317,\n",
       "  'obscene': 0.43022493,\n",
       "  'identity attack': 0.90531594,\n",
       "  'insult': 0.42289576,\n",
       "  'threat': 0.0058603976,\n",
       "  'asian': 0.000983668,\n",
       "  'atheist': 0.0005495089,\n",
       "  'bisexual': 0.0009623809,\n",
       "  'buddhist': 0.0003632398,\n",
       "  'christian': 0.0018632574,\n",
       "  'female': 0.006050684,\n",
       "  'heterosexual': 0.0025569045,\n",
       "  'indian': 0.0056869243,\n",
       "  'homosexual, gay or lesbian': 0.012232827,\n",
       "  'intellectual or learning disability': 0.00091394753,\n",
       "  'male': 0.011594971,\n",
       "  'muslim': 0.0042621437,\n",
       "  'other disability': 0.00027529505,\n",
       "  'other gender': 0.0010361207,\n",
       "  'other race or ethnicity': 0.0012320877,\n",
       "  'other religion': 0.00091365684,\n",
       "  'other sexual orientation': 0.0027996385,\n",
       "  'physical disability': 0.00010540871,\n",
       "  'psychiatric or mental illness': 0.000815311,\n",
       "  'transgender': 0.0016718076,\n",
       "  'malay': 0.96644485,\n",
       "  'chinese': 0.05199418}]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "malaya.stack.predict_stack([model, albert], [another_string])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
