Skip to content

Commit

Permalink
Update API approach and add progress bar
Browse files Browse the repository at this point in the history
  • Loading branch information
rasbt committed May 27, 2024
1 parent dd7ba32 commit 8ad5fac
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 112 deletions.
53 changes: 25 additions & 28 deletions ch07/02_dataset-utilities/create-passive-voice-entries.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"metadata": {},
"outputs": [],
"source": [
"# pip install -r requirements-exra.txt"
"# pip install -r requirements-extra.txt"
]
},
{
Expand Down Expand Up @@ -107,31 +107,28 @@
"id": "89343a84-0ddc-42fc-bf50-298a342b93c0",
"metadata": {},
"source": [
"- First, we need to provide our OpenAI API key, which can be found at https://platform.openai.com/api-keys\n",
"- Make sure not to share this key with anyone (make sure to delete it from this notebook in case you intend to share it; I recommend deleting the entire notebook cell that contains the key)\n",
"- Alternatively, delete the used API key from your account after you are finished to make sure it can't be abused later"
"- First, we need to provide our OpenAI API secret key, which can be found at https://platform.openai.com/api-keys\n",
"- Make sure not to share this key with anyone\n",
"- Add this secret key (`\"sk-...\"`) to the `config.json` file in this folder"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8ba8760c-1635-43cf-b039-9d1557b664c4",
"metadata": {},
"outputs": [],
"source": [
"OPENAI_API_KEY = \"your OpenAI API key\""
]
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 3,
"id": "26900564-aba7-48ba-8ee8-6cc9a505a25c",
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"from openai import OpenAI\n",
"\n",
"client = OpenAI(api_key=OPENAI_API_KEY)"
"# Load API key from a JSON file. \n",
"# Make sure to replace \"sk-...\" with your actual API key from https://platform.openai.com/api-keys\n",
"with open(\"config.json\", \"r\") as config_file:\n",
" config = json.load(config_file)\n",
" api_key = config[\"OPENAI_API_KEY\"]\n",
"\n",
"client = OpenAI(api_key=api_key)"
]
},
{
Expand All @@ -144,7 +141,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 4,
"id": "08e9ef2e-e816-4283-840e-43625791ad33",
"metadata": {},
"outputs": [
Expand All @@ -154,7 +151,7 @@
"'Breakfast was eaten by me.'"
]
},
"execution_count": 5,
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
Expand Down Expand Up @@ -193,7 +190,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 5,
"id": "8b2d393a-aa92-4190-9d44-44326a6f699b",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -226,7 +223,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 6,
"id": "735cc089-d127-480a-b39d-0782581f0c41",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -255,7 +252,7 @@
">> The three primary colors are red, blue, and yellow.\n",
"\n",
"Output:\n",
">> Red, blue, and yellow are the three primary colors.\n",
">> Red, blue, and yellow are considered the three primary colors.\n",
"\n",
"-------------------------\n",
"\n",
Expand Down Expand Up @@ -299,15 +296,15 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 7,
"id": "4f700d4b-19e5-4404-afa7-b0f093024232",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|█████████████████████████████████████████████| 5/5 [00:05<00:00, 1.12s/it]\n"
"100%|██████████████████████████████████████████████████████████████████████| 5/5 [00:04<00:00, 1.23it/s]\n"
]
}
],
Expand All @@ -331,7 +328,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 8,
"id": "5b6eaa87-a86d-42a1-a20a-b764b0d559d4",
"metadata": {},
"outputs": [
Expand All @@ -344,7 +341,7 @@
" 'output_2': 'The sentence is \"sleeps.\"'}"
]
},
"execution_count": 9,
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -363,15 +360,15 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 9,
"id": "eef99407-8ffd-4a63-b7ab-ffe30c0f0677",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|█████████████████████████████████████████| 200/200 [02:38<00:00, 1.26it/s]\n"
"100%|██████████████████████████████████████████████████████████████████| 200/200 [03:43<00:00, 1.12s/it]\n"
]
}
],
Expand All @@ -392,7 +389,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 10,
"id": "330cc30a-b08e-4bf0-bee2-bec0da4208de",
"metadata": {},
"outputs": [],
Expand Down
Loading

0 comments on commit 8ad5fac

Please sign in to comment.