Created
August 15, 2020 21:17
-
-
Save brockmanmatt/a65f54e33b48a14eb306494a686f0988 to your computer and use it in GitHub Desktop.
checkXKCD_corrections.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "checkXKCD_corrections.ipynb", | |
"provenance": [], | |
"collapsed_sections": [], | |
"authorship_tag": "ABX9TyPk9Vtfx4Ro69XSQOvxYn8I", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/brockmanmatt/a65f54e33b48a14eb306494a686f0988/checkxkcd_corrections.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "J7wnsgT2kPut", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"from google.colab import files\n", | |
"uploaded = files.upload()\n", | |
"print(\"done\")" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "WHPHrUnhpKnI", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"I'll install the API" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "zq0ltp2xn4yt", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"!pip install openai\n", | |
"import openai, json, pandas as pd" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "Q2yE0jcnpMEV", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"Loading in key.json that I uploaded; I do this so I don't need to worry about accidently leaking creds if I share the colab (which I'm 99% sure is just a json file that won't expose them)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "bwNXXwHen5x9", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"openai.api_key = json.load(open(\"key.json\", \"r\"))[\"key\"]" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "k67w5H0fpTkT", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"Default keyword arguments to pass the aPI" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "e1EwpqqJkTYh", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"#arguments to send the API\n", | |
"kwargs = {\n", | |
"\"engine\":\"davinci\",\n", | |
"\"temperature\":0,\n", | |
"\"max_tokens\":150,\n", | |
"\"stop\":\"\\n\\n\",\n", | |
"}" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "zZubgPoOpWDH", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"Quick wrapper to automatically save prompts and responses sent for later analysis if needed" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "sXTDJx0An9Bl", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"import datetime\n", | |
"def query(prompt, myKwargs = kwargs):\n", | |
" \"\"\"\n", | |
" wrapper for the API to save the prompt and the result\n", | |
" \"\"\"\n", | |
"\n", | |
" r = openai.Completion.create(prompt=prompt, **myKwargs)[\"choices\"][0][\"text\"].strip()\n", | |
" with open(\"{}.json\".format(datetime.datetime.now().strftime(\"%Y%m%d%s\")), \"w\") as fh:\n", | |
" json.dump({\"prompt\":prompt, \"response\":r}, fh, indent=4)\n", | |
" return r" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "EdFXafcJpZ3Q", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"Test to make sure my query works" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "rtWJBF0iSXAr", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"def interpretCorrections(text):\n", | |
" prompt = \"\"\"Text: I left my homework on the bus\n", | |
"Text: *CAR\n", | |
"Q: What is the sender trying to rephrase the text to say?\n", | |
"A: I left my homework in the car\n", | |
"\n", | |
"Text: I'm gonna take a quick nap and get it done\n", | |
"Text: *LONG\n", | |
"Q: What is the sender trying to rephrase the text to say?\n", | |
"A: I'm gonna take a long nap and get it done\n", | |
"\n", | |
"\"\"\"\n", | |
"\n", | |
" suffix = \"\"\"\n", | |
"Q: What is the sender trying to rephrase the text to say?\n", | |
"A:\"\"\"\n", | |
" kwargs = {\"engine\":\"davinci\", \"temperature\":0, \"max_tokens\":150, \"stop\":\"\\n\",}\n", | |
" return query(prompt + text + suffix, kwargs)\n" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "fEI2Djm3Qif_", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "97881225-1643-4249-e1a4-3c5acd05bb71" | |
}, | |
"source": [ | |
"text = \"\"\"Text: I'm gonna ride a horse on the beach at dawn\n", | |
"Text: *EAT\n", | |
"Text: *3AM\n", | |
"Text: *COUCH\n", | |
"Text: *PIZZA\"\"\"\n", | |
"interpretCorrections(text)" | |
], | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"application/vnd.google.colaboratory.intrinsic+json": { | |
"type": "string" | |
}, | |
"text/plain": [ | |
"\"I'm gonna eat pizza on the couch at 3am\"" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 106 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "xsNhLwdcQx10", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "097130c8-9cb3-48db-8752-06f58258c6f3" | |
}, | |
"source": [ | |
"text = \"\"\"Text: I'd love to meet up, maybe in a few days? Next week is looking pretty empty.\n", | |
"Text: *WITCHCRAFT\"\"\"\n", | |
"interpretCorrections(text)" | |
], | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"application/vnd.google.colaboratory.intrinsic+json": { | |
"type": "string" | |
}, | |
"text/plain": [ | |
"\"I'd love to meet up, maybe in a few days? Next week is looking pretty busy.\"" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 51 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "woA5gTQcQyss", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "a6e64417-c487-4d6a-99b4-f2a3ec1d63e7" | |
}, | |
"source": [ | |
"text = \"\"\"Text: I'd love to meet up, maybe in a few days? Next week is looking pretty empty.\n", | |
"Text: *WEEKS\"\"\"\n", | |
"interpretCorrections(text)" | |
], | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"application/vnd.google.colaboratory.intrinsic+json": { | |
"type": "string" | |
}, | |
"text/plain": [ | |
"\"I'd love to meet up, maybe in a few weeks? Next week is looking pretty empty.\"" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 50 | |
} | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment