Download controller/model/model.py from RightNowAI/duplex-v0: direct link, hf CLI and curl.
- Browser
- Download file 2 kB
-
https://huggingface.co/RightNowAI/duplex-v0/resolve/main/controller/model/model.py
- Command line
-
hf download hf://RightNowAI/duplex-v0/controller/model/model.py
-
curl -L -o model.py https://huggingface.co/RightNowAI/duplex-v0/resolve/main/controller/model/model.py
2 kB
| """The duplex controller: a small causal transformer that reads, per 80 ms frame, the | |
| streaming encoder features of the caller's audio and of the model's own output audio, | |
| and predicts one of WAIT, START, STOP, BACKCHANNEL. | |
| Default size is about 90M parameters (dim 768, 12 layers); the plan's ceiling is 1B. | |
| """ | |
| import torch | |
| import torch.nn as nn | |
| from duplex.spec import LABELS, TEACHER_DIM, TOK_PER_FRAME | |
| from duplex.streaming import CausalTransformer | |
| class Controller(nn.Module): | |
| def __init__(self, feat_dim=TEACHER_DIM, dim=768, layers=12, heads=12, window=375): | |
| super().__init__() | |
| self.cfg = dict(feat_dim=feat_dim, dim=dim, layers=layers, heads=heads, window=window) | |
| self.in_caller = nn.Linear(TOK_PER_FRAME * feat_dim, dim) | |
| self.in_self = nn.Linear(TOK_PER_FRAME * feat_dim, dim) | |
| self.tf = CausalTransformer(dim, layers, heads, window) # window in 80 ms frames (375 = 30 s) | |
| self.ln = nn.LayerNorm(dim) | |
| self.head = nn.Linear(dim, len(LABELS)) | |
| def _embed(self, caller, own): | |
| """caller, own: [B, 2T, feat_dim] at 25 Hz -> [B, T, dim] at 12.5 Hz.""" | |
| B, N, Fd = caller.shape | |
| shape = (B, N // TOK_PER_FRAME, TOK_PER_FRAME * Fd) | |
| return self.in_caller(caller.reshape(shape)) + self.in_self(own.reshape(shape)) | |
| def forward(self, caller, own): | |
| return self.head(self.ln(self.tf(self._embed(caller, own)))) | |
| def init_state(self): | |
| return self.tf.init_state() | |
| def step(self, caller, own, state): | |
| x, state = self.tf.step(self._embed(caller, own), state) | |
| return self.head(self.ln(x)), state | |
| def save_controller(path, model, extra=None): | |
| torch.save({"cfg": model.cfg, "model": model.state_dict(), "extra": extra or {}}, path) | |
| def load_controller(path, device="cpu"): | |
| from duplex.streaming import read_checkpoint | |
| cfg, state = read_checkpoint(path) | |
| model = Controller(**cfg) | |
| model.load_state_dict(state) | |
| return model.to(device).eval() | |