[BACK-AI-03] FastAPI Gateway Development for Speech Recognition via Whisper Model
AI Components (AI Service)
NoteTask Brief
- Repository / Component:
ai_voice_stt_gateway. - Language: Python 3.10+ (FastAPI, Pydantic, Requests).
- Task Type: Backend (Speech AI Integrations).
- Status: Ready for Implementation
Task Description
Implement a specialized FastAPI-based gateway microservice that accepts binary audio data (microphone stream buffers or .wav files) packed in Base64 within an array from the main Go service, proxies it to the local Ollama neural network API (whisper-base), and returns a structured transcription text log.
Step-by-Step Instructions (Python)
- Create the Request Domain Model: Implement the
VoiceRequestPydantic model. Theimagesvariable is reused as a container to pass the Base64 audio file string (in compliance with the Ollama API contract). Hardcode the default values to:model: "whisper-base",prompt: "Распознай русскую речь из аудио.". - Routing and Inference: Set up a POST endpoint at
/api/generatethat constructs the JSON payload to be sent to the local Ollama server at the address specified in theOLLAMA_HOSTvariable. - Execution Timeout Constraint: Since the Whisper model runs significantly faster on CPU compared to heavy Vision models, set a strict timeout of
20seconds (timeout=20). - Graceful Error Handling for Inference Failures:
- If the queue is overloaded or Ollama returns an invalid status code (non-200), catch the event and return a JSON string:
{"text": "Ошибка: ИИ перегружен"}. - In case of network exceptions or a timeout within the
except Exceptionblock, log the critical failure vialogger.errorand return the following structure:{"text": "Ошибка: превышено время ожидания расшифровки"}.
- If the queue is overloaded or Ollama returns an invalid status code (non-200), catch the event and return a JSON string: