# havish-coder/talking_heads architecture

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

- Interactive diagram: https://gitdiagram.com/havish-coder/talking_heads
- Repository: https://github.com/havish-coder/talking_heads

## Overview

1. This repository is a **single-purpose video generation pipeline** for audio-driven half-body human animation. It is not a service or app; the core architecture is split into **preprocessing**, **model definition**, **training**, and **inference**.

2. The top-level boundary is between **raw media inputs** and **training-ready artifacts**. Raw videos, audio, reference images, and pose sequences are transformed into intermediate files that the training code consumes from disk, rather than recomputing expensive encoders on every step.

3. The **Preprocessing/** folder is the ingestion and feature-extraction layer. Its scripts suggest separate passes for:
   - video encoding / latent preparation,
   - audio embedding extraction,
   - reference image encoding,
   - DWPose keypoint preprocessing,
   - renaming / dataset organization.
   This is the stage that turns heterogeneous media into consistent clip-aligned assets.

4. The main data products of preprocessing are likely the same ones described in the README: **VAE latents**, **audio embeddings**, and **pose arrays**. These become the stable interface between preprocessing and training, which is important for GPU memory control and reproducibility.

5. The **Src/Models/** folder contains the model-side architecture. `talking_heads_dit.py` is the central generator wrapper, and `audio_encoder.py` likely handles projection or adaptation of audio features into the DiT hidden space. This is where the project adapts CogVideoX-style components into a talking-head-specific diffusion model.

6. The generator architecture is a **DiT-based video diffusion model** rather than a UNet pipeline. The README indicates the reference image is injected by **token prepending** instead of a dedicated ReferenceNet, and audio is passed through an embedding projection into the transformer conditioning path. That means the model boundary is a unified transformer over video tokens plus conditioning tokens.

7. The **Src/training/** folder is the learning subsystem. `dataset.py` is the loader for aligned video/audio/pose samples and precomputed features, `loss.py` defines the diffusion objective, and `train.py` is the training entrypoint. `config.yaml` is the main control plane for batch size, resolution, frame count, checkpoints, and optimization settings.

8. Training is designed around **frozen heavy backbones** and **lightweight trainable adapters**. The README makes clear that the VAE, Wav2Vec2, and main CogVideoX backbone are kept fixed, while smaller projection/adapter layers are trained. Architecturally, that means the optimization boundary is narrow and memory-efficient.

9. The **Src/inference.py** module is the runtime generation path. It should load pretrained weights plus a fine-tuned checkpoint, accept a reference image, audio, and pose conditioning, run the denoising process, and decode the generated latent sequence back into a video file.

10. The repo has a clear **offline preprocessing → training → inference** flow. Training depends on precomputed artifacts on disk, while inference depends on a trained checkpoint plus the same conditioning inputs. That separation is one of the most important diagram boundaries.

11. External dependencies materially shape the architecture: **CogVideoX pretrained weights** provide the backbone, **Wav2Vec2** provides audio embeddings, **DWPose** provides hand/body pose inputs, and **ffmpeg** is used for audio extraction from video. These are not incidental tools; they define the modality pipeline.

12. The repository also reflects a **single-GPU / low-VRAM constraint**. The architecture intentionally avoids a second full reference network and avoids recomputing encoders during training. For an architecture diagram, that constraint explains why preprocessing artifacts exist and why the model uses lighter identity-conditioning strategies.

Main entry points: [`Src/Models/talking_heads_dit.py`](https://github.com/havish-coder/talking_heads/blob/main/Src/Models/talking_heads_dit.py) (Talking DiT), [`Src/training/dataset.py`](https://github.com/havish-coder/talking_heads/blob/main/Src/training/dataset.py) (Dataset), [`Src/training/train.py`](https://github.com/havish-coder/talking_heads/blob/main/Src/training/train.py) (Train loop), [`Preprocessing/encode_vid.py`](https://github.com/havish-coder/talking_heads/blob/main/Preprocessing/encode_vid.py) (Video encode)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_g_pre["Preprocessing"]
  node_n_vid["Video encode<br/>preprocess<br/>[encode_vid.py]"]
  node_n_audio_pre["Audio embed<br/>preprocess<br/>[Audioencoder.py]"]
  node_n_ref["Ref image<br/>preprocess"]
  node_n_pose["Pose prep<br/>preprocess"]
  node_n_rename["Dataset org<br/>preprocess<br/>[rename.py]"]
end

subgraph group_g_model["Models"]
  node_n_audio_model["Audio encoder<br/>conditioning<br/>[audio_encoder.py]"]
  node_n_dit["Talking DiT<br/>generator"]
end

subgraph group_g_train["Training"]
  node_n_data["Dataset<br/>loader<br/>[dataset.py]"]
  node_n_loss["Loss<br/>objective<br/>[loss.py]"]
  node_n_cfg["Config<br/>control<br/>[config.yaml]"]
  node_n_train["Train loop<br/>entrypoint<br/>[train.py]"]
end

subgraph group_g_runtime["Inference"]
  node_n_infer["Inference<br/>runtime<br/>[inference.py]"]
end

subgraph group_g_ext["External"]
  node_n_vae[("VAE latents<br/>artifact")]
  node_n_w2v["Wav2Vec2<br/>frozen backbone"]
  node_n_cog["CogVideoX<br/>frozen backbone"]
  node_n_dwpose["DWPose<br/>pose tool"]
  node_n_ffmpeg["ffmpeg<br/>media tool"]
end

node_n_vid -->|"writes"| node_n_vae
node_n_audio_pre -->|"uses"| node_n_w2v
node_n_ref -->|"conditions"| node_n_dit
node_n_pose -->|"uses"| node_n_dwpose
node_n_rename -->|"organizes"| node_n_data
node_n_vid -->|"feeds"| node_n_data
node_n_audio_pre -->|"feeds"| node_n_data
node_n_pose -->|"feeds"| node_n_data
node_n_cfg -->|"configures"| node_n_train
node_n_data -->|"batches"| node_n_train
node_n_dit -->|"optimized by"| node_n_train
node_n_loss -->|"supervises"| node_n_train
node_n_cog -->|"backbone"| node_n_dit
node_n_audio_model -->|"conditions"| node_n_dit
node_n_w2v -->|"features"| node_n_audio_model
node_n_vae -->|"decodes"| node_n_infer
node_n_dit -->|"runs"| node_n_infer
node_n_ffmpeg -->|"extracts"| node_n_audio_pre
node_n_ffmpeg -->|"converts"| node_n_vid
node_n_cog -->|"loads"| node_n_infer

click node_n_vid "https://github.com/havish-coder/talking_heads/blob/main/Preprocessing/encode_vid.py"
click node_n_audio_pre "https://github.com/havish-coder/talking_heads/blob/main/Preprocessing/Audioencoder.py"
click node_n_ref "https://github.com/havish-coder/talking_heads/blob/main/Preprocessing/Enocde_refernce_image.py"
click node_n_pose "https://github.com/havish-coder/talking_heads/blob/main/Preprocessing/preprocessing_dwpose.py"
click node_n_rename "https://github.com/havish-coder/talking_heads/blob/main/Preprocessing/rename.py"
click node_n_audio_model "https://github.com/havish-coder/talking_heads/blob/main/Src/Models/audio_encoder.py"
click node_n_dit "https://github.com/havish-coder/talking_heads/blob/main/Src/Models/talking_heads_dit.py"
click node_n_data "https://github.com/havish-coder/talking_heads/blob/main/Src/training/dataset.py"
click node_n_loss "https://github.com/havish-coder/talking_heads/blob/main/Src/training/loss.py"
click node_n_cfg "https://github.com/havish-coder/talking_heads/blob/main/Src/training/config.yaml"
click node_n_train "https://github.com/havish-coder/talking_heads/blob/main/Src/training/train.py"
click node_n_infer "https://github.com/havish-coder/talking_heads/blob/main/Src/inference.py"

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_n_vid,node_n_audio_pre,node_n_ref,node_n_pose,node_n_rename toneBlue
class node_n_audio_model,node_n_dit toneAmber
class node_n_data,node_n_loss,node_n_cfg,node_n_train toneMint
class node_n_infer toneRose
class node_n_vae,node_n_w2v,node_n_cog,node_n_dwpose,node_n_ffmpeg toneIndigo
```

## Components

### Preprocessing

Offline ingestion and feature extraction

- [Video encode](https://github.com/havish-coder/talking_heads/blob/main/Preprocessing/encode_vid.py): preprocess · Prepares video clips and latent-ready samples from raw media · `Preprocessing/encode_vid.py`
- [Audio embed](https://github.com/havish-coder/talking_heads/blob/main/Preprocessing/Audioencoder.py): preprocess · Extracts or prepares audio features for downstream conditioning · `Preprocessing/Audioencoder.py`
- [Ref image](https://github.com/havish-coder/talking_heads/blob/main/Preprocessing/Enocde_refernce_image.py): preprocess · Encodes reference images into reusable conditioning assets · `Preprocessing/Enocde_refernce_image.py`
- [Pose prep](https://github.com/havish-coder/talking_heads/blob/main/Preprocessing/preprocessing_dwpose.py): preprocess · Runs DWPose keypoint preprocessing and sequence alignment · `Preprocessing/preprocessing_dwpose.py`
- [Dataset org](https://github.com/havish-coder/talking_heads/blob/main/Preprocessing/rename.py): preprocess · Renames and organizes clips into training-ready layout · `Preprocessing/rename.py`

### Models

Transformer generator and conditioning modules

- [Audio encoder](https://github.com/havish-coder/talking_heads/blob/main/Src/Models/audio_encoder.py): conditioning · Projects audio features into the model hidden space · `Src/Models/audio_encoder.py`
- [Talking DiT](https://github.com/havish-coder/talking_heads/blob/main/Src/Models/talking_heads_dit.py): generator · Main diffusion transformer for audio-driven talking-head synthesis · `Src/Models/talking_heads_dit.py`

### Training

Dataset, loss, and optimization entrypoint

- [Dataset](https://github.com/havish-coder/talking_heads/blob/main/Src/training/dataset.py): loader · Loads aligned clips, precomputed latents, audio, and pose arrays · `Src/training/dataset.py`
- [Loss](https://github.com/havish-coder/talking_heads/blob/main/Src/training/loss.py): objective · Defines the diffusion training objective · `Src/training/loss.py`
- [Config](https://github.com/havish-coder/talking_heads/blob/main/Src/training/config.yaml): control · Central training settings for batch size, resolution, frames, and checkpoints · `Src/training/config.yaml`
- [Train loop](https://github.com/havish-coder/talking_heads/blob/main/Src/training/train.py): entrypoint · Training entrypoint that ties data, model, loss, and checkpointing together · `Src/training/train.py`

### Inference

Checkpoint-driven video synthesis

- [Inference](https://github.com/havish-coder/talking_heads/blob/main/Src/inference.py): runtime · Loads a checkpoint and generates video from reference image, audio, and pose · `Src/inference.py`

### External

Frozen backbones and third-party modality tools

- **VAE latents**: artifact · Precomputed video latent representation stored on disk
- **Wav2Vec2**: frozen backbone · External audio encoder backbone kept frozen during training
- **CogVideoX**: frozen backbone · Pretrained diffusion backbone adapted for talking-head generation
- **DWPose**: pose tool · External body and hand keypoint extractor used in preprocessing
- **ffmpeg**: media tool · Used to extract audio and support media conversion

## Connections

- Video encode → VAE latents: writes · Produces reusable latent video artifacts
- Audio embed → Wav2Vec2: uses · Relies on audio backbone for embeddings
- Ref image → Talking DiT: conditions · Provides reference-image identity tokens
- Pose prep → DWPose: uses · Depends on pose extractor for keypoints
- Dataset org → Dataset: organizes · Shapes files into training-friendly structure
- Video encode → Dataset: feeds · Supplies precomputed video latents
- Audio embed → Dataset: feeds · Supplies audio embeddings
- Pose prep → Dataset: feeds · Supplies pose arrays
- Config → Train loop: configures · Controls the optimization run
- Dataset → Train loop: batches · Provides aligned training samples
- Talking DiT → Train loop: optimized by · Main trainable generator module
- Loss → Train loop: supervises · Computes diffusion objective during training
- CogVideoX → Talking DiT: backbone · Frozen pretrained core adapted by the model
- Audio encoder → Talking DiT: conditions · Maps audio features into transformer space
- Wav2Vec2 → Audio encoder: features · Supplies frozen audio embeddings
- VAE latents → Inference: decodes · Latents are turned back into video frames
- Talking DiT → Inference: runs · Denoising generator used at inference time
- ffmpeg → Audio embed: extracts · Supports audio extraction from source video
- ffmpeg → Video encode: converts · Supports media conversion during preprocessing
- CogVideoX → Inference: loads · Pretrained weights combined with fine-tuned checkpoint
