A lightweight WebSocket service built with Starlette that receives audio chunks, transcribes them using OpenAI Whisper, and streams results back in near real-time.
- WebSocket-based audio streaming
- Real-time transcription using OpenAI Whisper API
- Audio file storage
- Docker support
- CLI test client
- Python 3.11+
- Docker and Docker Compose (for containerized deployment)
- OpenAI API key with Whisper access
Copy the example environment file and add your OpenAI API key:
cp .env.example .envEdit .env and replace your_openai_api_key_here with your actual OpenAI API key.
Build and run the service:
docker-compose up --buildThe service will be available at ws://localhost:8000/ws
Install dependencies:
pip install -r requirements.txtExport your OpenAI API key:
export OPENAI_API_KEY=your_openai_api_key_hereRun the service:
python main.pyThe included test client sends audio files to the service:
python test_client.py path/to/audio.wavOptions:
--uri: WebSocket server URI (default:ws://localhost:8000/ws)--chunk-size: Chunk size in bytes (default: 4096)
Example:
python test_client.py sample.wav --chunk-size 8192The service accepts WebSocket connections at /ws:
- Connect to
ws://localhost:8000/ws - Receive connection confirmation
- Send audio chunks as binary data
- Receive transcription results in JSON format:
{ "type": "transcription", "text": "transcribed text here" } - Send text message
"END"to finalize and save the audio file - Receive confirmation with saved file path
status: Connection status or completion messagestranscription: Transcribed text from audio chunkerror: Error messages
Audio files are automatically saved to the audio_files/ directory with timestamps:
- Format:
audio_YYYYMMDD_HHMMSS.wav - Files are saved when the client sends "END" or disconnects
The service uses:
- Starlette: Lightweight ASGI framework
- Uvicorn: ASGI server
- OpenAI: Whisper API integration
- websockets: WebSocket client library (for test client)
- Service do pseudostreaming - it runs accumulated buffer multiple times increasing request size
- The service processes each audio chunk independently through Whisper
- Temporary files are created for API calls and cleaned up immediately
- All audio chunks are stored in memory and saved at the end
- The service supports multiple concurrent connections