Skip to content

Instantly share code, notes, and snippets.

@dianachua
Created October 16, 2020 03:59
Show Gist options
  • Save dianachua/635414b50d13941f65a23c649048c60f to your computer and use it in GitHub Desktop.
Save dianachua/635414b50d13941f65a23c649048c60f to your computer and use it in GitHub Desktop.
Created on Skills Network Labs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Collecting ibm_watson\n",
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/a2/3c/c2cfb41db546fe98820e89017c892d73991cef61b9c48680191fe703a214/ibm-watson-4.7.1.tar.gz (385kB)\n",
"\u001b[K |████████████████████████████████| 389kB 3.6MB/s eta 0:00:01\n",
"\u001b[?25hCollecting bs4\n",
" Downloading https://files.pythonhosted.org/packages/10/ed/7e8b97591f6f456174139ec089c769f89a94a1a4025fe967691de971f314/bs4-0.0.1.tar.gz\n",
"Requirement already satisfied: requests<3.0,>=2.0 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from ibm_watson) (2.24.0)\n",
"Requirement already satisfied: python_dateutil>=2.5.3 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from ibm_watson) (2.8.1)\n",
"Collecting websocket-client==0.48.0 (from ibm_watson)\n",
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/8a/a1/72ef9aa26cfe1a75cee09fc1957e4723add9de098c15719416a1ee89386b/websocket_client-0.48.0-py2.py3-none-any.whl (198kB)\n",
"\u001b[K |████████████████████████████████| 204kB 32.8MB/s eta 0:00:01\n",
"\u001b[?25hCollecting ibm_cloud_sdk_core==1.7.3 (from ibm_watson)\n",
" Downloading https://files.pythonhosted.org/packages/b7/23/aa9ae242f6348a1ed28fca2e6d3e76e043c3db951f9b516e1992518fe2c3/ibm-cloud-sdk-core-1.7.3.tar.gz\n",
"Collecting beautifulsoup4 (from bs4)\n",
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/d1/41/e6495bd7d3781cee623ce23ea6ac73282a373088fcd0ddc809a047b18eae/beautifulsoup4-4.9.3-py3-none-any.whl (115kB)\n",
"\u001b[K |████████████████████████████████| 122kB 30.4MB/s eta 0:00:01\n",
"\u001b[?25hRequirement already satisfied: idna<3,>=2.5 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from requests<3.0,>=2.0->ibm_watson) (2.10)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from requests<3.0,>=2.0->ibm_watson) (2020.6.20)\n",
"Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from requests<3.0,>=2.0->ibm_watson) (1.25.10)\n",
"Requirement already satisfied: chardet<4,>=3.0.2 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from requests<3.0,>=2.0->ibm_watson) (3.0.4)\n",
"Requirement already satisfied: six>=1.5 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from python_dateutil>=2.5.3->ibm_watson) (1.15.0)\n",
"Collecting PyJWT>=1.7.1 (from ibm_cloud_sdk_core==1.7.3->ibm_watson)\n",
" Downloading https://files.pythonhosted.org/packages/87/8b/6a9f14b5f781697e51259d81657e6048fd31a113229cf346880bb7545565/PyJWT-1.7.1-py2.py3-none-any.whl\n",
"Collecting soupsieve>1.2; python_version >= \"3.0\" (from beautifulsoup4->bs4)\n",
" Downloading https://files.pythonhosted.org/packages/6f/8f/457f4a5390eeae1cc3aeab89deb7724c965be841ffca6cfca9197482e470/soupsieve-2.0.1-py3-none-any.whl\n",
"Building wheels for collected packages: ibm-watson, bs4, ibm-cloud-sdk-core\n",
" Building wheel for ibm-watson (setup.py) ... \u001b[?25ldone\n",
"\u001b[?25h Stored in directory: /home/jupyterlab/.cache/pip/wheels/6e/14/69/dbbd573a3bab3bf64984572284f13f174f430038308abdd73c\n",
" Building wheel for bs4 (setup.py) ... \u001b[?25ldone\n",
"\u001b[?25h Stored in directory: /home/jupyterlab/.cache/pip/wheels/a0/b0/b2/4f80b9456b87abedbc0bf2d52235414c3467d8889be38dd472\n",
" Building wheel for ibm-cloud-sdk-core (setup.py) ... \u001b[?25ldone\n",
"\u001b[?25h Stored in directory: /home/jupyterlab/.cache/pip/wheels/34/6e/58/589e0f841c2fae9dad99630d78ddc7a60c5c7663a16a39cdbb\n",
"Successfully built ibm-watson bs4 ibm-cloud-sdk-core\n",
"Installing collected packages: websocket-client, PyJWT, ibm-cloud-sdk-core, ibm-watson, soupsieve, beautifulsoup4, bs4\n",
"Successfully installed PyJWT-1.7.1 beautifulsoup4-4.9.3 bs4-0.0.1 ibm-cloud-sdk-core-1.7.3 ibm-watson-4.7.1 soupsieve-2.0.1 websocket-client-0.48.0\n"
]
}
],
"source": [
"!pip install ibm_watson bs4"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"from glob import glob\n",
"\n",
"from bs4 import BeautifulSoup\n",
"\n",
"import IPython\n",
"\n",
"from ibm_cloud_sdk_core.authenticators import IAMAuthenticator\n",
"\n",
"from ibm_watson import SpeechToTextV1\n",
"\n",
"from ibm_watson import AssistantV2\n",
"\n",
"from ibm_watson import TextToSpeechV1"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"recognition_service = SpeechToTextV1(IAMAuthenticator('F8kVB7fth8iFAp25t5hhezgb6Bpmx0AVAZssI5fl0cwC'))\n",
"recognition_service.set_service_url('https://api.us-south.speech-to-text.watson.cloud.ibm.com/instances/53dadef0-bba6-4c2c-8e5b-653a9c499675')\n",
"SPEECH_EXTENSION = \"*.webm\"\n",
"SPEECH_AUDIOTYPE = \"audio/webm\"\n",
"\n",
"def recognize_audio():\n",
" while len(glob(SPEECH_EXTENSION)) == 0:\n",
" pass\n",
" filename = glob(SPEECH_EXTENSION)[0]\n",
" audio_file = open(filename, \"rb\")\n",
" os.popen(\"rm \" + filename)\n",
" result = recognition_service.recognize(audio=audio_file, content_type=SPEECH_AUDIOTYPE).get_result()\n",
" return result[\"results\"][0][\"alternatives\"][0][\"transcript\"]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"assistant = AssistantV2(version='2020-09-24', authenticator=IAMAuthenticator(\"FkA2A-XI8PS5XYgPMUWsf552dGUlb-BZZemvOrxt9aJ7\"))\n",
"assistant.set_service_url('https://api.us-south.assistant.watson.cloud.ibm.com/instances/3154ac57-53b7-4460-9fa8-e449ffd50bb5')\n",
"ASSISTANT_ID = \"ddc55e7a-ab5d-439f-8f72-0978123ed742\"\n",
"session_id = assistant.create_session(assistant_id =ASSISTANT_ID).get_result()[\"session_id\"]\n",
"\n",
"def message_assistant(text):\n",
" response = assistant.message(assistant_id=ASSISTANT_ID,\n",
" session_id=session_id,\n",
" input={'message_type': 'text', 'text': text}).get_result()\n",
" return BeautifulSoup(response[\"output\"][\"generic\"][0][\"text\"]).get_text()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"synthesis_service = TextToSpeechV1(IAMAuthenticator('Sik5sFUdn2jejYO73EQqUNiZiYXLaNoggDkYbun5KFH7'))\n",
"synthesis_service.set_service_url('https://api.us-south.text-to-speech.watson.cloud.ibm.com/instances/52d54d6e-46c6-426f-900d-2e10b4948303')\n",
"\n",
"def speak_text(text):\n",
" with open('temp.wav', 'wb') as audio_file:\n",
" response = synthesis_service.synthesize(text, accept='audio/wav', voice=\"en-US_AllisonV3Voice\").get_result()\n",
" audio_file.write(response.content)\n",
" return IPython.display.Audio(\"temp.wav\", autoplay=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"speak_text(message_assistant(recognize_audio()))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python",
"language": "python",
"name": "conda-env-python-py"
},
"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.11"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment