Skip to content

Commit 366336d

Browse files
authored
Clean demo exit on python (#47)
1 parent 125553a commit 366336d

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

recipes/llm-voice-assistant/python/cli/main.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -624,13 +624,12 @@ def main():
624624
else:
625625
config_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'config.json')
626626

627+
config = {}
627628
if os.path.exists(config_path):
628-
with open(config_path, 'r') as fd:
629+
with open(config_path, 'r', encoding='utf-8') as fd:
629630
config = json.load(fd)
630631
elif args.config is not None:
631632
parser.error(f'File {config_path} does not exist')
632-
else:
633-
config = {}
634633

635634
for key in chain(REQUIRED_ARGS, DEFAULT_ARGS):
636635
arg = getattr(args, key)
@@ -641,9 +640,9 @@ def main():
641640
if len(missing) > 0:
642641
parser.error('the following arguments are required: ' + ', '.join(missing))
643642

644-
for key in DEFAULT_ARGS:
643+
for key, value in DEFAULT_ARGS.items():
645644
if key not in config:
646-
config[key] = DEFAULT_ARGS[key]
645+
config[key] = value
647646

648647
stop = [False]
649648

@@ -676,14 +675,19 @@ def handler(_, __) -> None:
676675

677676
print(f"→ Cheetah v{cheetah.version}")
678677

679-
pv_recorder = PvRecorder(frame_length=porcupine.frame_length)
680-
pv_speaker = PvSpeaker(sample_rate=int(orca_connection.recv()), bits_per_sample=16, buffer_size_secs=1)
678+
try:
679+
pv_recorder = PvRecorder(frame_length=porcupine.frame_length)
680+
pv_speaker = PvSpeaker(sample_rate=int(orca_connection.recv()), bits_per_sample=16, buffer_size_secs=1)
681681

682-
pllm_info = pllm_connection.recv()
683-
print(f"→ picoLLM v{pllm_info['version']} <{pllm_info['model']}>")
682+
pllm_info = pllm_connection.recv()
683+
print(f"→ picoLLM v{pllm_info['version']} <{pllm_info['model']}>")
684684

685-
orca_info = orca_connection.recv()
686-
print(f"→ Orca v{orca_info['version']}")
685+
orca_info = orca_connection.recv()
686+
print(f"→ Orca v{orca_info['version']}")
687+
except EOFError:
688+
for child in active_children():
689+
child.kill()
690+
exit(1)
687691

688692
speaker = Speaker(pv_speaker, config)
689693
synthesizer = Synthesizer(speaker, orca_connection, orca_process, config)

recipes/llm-voice-assistant/python/gui/main.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,8 +1018,13 @@ def resize_handler(_, __):
10181018
endpoint_duration_sec=config['cheetah_endpoint_duration_sec'],
10191019
enable_automatic_punctuation=True)
10201020

1021-
pv_recorder = PvRecorder(frame_length=porcupine.frame_length)
1022-
pv_speaker = PvSpeaker(sample_rate=int(orca_connection.recv()), bits_per_sample=16, buffer_size_secs=1)
1021+
try:
1022+
pv_recorder = PvRecorder(frame_length=porcupine.frame_length)
1023+
pv_speaker = PvSpeaker(sample_rate=int(orca_connection.recv()), bits_per_sample=16, buffer_size_secs=1)
1024+
except EOFError:
1025+
for child in active_children():
1026+
child.kill()
1027+
exit(1)
10231028

10241029
speaker = Speaker(queue, pv_speaker, config['orca_warmup_sec'])
10251030
synthesizer = Synthesizer(queue, speaker, orca_connection, orca_process)

0 commit comments

Comments
 (0)