# haujetzhao/capswriter-offline architecture

> Architecture diagram of the GitHub repository haujetzhao/capswriter-offline, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/haujetzhao/capswriter-offline
- Repository: https://github.com/haujetzhao/capswriter-offline

## Overview

##### Project Type and Purpose
- This is a full-stack (client/server) desktop application aimed at offline voice transcription.
- Its core functions are: (a) real-time voice input when a specific key (such as CapsLock) is pressed, recording and transcribing speech with hot-word substitutions; (b) supporting a transcription workflow for audio/video files to produce SRT subtitle files, JSON timing metadata, and plain text outputs.
- The README explains features such as dynamic hot-word updates, log storage (organized by date), and dual operation modes (interactive voice input and file transcription).

##### File Structure
- Key scripts:
- core_client.py and core_server.py form the backbone for client and server operations.
- start_client.py and start_server.py are entry points.
- config.py manages global configuration (server’s IP, port, shortcuts, etc.).
- The util folder contains many utility modules that handle tasks like:
- Audio streaming and file creations (client_send_audio, client_stream, client_write_file, etc.)
- Server-side model management and recognition (server_init_recognizer, server_recognize, server_classes, etc.)
- Transcription-specific post-processing (client_adjust_srt, srt_from_txt)
- The models folder contains documentation and test scripts for model conversion and testing workflows, reflecting the emphasis on voice recognition model handling.
- Build scripts (build.spec, run.sh, etc.) indicate packaging and deployment procedures.

##### Architectural Components
a. Client Component:
- Input Module: Handles keypress detection (e.g., CapsLock, right shift) and triggering of recording.
- Audio Capture & Processing: Scripts for capturing, possibly sending audio (e.g., client_send_audio, client_stream).
- Hot-word Management: Loading and dynamic updates managed with hot-en.txt, hot-zh.txt, and hot-rule.txt; modules like hot_sub_en, hot_sub_rule.
- Local File Handling: For logging transcriptions into date-organized Markdown files and audio file storage.

b. Server Component:
- Model Loader/Initializer: Loads the onnx-based recognition (Paraformer, punctuation model) using sherpa-onnx libraries; modules like server_init_recognizer.
- Recognition Engine: Processes incoming audio streams; implements recognition (server_recognize, server_ws_recv, server_ws_send).
- API Layer: Manages communication with multiple clients; client_check_websocket and related modules handle network interactions.
- Post-processing: May include punctuation insertion and formatting before returning results.

c. File Transcription Workflow:
- When audio/video files are “dropped” onto the client, the system initiates a workflow that:
- Sends the file to the server or processes it locally.
- Generates multiple output files: JSON (with timing stamps), TXT (line-separated text), merged TXT (full text with punctuation), and SRT (subtitles).
- There’s integration with FFmpeg (mentioned in the README) and client scripts that detect and handle such files.

d. Configuration and Integration:
- The config.py file is central to adjusting settings like server address, keyboard shortcuts, and file-saving behaviors.
- Dependency management: Separate requirements for client (requirements-client.txt) and server (requirements-server.txt).

##### Relationships and Data Flow
- After recording and pre-processing on the client, audio data is sent (via WebSocket or similar IPC as handled by modules like client_ws_recv) to the server.
- The server then processes the audio with its recognition engine, applies post-processing (hot-word substitutions, punctuation), and returns the transcription.
- The client receives transcription data and writes it to the appropriate log files or updates SRT files if file transcription is in progress.
- Optionally, file-based workflows (audio/video file drag-and-drop) can show a separate data flow where file conversion and subtitle generation modules are invoked.

