A newer version of the Gradio SDK is available: 6.20.0
title: Kush Digital Twin
emoji: ποΈ
colorFrom: indigo
colorTo: blue
sdk: gradio
sdk_version: 6.18.0
app_file: app.py
pinned: false
Digital Twin MultiModal (Voice and Text)
A multimodal digital twin chatbot with text and voice input, powered by OpenAI (LLM + RAG + tool calling), Deepgram (speech-to-text and text-to-speech), ChromaDB (vector retrieval), and Gradio (web UI).
Converted from the digital-twin.ipynb notebook in the AI Engineering course.
Architecture
High-level flow:
- Text path β user types β embed query β ChromaDB retrieval β OpenAI chat (with tools) β reply in chat
- Voice path β user records β Deepgram STT β same RAG + chat pipeline β Deepgram TTS β autoplay reply audio
RAG pipeline:
- Knowledge lives in
knowledge/*.md(identity, career, technical stack) chunking.pysplits documents with overlap at sentence/paragraph boundaries- OpenAI
text-embedding-3-smallembeds each chunk - Vectors are stored in a local ChromaDB collection (
chroma_db_twin/) - Each user message retrieves the top-N similar chunks and injects them as Context in the system prompt
Tools available to the LLM:
send_notificationβ Pushover alert to your phone (optional)roll_diceβ simulated dice roll
Dynamic context: keywords in the user's message (2011, dishes, sports, vacation) inject extra persona context from knowledge.md.
Prerequisites
- Python 3.10+
- API keys:
Quick start
# Clone or cd into this directory
cd digital-twin-voice
# Create a virtual environment (recommended)
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
# Configure environment
cp .env.example .env
# Edit .env and add your API keys
# Build the vector index (also runs automatically on first app launch if empty)
python build_rag_index.py
# Run the app
python app.py
Gradio opens at http://127.0.0.1:7860 (port may vary). Use the text box or microphone to chat.
Deploy to Hugging Face Spaces
This repo is ready to deploy as a Gradio Space. The YAML header at the top of this README configures the Space (sdk: gradio, sdk_version: 6.18.0, app_file: app.py).
- Create a new Space on Hugging Face and choose Gradio as the SDK.
- Push this repository (or connect your GitHub repo).
- In Settings β Variables and secrets, add:
OPENAI_API_KEY(required β chat + RAG embeddings)DEEPGRAM_API_KEY(required for voice input/output)- Optional:
PUSHOVER_USER,PUSHOVER_TOKEN, model overrides from.env.example
- On first load, the app builds the ChromaDB index from
knowledge/*.md(uses OpenAI embeddings). Cold starts on free Spaces may take ~30s.
For faster restarts, enable Persistent Storage and set CHROMA_PATH=/data/chroma_db_twin, then run python build_rag_index.py once in the Space terminal.
Project layout
digital-twin-voice/
βββ app.py # Entry point
βββ build_rag_index.py # Rebuild ChromaDB from knowledge files
βββ chunking.py # Text chunking with overlap
βββ rag.py # Embeddings, ChromaDB, retrieval
βββ config.py # Environment variables and API clients
βββ knowledge/ # Source documents for RAG
β βββ identity.md
β βββ career.md
β βββ technical.md
βββ knowledge.md # Keyword topic triggers only
βββ prompts.py # System prompt + topic loading
βββ tools.py # Pushover + dice tools, tool-call handler
βββ chat.py # OpenAI chat loop with RAG + tool calling
βββ voice.py # Deepgram STT / TTS
βββ ui.py # Gradio interface
βββ requirements.txt
βββ .env.example
βββ docs/
βββ Flow-chart.png
βββ digital-twin-voice-flow.excalidraw
Customization
- Persona facts: edit files in
knowledge/, then runpython build_rag_index.py - Topic keywords: edit
knowledge.mdunder## Topics - Chunking: adjust
RAG_CHUNK_SIZEandRAG_CHUNK_OVERLAPin.env - Retrieval depth: set
RAG_N_RESULTSin.env(default: 3) - Tools: add functions in
tools.pyand register them inTOOLS - Models: set
OPENAI_MODEL,EMBEDDING_MODEL,DEEPGRAM_STT_MODEL, andDEEPGRAM_TTS_MODELin.env
Environment variables
| Variable | Required | Description |
|---|---|---|
OPENAI_API_KEY |
Yes | OpenAI API key |
DEEPGRAM_API_KEY |
Yes | Deepgram API key |
OPENAI_MODEL |
No | Chat model (default: gpt-4.1-mini) |
EMBEDDING_MODEL |
No | Embedding model (default: text-embedding-3-small) |
RAG_N_RESULTS |
No | Chunks retrieved per query (default: 3) |
RAG_CHUNK_SIZE |
No | Chunk size in characters (default: 500) |
RAG_CHUNK_OVERLAP |
No | Overlap between chunks (default: 50) |
RAG_DEBUG |
No | Print retrieved chunk sources to console |
CHROMA_PATH |
No | ChromaDB directory (default: chroma_db_twin) |
DEEPGRAM_STT_MODEL |
No | Speech-to-text model (default: nova-3) |
DEEPGRAM_TTS_MODEL |
No | Text-to-speech model (default: aura-2-thalia-en) |
PUSHOVER_USER |
No | Pushover user key (for notification tool) |
PUSHOVER_TOKEN |
No | Pushover app token (for notification tool) |
License
Educational project from the AI Engineering course.
