Skip to content

Instantly share code, notes, and snippets.

@rmitra34
Created October 19, 2022 13:25
Show Gist options
  • Save rmitra34/0e978230cc50dd261aa2c27d4967a129 to your computer and use it in GitHub Desktop.
Save rmitra34/0e978230cc50dd261aa2c27d4967a129 to your computer and use it in GitHub Desktop.
Multi-Turn-Conversational-Interaction-Colab.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/rmitra34/0e978230cc50dd261aa2c27d4967a129/colab_demo.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "kyRjGKFLIwgY"
},
"source": [
"# Installations"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"id": "bZ_vm1gAIwga"
},
"outputs": [],
"source": [
"!pip install nl4dv &> /dev/null\n",
"!python -m nltk.downloader popular &> /dev/null\n",
"!python -m spacy download en_core_web_sm &> /dev/null\n",
"!pip install --upgrade notebook &> /dev/null\n",
"!jupyter nbextension install --sys-prefix --py vega &> /dev/null\n",
"!jupyter nbextension enable vega --py --sys-prefix &> /dev/null\n",
"!pip install altair &> /dev/null"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "yPiARaTYIwgc"
},
"source": [
"# Imports"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"id": "XqwNcyAVIwgc"
},
"outputs": [],
"source": [
"import json\n",
"from nl4dv import NL4DV\n",
"import os\n",
"import altair as alt\n",
"from altair import vega, vegalite\n",
"vega.renderers.enable('colab')\n",
"vegalite.renderers.enable('colab')\n",
"from IPython.display import display"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "uYu59lSPIwgc"
},
"source": [
"# Initializing NL4DV with a Housing Dataset"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"id": "Y6FK3u9YIwgc"
},
"outputs": [],
"source": [
"data_url = \"https://raw.githubusercontent.com/nl4dv/nl4dv/master/examples/assets/data/housing.csv\"\n",
"label_attribute = None\n",
"dependency_parser_config = {\"name\": \"spacy\", \"model\": \"en_core_web_sm\", \"parser\": None}\n",
"nl4dv_instance = NL4DV(verbose=False, \n",
" debug=True, \n",
" data_url=data_url, \n",
" label_attribute=label_attribute, \n",
" dependency_parser_config=dependency_parser_config\n",
" )"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "dck_PvmMIwgd"
},
"source": [
"# Rendering the Most Relevant Visualization\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"id": "wW49n8PBIwgd",
"outputId": "522ef6d9-6cea-419f-a5fc-bb6f58c5cdae",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 268
}
},
"outputs": [
{
"output_type": "display_data",
"data": {
"text/html": [
"\n",
"<div id=\"altair-viz-d819a81e53d8484993ac1520bc7fc387\"></div>\n",
"<script type=\"text/javascript\">\n",
" var VEGA_DEBUG = (typeof VEGA_DEBUG == \"undefined\") ? {} : VEGA_DEBUG;\n",
" (function(spec, embedOpt){\n",
" let outputDiv = document.currentScript.previousElementSibling;\n",
" if (outputDiv.id !== \"altair-viz-d819a81e53d8484993ac1520bc7fc387\") {\n",
" outputDiv = document.getElementById(\"altair-viz-d819a81e53d8484993ac1520bc7fc387\");\n",
" }\n",
" const paths = {\n",
" \"vega\": \"https://cdn.jsdelivr.net/npm//vega@5?noext\",\n",
" \"vega-lib\": \"https://cdn.jsdelivr.net/npm//vega-lib?noext\",\n",
" \"vega-lite\": \"https://cdn.jsdelivr.net/npm//[email protected]?noext\",\n",
" \"vega-embed\": \"https://cdn.jsdelivr.net/npm//vega-embed@6?noext\",\n",
" };\n",
"\n",
" function maybeLoadScript(lib, version) {\n",
" var key = `${lib.replace(\"-\", \"\")}_version`;\n",
" return (VEGA_DEBUG[key] == version) ?\n",
" Promise.resolve(paths[lib]) :\n",
" new Promise(function(resolve, reject) {\n",
" var s = document.createElement('script');\n",
" document.getElementsByTagName(\"head\")[0].appendChild(s);\n",
" s.async = true;\n",
" s.onload = () => {\n",
" VEGA_DEBUG[key] = version;\n",
" return resolve(paths[lib]);\n",
" };\n",
" s.onerror = () => reject(`Error loading script: ${paths[lib]}`);\n",
" s.src = paths[lib];\n",
" });\n",
" }\n",
"\n",
" function showError(err) {\n",
" outputDiv.innerHTML = `<div class=\"error\" style=\"color:red;\">${err}</div>`;\n",
" throw err;\n",
" }\n",
"\n",
" function displayChart(vegaEmbed) {\n",
" vegaEmbed(outputDiv, spec, embedOpt)\n",
" .catch(err => showError(`Javascript Error: ${err.message}<br>This usually means there's a typo in your chart specification. See the javascript console for the full traceback.`));\n",
" }\n",
"\n",
" if(typeof define === \"function\" && define.amd) {\n",
" requirejs.config({paths});\n",
" require([\"vega-embed\"], displayChart, err => showError(`Error loading script: ${err.message}`));\n",
" } else {\n",
" maybeLoadScript(\"vega\", \"5\")\n",
" .then(() => maybeLoadScript(\"vega-lite\", \"4.17.0\"))\n",
" .then(() => maybeLoadScript(\"vega-embed\", \"6\"))\n",
" .catch(showError)\n",
" .then(() => displayChart(vegaEmbed));\n",
" }\n",
" })({\"$schema\": \"https://vega.github.io/schema/vega-lite/v4.json\", \"mark\": {\"type\": \"line\", \"tooltip\": true}, \"encoding\": {\"y\": {\"field\": \"Price\", \"type\": \"quantitative\", \"aggregate\": \"mean\", \"axis\": {\"format\": \"s\"}}, \"color\": {\"field\": \"Home Type\", \"type\": \"nominal\"}, \"x\": {\"field\": \"Year\", \"type\": \"temporal\"}}, \"transform\": [], \"data\": {\"url\": \"https://raw.githubusercontent.com/nl4dv/nl4dv/master/examples/assets/data/housing.csv\", \"format\": {\"type\": \"csv\"}}}, {\"mode\": \"vega-lite\"});\n",
"</script>"
]
},
"metadata": {}
}
],
"source": [
"response = nl4dv_instance.analyze_query(\"Show average prices for different home types over the years\")\n",
"display(alt.display.html_renderer(response['visList'][0]['vlSpec']), raw=True)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "qYIHmyeKIwgd"
},
"source": [
"# Auto-detecting Follow Up Queries"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"id": "PL2x30reIwgd",
"outputId": "0d66e458-5ccd-4459-e927-51f6218ab088",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 367
}
},
"outputs": [
{
"output_type": "display_data",
"data": {
"text/html": [
"\n",
"<div id=\"altair-viz-fffa7e0e6e3c4f7483f2067f2d98ca1f\"></div>\n",
"<script type=\"text/javascript\">\n",
" var VEGA_DEBUG = (typeof VEGA_DEBUG == \"undefined\") ? {} : VEGA_DEBUG;\n",
" (function(spec, embedOpt){\n",
" let outputDiv = document.currentScript.previousElementSibling;\n",
" if (outputDiv.id !== \"altair-viz-fffa7e0e6e3c4f7483f2067f2d98ca1f\") {\n",
" outputDiv = document.getElementById(\"altair-viz-fffa7e0e6e3c4f7483f2067f2d98ca1f\");\n",
" }\n",
" const paths = {\n",
" \"vega\": \"https://cdn.jsdelivr.net/npm//vega@5?noext\",\n",
" \"vega-lib\": \"https://cdn.jsdelivr.net/npm//vega-lib?noext\",\n",
" \"vega-lite\": \"https://cdn.jsdelivr.net/npm//[email protected]?noext\",\n",
" \"vega-embed\": \"https://cdn.jsdelivr.net/npm//vega-embed@6?noext\",\n",
" };\n",
"\n",
" function maybeLoadScript(lib, version) {\n",
" var key = `${lib.replace(\"-\", \"\")}_version`;\n",
" return (VEGA_DEBUG[key] == version) ?\n",
" Promise.resolve(paths[lib]) :\n",
" new Promise(function(resolve, reject) {\n",
" var s = document.createElement('script');\n",
" document.getElementsByTagName(\"head\")[0].appendChild(s);\n",
" s.async = true;\n",
" s.onload = () => {\n",
" VEGA_DEBUG[key] = version;\n",
" return resolve(paths[lib]);\n",
" };\n",
" s.onerror = () => reject(`Error loading script: ${paths[lib]}`);\n",
" s.src = paths[lib];\n",
" });\n",
" }\n",
"\n",
" function showError(err) {\n",
" outputDiv.innerHTML = `<div class=\"error\" style=\"color:red;\">${err}</div>`;\n",
" throw err;\n",
" }\n",
"\n",
" function displayChart(vegaEmbed) {\n",
" vegaEmbed(outputDiv, spec, embedOpt)\n",
" .catch(err => showError(`Javascript Error: ${err.message}<br>This usually means there's a typo in your chart specification. See the javascript console for the full traceback.`));\n",
" }\n",
"\n",
" if(typeof define === \"function\" && define.amd) {\n",
" requirejs.config({paths});\n",
" require([\"vega-embed\"], displayChart, err => showError(`Error loading script: ${err.message}`));\n",
" } else {\n",
" maybeLoadScript(\"vega\", \"5\")\n",
" .then(() => maybeLoadScript(\"vega-lite\", \"4.17.0\"))\n",
" .then(() => maybeLoadScript(\"vega-embed\", \"6\"))\n",
" .catch(showError)\n",
" .then(() => displayChart(vegaEmbed));\n",
" }\n",
" })({\"$schema\": \"https://vega.github.io/schema/vega-lite/v4.json\", \"mark\": {\"type\": \"bar\", \"tooltip\": true}, \"encoding\": {\"y\": {\"field\": \"Price\", \"type\": \"quantitative\", \"aggregate\": \"mean\", \"axis\": {\"format\": \"s\"}}, \"x\": {\"field\": \"Home Type\", \"type\": \"nominal\"}, \"column\": {\"field\": \"Year\", \"type\": \"temporal\"}, \"color\": {\"field\": \"Home Type\", \"type\": \"nominal\"}}, \"transform\": [], \"data\": {\"url\": \"https://raw.githubusercontent.com/nl4dv/nl4dv/master/examples/assets/data/housing.csv\", \"format\": {\"type\": \"csv\"}}}, {\"mode\": \"vega-lite\"});\n",
"</script>"
]
},
"metadata": {}
}
],
"source": [
"response = nl4dv_instance.analyze_query(\"As a bar chart\", dialog='auto')\n",
"display(alt.display.html_renderer(response['visList'][0]['vlSpec']), raw=True)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "EeYcEkczIwge"
},
"source": [
"# Generating a New Standalone Visualization"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"id": "_YMgnQwuIwge",
"outputId": "64d214c4-7aba-49d9-93ae-637ca58556dd",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 268
}
},
"outputs": [
{
"output_type": "display_data",
"data": {
"text/html": [
"\n",
"<div id=\"altair-viz-fe35a1e8bbb44e1586bef8860289256e\"></div>\n",
"<script type=\"text/javascript\">\n",
" var VEGA_DEBUG = (typeof VEGA_DEBUG == \"undefined\") ? {} : VEGA_DEBUG;\n",
" (function(spec, embedOpt){\n",
" let outputDiv = document.currentScript.previousElementSibling;\n",
" if (outputDiv.id !== \"altair-viz-fe35a1e8bbb44e1586bef8860289256e\") {\n",
" outputDiv = document.getElementById(\"altair-viz-fe35a1e8bbb44e1586bef8860289256e\");\n",
" }\n",
" const paths = {\n",
" \"vega\": \"https://cdn.jsdelivr.net/npm//vega@5?noext\",\n",
" \"vega-lib\": \"https://cdn.jsdelivr.net/npm//vega-lib?noext\",\n",
" \"vega-lite\": \"https://cdn.jsdelivr.net/npm//[email protected]?noext\",\n",
" \"vega-embed\": \"https://cdn.jsdelivr.net/npm//vega-embed@6?noext\",\n",
" };\n",
"\n",
" function maybeLoadScript(lib, version) {\n",
" var key = `${lib.replace(\"-\", \"\")}_version`;\n",
" return (VEGA_DEBUG[key] == version) ?\n",
" Promise.resolve(paths[lib]) :\n",
" new Promise(function(resolve, reject) {\n",
" var s = document.createElement('script');\n",
" document.getElementsByTagName(\"head\")[0].appendChild(s);\n",
" s.async = true;\n",
" s.onload = () => {\n",
" VEGA_DEBUG[key] = version;\n",
" return resolve(paths[lib]);\n",
" };\n",
" s.onerror = () => reject(`Error loading script: ${paths[lib]}`);\n",
" s.src = paths[lib];\n",
" });\n",
" }\n",
"\n",
" function showError(err) {\n",
" outputDiv.innerHTML = `<div class=\"error\" style=\"color:red;\">${err}</div>`;\n",
" throw err;\n",
" }\n",
"\n",
" function displayChart(vegaEmbed) {\n",
" vegaEmbed(outputDiv, spec, embedOpt)\n",
" .catch(err => showError(`Javascript Error: ${err.message}<br>This usually means there's a typo in your chart specification. See the javascript console for the full traceback.`));\n",
" }\n",
"\n",
" if(typeof define === \"function\" && define.amd) {\n",
" requirejs.config({paths});\n",
" require([\"vega-embed\"], displayChart, err => showError(`Error loading script: ${err.message}`));\n",
" } else {\n",
" maybeLoadScript(\"vega\", \"5\")\n",
" .then(() => maybeLoadScript(\"vega-lite\", \"4.17.0\"))\n",
" .then(() => maybeLoadScript(\"vega-embed\", \"6\"))\n",
" .catch(showError)\n",
" .then(() => displayChart(vegaEmbed));\n",
" }\n",
" })({\"$schema\": \"https://vega.github.io/schema/vega-lite/v4.json\", \"mark\": {\"type\": \"point\", \"tooltip\": true}, \"encoding\": {\"x\": {\"field\": \"Lot Area\", \"type\": \"quantitative\", \"axis\": {\"format\": \"s\"}}, \"y\": {\"field\": \"Price\", \"type\": \"quantitative\", \"axis\": {\"format\": \"s\"}}}, \"transform\": [], \"data\": {\"url\": \"https://raw.githubusercontent.com/nl4dv/nl4dv/master/examples/assets/data/housing.csv\", \"format\": {\"type\": \"csv\"}}}, {\"mode\": \"vega-lite\"});\n",
"</script>"
]
},
"metadata": {}
}
],
"source": [
"response = nl4dv_instance.analyze_query(\"Correlate Price and Lot Area\", dialog=False)\n",
"display(alt.display.html_renderer(response['visList'][0]['vlSpec']), raw=True)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "zeNrZVl6Iwge"
},
"source": [
"# Following Up on a Previous Conversation"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"id": "rGEg7jNHIwge",
"outputId": "058b2227-28e5-4530-e18d-f48bb9eb9fb5",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 338
}
},
"outputs": [
{
"output_type": "display_data",
"data": {
"text/html": [
"\n",
"<div id=\"altair-viz-eaec70309eed4ec8816a0dbf3e6a4334\"></div>\n",
"<script type=\"text/javascript\">\n",
" var VEGA_DEBUG = (typeof VEGA_DEBUG == \"undefined\") ? {} : VEGA_DEBUG;\n",
" (function(spec, embedOpt){\n",
" let outputDiv = document.currentScript.previousElementSibling;\n",
" if (outputDiv.id !== \"altair-viz-eaec70309eed4ec8816a0dbf3e6a4334\") {\n",
" outputDiv = document.getElementById(\"altair-viz-eaec70309eed4ec8816a0dbf3e6a4334\");\n",
" }\n",
" const paths = {\n",
" \"vega\": \"https://cdn.jsdelivr.net/npm//vega@5?noext\",\n",
" \"vega-lib\": \"https://cdn.jsdelivr.net/npm//vega-lib?noext\",\n",
" \"vega-lite\": \"https://cdn.jsdelivr.net/npm//[email protected]?noext\",\n",
" \"vega-embed\": \"https://cdn.jsdelivr.net/npm//vega-embed@6?noext\",\n",
" };\n",
"\n",
" function maybeLoadScript(lib, version) {\n",
" var key = `${lib.replace(\"-\", \"\")}_version`;\n",
" return (VEGA_DEBUG[key] == version) ?\n",
" Promise.resolve(paths[lib]) :\n",
" new Promise(function(resolve, reject) {\n",
" var s = document.createElement('script');\n",
" document.getElementsByTagName(\"head\")[0].appendChild(s);\n",
" s.async = true;\n",
" s.onload = () => {\n",
" VEGA_DEBUG[key] = version;\n",
" return resolve(paths[lib]);\n",
" };\n",
" s.onerror = () => reject(`Error loading script: ${paths[lib]}`);\n",
" s.src = paths[lib];\n",
" });\n",
" }\n",
"\n",
" function showError(err) {\n",
" outputDiv.innerHTML = `<div class=\"error\" style=\"color:red;\">${err}</div>`;\n",
" throw err;\n",
" }\n",
"\n",
" function displayChart(vegaEmbed) {\n",
" vegaEmbed(outputDiv, spec, embedOpt)\n",
" .catch(err => showError(`Javascript Error: ${err.message}<br>This usually means there's a typo in your chart specification. See the javascript console for the full traceback.`));\n",
" }\n",
"\n",
" if(typeof define === \"function\" && define.amd) {\n",
" requirejs.config({paths});\n",
" require([\"vega-embed\"], displayChart, err => showError(`Error loading script: ${err.message}`));\n",
" } else {\n",
" maybeLoadScript(\"vega\", \"5\")\n",
" .then(() => maybeLoadScript(\"vega-lite\", \"4.17.0\"))\n",
" .then(() => maybeLoadScript(\"vega-embed\", \"6\"))\n",
" .catch(showError)\n",
" .then(() => displayChart(vegaEmbed));\n",
" }\n",
" })({\"$schema\": \"https://vega.github.io/schema/vega-lite/v4.json\", \"mark\": {\"type\": \"bar\", \"tooltip\": true}, \"encoding\": {\"y\": {\"field\": \"Price\", \"type\": \"quantitative\", \"aggregate\": \"mean\", \"axis\": {\"format\": \"s\"}}, \"x\": {\"field\": \"Home Type\", \"type\": \"nominal\"}, \"column\": {\"field\": \"Year\", \"type\": \"temporal\"}, \"color\": {\"field\": \"Home Type\", \"type\": \"nominal\"}}, \"transform\": [{\"filter\": {\"field\": \"Home Type\", \"oneOf\": [\"Condo\", \"Duplex\"]}}], \"data\": {\"url\": \"https://raw.githubusercontent.com/nl4dv/nl4dv/master/examples/assets/data/housing.csv\", \"format\": {\"type\": \"csv\"}}}, {\"mode\": \"vega-lite\"});\n",
"</script>"
]
},
"metadata": {}
}
],
"source": [
"response = nl4dv_instance.analyze_query(\"Just show condos and duplexes\", dialog=True, dialog_id = \"0\", query_id = \"1\")\n",
"display(alt.display.html_renderer(response['visList'][0]['vlSpec']), raw=True)"
]
}
],
"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.8.5"
},
"colab": {
"provenance": [],
"name": "Multi-Turn-Conversational-Interaction-Colab.ipynb",
"include_colab_link": true
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment