Files
everyone-can-use-english/new-edition-drafts/jupyter-notebooks/color-training.ipynb
2024-01-18 07:31:45 +08:00

133 lines
4.0 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "f330d08f-fa3c-4cf4-bf53-136be20393e9",
"metadata": {},
"outputs": [],
"source": [
"# %pip install python-dotenv openai\n",
"# %pip install --upgrade openai"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "71ad2e48-3b33-42d3-8e4c-9f89c61110de",
"metadata": {},
"outputs": [],
"source": [
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"from datetime import datetime\n",
"from IPython.display import HTML\n",
"import IPython\n",
"import re"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "78e09680-77f9-411d-b792-d339c3ccb076",
"metadata": {},
"outputs": [],
"source": [
"load_dotenv() # take environment variables from .env.\n",
" # you should set OPENAI_API_KEY=\"sk-xxxxx\" in .env in current working_dir."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7a492b90-fa90-4d8b-934a-05d0cc2ff2c1",
"metadata": {},
"outputs": [],
"source": [
"client = OpenAI()\n",
"\n",
"role_definition = \"\"\"\n",
"You're my English tutor.\n",
"You have a list of colors: \"Scarlet,Crimson,Maroon,Burgundy,Coral,Tangerine,Apricot,Burnt Orange,Mustard,Gold,Amber,Lemon,Emerald,Olive,Lime,Jade,Navy,Azure,Cobalt,Turquoise,Lavender,Mauve,Plum,Violet,Fuchsia,Salmon,Rose,Blush,Sienna,Bronze,Taupe,Beige,Charcoal,Slate,Silver,Pewter,Ivory,Pearl,Alabaster,Jet,Onyx,Ebony\".\n",
"Whatever I say, you'll randomly choose one of colors in the list, \n",
"1. tell me the color's name, and give the pronunciation of the word in ipa.\n",
"2. give me the hex code of the color;\n",
"3. give me the name of the color in Chinese, \n",
"4. tell me what basic color's variation it is.\n",
"5. explain to me it's origin, where I would see the color most often, in both English and Chinese; and please surround English explanation with tripple ```\n",
"\n",
"\"\"\"\n",
"\n",
"user_prompt = \"\"\"\n",
"hi\n",
"\"\"\"\n",
"\n",
"rspd_translation = client.chat.completions.create(\n",
" model=\"gpt-4\",\n",
" messages=[\n",
" {\n",
" \"role\": \"system\", \n",
" \"content\": role_definition\n",
" },\n",
" {\n",
" \"role\": \"user\", \n",
" \"content\": user_prompt\n",
" }\n",
" ]\n",
")\n",
"\n",
"colors = re.findall(r'#[0-9a-fA-F]{3}(?:[0-9a-fA-F]{3})?', rspd_translation.choices[0].message.content)\n",
"html_str = f\"<div style='width:100px; height:100px; background-color:{colors[0]};'></div>\"\n",
"color_explanation = re.findall(r'```(.*?)```', rspd_translation.choices[0].message.content)\n",
"print(\"\")\n",
"display(HTML(html_str))\n",
"print(\"\")\n",
"print(rspd_translation.choices[0].message.content)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "37679a3d-f1b8-49f3-9b31-2b810441b348",
"metadata": {},
"outputs": [],
"source": [
"speech_file_path = f\"{datetime.now().strftime(\"%Y%m%d_%H%M%S\")}_color.mp3\"\n",
"voice_performer = \"alloy\"\n",
"# alloy, echo, fable, onyx, nova, and shimmer, the last two of which are femail voices.\n",
"\n",
"rspd_audio = client.audio.speech.create(\n",
" model=\"tts-1\",\n",
" voice=voice_performer,\n",
" input=color_explanation[0]\n",
")\n",
"\n",
"rspd_audio.stream_to_file(speech_file_path)\n",
"\n",
"IPython.display.Audio(speech_file_path)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.18"
}
},
"nbformat": 4,
"nbformat_minor": 5
}