[BACK-AI-03] FastAPI Gateway Development for Speech Recognition via Whisper Model

AI Components (AI Service)

Published

October 14, 2026

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)

  1. Create the Request Domain Model: Implement the VoiceRequest Pydantic model. The images variable 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: "Распознай русскую речь из аудио.".
  2. Routing and Inference: Set up a POST endpoint at /api/generate that constructs the JSON payload to be sent to the local Ollama server at the address specified in the OLLAMA_HOST variable.
  3. Execution Timeout Constraint: Since the Whisper model runs significantly faster on CPU compared to heavy Vision models, set a strict timeout of 20 seconds (timeout=20).
  4. 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 Exception block, log the critical failure via logger.error and return the following structure: {"text": "Ошибка: превышено время ожидания расшифровки"}.