Real-time conversational AI avatar engine for pharmaceutical and life sciences sales training.
The demo covers: persona switch mid-session, on-screen latency HUD, barge-in interruption (audio cuts within ~150ms), phoneme-timed lip sync (mouth shapes follow audio content), mobile Safari run, and a second tab embedding the <AvatarEngine /> SDK component.
Avatar Engine lets a sales representative speak to a simulated healthcare professional (HCP) in the browser. The app transcribes the rep's voice, streams a persona-driven LLM response, synthesizes speech, and drives a real-time WebGL avatar with phoneme-timed lip sync, head/eye motion, persona-specific expressions, and barge-in interruption - all within a 300ms latency budget.
Personas:
| Persona | Specialty | Expression Profile |
|---|---|---|
| Dr. Lisa Chen | Interventional Cardiologist | Skeptical, brow-down, data-demanding |
| Dr. James Morrison | General Practitioner | Tired, practical, distracted |
| Dr. Raj Patel | Medical Oncologist | Curious, warm, mechanistically rigorous |
| Dr. Sarah Williams | Orthopedic Surgeon | Confident, outcome-focused, decisive |
| Capability | Status | Implementation |
|---|---|---|
| Real-time lip sync (<300ms) | Phoneme-timed visemes | Cartesia word timestamps → grapheme heuristic → Oculus visemes → ARKit 52 blendshapes (jawOpen, mouthFunnel, mouthPucker, ...) on Streamoji GLB. RMS amplitude layered on top for sub-frame jaw motion. |
| Voice input | Three STT paths | Deepgram Nova-3 streaming (~100ms, default), Web Speech API (browser-native, Chrome), Groq Whisper Large v3 (iOS Safari fallback). |
| LLM integration | Provider-switchable | Groq Llama 3.3 70B (default, ~110ms first token), Anthropic Claude claude-sonnet-4-20250514 (direct SDK), OpenRouter (any model). Streaming on all paths via LLM_PROVIDER env var. |
| Expressive animation | Status-aware expressions | Idle breathing, blink, saccadic gaze, per-persona resting expression (skeptical/engaged/positive/thinking). All driven by useFrame bone + morph target updates. |
| Barge-in | <150ms p50 | Dedicated mic VAD during TTS playback. User speech aborts current audio + clears sentence queue. |
| Web delivery | React + Three.js | Modern browser, no install required. |
| Persona config | JSON | personas/ folder. Each file defines name, role, mood, system prompt, and avatar URL. |
| Session state | Multi-turn | Full conversation history maintained client-side, sent to LLM each turn. 15+ turns tested. |
| Mobile | iOS + Android | Responsive UI. Deepgram + Groq Whisper fallback for Safari. Audio primed on first gesture. |
| Docker | Compose | docker/docker-compose.yml - backend + frontend in one command. |
| Embeddable SDK | ESM + CJS bundles | npm run build:lib produces dist-lib/. Drop-in <AvatarEngine /> component + useAvatarSession hook. |
Browser mic
│ Web Speech API / Deepgram WS
▼
FastAPI backend (/v1/sessions/{id}/stream - bidirectional WS)
├─► Groq Llama 3.3 70B streaming LLM tokens
├─► Cartesia Sonic-2 streaming TTS + word timestamps → viseme schedule
└─► Groq Whisper Large v3 STT fallback (iOS Safari)
▼
Browser (React + React Three Fiber)
├─ Web Audio API gapless audio scheduling, amplitude RMS
├─ Three.js useFrame morph target + bone drives each frame
└─ Streamoji GLB avatar ARKit 52 blendshapes, Mixamo rig
No vendor key reaches the client - all API calls go through the FastAPI backend.
Full pipeline detail: ARCHITECTURE.md | docs/TECHNICAL_APPROACH_FULL.md
- Node.js 22+, Python 3.12+
- Cartesia API key (streaming TTS)
- Groq API key (LLM + Whisper fallback)
- Deepgram API key (streaming STT)
- Optional: Anthropic key (
LLM_PROVIDER=anthropic), OpenRouter key (LLM_PROVIDER=openrouter)
cp .env.example backend/.env
# Fill in GROQ_API_KEY, CARTESIA_API_KEY, DEEPGRAM_API_KEY at minimumKey variables:
LLM_PROVIDER=groq # groq | anthropic | openrouter
GROQ_API_KEY=...
CARTESIA_API_KEY=...
CARTESIA_VOICE_ID=db6b0ed5-d5d3-463d-ae85-518a07d3c2b4
DEEPGRAM_API_KEY=...
# ANTHROPIC_API_KEY=... # optionalSee .env.example for the full list including Kokoro TTS fallback and CORS settings.
cd backend
python -m venv .venv && source .venv/bin/activate
pip install -r requirements.txt
uvicorn main:app --reload --port 8000cd frontend
npm install
npm run devOpen http://localhost:3000.
curl http://localhost:8000/v1/health
curl http://localhost:8000/v1/avatars/manifestBoth should return status: "ready" for all four personas.
cd docker
docker compose up --build
# Frontend: http://localhost:3000
# Backend: http://localhost:8000The app supports two rendering paths behind an error boundary + Suspense fallback - the demo never breaks on a missing GLB.
Path 1 - Streamoji GLBs (preferred):
Drop dr_chen.glb, dr_morrison.glb, dr_patel.glb, dr_williams.glb into frontend/public/avatars/.
Download pre-built GLBs (Google Drive):
https://drive.google.com/drive/u/0/folders/1J7OizxlbH1bwUHmAoGDT691xZH1dWWnU
Or regenerate with your Streamoji client credentials:
node scripts/generate_streamoji_avatars.mjsRequired export settings: ARKit Blendshapes + Oculus Visemes. See frontend/public/avatars/README.md for per-persona appearance specs and morph target checklist.
Path 2 - Procedural Three.js fallback:
Automatically used when a GLB is missing or fails to load. Mouth, blink, and expression all work via bone/scale animation. Reliable for demo recording when external model hosts are unavailable.
GET /v1/health provider + STT + avatar manifest readiness
GET /v1/personas list all configured HCP personas
GET /v1/personas/{persona_id} single persona config
GET /v1/avatars/manifest GLB readiness + required morph target check
POST /v1/sessions create session (returns session_id + websocket_url)
WS /v1/sessions/{session_id}/stream bidirectional realtime: transcript, LLM deltas,
audio chunks, viseme batches, avatar state, metrics
GET /v1/sessions/{session_id}/metrics p50/p95 latency summary
Full WebSocket event contract: docs/API_EVENTS.md
cd frontend && npm run build:lib
# Outputs dist-lib/index.js (ESM) + dist-lib/index.cjsIn any React 18 app with three, @react-three/fiber, @react-three/drei as peers:
import { AvatarEngine } from "avatar-engine";
const drChen = {
id: "dr_chen",
name: "Dr. Lisa Chen",
systemPrompt: "You are Dr. Lisa Chen, a skeptical interventional cardiologist...",
avatarUrl: "/avatars/dr_chen.glb",
};
export default function Page() {
return (
<div style={{ width: "100%", height: 600 }}>
<AvatarEngine
persona={drChen}
apiBase="https://your-backend.example.com"
transport="websocket"
enableBargeIn
debugHud
onTurn={(turn) => console.log(turn)}
onMetric={(m) => console.log(m.firstMouthMovement)}
/>
</div>
);
}Headless hook also exported:
import { useAvatarSession } from "avatar-engine";
const { sendMessage, avatarStatus, history, latency } = useAvatarSession({ persona, apiBase });See frontend/examples/host-app-example.jsx for a minimal host app integration.
| Doc | Purpose |
|---|---|
| TECHNICAL_APPROACH.md | Condensed technical write-up |
| ARCHITECTURE.md | Pipeline diagram, latency budget, per-turn sequence |
| docs/TECHNICAL_APPROACH_FULL.md | Full pipeline with code-level detail |
| docs/PERFORMANCE.md | Latency methodology + measured p50/p95 |
| docs/INTEGRATION_GUIDE.md | How a host app embeds the engine |
| docs/API_EVENTS.md | REST + WebSocket event contract |
| docs/THIRD_PARTY_LICENSES.md | Every external API, model, and library + license |
| Service | Role | License |
|---|---|---|
| Cartesia Sonic-2 | Streaming TTS, ~200ms first byte, word timestamps | Commercial API |
| Groq (Llama 3.3 70B + Whisper Large v3) | Default LLM + iOS STT fallback | Commercial API |
| Deepgram Nova-3 | Streaming STT, ~100ms endpointing | Commercial API |
| Anthropic Claude | Optional LLM (claude-sonnet-4-20250514) | Commercial API |
| Streamoji Avatars | ARKit 52-blendshape GLB avatars | Commercial API |
| Kokoro FastAPI | Self-hosted TTS fallback | Apache 2.0 |
| React, Three.js, React Three Fiber, Drei | Frontend rendering stack | MIT |
Full detail including bundled library licenses: docs/THIRD_PARTY_LICENSES.md
- Viseme timing uses Cartesia word timestamps + a digraph-aware grapheme heuristic. Sub-word phoneme boundaries can drift 30-80ms from ground truth. A future iteration could use phoneme-level Cartesia events.
- Cartesia is the primary TTS. Kokoro is the self-hosted fallback if
CARTESIA_API_KEYis absent or the request fails. - Web Speech API is unavailable on iOS Safari. The backend
/transcribefalls back to Groq Whisper Large v3 (~200ms extra latency). - Barge-in relies on browser echo cancellation. Use a headset to prevent avatar playback from triggering self-interruption.
- Production deployments should set
ALLOWED_ORIGINSto an explicit allowlist instead of*. - Expression blendshapes require the loaded GLB to expose ARKit 52 shapes. Missing shapes are logged once to console with the absent shape names.
MIT - Copyright (c) 2026 Rahul Joshi
