# maihuuthien/totem architecture

> Architecture diagram of the GitHub repository maihuuthien/totem, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/maihuuthien/totem
- Repository: https://github.com/maihuuthien/totem

## Overview

1. **Overall shape**
   - This repo is a single-user “proxy chatbot” application with one main runtime entrypoint, `app.py`.
   - The app serves a Gradio UI, answers questions about the owner, and can optionally turn the last answer into a talking-avatar playback.

2. **Interactive front end**
   - The user-facing boundary is the Gradio app.
   - It exposes a chat experience plus secondary actions like “play the last answer,” so the UI is not just text chat; it also triggers media generation/playback.

3. **Conversation / answer generation layer**
   - The core chat logic lives in `app.py` and is driven by a system prompt built from personal source material.
   - It can use either OpenAI-hosted models or a local Ollama model, which creates a clear split between cloud inference and local inference modes.

4. **Personal knowledge ingestion**
   - The app reads the `me/` folder as its knowledge base:
     - `me/linkedin.pdf` for structured career history
     - `me/summary.txt` for a short personal narrative
   - These files feed the prompt/context construction used to answer questions as if the chatbot were the owner.

5. **Unknown-question and lead-capture workflow**
   - The README describes two side-channel actions beyond answering:
     - collecting visitor contact information
     - logging questions it could not answer
   - These are treated as application-level events, separate from the model response path, and can be used for follow-up or future prompt improvements.

6. **Notification boundary**
   - Optional Pushover integration sends real-time notifications for new contacts and unknown questions.
   - This is an outbound integration layer, not part of the core conversational path, and is only active when configured via environment variables.

7. **Talking-avatar media pipeline**
   - The avatar/speaking feature is backed by the vendored `LatentSync/` package.
   - That package contains the lip-sync inference stack: model definitions, pipeline orchestration, utilities, and config files for syncnet/unet stages.
   - This subsystem is effectively isolated as a media-generation engine used after the text answer is produced.

8. **Audio feature extraction**
   - Inside `LatentSync/latentsync/whisper/` is a bundled Whisper-based transcription/audio-feature implementation.
   - `audio2feature.py` and the embedded Whisper code turn audio into features that can be consumed by the lip-sync pipeline.
   - This indicates a pre-processing step between audio/text generation and synchronized video output.

9. **Model/config separation**
   - The `LatentSync/configs/` directory holds the architecture and stage configs for the sync pipeline:
     - `syncnet` variants for latent/pixel modes
     - `unet` first/second stage configs
     - audio and scheduler settings
   - This implies the media subsystem is heavily configuration-driven, with inference behavior decoupled from application code.

10. **Inference entrypoint for lip-sync**
    - `LatentSync/scripts/inference.py` is the dedicated inference driver for the avatar pipeline.
    - The main app likely invokes this subsystem when the user requests playback of the last answer, using `me/ref_video.mp4` as the source face video and a synthesized voice track as input.

11. **Reference assets and personalization**
    - The `me/` directory also contains `ref_audio.mp3`, `ref_video.mp4`, and `summary.txt`, which are user-specific assets.
    - These files are part of the application data, not generic library code, and are required to make the chatbot visually and vocally personalized.

12. **Deployment/runtime boundary**
    - The repo is designed to run in two environments:
      - local development with Python 3.10 and optionally Ollama
      - HuggingFace Spaces deployment with OpenAI-backed inference
    - `Dockerfile`, `requirements.txt`, and `pyproject.toml` support containerized and reproducible execution, while the Gradio SDK metadata in the README identifies the app as a Space-style deployment.

13. **Important architectural split to show in a diagram**
    - One branch is the conversational stack: Gradio UI → prompt/context assembly → LLM backend → response.
    - The second branch is the media stack: response text → TTS/audio assets → Whisper/audio feature extraction → LatentSync lip-sync inference → avatar playback.
    - Side integrations branch off from the app logic to Pushover and contact logging.

14. **What is intentionally vendored**
    - `LatentSync/` includes substantial third-party-style code and assets rather than depending on it as a remote service.
    - That means the repository is self-contained for the avatar pipeline, while the LLM remains external or locally hosted depending on configuration.

