From f1d2ec3bf8a34928c96577ae8dea06fb469cac72 Mon Sep 17 00:00:00 2001 From: Cristiano Ferreira Date: Tue, 26 Mar 2024 23:35:26 +0000 Subject: [PATCH] Fix repeated voices in listening --- helper/exercises.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/helper/exercises.py b/helper/exercises.py index 2fb168b..d3d409b 100644 --- a/helper/exercises.py +++ b/helper/exercises.py @@ -278,6 +278,7 @@ def generate_listening_1_conversation(topic: str): GEN_QUESTION_TEMPERATURE ) + chosen_voices = [] name_to_voice = {} for segment in processed['conversation']: if 'voice' not in segment: @@ -285,10 +286,17 @@ def generate_listening_1_conversation(topic: str): if name in name_to_voice: voice = name_to_voice[name] else: - if segment['gender'].lower() == 'male': - voice = random.choice(MALE_NEURAL_VOICES)['Id'] - else: - voice = random.choice(FEMALE_NEURAL_VOICES)['Id'] + voice = None + while voice is None: + if segment['gender'].lower() == 'male': + available_voices = MALE_NEURAL_VOICES + else: + available_voices = FEMALE_NEURAL_VOICES + + chosen_voice = random.choice(available_voices)['Id'] + if chosen_voice not in chosen_voices: + voice = chosen_voice + chosen_voices.append(voice) name_to_voice[name] = voice segment['voice'] = voice return response, processed