Skip to content

🦜 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ΒΆ

python server.py

4. Open the InterfaceΒΆ

Navigate to: http://localhost:8765

πŸŽ™οΈ UsageΒΆ

  1. Connect: The interface auto-connects to the WebSocket server
  2. Configure: Click βš™οΈ to select voice and language
  3. Speak: Press and hold the microphone button to talk
  4. Listen: Release to hear the AI's voice response
  5. 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ΒΆ

cp -r parrot/voice /path/to/ai-parrot/parrot/

2. Add routes to your applicationΒΆ

from parrot.voice.handlers import setup_voice_routes

# In your app setup
setup_voice_routes(app)

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ΒΆ

export LOG_LEVEL=DEBUG
python server.py

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