π¦ AI-Parrot Voice ChatΒΆ
Real-time voice chat interface using Gemini Live API for native speech-to-speech interactions.
π― FeaturesΒΆ
- Bidirectional Voice Streaming: WebSocket-based real-time audio streaming
- Native Speech-to-Speech: Uses Gemini 2.5 Flash Native Audio for low-latency voice interactions
- Multimodal Responses: Receive both text and audio responses simultaneously
- Voice Activity Detection: Automatic speech detection and interruption handling
- Modern UI: Elegant chat interface with audio visualization
π ArchitectureΒΆ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β FRONTEND (HTML) β
β βββββββββββββββ ββββββββββββββββ ββββββββββββββββββββββ β
β β Microphone βββββΆβ AudioContext βββββΆβ WebSocket Client β β
β β (16kHz PCM) β β + Processor β β (Binary + JSON) β β
β βββββββββββββββ ββββββββββββββββ ββββββββββββββββββββββ β
β β β
β βββββββββββββββ ββββββββββββββββ β β
β β Chat Panel ββββββ Audio Player ββββββββββββββββ β
β β (Text) β β (24kHz PCM) β β
β βββββββββββββββ ββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β WebSocket
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β BACKEND (Python/aiohttp) β
β ββββββββββββββββββββββ β
β β VoiceChatServer β β
β β - WebSocket β ββββββββββββββββββββββββββββββββ β
β β - Session Mgmt ββββββΆβ Gemini Live API β β
β β - Audio Buffer β β (gemini-2.5-flash-native) β β
β ββββββββββββββββββββββ β - Audio In β Audio Out β β
β β - Text Transcription β β
β β - Tool Calling β β
β ββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
π Quick StartΒΆ
1. Install DependenciesΒΆ
# Create virtual environment (recommended)
python -m venv venv
source venv/bin/activate # Linux/Mac
# or: venv\Scripts\activate # Windows
# Install requirements
pip install -r requirements.txt
2. Set API KeyΒΆ
# Linux/Mac
export GOOGLE_API_KEY=your_api_key_here
# Windows (PowerShell)
$env:GOOGLE_API_KEY="your_api_key_here"
Get your API key from: https://aistudio.google.com/apikey
3. Run the ServerΒΆ
4. Open the InterfaceΒΆ
Navigate to: http://localhost:8765
ποΈ UsageΒΆ
- Connect: The interface auto-connects to the WebSocket server
- Configure: Click βοΈ to select voice and language
- Speak: Press and hold the microphone button to talk
- Listen: Release to hear the AI's voice response
- Read: See the transcribed text in the chat
π Project StructureΒΆ
voice_poc/
βββ server.py # Main server (WebSocket + Static files)
βββ requirements.txt # Python dependencies
βββ README.md # This file
βββ frontend/
β βββ voice_chat.html # Web interface (HTML + JS + CSS)
βββ parrot/
βββ voice/
βββ __init__.py # Module exports
βββ models.py # Data models (VoiceChunk, VoiceResponse, etc.)
βββ session.py # VoiceSession manager
βββ bot.py # VoiceBot implementation
βββ handlers.py # WebSocket handlers
π§ Configuration OptionsΒΆ
Voice Selection (Gemini)ΒΆ
| Voice | Description |
|---|---|
Puck |
Friendly, approachable (default) |
Aoede |
Warm, expressive |
Charon |
Deep, authoritative |
Fenrir |
Strong, confident |
Kore |
Gentle, soft |
Audio FormatsΒΆ
- Input: PCM 16-bit, 16kHz, mono
- Output: PCM 16-bit, 24kHz, mono
WebSocket ProtocolΒΆ
// Client β Server Messages
{ "type": "start_session", "config": { "voice_name": "Puck", "language": "en-US" } }
{ "type": "audio_chunk", "data": "<base64_pcm>" }
{ "type": "stop_recording" }
{ "type": "end_session" }
// Server β Client Messages
{ "type": "session_started", "session_id": "..." }
{ "type": "response_chunk", "text": "...", "audio_base64": "..." }
{ "type": "response_complete", "text": "...", "audio_base64": "..." }
{ "type": "error", "message": "..." }
π Integration with AI-ParrotΒΆ
To integrate with your existing AI-Parrot project:
1. Copy the voice moduleΒΆ
2. Add routes to your applicationΒΆ
3. Create a voice-enabled botΒΆ
from parrot.voice import VoiceBot, VoiceConfig
bot = VoiceBot(
name="My Voice Assistant",
system_prompt="You are a helpful assistant...",
voice_config=VoiceConfig(
voice_name="Puck",
language="es-ES" # Spanish
),
tools=[my_tool] # Your existing tools work!
)
# Use in your handler
async for response in bot.ask_voice_stream(audio_bytes):
await send_to_client(response)
π οΈ DevelopmentΒΆ
Mock ModeΒΆ
If GOOGLE_API_KEY is not set, the server runs in mock mode for UI testing.
Debug LoggingΒΆ
Browser ConsoleΒΆ
Open DevTools to see WebSocket messages and audio processing logs.
π RequirementsΒΆ
- Python 3.10+
- Modern browser with Web Audio API support
- Google Cloud API key with Gemini access
- Microphone access
π Security NotesΒΆ
- API key is stored server-side only
- Audio is processed in real-time, not stored
- Use HTTPS in production
- Implement authentication for production use
π TroubleshootingΒΆ
"Microphone access denied"ΒΆ
- Check browser permissions
- Use HTTPS or localhost
"Connection error"ΒΆ
- Verify server is running
- Check WebSocket URL in settings
- Look for CORS errors in console
"No audio response"ΒΆ
- Verify GOOGLE_API_KEY is set
- Check server logs for API errors
- Ensure google-genai is installed
High latencyΒΆ
- Use wired internet connection
- Check server location vs API region
- Reduce audio chunk size
π ResourcesΒΆ
π LicenseΒΆ
MIT License - Part of AI-Parrot project