mirror of
https://github.com/ijaric/voice_assistant.git
synced 2025-05-24 14:33:26 +00:00
feat: [#56] add handler
This commit is contained in:
parent
71387f5e8c
commit
1a622c3cf0
|
@ -1,7 +1,9 @@
|
|||
from .health import basic_router
|
||||
from .tts import *
|
||||
from .voice_responce_handler import VoiceResponseHandler
|
||||
|
||||
__all__ = [
|
||||
"TTSHandler",
|
||||
"VoiceResponseHandler",
|
||||
"basic_router",
|
||||
]
|
||||
|
|
66
src/assistant/lib/api/v1/handlers/tts.py
Normal file
66
src/assistant/lib/api/v1/handlers/tts.py
Normal file
|
@ -0,0 +1,66 @@
|
|||
import http
|
||||
|
||||
import fastapi
|
||||
|
||||
import lib.models as models
|
||||
import lib.tts.services as tts_service
|
||||
|
||||
|
||||
class TTSHandler:
|
||||
def __init__(
|
||||
self,
|
||||
tts: tts_service.TTSService,
|
||||
):
|
||||
self.tts = tts
|
||||
self.router = fastapi.APIRouter()
|
||||
self.router.add_api_route(
|
||||
"/fields",
|
||||
self.get_by_fields,
|
||||
methods=["POST"],
|
||||
summary="Получение моделей по полю",
|
||||
description="Возвращает список моделей с указанными полями",
|
||||
)
|
||||
self.router.add_api_route(
|
||||
"/name",
|
||||
self.get_by_name,
|
||||
methods=["POST"],
|
||||
summary="Получение модели по имени",
|
||||
description="Позволяет получить одну модель по её имени",
|
||||
)
|
||||
self.router.add_api_route(
|
||||
"/",
|
||||
self.get_all,
|
||||
methods=["GET"],
|
||||
summary="Получение всех доступных моделей",
|
||||
description="Возвращает список всех доступных моделей",
|
||||
)
|
||||
self.router.add_api_route(
|
||||
"/languages",
|
||||
self.get_languages,
|
||||
methods=["GET"],
|
||||
summary="Получение всех доступных языков",
|
||||
description="Возвращает список всех доступных языков",
|
||||
)
|
||||
|
||||
async def get_by_fields(
|
||||
self,
|
||||
data: models.TTSSearchVoiceRequestModel,
|
||||
) -> list[models.AVAILABLE_MODELS_TYPE]:
|
||||
response = await self.tts.get_list_voice_models_by_fields(data)
|
||||
return response
|
||||
|
||||
async def get_by_name(
|
||||
self,
|
||||
model_name: str,
|
||||
) -> models.BaseVoiceModel:
|
||||
response = await self.tts.get_voice_model_by_name(model_name)
|
||||
if not response:
|
||||
raise fastapi.HTTPException(status_code=http.HTTPStatus.BAD_REQUEST, detail="Model not found")
|
||||
return response
|
||||
|
||||
async def get_all(self) -> list[models.AVAILABLE_MODELS_TYPE]:
|
||||
return await self.tts.get_all_models()
|
||||
|
||||
@classmethod
|
||||
async def get_languages(cls) -> dict[str, str]:
|
||||
return {language.name: language.value for language in models.BaseLanguageCodesEnum}
|
|
@ -107,15 +107,15 @@ class Application:
|
|||
# Services
|
||||
|
||||
logger.info("Initializing services")
|
||||
stt_service: stt.SpeechService = stt.SpeechService(repository=stt_repository) # type: ignore
|
||||
stt_service: stt.SpeechService = stt.SpeechService(repository=stt_repository)
|
||||
|
||||
tts_service: tts.TTSService = tts.TTSService( # type: ignore
|
||||
tts_service: tts.TTSService = tts.TTSService(
|
||||
repositories={
|
||||
models.VoiceModelProvidersEnum.YANDEX: tts_yandex_repository,
|
||||
models.VoiceModelProvidersEnum.ELEVEN_LABS: tts_eleven_labs_repository,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
# Handlers
|
||||
|
||||
logger.info("Initializing handlers")
|
||||
|
@ -127,6 +127,8 @@ class Application:
|
|||
# tts=tts_service, # TODO
|
||||
).router
|
||||
|
||||
tts_handler = api_v1_handlers.TTSHandler(tts=tts_service).router
|
||||
|
||||
logger.info("Creating application")
|
||||
|
||||
fastapi_app = fastapi.FastAPI(
|
||||
|
@ -140,6 +142,7 @@ class Application:
|
|||
# Routes
|
||||
fastapi_app.include_router(liveness_probe_handler, prefix="/api/v1/health", tags=["health"])
|
||||
fastapi_app.include_router(voice_response_handler, prefix="/api/v1/voice", tags=["voice"])
|
||||
fastapi_app.include_router(tts_handler, prefix="/api/v1/tts", tags=["tts"])
|
||||
|
||||
application = Application(
|
||||
settings=settings,
|
||||
|
|
Loading…
Reference in New Issue
Block a user