Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Hiyouga LLaMA Factory WebUI Engine

From Leeroopedia


Knowledge Sources
Domains WebUI, Orchestration, State Management
Last Updated 2026-02-06 19:00 GMT

Overview

Central engine class that orchestrates all WebUI subsystems including component management, training execution, chat inference, and application lifecycle.

Description

The engine.py module defines the Engine class, which serves as the core orchestrator for the LLaMA Factory WebUI. On initialization, it creates three key subsystems: a Manager for component registration and lookup, a Runner for managing training and evaluation processes, and a WebChatModel (chatter) for chat inference. In non-demo mode, it also generates DeepSpeed configuration files. The class provides resume() to restore UI state from saved user configuration and detect running training processes, change_lang() to dynamically update all component labels using the LOCALES dictionary, and _update_component() to batch-update Gradio components from a dictionary of property mappings.

Usage

Use this class when initializing the WebUI application. It is instantiated by create_ui and create_web_demo in the interface module and serves as the shared context passed to all tab creation functions.

Code Reference

Source Location

Signature

class Engine:
    def __init__(self, demo_mode: bool = False, pure_chat: bool = False) -> None: ...

    def _update_component(
        self, input_dict: dict[str, dict[str, Any]]
    ) -> dict["Component", "Component"]: ...

    def resume(self): ...

    def change_lang(self, lang: str): ...

Import

from llamafactory.webui.engine import Engine

I/O Contract

Inputs

Name Type Required Description
demo_mode bool No If True, disables config persistence and DeepSpeed config creation (default: False)
pure_chat bool No If True, creates a minimal chat-only interface without training/eval/export tabs (default: False)
lang (change_lang) str Yes Language code ("en", "ru", "zh", "ko", "ja") for UI localization

Outputs

Name Type Description
self.manager Manager Component registry for registering and retrieving Gradio elements by ID
self.runner Runner Training/evaluation process manager with run, preview, monitor, and abort methods
self.chatter WebChatModel Chat inference engine with load_model, unload_model, append, and stream methods
resume() Generator[dict] Yields dictionaries mapping Component instances to updated Component instances with initial values
change_lang() dict[Component, Component] Dictionary mapping each localized component to its updated version with new language labels

Usage Examples

# Creating an engine for the full WebUI
from llamafactory.webui.engine import Engine

engine = Engine(demo_mode=False, pure_chat=False)
# engine.manager - register and look up components
# engine.runner  - run training/evaluation
# engine.chatter - load models and chat
# Creating an engine for a chat-only demo
from llamafactory.webui.engine import Engine

engine = Engine(pure_chat=True)
# engine.chatter is eagerly initialized (lazy_init=False)
# Wiring resume and language change to the Gradio demo
demo.load(engine.resume, outputs=engine.manager.get_elem_list(), concurrency_limit=None)
lang.change(engine.change_lang, [lang], engine.manager.get_elem_list(), queue=False)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment