diff --git a/programs/chatCompletion/chatCompletion.py b/programs/chatCompletion/chatCompletion.py index 760e702..f4f7614 100644 --- a/programs/chatCompletion/chatCompletion.py +++ b/programs/chatCompletion/chatCompletion.py @@ -4,13 +4,12 @@ import os import openai import roboticslab_speech -import signal import time import yarp MODEL = "gpt-3.5-turbo" -# openai.api_key = os.getenv("OPENAI_API_KEY") +openai.api_key = os.getenv("OPENAI_API_KEY") class SpeechRecognitionCallback(yarp.BottleCallback): def __init__(self): @@ -19,7 +18,7 @@ def __init__(self): def onRead(self, bottle, reader): print("Port %s received: %s" % (reader.getName(), bottle.toString())) -parser = argparse.ArgumentParser(prog='chatCompletion', description='a simple ChatGPT client') +parser = argparse.ArgumentParser(prog='chatCompletion', description='A simple GPT chat client') parser.add_argument('--port', default='/chatCompletion', help='port prefix (default: /chatCompletion)') args = parser.parse_args() @@ -43,7 +42,7 @@ def onRead(self, bottle, reader): print('Cannot open port %s' % ttsRpcPort.getName()) raise SystemExit -if not asrStreamPort.open(args.port + '/asr/stream:i'): +if not asrStreamPort.open(args.port + '/asr/result:i'): print('Cannot open port %s' % asrStreamPort.getName()) raise SystemExit @@ -55,26 +54,18 @@ def onRead(self, bottle, reader): tts.yarp().attachAsClient(ttsRpcPort) asrStreamPort.useCallback(callback) -quitRequested = False +try: + while True: + time.sleep(1) +except KeyboardInterrupt: + # tts.stop() -def askToStop(): - global quitRequested - quitRequested = True + asrStreamPort.disableCallback() + asrStreamPort.interrupt() + asrStreamPort.close() -signal.signal(signal.SIGINT, lambda signal, frame: askToStop()) -signal.signal(signal.SIGTERM, lambda signal, frame: askToStop()) + asrRpcPort.interrupt() + asrRpcPort.close() -while not quitRequested: - time.sleep(0.1) - -# tts.stop() - -asrStreamPort.disableCallback() -asrStreamPort.interrupt() -asrStreamPort.close() - -asrRpcPort.interrupt() -asrRpcPort.close() - -ttsRpcPort.interrupt() -ttsRpcPort.close() + ttsRpcPort.interrupt() + ttsRpcPort.close() \ No newline at end of file diff --git a/programs/speechRecognition/README.md b/programs/speechRecognition/README.md index b6b45ba..c126d21 100644 --- a/programs/speechRecognition/README.md +++ b/programs/speechRecognition/README.md @@ -12,7 +12,7 @@ Depending on the selected backend, additional dependencies might be required (se Launch the program with `--help` to see available options. You can display and select the preferred input device with `--list-devices` and `--device`, respectively (otherwise the system default will be chosen). -This application opens two YARP ports: an `/rpc:s` port that allows to request a dictionary/model change and to mute/unmute the microphone, and a `/result:o` port that broadcasts the transcribed text. The default prefix is `/speechRecognition`, but it can be changed with the `--prefix` option. +This application opens two YARP ports: an `/rpc:s` port that allows to request a dictionary/model change and to mute/unmute the microphone, and a `/result:o` port that broadcasts the transcribed text. The default prefix is `/speechRecognition`, but it can be changed with the `--port` option. ## PocketSphinx backend diff --git a/programs/speechRecognition/speechRecognition.py b/programs/speechRecognition/speechRecognition.py index 80ff965..555ad9c 100644 --- a/programs/speechRecognition/speechRecognition.py +++ b/programs/speechRecognition/speechRecognition.py @@ -218,7 +218,7 @@ def int_or_str(text): parser.add_argument('--dictionary', type=str, help='(only --backend pocketsphinx) dictionary, e.g. follow-me') parser.add_argument('--language', type=str, help='(only --backend pocketsphinx) language, e.g. en-us') parser.add_argument('--model', type=str, help='(only --backend vosk) model, e.g. es-0.42') -parser.add_argument('--prefix', '-p', type=str, default='/speechRecognition', help='YARP port prefix') +parser.add_argument('--port', '-p', type=str, default='/speechRecognition', help='YARP port prefix') parser.add_argument('--context', type=str, default='speechRecognition', help='YARP context directory') parser.add_argument('--from', type=str, dest='ini', default='speechRecognition.ini', help='YARP configuration (.ini) file') args = parser.parse_args(remaining) @@ -252,11 +252,11 @@ def int_or_str(text): asrPort = yarp.BufferedPortBottle() configPort = yarp.RpcServer() -if not asrPort.open(args.prefix + '/result:o'): +if not asrPort.open(args.port + '/result:o'): print('Unable to open output port') raise SystemExit -if not configPort.open(args.prefix + '/rpc:s'): +if not configPort.open(args.port + '/rpc:s'): print('Unable to open RPC port') raise SystemExit