How haujetzhao/capswriter-offline works
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).