mirror of
https://github.com/ijaric/voice_assistant.git
synced 2025-12-17 11:46:20 +00:00
feat: [#46] хендлер с рабочим stt
This commit is contained in:
@@ -1,3 +1,7 @@
|
||||
from .health import basic_router
|
||||
from .voice_responce_handler import VoiceResponseHandler
|
||||
|
||||
__all__ = ["basic_router"]
|
||||
__all__ = [
|
||||
"VoiceResponseHandler",
|
||||
"basic_router",
|
||||
]
|
||||
|
||||
28
src/assistant/lib/api/v1/handlers/voice_responce_handler.py
Normal file
28
src/assistant/lib/api/v1/handlers/voice_responce_handler.py
Normal file
@@ -0,0 +1,28 @@
|
||||
import fastapi
|
||||
|
||||
import lib.models.tts.voice as models_tts_voice
|
||||
import lib.stt.services as stt_services
|
||||
|
||||
|
||||
class VoiceResponseHandler:
|
||||
def __init__(
|
||||
self,
|
||||
stt: stt_services.SpeechService,
|
||||
):
|
||||
self.stt = stt
|
||||
self.router = fastapi.APIRouter()
|
||||
self.router.add_api_route(
|
||||
"/",
|
||||
self.voice_response,
|
||||
methods=["POST"],
|
||||
summary="Ответ голосового помощника",
|
||||
description="Ответ голосового помощника",
|
||||
)
|
||||
|
||||
async def voice_response(
|
||||
self,
|
||||
voice: bytes = fastapi.File(...),
|
||||
voice_model: models_tts_voice.VoiceModelProvidersEnum = fastapi.Depends(),
|
||||
) -> dict[str, str]:
|
||||
voice_text: str = await self.stt.recognize(voice)
|
||||
return {"text": voice_text}
|
||||
@@ -1,3 +1,6 @@
|
||||
from .base import HealthResponse
|
||||
from .base import HealthResponse, VoiceResponse
|
||||
|
||||
__all__ = ["HealthResponse"]
|
||||
__all__ = [
|
||||
"HealthResponse",
|
||||
"VoiceResponse",
|
||||
]
|
||||
|
||||
@@ -3,3 +3,7 @@ import pydantic
|
||||
|
||||
class HealthResponse(pydantic.BaseModel):
|
||||
status: str = pydantic.Field(default=..., examples=["healthy"], description="Схема доступности сервиса")
|
||||
|
||||
|
||||
class VoiceResponse(pydantic.BaseModel):
|
||||
voice: bytes = pydantic.Field(default=..., description="Голосовой ответ")
|
||||
|
||||
@@ -81,12 +81,12 @@ 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)
|
||||
# Handlers
|
||||
|
||||
logger.info("Initializing handlers")
|
||||
liveness_probe_handler = api_v1_handlers.basic_router
|
||||
voice_response_handler = api_v1_handlers.VoiceResponseHandler(stt=stt_service).router
|
||||
|
||||
logger.info("Creating application")
|
||||
|
||||
@@ -100,6 +100,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"])
|
||||
|
||||
application = Application(
|
||||
settings=settings,
|
||||
|
||||
@@ -3,6 +3,7 @@ import tempfile
|
||||
|
||||
import magic
|
||||
import openai
|
||||
import pydantic
|
||||
|
||||
import lib.app.settings as app_settings
|
||||
import lib.stt as stt
|
||||
@@ -26,13 +27,15 @@ class OpenaiSpeechRepository:
|
||||
file_extension = self.__get_file_extension_from_bytes(audio)
|
||||
if not file_extension:
|
||||
raise ValueError("File extension is not supported")
|
||||
|
||||
voice: stt.models.SttVoice = stt.models.SttVoice(
|
||||
audio_size=len(audio) // 1024, # audio size in MB,
|
||||
audio_format=file_extension,
|
||||
audio_data=audio,
|
||||
voice_settings=self.settings.voice,
|
||||
)
|
||||
try:
|
||||
voice: stt.models.SttVoice = stt.models.SttVoice(
|
||||
audio_size=len(audio) // 1024, # audio size in MB,
|
||||
audio_format=file_extension,
|
||||
audio_data=audio,
|
||||
voice_settings=self.settings.voice,
|
||||
)
|
||||
except (pydantic.ValidationError, ValueError) as e:
|
||||
raise ValueError(f"Voice validation error: {e}")
|
||||
|
||||
try:
|
||||
with tempfile.NamedTemporaryFile(suffix=f".{file_extension}") as temp_file:
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
from .services import TTSService
|
||||
|
||||
__all__ = [
|
||||
"TTSService",
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user