Async release
This commit is contained in:
63
app/controllers/impl/speaking.py
Normal file
63
app/controllers/impl/speaking.py
Normal file
@@ -0,0 +1,63 @@
|
||||
import logging
|
||||
import uuid
|
||||
|
||||
from fastapi import BackgroundTasks
|
||||
|
||||
from app.controllers.abc import ISpeakingController
|
||||
from app.dtos import (
|
||||
SaveSpeakingDTO, SpeakingGenerateVideoDTO,
|
||||
SpeakingGenerateInteractiveVideoDTO
|
||||
)
|
||||
from app.services.abc import ISpeakingService
|
||||
from app.configs.constants import ExamVariant, MinTimers
|
||||
from app.configs.question_templates import getSpeakingTemplate
|
||||
|
||||
|
||||
class SpeakingController(ISpeakingController):
|
||||
|
||||
def __init__(self, speaking_service: ISpeakingService):
|
||||
self._service = speaking_service
|
||||
self._logger = logging.getLogger(__name__)
|
||||
|
||||
async def get_speaking_task(self, task: int, topic: str, difficulty: str):
|
||||
try:
|
||||
return await self._service.get_speaking_task(task, topic, difficulty)
|
||||
except Exception as e:
|
||||
return str(e)
|
||||
|
||||
async def save_speaking(self, data: SaveSpeakingDTO, background_tasks: BackgroundTasks):
|
||||
try:
|
||||
exercises = data.exercises
|
||||
min_timer = data.minTimer
|
||||
|
||||
template = getSpeakingTemplate()
|
||||
template["minTimer"] = min_timer
|
||||
|
||||
if min_timer < MinTimers.SPEAKING_MIN_TIMER_DEFAULT:
|
||||
template["variant"] = ExamVariant.PARTIAL.value
|
||||
else:
|
||||
template["variant"] = ExamVariant.FULL.value
|
||||
|
||||
req_id = str(uuid.uuid4())
|
||||
self._logger.info(f'Received request to save speaking with id: {req_id}')
|
||||
|
||||
background_tasks.add_task(self._service.create_videos_and_save_to_db, exercises, template, req_id)
|
||||
|
||||
self._logger.info('Started background task to save speaking.')
|
||||
|
||||
# Return response without waiting for create_videos_and_save_to_db to finish
|
||||
return {**template, "id": req_id}
|
||||
except Exception as e:
|
||||
return str(e)
|
||||
|
||||
async def generate_speaking_video(self, data: SpeakingGenerateVideoDTO):
|
||||
try:
|
||||
return await self._service.generate_speaking_video(data.question, data.topic, data.avatar, data.prompts)
|
||||
except Exception as e:
|
||||
return str(e)
|
||||
|
||||
async def generate_interactive_video(self, data: SpeakingGenerateInteractiveVideoDTO):
|
||||
try:
|
||||
return await self._service.generate_interactive_video(data.questions, data.topic, data.avatar)
|
||||
except Exception as e:
|
||||
return str(e)
|
||||
Reference in New Issue
Block a user