Overview
The base module system provides the foundation for all modules in BallonTranslator, including text detectors, OCR engines, inpainters, and translators. It handles module registration, parameter management, model loading, and device management.Module Registry
BallonTranslator uses a registry pattern to manage different module types. Each module type has its own registry:Module Scripts Configuration
Module discovery is configured through theMODULE_SCRIPTS dictionary:
BaseModule Class
All modules inherit from theBaseModule class, which provides common functionality.
Class Definition
Class Attributes
Module parameters configuration. Each parameter can be a simple value or a dictionary with metadata:
Set of attribute names that hold loaded models. Used for automatic model lifecycle management.
Preprocessing hooks that run before module execution. Shared across all instances of the class.
Postprocessing hooks that run after module execution. Shared across all instances of the class.
List of files to download for the module. Each entry specifies:
Methods
Parameter Management
Get the current value of a parameter.Parameters:
param_key(str): Parameter name
Set a parameter value with optional type conversion.Parameters:
param_key(str): Parameter nameparam_value: New valueconvert_dtype(bool): Whether to convert to expected type (default: True)
Update a parameter value. Override this to react to parameter changes.Parameters:
param_key(str): Parameter nameparam_content: New value
Model Lifecycle
Load the module’s model. Acquires a loading lock to prevent concurrent loads.
Internal method to load the model. Override this in your module.
Unload the module’s model to free memory.Parameters:
empty_cache(bool): Whether to empty GPU cache after unloading
Check if all required models are loaded.Returns: bool
Hook Management
Register preprocessing hooks (shared across all instances).Parameters:
callbacks(Union[List, Callable, Dict]): Hook functions
Register postprocessing hooks (shared across all instances).Parameters:
callbacks(Union[List, Callable, Dict]): Hook functions
Properties
Whether low VRAM mode is enabled (from params).
Whether debug mode is enabled (from shared config).
Device Detection
Check if module runs on CPU.Returns: bool
Check if module runs on GPU (cuda/mps/xpu/privateuseone).Returns: bool
Check if module has device parameter (implies computational load).Returns: bool
Device Management
Available Devices
Device Constants
Auto-detected default device (cuda, mps, xpu, or cpu).
List of all available devices on the system.
Whether bfloat16 is supported on the default device.
Set of GPU device types: .
Utility Functions
Empty GPU cache and run garbage collection.
Check if running on NVIDIA CUDA.Returns: bool
Check if running on Intel XPU.Returns: bool
Torch Utilities
Data Type Mapping
Parameter Utilities
Helper Functions
Convert simple params to standardized dict format.
Merge saved config params with module default params.Parameters:
cfg_param(dict): Config parametersmodule_params(dict): Module default parametersmodule_name(str): Module name for logging
Merge config with multiple modules’ parameters.Parameters:
config_params(Dict): Configurationmodule_keys(List): Module namesget_module(Callable): Function to get module by key
Example: Custom Module
Registry Class
TheRegistry class manages module registration and retrieval.
Registry Methods
Register a module class.Parameters:
name(str | None): Registration name (defaults to class name)force(bool): Override existing registrationmodule(type): Module class
Get a registered module class by name.Parameters:
key(str): Module name
Dictionary of all registered modules.