##### Important Architectural Patterns and Design Details
- Client-Server Separation: Clearly show that the client and server are distinct processes possibly running on separate machines.
- Real-time Data Flow: Emphasize low-latency flow from recording input to recognition output.
- Modular Utilities: Highlight how various utility modules (in the util directory) plug into the main workflow to provide functionalities such as file handling, format conversion, and asynchronous processing.
- Offline Operation: Note that no external cloud service is involved; all processing is done on the local machine (or within the local network in a client-server context).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global Configuration
    CONFIG["Configuration (config.py)"]:::config

    %% Client Side Components
    subgraph "Client"
        CI["Input Module"]:::client
        ACAP["Audio Capture & Processing"]:::client
        HOT["Hot-word Management"]:::client
        LF["Local File Handling"]:::client
        FTW["File Transcription Workflow"]:::client
    end

    %% Communication Interface
    COMM["Communication Interface (WebSocket)"]:::comm

    %% Server Side Components
    subgraph "Server"
        SM["Model Loader/Initializer"]:::server
        RE["Recognition Engine"]:::server
        API["API Layer"]:::server
    end

    %% Model Management & Testing
    MM["Model Management & Testing"]:::testing

    %% Connections
    CONFIG --> CI
    CONFIG --> SM

    CI --> ACAP
    ACAP --> COMM
    FTW --> COMM

    COMM --> RE
    SM --> RE
    RE --> API
    API --> COMM
    COMM --> HOT
    HOT --> LF

    %% Click Events for Client Input Module
    click CI "https://github.com/haujetzhao/capswriter-offline/blob/master/core_client.py"
    click CI "https://github.com/haujetzhao/capswriter-offline/blob/master/util/client_shortcut_handler.py"

    %% Click Events for Audio Capture & Processing (Client)
    click ACAP "https://github.com/haujetzhao/capswriter-offline/blob/master/util/client_send_audio.py"
    click ACAP "https://github.com/haujetzhao/capswriter-offline/blob/master/util/client_stream.py"

    %% Click Events for Hot-word Management (Client)
    click HOT "https://github.com/haujetzhao/capswriter-offline/blob/master/hot-en.txt"
    click HOT "https://github.com/haujetzhao/capswriter-offline/blob/master/hot-rule.txt"
    click HOT "https://github.com/haujetzhao/capswriter-offline/blob/master/hot-zh.txt"
    click HOT "https://github.com/haujetzhao/capswriter-offline/blob/master/util/client_hot_sub.py"
    click HOT "https://github.com/haujetzhao/capswriter-offline/blob/master/util/client_hot_update.py"
    click HOT "https://github.com/haujetzhao/capswriter-offline/blob/master/util/hot_sub_en.py"
    click HOT "https://github.com/haujetzhao/capswriter-offline/blob/master/util/hot_sub_rule.py"
    click HOT "https://github.com/haujetzhao/capswriter-offline/blob/master/util/hot_sub_zh.py"

    %% Click Events for Local File Handling (Client)
    click LF "https://github.com/haujetzhao/capswriter-offline/blob/master/util/client_create_file.py"
    click LF "https://github.com/haujetzhao/capswriter-offline/blob/master/util/client_write_file.py"
    click LF "https://github.com/haujetzhao/capswriter-offline/blob/master/util/client_write_md.py"
    click LF "https://github.com/haujetzhao/capswriter-offline/blob/master/util/client_finish_file.py"

    %% Click Events for File Transcription Workflow
    click FTW "https://github.com/haujetzhao/capswriter-offline/blob/master/util/client_adjust_srt.py"
    click FTW "https://github.com/haujetzhao/capswriter-offline/blob/master/util/srt_from_txt.py"

    %% Click Events for Server Model Loader/Initializer
    click SM "https://github.com/haujetzhao/capswriter-offline/blob/master/core_server.py"
    click SM "https://github.com/haujetzhao/capswriter-offline/blob/master/util/server_init_recognizer.py"

    %% Click Events for Server Recognition Engine
    click RE "https://github.com/haujetzhao/capswriter-offline/blob/master/util/server_recognize.py"
    click RE "https://github.com/haujetzhao/capswriter-offline/blob/master/util/server_ws_recv.py"
    click RE "https://github.com/haujetzhao/capswriter-offline/blob/master/util/server_ws_send.py"
    click RE "https://github.com/haujetzhao/capswriter-offline/blob/master/util/server_classes.py"

    %% Click Event for Configuration
    click CONFIG "https://github.com/haujetzhao/capswriter-offline/blob/master/config.py"

    %% Click Event for Model Management & Testing
    click MM "https://github.com/haujetzhao/capswriter-offline/tree/master/models"

    %% Styles
    classDef client fill:#AED6F1,stroke:#2E86C1,stroke-width:2px;
    classDef server fill:#A9DFBF,stroke:#239B56,stroke-width:2px;
    classDef config fill:#D2B4DE,stroke:#8E44AD,stroke-width:2px;
    classDef comm fill:#FAD7A0,stroke:#E67E22,stroke-width:2px;
    classDef testing fill:#D5F5E3,stroke:#27AE60,stroke-width:2px;
```
