Last active
March 14, 2023 13:26
-
-
Save VincentK16/abb5241c1067aa0485d272a1fd05b4ea to your computer and use it in GitHub Desktop.
IBM Watson Assistant integration with the Alpha Mini speech recognizer and text to speech system using the Alpha Mini Python SDK from the available demo code.
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
# To run this code, make sure: | |
# 1. Have installed "alphamini" and "ibm-watson" Python modules | |
# 2. Changes to your IBM Watson Assistant parameters: | |
# a. API key | |
# b. Assistant URL | |
# c. Assistant ID | |
# 3. Alpha Mini serial number | |
# Follow through the code below to see where are the changes needed. | |
import asyncio | |
import mini.mini_sdk as MiniSdk | |
from mini.apis.api_observe import ObserveSpeechRecognise | |
from mini.apis.api_sound import StartPlayTTS | |
from mini.apis.api_action import PlayAction, PlayActionResponse | |
from mini.dns.dns_browser import WiFiDevice | |
from mini.apis.base_api import MiniApiResultType | |
from mini.pb2.codemao_speechrecognise_pb2 import SpeechRecogniseResponse | |
#IBM Watson Assistant required modules | |
import json | |
from ibm_watson import AssistantV2 | |
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator | |
#API Key from IBM Watson Assistant settings (change with your API key) | |
authenticator = IAMAuthenticator( | |
'2mLWS_yLl13ZgCy7WT_G6YFXJmCskbUUEb078vTIfZ2O') | |
assistant = AssistantV2( | |
version='2020-09-24', | |
authenticator=authenticator | |
) | |
#Service URL from IBM Watson Assistant settings (copy till "".cloud.ibm.com/") | |
assistant.set_service_url('https://api.us-south.assistant.watson.cloud.ibm.com/') | |
async def test_connect(dev: WiFiDevice) -> bool: | |
"""Connect the device | |
Connect the specified device | |
Args: | |
dev (WiFiDevice): Specified device object WiFiDevice | |
Returns: | |
bool: Whether the connection is successful | |
""" | |
return await MiniSdk.connect(dev) | |
# Disconnect and release resources | |
async def shutdown(): | |
"""Disconnect and release resources | |
Disconnect the currently connected device and release resources | |
""" | |
await MiniSdk.quit_program() | |
await MiniSdk.release() | |
#Change the serial number for your Alpha Mini on the MiniSdk.get_device_by name ("s/n", 10) | |
async def test_get_device_by_name(): | |
"""Search for devices based on the suffix of the robot serial number | |
To search for the robot with the specified serial number (behind the robot's butt), you can just enter the tail character of the serial number, any length, it is recommended that more than 5 characters can be matched accurately, and the timeout is 10 seconds | |
Returns: | |
WiFiDevice: Contains robot name, ip, port and other information | |
""" | |
result: WiFiDevice = await MiniSdk.get_device_by_name("1644", 10) | |
print(f"test_get_device_by_name result:{result}") | |
return result | |
async def test_start_run_program(): | |
"""Enter programming mode | |
Make the robot enter the programming mode, wait for the response result, and delay 6 seconds, let the robot finish "Enter the programming mode" | |
Returns: | |
None: | |
""" | |
await MiniSdk.enter_program() | |
#speech to text function for Alpha Mini | |
async def __tts(x): | |
block: StartPlayTTS = StartPlayTTS(text=x) | |
response = await block.execute() | |
print(f'tes_play_tts: {response}') | |
# Test ,monitor speech recognition | |
async def test_speech_recognise(): | |
#Remember to change your "assistant_id" from the assistant settings | |
create = assistant.create_session(assistant_id='b197f077-ca47-44ca-9ea2-fcf0f9215a8e').get_result() | |
print(create) | |
value = create["session_id"] | |
"""Monitor voice recognition demo | |
Monitor voice recognition events, and the robot reports the text after voice recognition | |
When the voice is recognized as "hello", broadcast "Hello, I am alphamini, Lalila, Lalila" | |
When the voice is recognized as "stop", stop monitoring | |
# SpeechRecogniseResponse.text | |
# SpeechRecogniseResponse.isSuccess | |
# SpeechRecogniseResponse.resultCode | |
""" | |
# Voice monitoring object | |
observe: ObserveSpeechRecognise = ObserveSpeechRecognise() | |
# SpeechRecogniseResponse.text | |
# SpeechRecogniseResponse.isSuccess | |
# SpeechRecogniseResponse.resultCode | |
def handler(msg: SpeechRecogniseResponse): | |
print(f'=======handle speech recognise:{msg}') | |
print(msg.text.lower()) | |
print(type(msg.text.lower())) | |
print("{0}".format(str(msg.text.lower()))) | |
#Remember to change your "assistant_id" from the assistant settings | |
response = assistant.message(assistant_id='b197f077-ca47-44ca-9ea2-fcf0f9215a8e', session_id=value, input={'message_type': 'text','text':str(msg.text)}).get_result() | |
text = response["output"]["generic"][0]["text"] | |
#print the output returned from IBM Watson Assistant | |
print(text) | |
asyncio.create_task(__tts(text)) | |
observe.set_handler(handler) | |
observe.start() | |
await asyncio.sleep(0) | |
if __name__ == '__main__': | |
MiniSdk.set_robot_type(MiniSdk.RobotType.EDU) | |
device: WiFiDevice = asyncio.get_event_loop().run_until_complete(test_get_device_by_name()) | |
if device: | |
asyncio.get_event_loop().run_until_complete(test_connect(device)) | |
asyncio.get_event_loop().run_until_complete(test_start_run_program()) | |
asyncio.get_event_loop().run_until_complete(test_speech_recognise()) | |
# The event listener object is defined, and event_loop.run_forver() must be | |
asyncio.get_event_loop().run_forever() | |
asyncio.get_event_loop().run_until_complete(shutdown()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I want my alpha mini robot speaks English only.