A cloud-native outbound calling application that uses Twilio for telephony and Google Gemini Live API for real-time AI-powered conversations. The system enables automated policy renewal reminders and handles customer queries with natural voice interactions.
The system consists of three main components:
βββββββββββββββ βββββββββββββββ βββββββββββββββ
β React β HTTP β FastAPI β WSS β Twilio β
β Frontend ββββββββββΆβ Backend βββββββββββ Media β
β β β β β Stream β
βββββββββββββββ ββββββββ¬βββββββ βββββββββββββββ
β
β WebSocket
βΌ
βββββββββββββββ
β Gemini β
β Live API β
β (Vertex AI) β
βββββββββββββββ
- Call Initiation: Customer service representative enters a phone number in the React frontend
- Twilio Connection: Backend initiates an outbound call via Twilio Voice API
- Media Streaming: When customer answers, Twilio establishes a bidirectional WebSocket media stream
- AI Processing: Audio from customer is sent to Gemini Live API for real-time processing
- Natural Conversation: AI responds with voice, creating a natural conversation flow
- Transcription: Full conversation is transcribed and displayed in real-time on the frontend
Customer ββΆ Twilio (ΞΌ-law 8kHz) ββΆ Backend ββΆ Gemini (PCM 16kHz)
β
Customer ββ Twilio (ΞΌ-law 8kHz) ββ Backend ββββ (PCM 24kHz)
- Real-time Voice AI: Powered by Google Gemini 2.5 Flash Live with native audio support
- Natural Conversations: Supports interruptions, turn-taking, and multilingual interactions
- Indian English Support: Optimized for Indian accents and regional languages (Hindi, Tamil, Telugu, etc.)
- Live Transcription: Real-time conversation transcript displayed on frontend
- WebSocket Streaming: Bidirectional audio streaming with minimal latency
- Cloud-Native: Designed for deployment on Google Cloud Run
- Scalable: Handles multiple concurrent calls with automatic scaling
Before you begin, ensure you have:
- Python 3.11+ installed
- Node.js 18+ and npm
- Google Cloud Account with Vertex AI API enabled
- Twilio Account with a phone number
- uv package manager (
pip install uv)
git clone git@github.com:kkrishnan90/Gemini-Live-Twilio-.git
cd Gemini-Live-Twilio-cd backend
uv venv
source .venv/bin/activate
uv pip install -r requirements.txtCopy the example environment file:
cp .env.example .envEdit .env with your credentials:
# Application Settings
DEBUG_MODE=false
LOG_LEVEL=INFO
# Server Configuration
BACKEND_HOST=0.0.0.0
BACKEND_PORT=8000
BACKEND_URL=http://localhost:8000
# Twilio Configuration
TWILIO_ACCOUNT_SID=your_twilio_account_sid_here
TWILIO_AUTH_TOKEN=your_twilio_auth_token_here
TWILIO_PHONE_NUMBER=your_twilio_phone_number_here
# Google Cloud / Vertex AI
GOOGLE_CLOUD_PROJECT=your_google_cloud_project_id
GOOGLE_CLOUD_LOCATION=us-central1
GOOGLE_APPLICATION_CREDENTIALS=/path/to/your/service-account-key.json
# Gemini Live API
GEMINI_MODEL=gemini-live-2.5-flash-preview-native-audio-09-2025
GEMINI_TEMPERATURE=0.7
GEMINI_TOP_P=0.95
# Frontend
FRONTEND_URL=http://localhost:3000
# Audio Settings (in Hz)
TWILIO_SAMPLE_RATE=8000
GEMINI_INPUT_SAMPLE_RATE=16000
GEMINI_OUTPUT_SAMPLE_RATE=24000source .venv/bin/activate
uv run python app.pyThe backend will start on http://localhost:8000
cd frontend
npm installCopy the example environment file:
cp .env.example .envEdit .env:
REACT_APP_BACKEND_URL=http://localhost:8000npm startThe frontend will open in your browser at http://localhost:3000
- Sign up at twilio.com
- Get your Account SID and Auth Token from the dashboard
- Purchase a phone number with voice capabilities
For local development with ngrok:
# Install ngrok
brew install ngrok # macOS
# or download from https://ngrok.com
# Start ngrok tunnel
ngrok http 8000
# Copy the HTTPS URL (e.g., https://abc123.ngrok.io)Configure your Twilio phone number:
- Go to Twilio Console β Phone Numbers
- Click on your phone number
- Under Voice Configuration:
- A Call Comes In:
https://your-ngrok-url.ngrok.io/voice/twiml(HTTP POST) - Status Callback URL:
https://your-ngrok-url.ngrok.io/voice/status(HTTP POST)
- A Call Comes In:
- Save
For production (Cloud Run):
- Replace ngrok URL with your Cloud Run URL
- Example:
https://your-service-name.run.app/voice/twiml
- Start the backend:
uv run python app.py - Start ngrok:
ngrok http 8000 - Update Twilio webhook with ngrok URL
- Make a test call through the frontend
gcloud services enable aiplatform.googleapis.com
gcloud services enable cloudresourcemanager.googleapis.com# Create service account
gcloud iam service-accounts create gemini-calling-sa \
--display-name="Gemini Calling Service Account"
# Grant Vertex AI User role
gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \
--member="serviceAccount:gemini-calling-sa@YOUR_PROJECT_ID.iam.gserviceaccount.com" \
--role="roles/aiplatform.user"
# Create and download key
gcloud iam service-accounts keys create ~/gemini-calling-key.json \
--iam-account=gemini-calling-sa@YOUR_PROJECT_ID.iam.gserviceaccount.comexport GOOGLE_APPLICATION_CREDENTIALS="/path/to/gemini-calling-key.json"Or add it to your .env file:
GOOGLE_APPLICATION_CREDENTIALS=/path/to/gemini-calling-key.jsonGemini-Live-Twilio-/
βββ backend/
β βββ app.py # FastAPI application
β βββ config.py # Configuration management
β βββ requirements.txt # Python dependencies
β βββ .env # Environment variables (not committed)
β βββ .env.example # Environment template
β βββ services/
β β βββ twilio_service.py # Twilio integration
β β βββ gemini_service.py # Gemini Live API client
β β βββ audio_bridge.py # Audio format conversion
β β βββ call_orchestrator.py # Call flow management
β βββ models/
β βββ call_session.py # Session state models
β βββ user_context.py # User/policy models
βββ frontend/
β βββ src/
β β βββ App.js # Main React component
β β βββ components/
β β β βββ CallInitiator.jsx # Call initiation UI
β β β βββ CallStatus.jsx # Real-time status display
β β βββ services/
β β βββ api.js # Backend API client
β βββ package.json
β βββ .env # Frontend environment (not committed)
βββ deployment/
β βββ Dockerfile # Docker configuration
β βββ cloudbuild.yaml # Cloud Build config
β βββ smoke_test.sh # Health check script
βββ .gitignore
βββ README.md
βββ TECHNICAL_ARCHITECTURE.md
| Variable | Description | Required | Default |
|---|---|---|---|
DEBUG_MODE |
Enable debug logging | No | false |
LOG_LEVEL |
Logging level (INFO, DEBUG, WARNING, ERROR) | No | INFO |
BACKEND_HOST |
Server host address | No | 0.0.0.0 |
BACKEND_PORT |
Server port | No | 8000 |
BACKEND_URL |
Public backend URL | Yes | - |
TWILIO_ACCOUNT_SID |
Twilio Account SID | Yes | - |
TWILIO_AUTH_TOKEN |
Twilio Auth Token | Yes | - |
TWILIO_PHONE_NUMBER |
Twilio phone number (E.164 format) | Yes | - |
GOOGLE_CLOUD_PROJECT |
GCP Project ID | Yes | - |
GOOGLE_CLOUD_LOCATION |
GCP Region | No | us-central1 |
GOOGLE_APPLICATION_CREDENTIALS |
Path to service account key | Yes | - |
GEMINI_MODEL |
Gemini model name | No | gemini-live-2.5-flash-preview-native-audio-09-2025 |
GEMINI_TEMPERATURE |
Model temperature (0.0-1.0) | No | 0.7 |
GEMINI_TOP_P |
Model top_p (0.0-1.0) | No | 0.95 |
FRONTEND_URL |
Frontend URL for CORS | No | http://localhost:3000 |
| Variable | Description | Required | Default |
|---|---|---|---|
REACT_APP_BACKEND_URL |
Backend API URL | Yes | http://localhost:8000 |
GET /healthResponse:
{
"status": "healthy",
"service": "Cymbal Insurance Provider Calling System"
}POST /api/calls/initiate
Content-Type: application/json
{
"to_number": "+919876543210"
}Response:
{
"success": true,
"call_sid": "CAxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"message": "Call initiated successfully"
}GET /api/calls/{call_sid}/sessionResponse:
{
"call_sid": "CAxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"user_phone": "+919876543210",
"state": "in_progress",
"transcript": [
{
"speaker": "ai",
"text": "Hello, this is Lucy from Cymbal Insurance Provider...",
"timestamp": "2024-11-14T10:30:00Z"
}
],
"metrics": {
"audio_packets_sent": 1500,
"audio_packets_received": 1200,
"ai_response_count": 5
}
}WS /ws/media-stream/{call_sid}
Used by Twilio for bidirectional audio streaming.
curl http://localhost:8000/healthcurl -X POST http://localhost:8000/api/calls/initiate \
-H "Content-Type: application/json" \
-d '{"to_number": "+919876543210"}'cd deployment
chmod +x smoke_test.sh
./smoke_test.sh http://localhost:8000- Build and deploy:
gcloud builds submit \
--config=deployment/cloudbuild.yaml \
--project=YOUR_PROJECT_ID- Configure secrets in Secret Manager:
# Store Twilio credentials
echo -n "your_account_sid" | gcloud secrets create twilio-account-sid \
--data-file=- --project=YOUR_PROJECT_ID
echo -n "your_auth_token" | gcloud secrets create twilio-auth-token \
--data-file=- --project=YOUR_PROJECT_ID
echo -n "+1234567890" | gcloud secrets create twilio-phone-number \
--data-file=- --project=YOUR_PROJECT_ID- Update Twilio webhook with your Cloud Run URL
See deployment/DEPLOYMENT_SUMMARY.md for detailed deployment instructions.
Issue: ModuleNotFoundError or import errors
Solution:
cd backend
source .venv/bin/activate
uv pip install -r requirements.txtIssue: 404 Not Found on Twilio webhook
Solution:
- Verify backend is running
- Check ngrok is active and URL is correct
- Ensure webhook URL in Twilio console matches ngrok URL
- Check backend logs:
tail -f backend/logs/app.log
Issue: 403 Forbidden or Authentication failed
Solution:
# Verify service account has correct permissions
gcloud projects get-iam-policy YOUR_PROJECT_ID \
--flatten="bindings[].members" \
--filter="bindings.members:serviceAccount:gemini-calling-sa@YOUR_PROJECT_ID.iam.gserviceaccount.com"
# Should show roles/aiplatform.user roleIssue: Call connects but no AI voice
Solution:
- Check backend logs for WebSocket connection
- Verify Gemini API quotas in GCP Console
- Test audio conversion: Check
TWILIO_SAMPLE_RATEandGEMINI_INPUT_SAMPLE_RATEsettings - Ensure TwiML webhook is returning correct XML
Issue: CORS errors in browser console
Solution:
- Verify
REACT_APP_BACKEND_URLin frontend.env - Check backend is running on correct port
- Ensure CORS is configured in
backend/app.py
# Backend logs
tail -f backend/logs/app.log
# Frontend logs
# Check browser console (F12)gcloud logging read \
"resource.type=cloud_run_revision AND resource.labels.service_name=cymbal-insurance-backend" \
--limit=50 \
--project=YOUR_PROJECT_ID- Call Success Rate: Percentage of successful calls
- Average Call Duration: Mean duration of completed calls
- Audio Latency: Time between user speech and AI response
- API Error Rate: Rate of Gemini API errors
- WebSocket Connection Drops: Number of unexpected disconnections
- Never commit
.envfiles - They contain sensitive credentials - Use Secret Manager for production deployments
- Rotate credentials regularly (Twilio tokens, service account keys)
- Enable audit logging in Google Cloud Console
- Use HTTPS for all production endpoints
- Implement rate limiting to prevent abuse
- Validate phone numbers before initiating calls
- Sanitize user inputs to prevent injection attacks
Edit backend/services/gemini_service.py:
def _generate_system_instruction(self) -> str:
instruction = f"""You are Lucy, a professional customer service representative...
# Customize greeting, tone, and behavior here
"""
return instructionEdit backend/services/gemini_service.py:
voice_config=types.VoiceConfig(
prebuilt_voice_config=types.PrebuiltVoiceConfig(
voice_name="Aoede" # Options: Aoede, Charon, Fenrir, Kore, Puck
)
)Edit frontend/src/App.js and component files in frontend/src/components/
Contributions are welcome! Please follow these guidelines:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Google Gemini Live API - Real-time AI conversation capabilities
- Twilio - Voice and media streaming infrastructure
- FastAPI - Modern Python web framework
- React - Frontend user interface
For issues and questions:
- Check the Troubleshooting section
- Review TECHNICAL_ARCHITECTURE.md for architecture details
- Open an issue on GitHub
- Contact the development team
- Add support for multiple languages (Spanish, French, German)
- Implement call recording and storage
- Add analytics dashboard
- Support for inbound calls
- Integration with CRM systems
- Automated testing suite
- Multi-region deployment support
Built with β€οΈ for modern customer service experiences