{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Predict sentences using the pretrained DeepOpinion ABSA model\n", "In this tutorial, we show how you can use an already pre-trained model from DeepOpinion to predict sentences within the hotel domain. No training is required as an already pre-trained model is automatically downloaded from the DeepOpinion database. Since no training is involved, this example can easily be executed locally on your CPU. Please note that we assume that you installed all dependencies (see the Installation section of the documentation: https://autonlu.com/installation.html)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Include libs\n", "\n", "The first step is to include all necessary libs." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import autonlu\n", "from autonlu import Model, list_models\n", "from pprint import pprint" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "User name/Email: admin\n", "Password: ········\n" ] } ], "source": [ "autonlu.login()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Load model\n", "We successfully imported the AutoNLU. So our next step is to check which pre-trained models are available:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "34 pretrained models are availabe!\n", "\n", "['Admin/base_arabic',\n", " 'DeepOpinion/insurance_base_en',\n", " 'DeepOpinion/telco_base_en',\n", " 'DeepOpinion/utility_base_en',\n", " 'DeepOpinion/banking_base_en',\n", " 'DeepOpinion/base_xx',\n", " 'DeepOpinion/base_de',\n", " 'DeepOpinion/automotive_base_en',\n", " 'DeepOpinion/household-electronics_base_de',\n", " 'DeepOpinion/consumer-electronics_base_en',\n", " 'DeepOpinion/consumer-packaged-goods_base_en',\n", " 'DeepOpinion/hotels_base_de',\n", " 'DeepOpinion/employee-experience_base_en',\n", " 'DeepOpinion/sights_base_en',\n", " 'DeepOpinion/household-electronics_base_en',\n", " 'DeepOpinion/hotels_base_en',\n", " 'DeepOpinion/household-electronics_absa_de',\n", " 'DeepOpinion/utility_absa_en',\n", " 'DeepOpinion/telco_absa_en',\n", " 'DeepOpinion/banking_absa_en',\n", " 'DeepOpinion/insurance_absa_en',\n", " 'DeepOpinion/base_en',\n", " 'DeepOpinion/vacuum-cleaners_absa_en',\n", " 'DeepOpinion/sights-activities_absa_en',\n", " 'DeepOpinion/alpine-hotels_absa_en',\n", " 'DeepOpinion/alpine-hotels_absa_de',\n", " 'DeepOpinion/hotels_absa_en',\n", " 'DeepOpinion/employee-experience_absa_en',\n", " 'DeepOpinion/automotive_absa_en',\n", " 'DeepOpinion/conversational-demo_absa_en',\n", " 'DeepOpinion/household-electronics_absa_en',\n", " 'DeepOpinion/laptops_absa_en',\n", " 'DeepOpinion/consumer-electronics_absa_en',\n", " 'DeepOpinion/consumer-packaged-goods_absa_en']\n" ] } ], "source": [ "pretrained_models = list_models()\n", "print(f\"{len(pretrained_models)} pretrained models are availabe!\\n\")\n", "pprint(pretrained_models)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Wow, 34 different models with different tasks are available! In this tutorial we are interested to predict sentences within the hotel's domain, therefore we use the ```hotels_absa_en``` model." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "model = Model(\"DeepOpinion/hotels_absa_en\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Check what device the model was loaded to. (cpu or cuda if it will run on the GPU)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Model is running on the GPU\n" ] } ], "source": [ "print(\"Model is running on the\", \"CPU\" if model.device == \"cpu\" else \"GPU\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Predict\n", "Finally, we want to use this model to predict some sentences. Simply specify an array, or load your sentences from a file and feed the information into your model." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "The room was very clean.\n", " Cleanliness: POS\n", " Room: POS\n", "\n", "The food was good, but the guys at the reception were bad.\n", " Food: POS\n", " Reception: NEG\n", " Staff: NEG\n" ] } ], "source": [ "# Show results\n", "X = [\"The room was very clean.\", \n", " \"The food was good, but the guys at the reception were bad.\"]\n", "res = model.predict(X)\n", "\n", "for i, sentence in enumerate(X):\n", " print(f\"\\n{sentence}\")\n", " for pred in res[i]:\n", " print(f\" {pred[0]}: {pred[1]}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "That's it - quite easy to predict with AutoNLU right? In the next tutorial, we will show how you can easily train your own model using your own dataset :)" ] } ], "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.9.1" } }, "nbformat": 4, "nbformat_minor": 2 }