Skip to content

Commit

Permalink
[WIP]
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterBowman committed Dec 2, 2023
1 parent c66bc94 commit b0b0933
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 28 deletions.
39 changes: 15 additions & 24 deletions programs/chatCompletion/chatCompletion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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()
Expand All @@ -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

Expand All @@ -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()
2 changes: 1 addition & 1 deletion programs/speechRecognition/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<prefix>/rpc:s` port that allows to request a dictionary/model change and to mute/unmute the microphone, and a `<prefix>/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 `<prefix>/rpc:s` port that allows to request a dictionary/model change and to mute/unmute the microphone, and a `<prefix>/result:o` port that broadcasts the transcribed text. The default prefix is `/speechRecognition`, but it can be changed with the `--port` option.

## PocketSphinx backend

Expand Down
6 changes: 3 additions & 3 deletions programs/speechRecognition/speechRecognition.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit b0b0933

Please sign in to comment.