ENCOA-295

This commit is contained in:
Carlos-Mesquita
2024-12-26 12:31:22 +00:00
parent 09d6242360
commit 9bfad2d47f
13 changed files with 170 additions and 38 deletions

View File

@@ -3,10 +3,9 @@ from logging import getLogger
import random
from typing import Dict, Any
import aiofiles
from starlette.datastructures import UploadFile
from ielts_be.dtos.listening import GenerateListeningExercises, Dialog, ListeningExercises
from ielts_be.dtos.listening import ListeningExercisesDTO, Dialog, ListeningExercises, ConversationPayload
from ielts_be.exceptions.exceptions import TranscriptionException
from ielts_be.repositories import IFileStorage, IDocumentStore
from ielts_be.services import IListeningService, ILLMService, ITextToSpeechService, ISpeechToTextService
@@ -114,7 +113,16 @@ class ListeningService(IListeningService):
async def generate_mp3(self, dto: Dialog) -> bytes:
return await self._tts.text_to_speech(dto)
async def get_listening_question(self, dto: GenerateListeningExercises):
async def create_instructions(self, text: str) -> bytes:
script = Dialog(conversation=[ConversationPayload(**{
"text": text,
"voice": "Matthew",
"name": "",
"gender": ""
})])
return await self._tts.text_to_speech(script, False)
async def get_listening_question(self, dto: ListeningExercisesDTO):
start_id = 1
exercise_tasks = []

View File

@@ -1,4 +1,5 @@
import random
from typing import Optional
from aiobotocore.client import BaseClient
@@ -21,7 +22,7 @@ class AWSPolly(ITextToSpeechService):
)
return await tts_response['AudioStream'].read()
async def text_to_speech(self, dialog: Dialog) -> bytes:
async def text_to_speech(self, dialog: Dialog, include_final_clue = True) -> bytes:
if not dialog.conversation and not dialog.monologue:
raise ValueError("Unsupported argument for text_to_speech")
@@ -30,13 +31,14 @@ class AWSPolly(ITextToSpeechService):
else:
audio_segments = await self._conversation_to_speech(dialog)
final_message = await self.synthesize_speech(
"This audio recording, for the listening exercise, has finished.",
"Stephen"
)
if include_final_clue:
final_message = await self.synthesize_speech(
"This audio recording, for the listening exercise, has finished.",
"Stephen"
)
# Add finish message
audio_segments.append(final_message)
# Add finish message
audio_segments.append(final_message)
# Combine the audio segments into a single audio file
combined_audio = b"".join(audio_segments)