A real-time audio translation application built with FastAPI, Twilio, and Palabra AI that enables live voice conversations between speakers of different languages.
- Real-time Audio Processing: Live audio streaming and processing using Twilio Media Streams
- Automatic Speech Recognition: Real-time transcription with language detection (English/Russian)
- Live Translation: Instant translation among different languages
- Web Interface: Real-time transcription display with WebSocket updates
- Multi-party Calls: Support for client-operator conversations
- Audio Mixing: Intelligent mixing of original and translated audio
βββββββββββββββ βββββββββββββββ βββββββββββββββββββββββββ
β Client β β Twilio β β Websocket Server β
β (Phone) βββββΊβ (Gateway) βββββΊβ (FastAPI) β
βββββββββββββββ βββββββββββββββ βββββββββββββββββββββββββ
β² β²
β β
| β
β β
β βΌ
β βββββββββββββββ
β β Palabra β
β β API β
β β β
β βββββββββββββββ
β
βΌ
βββββββββββββββ
β Operator β
β (Phone) β
βββββββββββββββ
- Backend: FastAPI, Python 3.11+
- Audio Processing: NumPy, audioop
- WebSocket: Starlette WebSockets
- Telephony: Twilio API
- AI Services: Palabra AI (ASR, Translation, TTS)
- Frontend: HTML, CSS, JavaScript
- Process Management: Multiprocessing with async workers
- Python 3.11 or higher
- Twilio paid account with phone numbers (free trial accounts have limitations)
- Palabra AI API credentials
- Environment variables configured
-
Clone the repository
git clone <repository-url> cd twilio-demo
-
Configure environment variables Edit the
Makefileand set your actual values for:- Twilio credentials
- Palabra AI credentials
- Server configuration
- Language settings
-
Install dependencies
make install
-
Install development dependencies (optional)
make dev
All environment variables are configured in the Makefile. Edit the variables in the Makefile according to your setup:
# Environment variables
export TWILIO_ACCOUNT_SID = your_account_sid
export TWILIO_AUTH_TOKEN = your_auth_token
export TWILIO_NUMBER = your_twilio_phone_number
export PALABRA_CLIENT_ID = your_client_id
export PALABRA_CLIENT_SECRET = your_client_secret
export HOST = your_server_hostname_or_ip
export OPERATOR_NUMBER = operator_phone_number
export PORT = 7839
export SOURCE_LANGUAGE = en
export TARGET_LANGUAGE = plTWILIO_ACCOUNT_SID- Your Twilio Account SIDTWILIO_AUTH_TOKEN- Your Twilio Auth TokenTWILIO_NUMBER- Your Twilio phone number that clients will call
This Twilio article explains how to obtain both credentials.
PALABRA_CLIENT_ID- Your Palabra AI client identifierPALABRA_CLIENT_SECRET- Your Palabra AI client secret key
This Palabra article explains how to obtain both credentials.
HOST- Your server's hostname or IP address (for local development you may use Cloudflare Tunnel URL or its alternatives)OPERATOR_NUMBER- The operator's phone number for receiving callsPORT- Server port number (defaults to 7839)
SOURCE_LANGUAGE- Language spoken by the client (e.g., en, ru, de, es)TARGET_LANGUAGE- Language spoken by the operator (e.g., en, ru, de, es)
The project includes a Makefile for common operations:
make help- Show all available commandsmake install- Create virtual environment and install dependenciesmake dev- Install dependencies with development toolsmake run- Start the server with environment variables from Makefilemake clean- Remove virtual environmentmake format- Format code with black and isortmake check- Run all code quality checks
All environment variables are defined in the Makefile using export statements. This ensures they are available when running make run or other commands.
For local development, you'll need to expose your local server to the internet so Twilio can send webhooks. The recommended tool for this is Cloudflare Tunnel (cloudflared).
-
Install cloudflared Follow the Cloudflare Tunnel documentation for installation instructions.
-
Start cloudflared tunnel
cloudflared tunnel --url http://localhost:${PORT} -
Copy the tunnel URL
https://abc123.trycloudflare.com -
Set HOST variable Use the tunnel URL (without protocol) as your
HOSTvalue:HOST=abc123.trycloudflare.com
- HTTPS Required: Twilio requires HTTPS for webhooks, which Cloudflare Tunnel provides
- Stable URLs: Cloudflare Tunnel provides stable URLs that don't change on restart
- Update Twilio Webhooks: Remember to update your Twilio webhook URLs when the tunnel URL changes
After setting up your tunnel, you need to configure Twilio webhooks to point to your server:
- Go to Twilio Console β Phone Numbers β Manage β Active numbers
- Click on your phone number
- In the "Voice Configuration" section, set:
- Webhook URL:
https://${HOST}/twiml/client - HTTP Method:
POST
- Webhook URL:
For detailed instructions, see the Twilio Phone Number Configuration documentation.
Important: Replace ${HOST} with your actual tunnel hostname (e.g., abc123.trycloudflare.com).
Critical: Ensure that the country of your operator's phone number is enabled in Twilio's Geographic Permissions. If the operator's country is not enabled, Twilio will block outbound calls to that number.
To configure Geographic Permissions:
- Go to Twilio Console β Voice β Geographic Permissions
- Enable calling to the country where your operator's phone number is located
For detailed information about Geographic Permissions and toll fraud protection, see the Twilio Geographic Permissions documentation.
make runThe server will start on http://0.0.0.0:${PORT}
Note: Make sure you have configured all environment variables in the Makefile before starting the server.
- Client calls your Twilio number
- System automatically calls the operator
- Both parties are connected via WebSocket
- Real-time translation begins automatically
Access the transcription interface at:
https://${HOST}:${PORT}/transcription
Replace ${HOST} and ${PORT} with your actual server hostname/IP address and port number.
twilio-demo/
βββ main.py # FastAPI application entry point
βββ bridge.py # Audio bridge and WebSocket handling
βββ settings.py # Configuration and role settings
βββ transcription.py # Transcription broadcasting
βββ utils/
β βββ audio.py # Audio processing workers
β βββ calls.py # Call session management
β βββ worker.py # Async process manager
βββ templates/
β βββ transcription.html # Web interface template
βββ static/
β βββ css/
β β βββ styles.css # Styling for web interface
β βββ js/
β βββ app.js # WebSocket client logic
βββ pyproject.toml # Project configuration
βββ README.md # This file
POST /twiml/client- Handle incoming client callsPOST /voice/callback/{session_id}- Handle call status updatesPOST /voice/disconnect/{role}/{session_id}- Handle call terminationGET /transcription- Web interface for transcriptions
WS /voice/{role}/{session_id}- Audio streaming for callsWS /transcription-ws- Real-time transcription updates
The application processes audio in the following pipeline:
- Input: ΞΌ-law encoded audio from Twilio (8kHz, mono)
- Conversion: Convert to PCM (24kHz, 16-bit, mono)
- Processing: Send to Palabra AI for ASR and translation
- Output: Receive translated audio and mix with original
- Delivery: Send mixed audio back to participants
- Input Format: ΞΌ-law, 8kHz, 1 channel
- Processing Format: PCM s16le, 24kHz, 1 channel
- Output Format: ΞΌ-law, 8kHz, 1 channel
- Twilio Buffer Size: 960 bytes (20ms at 24kHz)
The web interface provides:
- Real-time Transcription: Live display of conversation
- Translation Status: Indicates when translations are pending
- Connection Status: WebSocket connection monitoring
- Responsive Design: Works on desktop and mobile devices
- Twilio Signature Validation: All webhooks are verified
- Environment Variables: Sensitive data stored securely
- Input Validation: All user inputs are validated
- Error Handling: Comprehensive error handling and logging
- Black: Code formatting
- Ruff: Linting and formatting
- isort: Import sorting
- Vulture: Dead code detection
# Format code
ruff format .
# Lint code
ruff check .
# Sort imports
ruff check --select I .
# Check for dead code
vulture .-
WebSocket Connection Failed
- Check if server is running
- Verify firewall settings
- Check WebSocket URL configuration
-
Audio Not Processing
- Verify Palabra AI credentials
- Check audio format compatibility
- Monitor server logs for errors
-
Calls Not Connecting
- Verify Twilio credentials
- Check phone number configuration
- Ensure proper webhook URLs
The application provides detailed logging:
- INFO: Connection status and call events
- WARNING: Non-critical issues
- ERROR: Errors and exceptions
Once the server is running, access the interactive API documentation at:
https://${HOST}:${PORT}/docs

