{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# Copyright 2020 NVIDIA Corporation. All Rights Reserved.\n", "#\n", "# Licensed under the Apache License, Version 2.0 (the \"License\");\n", "# you may not use this file except in compliance with the License.\n", "# You may obtain a copy of the License at\n", "#\n", "# http://www.apache.org/licenses/LICENSE-2.0\n", "#\n", "# Unless required by applicable law or agreed to in writing, software\n", "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", "# See the License for the specific language governing permissions and\n", "# limitations under the License.\n", "# ==============================================================================" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "# Wikipedia Question Answering\n", "\n", "This notebook walks through using Jarvis NLP Services to generate an answer to an incoming user question using Jarvis' Question and Answering modul by querying Wikipedia for a summary of the topic in question.\n", "\n", "First make sure the Wikipedia API is installed. Alter the bellow command if you use a different package manager. Then we import the required packages.\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "!pip install wikipedia" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import wikipedia as wiki\n", "\n", "import grpc\n", "\n", "import jarvis_api.jarvis_nlp_pb2 as jnlp\n", "import jarvis_api.jarvis_nlp_pb2_grpc as jnlp_srv" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Wikipedia Summary\n", "Next we accept user input. We use the Wikipedia API to find the most relevant articles. For the purpose of this example, we combine the top few article summaries. You can change the number of articles with `max_articles_combine`." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Getting summary for: Speech recognition\n", "Getting summary for: Windows Speech Recognition\n", "Getting summary for: Speaker recognition\n" ] } ], "source": [ "input_query = \"What is speech recognition?\"\n", "\n", "wiki_articles = wiki.search(input_query)\n", "max_articles_combine = 3\n", "combined_summary = \"\"\n", "\n", "if len(wiki_articles) == 0:\n", " print(\"ERROR: Could not find any matching results in Wikipedia.\")\n", "else:\n", " for article in wiki_articles[:min(len(wiki_articles), max_articles_combine)]:\n", " print(f\"Getting summary for: {article}\")\n", " combined_summary += \"\\n\" + wiki.summary(article)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Query Jarvis Server\n", "\n", "Lastly, we define the GRPC channel to send a request to the Jarvis server, and define a corresponding request. This allows us to query the Jarvis server with the given input query and the context taken from Wikipedia.\n", "\n", "Make sure you set your grpc channel to the appropriate port." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Query: What is speech recognition?\n", "Answer: an interdisciplinary subfield of computer science and computational linguistics that develops methodologies and technologies that enable the recognition and translation of spoken language into text by computers.\n" ] } ], "source": [ "# Alter if using a different port\n", "channel = grpc.insecure_channel('localhost:50051')\n", "\n", "jarvis_nlp = jnlp_srv.JarvisNLPStub(channel)\n", "\n", "req = jnlp.NaturalQueryRequest()\n", "\n", "req.query = input_query\n", "req.context = combined_summary\n", "resp = jarvis_nlp.NaturalQuery(req)\n", "\n", "print(f\"Query: {input_query}\")\n", "print(f\"Answer: {resp.results[0].answer}\")" ] }, { "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.6.12" } }, "nbformat": 4, "nbformat_minor": 4 }