Main entry points: [`app.py`](https://github.com/maihuuthien/totem/blob/main/app.py) (App), [`LatentSync/latentsync/pipelines/lipsync_pipeline.py`](https://github.com/maihuuthien/totem/blob/main/LatentSync/latentsync/pipelines/lipsync_pipeline.py) (Pipeline Core), [`LatentSync/scripts/inference.py`](https://github.com/maihuuthien/totem/blob/main/LatentSync/scripts/inference.py) (Lip-sync Inference), [`me/ref_audio.mp3`](https://github.com/maihuuthien/totem/blob/main/me/ref_audio.mp3) (Voice Track)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_app["Application"]
  node_app_py["App<br/>runtime<br/>[app.py]"]
  node_gradio_ui["Chat UI"]
  node_prompt_builder["Prompt Builder<br/>context assembly"]
  node_llm_router{{"LLM Router<br/>model selector"}}
  node_openai_backend(("OpenAI<br/>cloud LLM"))
  node_ollama_backend(("Ollama<br/>local LLM"))
  node_answer_flow["Answer Flow<br/>chat response"]
  node_lead_capture["Contact Capture<br/>event log"]
  node_unknown_log["Unknown Questions<br/>event log"]
  node_pushover(("Pushover<br/>notification"))
  node_playback_gate{{"Playback Trigger<br/>media control"}}
end

subgraph group_knowledge["Personal Data"]
  node_personal_kb["Personal KB<br/>source data"]
  node_linkedin["Career PDF<br/>bio source<br/>[linkedin.pdf]"]
  node_summary_txt["Summary<br/>bio source<br/>[summary.txt]"]
end

subgraph group_media["Avatar Media"]
  node_answer_audio["Voice Track<br/>audio generation<br/>[ref_audio.mp3]"]
  node_ref_video["Ref Video<br/>avatar asset<br/>[ref_video.mp4]"]
  node_whisper_features["Audio Features<br/>audio preprocess<br/>[audio2feature.py]"]
end

subgraph group_latentsync["LatentSync"]
  node_lipsync_infer["Lip-sync Inference<br/>pipeline<br/>[inference.py]"]
  node_lipsync_pipeline["Pipeline Core"]
  node_syncnet_unet{{"Model Stages<br/>diffusion/lipsync models<br/>[unet.py]"}}
  node_latent_config["Configs<br/>pipeline config"]
end

node_gradio_ui -->|"chat / actions"| node_app_py
node_app_py -->|"build context"| node_prompt_builder
node_prompt_builder -->|"reads"| node_personal_kb
node_personal_kb -->|"includes"| node_linkedin
node_personal_kb -->|"includes"| node_summary_txt
node_prompt_builder -->|"send prompt"| node_llm_router
node_llm_router -->|"cloud mode"| node_openai_backend
node_llm_router -->|"local mode"| node_ollama_backend
node_openai_backend -->|"return text"| node_answer_flow
node_ollama_backend -->|"return text"| node_answer_flow
node_app_py -->|"capture contact"| node_lead_capture
node_app_py -->|"log miss"| node_unknown_log
node_lead_capture -.->|"notify"| node_pushover
node_unknown_log -.->|"notify"| node_pushover
node_answer_flow -->|"play last answer"| node_playback_gate
node_playback_gate -->|"use voice"| node_answer_audio
node_playback_gate -->|"use face video"| node_ref_video
node_answer_audio -->|"extract features"| node_whisper_features
node_whisper_features -->|"condition"| node_lipsync_infer
node_ref_video -->|"reference input"| node_lipsync_infer
node_lipsync_infer -->|"orchestrate"| node_lipsync_pipeline
node_lipsync_pipeline -->|"use models"| node_syncnet_unet
node_lipsync_pipeline -->|"load config"| node_latent_config

click node_app_py "https://github.com/maihuuthien/totem/blob/main/app.py"
click node_answer_audio "https://github.com/maihuuthien/totem/blob/main/me/ref_audio.mp3"
click node_ref_video "https://github.com/maihuuthien/totem/blob/main/me/ref_video.mp4"
click node_whisper_features "https://github.com/maihuuthien/totem/blob/main/LatentSync/latentsync/whisper/audio2feature.py"
click node_lipsync_infer "https://github.com/maihuuthien/totem/blob/main/LatentSync/scripts/inference.py"
click node_lipsync_pipeline "https://github.com/maihuuthien/totem/blob/main/LatentSync/latentsync/pipelines/lipsync_pipeline.py"
click node_syncnet_unet "https://github.com/maihuuthien/totem/blob/main/LatentSync/latentsync/models/unet.py"
click node_latent_config "https://github.com/maihuuthien/totem/tree/main/LatentSync/configs"
click node_linkedin "https://github.com/maihuuthien/totem/blob/main/me/linkedin.pdf"
click node_summary_txt "https://github.com/maihuuthien/totem/blob/main/me/summary.txt"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_app_py,node_gradio_ui,node_prompt_builder,node_llm_router,node_openai_backend,node_ollama_backend,node_answer_flow,node_lead_capture,node_unknown_log,node_pushover,node_playback_gate toneBlue
class node_personal_kb,node_linkedin,node_summary_txt toneAmber
class node_answer_audio,node_ref_video,node_whisper_features toneMint
class node_lipsync_infer,node_lipsync_pipeline,node_syncnet_unet,node_latent_config toneRose
```

## Components

### Application

Main chatbot runtime and outbound integrations.

- [App](https://github.com/maihuuthien/totem/blob/main/app.py): runtime · Main entrypoint serving the chatbot and orchestration logic. · `app.py`
- **Chat UI**: ui · User-facing chat interface and playback controls.
- **Prompt Builder**: context assembly · Builds the system prompt and conversation context from personal source material.
- **LLM Router**: model selector · Chooses between hosted OpenAI models and local Ollama inference.
- **OpenAI**: cloud LLM · External hosted inference path.
- **Ollama**: local LLM · Local model inference path.
- **Answer Flow**: chat response · Produces the owner-style answer returned to the user.
- **Contact Capture**: event log · Collects visitor contact information for follow-up.
- **Unknown Questions**: event log · Records questions the bot could not answer.
- **Pushover**: notification · Optional realtime notifications for new contacts and unknown questions.
- **Playback Trigger**: media control · Starts the last-answer speaking/avatar playback path.

### Personal Data

User-specific source material used to personalize answers.

- **Personal KB**: source data · Owner-specific knowledge base used to answer as the user.
- [Career PDF](https://github.com/maihuuthien/totem/blob/main/me/linkedin.pdf): bio source · Structured career history source. · `me/linkedin.pdf`
- [Summary](https://github.com/maihuuthien/totem/blob/main/me/summary.txt): bio source · Short personal narrative used for grounding. · `me/summary.txt`

### Avatar Media

Audio/video generation path for speaking playback.

- [Voice Track](https://github.com/maihuuthien/totem/blob/main/me/ref_audio.mp3): audio generation · Audio used as the spoken version of the last answer. · `me/ref_audio.mp3`
- [Ref Video](https://github.com/maihuuthien/totem/blob/main/me/ref_video.mp4): avatar asset · Reference face video used for personalized playback. · `me/ref_video.mp4`
- [Audio Features](https://github.com/maihuuthien/totem/blob/main/LatentSync/latentsync/whisper/audio2feature.py): audio preprocess · Extracts audio features for lip-sync conditioning. · `LatentSync/latentsync/whisper/audio2feature.py`

### LatentSync

Vendored lip-sync inference stack.

- [Lip-sync Inference](https://github.com/maihuuthien/totem/blob/main/LatentSync/scripts/inference.py): pipeline · Runs the talking-avatar inference pipeline. · `LatentSync/scripts/inference.py`
- [Pipeline Core](https://github.com/maihuuthien/totem/blob/main/LatentSync/latentsync/pipelines/lipsync_pipeline.py): pipeline core · Orchestrates avatar synthesis and frame generation. · `LatentSync/latentsync/pipelines/lipsync_pipeline.py`
- [Model Stages](https://github.com/maihuuthien/totem/blob/main/LatentSync/latentsync/models/unet.py): diffusion/lipsync models · SyncNet and U-Net stages plus supporting model blocks. · `LatentSync/latentsync/models/unet.py`
- [Configs](https://github.com/maihuuthien/totem/tree/main/LatentSync/configs): pipeline config · Architecture and scheduler configuration for the vendored media stack. · `LatentSync/configs`

## Connections

- Chat UI → App: chat / actions · UI sends user messages and control actions to the runtime.
- App → Prompt Builder: build context · Creates the system prompt and conversation context.
- Prompt Builder → Personal KB: reads · Uses personal source material for grounding.
- Personal KB → Career PDF: includes · Structured career history.
- Personal KB → Summary: includes · Short personal narrative.
- Prompt Builder → LLM Router: send prompt · Hands the assembled prompt to the model selector.
- LLM Router → OpenAI: cloud mode · Routes to hosted inference when configured.
- LLM Router → Ollama: local mode · Routes to local model inference when configured.
- OpenAI → Answer Flow: return text · Produces the chat answer.
- Ollama → Answer Flow: return text · Produces the chat answer.
- App → Contact Capture: capture contact · Records visitor contact details as an application event.
- App → Unknown Questions: log miss · Records unanswered questions as an application event.
- Contact Capture → Pushover: notify · Optional notification for new contacts.
- Unknown Questions → Pushover: notify · Optional notification for unknown questions.
- Answer Flow → Playback Trigger: play last answer · Triggers the speaking-avatar branch after response generation.
- Playback Trigger → Voice Track: use voice · Uses the synthesized or prepared voice track.
- Playback Trigger → Ref Video: use face video · Uses the reference avatar video asset.
- Voice Track → Audio Features: extract features · Converts audio into conditioning features.
- Audio Features → Lip-sync Inference: condition · Feeds lip-sync inference.
- Ref Video → Lip-sync Inference: reference input · Provides the source face video.
- Lip-sync Inference → Pipeline Core: orchestrate · Runs the core synthesis pipeline.
- Pipeline Core → Model Stages: use models · Applies the vendored model stack.
- Pipeline Core → Configs: load config · Uses stage and scheduler configuration.
