# gabotechs/musicgpt architecture

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

- Interactive diagram: https://gitdiagram.com/gabotechs/musicgpt
- Repository: https://github.com/gabotechs/musicgpt

## Overview

MusicGPT turns natural-language prompts into generated audio, either through a terminal workflow or a chat-style web UI. The UI routes prompts over WebSocket into queued, cancellable generation; generated progress/results feed chat history and audio files, while the CLI invokes the same processor interface and can play/save WAV output. MusicGen inference is implemented through encoder/decoder components with model and runtime setup. The README also documents model selection, GPU use, persistent chat/audio storage, and remote UI access. Some links are based on README or index references; unsampled internals are not asserted beyond those references.

Main entry points: [`src/backend/music_gpt_ws_handler.rs`](https://github.com/gabotechs/musicgpt/blob/main/src/backend/music_gpt_ws_handler.rs) (Chat protocol), [`src/backend/server.rs`](https://github.com/gabotechs/musicgpt/blob/main/src/backend/server.rs) (HTTP and WS server), [`src/musicgen/music_gen_decoder.rs`](https://github.com/gabotechs/musicgpt/blob/main/src/musicgen/music_gen_decoder.rs) (MusicGen inference), [`src/terminal/mod.rs`](https://github.com/gabotechs/musicgpt/blob/main/src/terminal/mod.rs) (Terminal interface)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_interaction["User interfaces"]
  node_cli["Terminal interface<br/>[mod.rs]"]
  node_cliopts["CLI options<br/>[cli.rs]"]
  node_audioplayer["Audio playback<br/>[audio_manager.rs]"]
end

subgraph group_service["Web service"]
  node_server["HTTP and WS server<br/>[server.rs]"]
  node_wsroute["WebSocket handling<br/>[ws_handler.rs]"]
  node_chatprotocol["Chat protocol"]
  node_chatdomain["Chat records<br/>[music_gpt_chat.rs]"]
end

subgraph group_generation["Audio generation"]
  node_jobqueue["Generation queue"]
  node_fanout["Result broadcast"]
  node_models["Model selection<br/>[musicgen_models.rs]"]
  node_inference["MusicGen inference"]
  node_inputs["Prompt inputs"]
  node_logits["Token processing<br/>[logits.rs]"]
  node_audioencodec["Audio encoding"]
  node_runtime["ONNX runtime setup<br/>[onnxruntime_lib.rs]"]
end

subgraph group_persistence["Local state"]
  node_storage[("Application files<br/>[app_fs.rs]")]
  node_storageext["Model downloads<br/>[storage_ext.rs]"]
end

subgraph group_frontend["Web client"]
  node_webapp["Chat application<br/>[App.tsx]"]
  node_chatui["Chat rendering<br/>[ChatHistory.tsx]"]
  node_chatstate["Chat state<br/>[useChat.ts]"]
  node_backendhook["WebSocket client<br/>[useBackend.ts]"]
  node_bindings["Protocol bindings<br/>[bindings.ts]"]
end

node_user(("Prompting user"))
node_browser(("Remote browser"))

node_user -->|"enters prompt"| node_cli
node_cli -.->|"requests generation"| node_jobqueue
node_cli -->|"plays audio"| node_audioplayer
node_user -->|"enters prompt"| node_webapp
node_browser -->|"opens UI"| node_server
node_server -->|"routes WebSocket"| node_chatprotocol
node_server -->|"starts backend"| node_jobqueue
node_server -->|"starts broadcast"| node_fanout
node_chatprotocol -->|"submits or aborts"| node_jobqueue
node_chatprotocol -->|"loads and saves"| node_chatdomain
node_chatprotocol -->|"subscribes"| node_fanout
node_fanout -.->|"writes audio"| node_storage
node_chatdomain -->|"persists chats"| node_storage
node_wsroute -->|"dispatches messages"| node_chatprotocol
node_webapp -->|"renders history"| node_chatui
node_webapp -->|"manages chat"| node_chatstate
node_chatstate -->|"sends messages"| node_backendhook
node_backendhook -->|"uses protocol"| node_bindings
node_models -->|"uses inference"| node_inference
node_inference -->|"consumes inputs"| node_inputs
node_inference -->|"uses token logic"| node_logits
node_models -.->|"fetches model files"| node_storageext
node_storageext -->|"stores downloads"| node_storage
node_cliopts -->|"selects model"| node_models
node_cliopts -->|"configures runtime"| node_runtime
node_inference -.->|"decodes audio"| node_audioencodec

click node_cli "https://github.com/gabotechs/musicgpt/blob/main/src/terminal/mod.rs"
click node_cliopts "https://github.com/gabotechs/musicgpt/blob/main/src/cli.rs"
click node_webapp "https://github.com/gabotechs/musicgpt/blob/main/web/src/App.tsx"
click node_chatui "https://github.com/gabotechs/musicgpt/blob/main/web/src/ChatHistory.tsx"
click node_chatstate "https://github.com/gabotechs/musicgpt/blob/main/web/src/backend/useChat.ts"
click node_backendhook "https://github.com/gabotechs/musicgpt/blob/main/web/src/backend/useBackend.ts"
click node_bindings "https://github.com/gabotechs/musicgpt/blob/main/web/src/backend/bindings.ts"
click node_server "https://github.com/gabotechs/musicgpt/blob/main/src/backend/server.rs"
click node_wsroute "https://github.com/gabotechs/musicgpt/blob/main/src/backend/ws_handler.rs"
click node_chatprotocol "https://github.com/gabotechs/musicgpt/blob/main/src/backend/music_gpt_ws_handler.rs"
click node_chatdomain "https://github.com/gabotechs/musicgpt/blob/main/src/backend/music_gpt_chat.rs"
click node_jobqueue "https://github.com/gabotechs/musicgpt/blob/main/src/backend/audio_generation_backend.rs"
click node_fanout "https://github.com/gabotechs/musicgpt/blob/main/src/backend/audio_generation_fanout.rs"
click node_models "https://github.com/gabotechs/musicgpt/blob/main/src/musicgen_models.rs"
click node_inference "https://github.com/gabotechs/musicgpt/blob/main/src/musicgen/music_gen_decoder.rs"
click node_inputs "https://github.com/gabotechs/musicgpt/blob/main/src/musicgen/music_gen_inputs.rs"
click node_logits "https://github.com/gabotechs/musicgpt/blob/main/src/musicgen/logits.rs"
click node_audioencodec "https://github.com/gabotechs/musicgpt/blob/main/src/musicgen/music_gen_audio_encodec.rs"
click node_runtime "https://github.com/gabotechs/musicgpt/blob/main/src/onnxruntime_lib.rs"
click node_audioplayer "https://github.com/gabotechs/musicgpt/blob/main/src/audio/audio_manager.rs"
click node_storage "https://github.com/gabotechs/musicgpt/blob/main/src/storage/app_fs.rs"
click node_storageext "https://github.com/gabotechs/musicgpt/blob/main/src/storage_ext.rs"

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_cli,node_cliopts,node_audioplayer,node_user,node_browser toneBlue
class node_server,node_wsroute,node_chatprotocol,node_chatdomain toneAmber
class node_jobqueue,node_fanout,node_models,node_inference,node_inputs,node_logits,node_audioencodec,node_runtime toneMint
class node_storage,node_storageext toneRose
class node_webapp,node_chatui,node_chatstate,node_backendhook,node_bindings toneIndigo
```

## Components

### User interfaces

- [Terminal interface](https://github.com/gabotechs/musicgpt/blob/main/src/terminal/mod.rs): `src/terminal/mod.rs`
- [CLI options](https://github.com/gabotechs/musicgpt/blob/main/src/cli.rs): `src/cli.rs`
- [Audio playback](https://github.com/gabotechs/musicgpt/blob/main/src/audio/audio_manager.rs): `src/audio/audio_manager.rs`

### Web service

- [HTTP and WS server](https://github.com/gabotechs/musicgpt/blob/main/src/backend/server.rs): `src/backend/server.rs`
- [WebSocket handling](https://github.com/gabotechs/musicgpt/blob/main/src/backend/ws_handler.rs): `src/backend/ws_handler.rs`
- [Chat protocol](https://github.com/gabotechs/musicgpt/blob/main/src/backend/music_gpt_ws_handler.rs): `src/backend/music_gpt_ws_handler.rs`
- [Chat records](https://github.com/gabotechs/musicgpt/blob/main/src/backend/music_gpt_chat.rs): `src/backend/music_gpt_chat.rs`

### Audio generation

- [Generation queue](https://github.com/gabotechs/musicgpt/blob/main/src/backend/audio_generation_backend.rs): `src/backend/audio_generation_backend.rs`
- [Result broadcast](https://github.com/gabotechs/musicgpt/blob/main/src/backend/audio_generation_fanout.rs): `src/backend/audio_generation_fanout.rs`
- [Model selection](https://github.com/gabotechs/musicgpt/blob/main/src/musicgen_models.rs): `src/musicgen_models.rs`
- [MusicGen inference](https://github.com/gabotechs/musicgpt/blob/main/src/musicgen/music_gen_decoder.rs): `src/musicgen/music_gen_decoder.rs`
- [Prompt inputs](https://github.com/gabotechs/musicgpt/blob/main/src/musicgen/music_gen_inputs.rs): `src/musicgen/music_gen_inputs.rs`
- [Token processing](https://github.com/gabotechs/musicgpt/blob/main/src/musicgen/logits.rs): `src/musicgen/logits.rs`
- [Audio encoding](https://github.com/gabotechs/musicgpt/blob/main/src/musicgen/music_gen_audio_encodec.rs): `src/musicgen/music_gen_audio_encodec.rs`
- [ONNX runtime setup](https://github.com/gabotechs/musicgpt/blob/main/src/onnxruntime_lib.rs): `src/onnxruntime_lib.rs`

### Local state

- [Application files](https://github.com/gabotechs/musicgpt/blob/main/src/storage/app_fs.rs): `src/storage/app_fs.rs`
- [Model downloads](https://github.com/gabotechs/musicgpt/blob/main/src/storage_ext.rs): `src/storage_ext.rs`

### Web client

- [Chat application](https://github.com/gabotechs/musicgpt/blob/main/web/src/App.tsx): `web/src/App.tsx`
- [Chat rendering](https://github.com/gabotechs/musicgpt/blob/main/web/src/ChatHistory.tsx): `web/src/ChatHistory.tsx`
- [Chat state](https://github.com/gabotechs/musicgpt/blob/main/web/src/backend/useChat.ts): `web/src/backend/useChat.ts`
- [WebSocket client](https://github.com/gabotechs/musicgpt/blob/main/web/src/backend/useBackend.ts): `web/src/backend/useBackend.ts`
- [Protocol bindings](https://github.com/gabotechs/musicgpt/blob/main/web/src/backend/bindings.ts): `web/src/backend/bindings.ts`

### Other components

- **Prompting user**
- **Remote browser**

## Connections

- Prompting user → Terminal interface: enters prompt (evidence: `README.md`)
- Terminal interface → Generation queue: requests generation
- Terminal interface → Audio playback: plays audio (evidence: `src/terminal/mod.rs`)
- Prompting user → Chat application: enters prompt (evidence: `README.md`)
- Remote browser → HTTP and WS server: opens UI (evidence: `README.md`)
- HTTP and WS server → Chat protocol: routes WebSocket (evidence: `src/backend/server.rs`)
- HTTP and WS server → Generation queue: starts backend (evidence: `src/backend/server.rs`)
- HTTP and WS server → Result broadcast: starts broadcast (evidence: `src/backend/server.rs`)
- Chat protocol → Generation queue: submits or aborts (evidence: `src/backend/music_gpt_ws_handler.rs`)
- Chat protocol → Chat records: loads and saves (evidence: `src/backend/music_gpt_ws_handler.rs`)
- Chat protocol → Result broadcast: subscribes (evidence: `src/backend/music_gpt_ws_handler.rs`)
- Result broadcast → Application files: writes audio
- Chat records → Application files: persists chats (evidence: `src/backend/music_gpt_chat.rs`)
- WebSocket handling → Chat protocol: dispatches messages (evidence: `src/backend/ws_handler.rs`)
- Chat application → Chat rendering: renders history (evidence: `web/src/App.tsx`)
- Chat application → Chat state: manages chat (evidence: `web/src/App.tsx`)
- Chat state → WebSocket client: sends messages (evidence: `web/src/backend/useChat.ts`)
- WebSocket client → Protocol bindings: uses protocol (evidence: `web/src/backend/useBackend.ts`)
- Model selection → MusicGen inference: uses inference (evidence: `src/musicgen_models.rs`)
- MusicGen inference → Prompt inputs: consumes inputs (evidence: `src/musicgen/music_gen_decoder.rs`)
- MusicGen inference → Token processing: uses token logic (evidence: `src/musicgen/music_gen_decoder.rs`)
- Model selection → Model downloads: fetches model files (evidence: `src/musicgen_models.rs`)
- Model downloads → Application files: stores downloads (evidence: `src/storage_ext.rs`)
- CLI options → Model selection: selects model (evidence: `src/musicgen_models.rs`)
- CLI options → ONNX runtime setup: configures runtime (evidence: `src/cli.rs`)
- MusicGen inference → Audio encoding: decodes audio
