Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

feat(create): local execution backend for the five hosted categories

purple12cda57da5ef
diff --git a/src-tauri/src/commands/install.rs b/src-tauri/src/commands/install.rs
index 0e1d3afa..e6895a36 100644
--- a/src-tauri/src/commands/install.rs
+++ b/src-tauri/src/commands/install.rs
@@ -809,6 +809,161 @@ pub fn install_comfyui_status(state: State<'_, AppState>) -> Result<serde_json::
}))
}
+/// Update an existing ComfyUI install in place: `git pull --ff-only` plus a
+/// venv-aware `pip install -r requirements.txt`. The 2.5.8 local Create lanes
+/// (music / talking character / extend / motion) need node classes that ship
+/// with current ComfyUI cores, and the UI gates on node PRESENCE — when the
+/// nodes are missing this command is the one-click "Update ComfyUI" path.
+/// Progress streams through the same `install_status` channel the installer
+/// uses, so the existing `install_comfyui_status` polling UI works unchanged.
+#[tauri::command]
+pub fn update_comfyui(state: State<'_, AppState>) -> Result<serde_json::Value, String> {
+ {
+ let mut install = state.install_status.lock().unwrap();
+ if install.status == "installing" || install.status == "downloading" {
+ return Ok(serde_json::json!({"status": "already_installing"}));
+ }
+ install.status = "installing".to_string();
+ install.logs.clear();
+ install.logs.push("Updating ComfyUI...".to_string());
+ }
+
+ info!("comfyui update start");
+
+ let comfy_dir = {
+ let p = state.comfy_path.lock().unwrap().clone();
+ p.or_else(crate::commands::process::find_comfyui_path)
+ .map(PathBuf::from)
+ };
+ let fail = |state: &State<'_, AppState>, msg: &str| -> Result<serde_json::Value, String> {
+ let mut install = state.install_status.lock().unwrap();
+ install.status = "error".to_string();
+ install.logs.push(msg.to_string());
+ error!("comfyui update aborted: {}", msg);
+ Err(msg.to_string())
+ };
+ let Some(comfy_dir) = comfy_dir else {
+ return fail(&state, "ComfyUI not found. Install ComfyUI first.");
+ };
+ if !comfy_dir.join(".git").exists() {
+ // Portable / zip installs carry no git metadata — nothing to pull.
+ return fail(
+ &state,
+ "This ComfyUI was not installed from git, so it can't be updated in place. \
+ Update it with its own updater, or reinstall from Settings.",
+ );
+ }
+
+ // Prefer the install's venv Python (same preference the launcher uses);
+ // refuse without a usable interpreter — a pulled core with stale
+ // requirements is worse than no update (frontend package pins move often).
+ let python_bin = crate::python::resolve_comfyui_venv_python(&comfy_dir)
+ .unwrap_or_else(|| state.python_bin.lock().unwrap().clone());
+ if python_bin.is_empty() || !crate::python::is_real_python(&python_bin) {
+ return fail(
+ &state,
+ "No usable Python found for this ComfyUI. Install Python first, then retry the update.",
+ );
+ }
+
+ let install_status = state.install_status.clone();
+ std::thread::spawn(move || {
+ let update = |status: &str, msg: &str| {
+ if let Ok(mut s) = install_status.lock() {
+ s.status = status.to_string();
+ s.logs.push(msg.to_string());
+ }
+ };
+
+ #[cfg(target_os = "windows")]
+ {
+ let probe = windows_git_probe();
+ if probe == WindowsGitState::Missing {
+ update("error", &windows_git_install_hint(&probe).unwrap_or_default());
+ return;
+ }
+ }
+
+ update("installing", "Step 1/2: Pulling the latest ComfyUI...");
+ let mut pull = Command::new("git");
+ // --ff-only: a user-modified checkout must not silently merge; surface
+ // the divergence honestly instead.
+ pull.args(["pull", "--ff-only"])
+ .current_dir(&comfy_dir)
+ .stdout(Stdio::piped())
+ .stderr(Stdio::piped());
+ #[cfg(target_os = "windows")]
+ pull.creation_flags(CREATE_NO_WINDOW);
+ match pull.output() {
+ Ok(o) if o.status.success() => {
+ let out = String::from_utf8_lossy(&o.stdout);
+ let line = out.lines().last().unwrap_or("").trim().to_string();
+ update(
+ "installing",
+ if line.is_empty() { "Repository updated." } else { &line },
+ );
+ }
+ Ok(o) => {
+ let stderr = String::from_utf8_lossy(&o.stderr);
+ update(
+ "error",
+ &format!(
+ "git pull failed. If you changed files inside the ComfyUI folder, \
+ stash or revert them and retry.\n\n{}",
+ stderr.trim(),
+ ),
+ );
+ return;
+ }
+ Err(e) => {
+ update("error", &format!("Could not run git: {}", e));
+ return;
+ }
+ }
+
+ update(
+ "installing",
+ "Step 2/2: Updating Python dependencies (live pip output below)...",
+ );
+ let reqs = comfy_dir.join("requirements.txt");
+ if reqs.exists() {
+ let reqs_str = reqs.to_string_lossy().to_string();
+ let req_args = vec![
+ "-m", "pip", "install",
+ "--progress-bar", "off",
+ "--no-input",
+ "-r", reqs_str.as_str(),
+ ];
+ match pip_install_streaming_with_retry_cancellable(
+ &req_args,
+ &python_bin,
+ 3,
+ &install_status,
+ None,
+ ) {
+ Ok(()) => update("installing", "Dependencies updated."),
+ Err(diagnosis) => {
+ // Same stance as the installer's step 3: optional deps may
+ // fail while ComfyUI still starts — log, don't abort.
+ println!("[Update] Requirements warning: {}", diagnosis);
+ update(
+ "installing",
+ "Some dependencies had warnings (non-critical, ComfyUI should still start).",
+ );
+ }
+ }
+ }
+
+ println!("[Update] ComfyUI update complete");
+ update(
+ "complete",
+ "ComfyUI updated. Restart ComfyUI to load the new nodes.",
+ );
+ });
+
+ Ok(serde_json::json!({"status": "installing"}))
+}
+
// ── Shared helper: download a file with progress tracking ────────────────────
fn download_file_blocking(
diff --git a/src-tauri/src/commands/mod.rs b/src-tauri/src/commands/mod.rs
index 7e2f79c8..dae43ace 100644
--- a/src-tauri/src/commands/mod.rs
+++ b/src-tauri/src/commands/mod.rs
@@ -17,6 +17,7 @@ pub mod search;
pub mod secret;
pub mod shell;
pub mod system;
+pub mod trainer;
pub mod tts;
pub mod waitlist;
pub mod whisper;
diff --git a/src-tauri/src/commands/process.rs b/src-tauri/src/commands/process.rs
index 7601285a..bf8a40e0 100644
--- a/src-tauri/src/commands/process.rs
+++ b/src-tauri/src/commands/process.rs
@@ -1867,8 +1867,17 @@ mod tests {
/// mode reloads everything LAZILY on first use, so this is safe to run on every
/// switch into cloud and on launch-in-cloud. (LM Studio is offloaded separately
/// by the frontend via `lmstudio_unload_model("--all")`.)
+/// `include_comfyui` = whether to also free ComfyUI's VRAM. Cloud switch passes
+/// nothing (defaults true — release everything). A LOCAL render passes `false`:
+/// it wants the chat LLMs out of VRAM to make room, but ComfyUI keeps its own
+/// checkpoint cached across consecutive Create runs (freeing it here would force
+/// a slow reload between every generate).
#[tauri::command]
-pub fn offload_local_models(state: State<'_, AppState>) -> Result<serde_json::Value, String> {
+pub fn offload_local_models(
+ state: State<'_, AppState>,
+ include_comfyui: Option<bool>,
+) -> Result<serde_json::Value, String> {
+ let free_comfy = include_comfyui.unwrap_or(true);
let mut freed: Vec<&str> = Vec::new();
// 1) Whisper STT — the python server holds the model resident; stopping it
@@ -1895,12 +1904,13 @@ pub fn offload_local_models(state: State<'_, AppState>) -> Result<serde_json::Va
}
// 4) ComfyUI — free VRAM/RAM without killing the server, so the next local
- // render just reloads the checkpoint (no slow process restart).
- if free_comfyui_memory() {
+ // render just reloads the checkpoint (no slow process restart). Skipped
+ // when a local render is the caller (it keeps its own checkpoint cached).
+ if free_comfy && free_comfyui_memory() {
freed.push("comfyui");
}
- println!("[Offload] cloud mode — released local model backends: {:?}", freed);
+ println!("[Offload] released local model backends (comfyui={}): {:?}", free_comfy, freed);
Ok(serde_json::json!({ "offloaded": freed }))
}
@@ -1946,7 +1956,9 @@ fn offload_ollama_loaded_models() -> bool {
/// Ask ComfyUI to unload checkpoints and free memory, keeping the server up so
/// the next local render reloads on demand. Best-effort (default port 8188).
-fn free_comfyui_memory() -> bool {
+/// Also used by the character trainer — on a 12 GB card a cached video
+/// checkpoint next door is the difference between training and CUDA OOM.
+pub(crate) fn free_comfyui_memory() -> bool {
let client = match reqwest::blocking::Client::builder()
.timeout(std::time::Duration::from_secs(2))
.build()
diff --git a/src-tauri/src/commands/trainer.rs b/src-tauri/src/commands/trainer.rs
new file mode 100644
index 00000000..d67292a1
--- /dev/null
+++ b/src-tauri/src/commands/trainer.rs
@@ -0,0 +1,704 @@
+// 2.5.8 — local character trainer (Character Studio, local lane).
+//
+// Trains a character LoRA fully on the user's GPU with kohya's musubi-tuner,
+// pinned to tag v0.3.4, inside its OWN venv (never the ComfyUI one — torch
+// versions must be free to diverge). Z-Image is the trained architecture:
+// Apache-licensed base, the only image family whose 12 GB training path is
+// community-proven, and its finished LoRA drops straight into
+// ComfyUI/models/loras after the documented Diffusers conversion — the
+// existing local LoRA chain picks it up with no extra wiring.
+//
+// Command surface (mirrors the whisper/tts installer contracts):
+// install_character_trainer(installPath?) one-time env setup, streamed
+// character_trainer_status() env + base-model readiness probe
+// stage_training_image(setId, name, bytes) stage one photo of the set
+// start_character_training{..} cache -> train -> convert -> loras/
+// character_training_status() run status + logs + step counter
+// cancel_character_training() cooperative cancel + child kill
+//
+// Security stance: no user-supplied URLs anywhere — repo + tag are hardcoded,
+// base models resolve only from known filenames inside LU-managed dirs, and
+// the training-set id / names are sanitized before any path join.
+
+use crate::state::AppState;
+use std::fs;
+use std::io::{BufRead, BufReader};
+use std::path::{Path, PathBuf};
+use std::process::{Command, Stdio};
+use std::sync::atomic::Ordering;
+use std::sync::{Arc, Mutex};
+use tauri::{Manager, State};
+use tracing::info;
+
+#[cfg(target_os = "windows")]
+use std::os::windows::process::CommandExt;
+#[cfg(target_os = "windows")]
+const CREATE_NO_WINDOW: u32 = 0x08000000;
+
+const MUSUBI_REPO: &str = "https://github.com/kohya-ss/musubi-tuner.git";
+const MUSUBI_TAG: &str = "v0.3.4";
+
+/// Known Z-Image training-base files, resolved by exact filename from the
+/// trainer root's models dir or the active ComfyUI models tree.
+/// Deliberately NOT the turbo checkpoint: musubi's own docs call turbo
+/// training unstable and point to ostris' De-Turbo for that lane — and the
+/// circulating NSFW full finetunes are ComfyUI-saved with a
+/// `model.diffusion_model.` key prefix that musubi's strict loader rejects
+/// (verified against zimage_model.py, 2026-07-18).
+const DIT_CANDIDATES: &[&str] = &["z_image_bf16.safetensors", "z_image_de_turbo_v1_bf16.safetensors"];
+const TE_CANDIDATES: &[&str] = &["qwen_3_4b.safetensors"];
+const VAE_CANDIDATES: &[&str] = &["ae.safetensors"];
+
+fn sanitize_component(s: &str) -> String {
+ let cleaned: String = s
+ .chars()
+ .map(|c| if c.is_ascii_alphanumeric() || c == '-' || c == '_' { c } else { '_' })
+ .collect();
+ cleaned.trim_matches('_').chars().take(48).collect()
+}
+
+fn config_json_path() -> Option<PathBuf> {
+ dirs::config_dir().map(|d| d.join("locally-uncensored").join("config.json"))
+}
+
+fn read_config_value(key: &str) -> Option<String> {
+ let path = config_json_path()?;
+ let content = fs::read_to_string(path).ok()?;
+ let json: serde_json::Value = serde_json::from_str(&content).ok()?;
+ json.get(key)?.as_str().map(|s| s.to_string())
+}
+
+fn write_config_value(key: &str, value: &str) {
+ let Some(path) = config_json_path() else { return };
+ let _ = fs::create_dir_all(path.parent().unwrap_or(Path::new(".")));
+ let mut json: serde_json::Value = fs::read_to_string(&path)
+ .ok()
+ .and_then(|s| serde_json::from_str(&s).ok())
+ .unwrap_or_else(|| serde_json::json!({}));
+ json[key] = serde_json::json!(value);
+ let _ = fs::write(&path, serde_json::to_string_pretty(&json).unwrap_or_default());
+}
+
+/// Trainer root: persisted override (config `trainer_root`) else
+/// `<app_data>/musubi`. Layout: `<root>/venv`, `<root>/musubi-tuner`,
+/// `<root>/models`, `<root>/train/<set_id>/...`.
+fn trainer_root(app: &tauri::AppHandle) -> PathBuf {
+ if let Some(p) = read_config_value("trainer_root") {
+ if !p.is_empty() {
+ return PathBuf::from(p);
+ }
+ }
+ app.path()
+ .app_data_dir()
+ .unwrap_or_else(|_| PathBuf::from("."))
+ .join("musubi")
+}
+
+fn venv_python(root: &Path) -> PathBuf {
+ #[cfg(target_os = "windows")]
+ { root.join("venv").join("Scripts").join("python.exe") }
+ #[cfg(not(target_os = "windows"))]
+ { root.join("venv").join("bin").join("python") }
+}
+
+fn repo_dir(root: &Path) -> PathBuf {
+ root.join("musubi-tuner")
+}
+
+fn push_log(state: &Arc<Mutex<crate::state::InstallState>>, msg: &str) {
+ if let Ok(mut s) = state.lock() {
+ s.logs.push(msg.to_string());
+ if s.logs.len() > 400 {
+ let cut = s.logs.len() - 400;
+ s.logs.drain(0..cut);
+ }
+ }
+}
+
+fn set_status(state: &Arc<Mutex<crate::state::InstallState>>, status: &str, msg: &str) {
+ if let Ok(mut s) = state.lock() {
+ s.status = status.to_string();
+ s.logs.push(msg.to_string());
+ }
+}
+
+/// Resolve a base-model file by exact name: `<root>/models` first, then the
+/// active ComfyUI models tree (so files pulled via the Model Manager count).
+fn resolve_base_file(root: &Path, comfy_dir: Option<&Path>, names: &[&str], sub: &str) -> Option<PathBuf> {
+ for n in names {
+ let local = root.join("models").join(n);
+ if local.exists() {
+ return Some(local);
+ }
+ if let Some(c) = comfy_dir {
+ let in_comfy = c.join("models").join(sub).join(n);
+ if in_comfy.exists() {
+ return Some(in_comfy);
+ }
+ }
+ }
+ None
+}
+
+fn active_comfy_dir(state: &AppState) -> Option<PathBuf> {
+ let p = state.comfy_path.lock().ok()?.clone();
+ p.map(PathBuf::from)
+ .or_else(|| crate::commands::process::find_comfyui_path().map(PathBuf::from))
+}
+
+/// Run one child to completion, streaming stdout+stderr lines into the run
+/// state. Registers the child pid so cancel can kill it. Returns Err on
+/// non-zero exit (with the last stderr lines) or on cancel.
+fn run_streamed(
+ mut cmd: Command,
+ label: &str,
+ run: &Arc<Mutex<crate::state::InstallState>>,
+ cancel: &Arc<std::sync::atomic::AtomicBool>,
+ pid_slot: &Arc<Mutex<Option<u32>>>,
+) -> Result<(), String> {
+ cmd.stdout(Stdio::piped()).stderr(Stdio::piped());
+ #[cfg(target_os = "windows")]
+ cmd.creation_flags(CREATE_NO_WINDOW);
+ let mut child = cmd
+ .spawn()
+ .map_err(|e| format!("{label} could not start: {e}"))?;
+ if let Ok(mut slot) = pid_slot.lock() {
+ *slot = Some(child.id());
+ }
+
+ let tail: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(Vec::new()));
+ let mut handles = Vec::new();
+ for stream in [
+ child.stdout.take().map(|s| Box::new(s) as Box<dyn std::io::Read + Send>),
+ child.stderr.take().map(|s| Box::new(s) as Box<dyn std::io::Read + Send>),
+ ]
+ .into_iter()
+ .flatten()
+ {
+ let run = run.clone();
+ let tail = tail.clone();
+ handles.push(std::thread::spawn(move || {
+ let reader = BufReader::new(stream);
+ for line in reader.lines().map_while(Result::ok) {
+ let trimmed = line.trim();
+ if trimmed.is_empty() {
+ continue;
+ }
+ // Step counter for the UI meter: musubi/tqdm emit
+ // "steps: NN%|...| 123/1600 [...]" style lines.
+ if let Some((cur, total)) = parse_step_counter(trimmed) {
+ if let Ok(mut s) = run.lock() {
+ s.download_progress = cur;
+ s.download_total = total;
+ }
+ }
+ if let Ok(mut t) = tail.lock() {
+ t.push(trimmed.to_string());
+ if t.len() > 12 {
+ t.remove(0);
+ }
+ }
+ push_log(&run, trimmed);
+ }
+ }));
+ }
+
+ let exit = loop {
+ if cancel.load(Ordering::SeqCst) {
+ let _ = child.kill();
+ let _ = child.wait();
+ for h in handles {
+ let _ = h.join();
+ }
+ return Err("cancelled".to_string());
+ }
+ match child.try_wait() {
+ Ok(Some(s)) => break s,
+ Ok(None) => std::thread::sleep(std::time::Duration::from_millis(300)),
+ Err(e) => return Err(format!("{label} wait failed: {e}")),
+ }
+ };
+ for h in handles {
+ let _ = h.join();
+ }
+ if let Ok(mut slot) = pid_slot.lock() {
+ *slot = None;
+ }
+ if exit.success() {
+ Ok(())
+ } else {
+ let last = tail
+ .lock()
+ .map(|t| t.join("\n"))
+ .unwrap_or_default();
+ Err(format!("{label} failed (exit {:?}).\n{last}", exit.code()))
+ }
+}
+
+/// Pull "123/1600" out of a tqdm-ish progress line.
+pub fn parse_step_counter(line: &str) -> Option<(u64, u64)> {
+ // Cheap scan without regex: find "N/M" where both sides are digits and M
+ // looks like a step total (>= 10, filters version strings like 2/3).
+ let bytes = line.as_bytes();
+ for (i, &b) in bytes.iter().enumerate() {
+ if b != b'/' {
+ continue;
+ }
+ let left_start = line[..i]
+ .rfind(|c: char| !c.is_ascii_digit())
+ .map(|p| p + 1)
+ .unwrap_or(0);
+ let right_end = line[i + 1..]
+ .find(|c: char| !c.is_ascii_digit())
+ .map(|p| i + 1 + p)
+ .unwrap_or(line.len());
+ if left_start >= i || right_end <= i + 1 {
+ continue;
+ }
+ if let (Ok(cur), Ok(total)) = (line[left_start..i].parse::<u64>(), line[i + 1..right_end].parse::<u64>()) {
+ if total >= 10 && cur <= total {
+ return Some((cur, total));
+ }
+ }
+ }
+ None
+}
+
+// ── one-time environment install ─────────────────────────────────────────────
+
+#[allow(non_snake_case)]
+#[tauri::command]
+pub fn install_character_trainer(
+ app: tauri::AppHandle,
+ state: State<'_, AppState>,
+ installPath: Option<String>,
+) -> Result<serde_json::Value, String> {
+ {
+ let mut st = state.trainer_install.lock().unwrap();
+ if st.status == "installing" {
+ return Ok(serde_json::json!({"status": "already_installing"}));
+ }
+ st.status = "installing".to_string();
+ st.logs.clear();
+ st.logs.push("Setting up the local character trainer...".to_string());
+ }
+ info!("character trainer install start");
+
+ if let Some(p) = installPath.as_deref() {
+ if !p.trim().is_empty() {
+ write_config_value("trainer_root", p.trim());
+ }
+ }
+ let root = trainer_root(&app);
+ let python_bin = state.python_bin.lock().unwrap().clone();
+ if python_bin.is_empty() || !crate::python::is_real_python(&python_bin) {
+ set_status(
+ &state.trainer_install,
+ "error",
+ "No usable Python found. Install Python first (Settings), then retry.",
+ );
+ return Err("no_python".to_string());
+ }
+
+ let install = state.trainer_install.clone();
+ let cancel = state.trainer_cancel.clone();
+ let pid_slot = state.trainer_process.clone();
+ cancel.store(false, Ordering::SeqCst);
+
+ std::thread::spawn(move || {
+ let _ = fs::create_dir_all(root.join("models"));
+
+ // 1) pinned clone (releases are the project's own stability advice)
+ if !repo_dir(&root).join(".git").exists() {
+ set_status(&install, "installing", &format!("Step 1/4: Getting musubi tuner {MUSUBI_TAG}..."));
+ let mut clone = Command::new("git");
+ clone.args(["clone", "--branch", MUSUBI_TAG, "--depth", "1", MUSUBI_REPO])
+ .arg(repo_dir(&root));
+ if let Err(e) = run_streamed(clone, "git clone", &install, &cancel, &pid_slot) {
+ set_status(&install, if e == "cancelled" { "cancelled" } else { "error" }, &e);
+ return;
+ }
+ } else {
+ push_log(&install, "musubi tuner already present, keeping the pinned checkout.");
+ }
+
+ // 2) venv
+ if !venv_python(&root).exists() {
+ set_status(&install, "installing", "Step 2/4: Creating the training environment (venv)...");
+ let mut venv = Command::new(&python_bin);
+ venv.args(["-m", "venv"]).arg(root.join("venv"));
+ if let Err(e) = run_streamed(venv, "venv create", &install, &cancel, &pid_slot) {
+ set_status(&install, if e == "cancelled" { "cancelled" } else { "error" }, &e);
+ return;
+ }
+ }
+ let vpy = venv_python(&root).to_string_lossy().to_string();
+
+ // 3) torch (cu121 wheels run on every driver the app supports)
+ set_status(&install, "installing", "Step 3/4: Installing PyTorch into the trainer venv (~2.5 GB, one time)...");
+ let mut torch = Command::new(&vpy);
+ torch.args([
+ "-m", "pip", "install", "--progress-bar", "off", "--no-input",
+ "torch", "torchvision", "--index-url", "https://download.pytorch.org/whl/cu121",
+ ]);
+ if let Err(e) = run_streamed(torch, "torch install", &install, &cancel, &pid_slot) {
+ set_status(&install, if e == "cancelled" { "cancelled" } else { "error" }, &e);
+ return;
+ }
+
+ // 4) musubi + deps
+ set_status(&install, "installing", "Step 4/4: Installing the trainer package...");
+ let mut pkg = Command::new(&vpy);
+ pkg.args(["-m", "pip", "install", "--progress-bar", "off", "--no-input", "-e", "."])
+ .current_dir(repo_dir(&root));
+ if let Err(e) = run_streamed(pkg, "musubi install", &install, &cancel, &pid_slot) {
+ set_status(&install, if e == "cancelled" { "cancelled" } else { "error" }, &e);
+ return;
+ }
+
+ set_status(&install, "complete", "Trainer environment ready.");
+ });
+
+ Ok(serde_json::json!({"status": "installing"}))
+}
+
+#[tauri::command]
+pub fn character_trainer_status(
+ app: tauri::AppHandle,
+ state: State<'_, AppState>,
+) -> Result<serde_json::Value, String> {
+ let root = trainer_root(&app);
+ let comfy = active_comfy_dir(state.inner());
+ let env_ready = venv_python(&root).exists() && repo_dir(&root).join("src").exists();
+ let dit = resolve_base_file(&root, comfy.as_deref(), DIT_CANDIDATES, "diffusion_models");
+ let te = resolve_base_file(&root, comfy.as_deref(), TE_CANDIDATES, "text_encoders");
+ let vae = resolve_base_file(&root, comfy.as_deref(), VAE_CANDIDATES, "vae");
+ let install = state.trainer_install.lock().unwrap();
+ Ok(serde_json::json!({
+ "envReady": env_ready,
+ "basesReady": dit.is_some() && te.is_some() && vae.is_some(),
+ "dit": dit.map(|p| p.to_string_lossy().to_string()),
+ "textEncoder": te.map(|p| p.to_string_lossy().to_string()),
+ "vae": vae.map(|p| p.to_string_lossy().to_string()),
+ "root": root.to_string_lossy().to_string(),
+ "install": { "status": install.status, "logs": install.logs },
+ }))
+}
+
+// ── training-set staging ─────────────────────────────────────────────────────
+
+#[allow(non_snake_case)]
+#[tauri::command]
+pub fn stage_training_image(
+ app: tauri::AppHandle,
+ setId: String,
+ filename: String,
+ fileBytes: Vec<u8>,
+ caption: String,
+) -> Result<serde_json::Value, String> {
+ let set = sanitize_component(&setId);
+ let name = sanitize_component(filename.trim_end_matches(|c: char| c.is_ascii_alphanumeric()).trim_end_matches('.'));
+ if set.is_empty() {
+ return Err("invalid set id".to_string());
+ }
+ let ext = Path::new(&filename)
+ .extension()
+ .and_then(|e| e.to_str())
+ .unwrap_or("png")
+ .to_ascii_lowercase();
+ if !["png", "jpg", "jpeg", "webp"].contains(&ext.as_str()) {
+ return Err("unsupported image type (png, jpg, webp)".to_string());
+ }
+ if fileBytes.is_empty() || fileBytes.len() > 40 * 1024 * 1024 {
+ return Err("image is empty or larger than 40 MB".to_string());
+ }
+ let img_dir = trainer_root(&app).join("train").join(&set).join("img");
+ fs::create_dir_all(&img_dir).map_err(|e| format!("could not create the set dir: {e}"))?;
+ let stem = if name.is_empty() { format!("photo_{}", fileBytes.len() % 100000) } else { name };
+ fs::write(img_dir.join(format!("{stem}.{ext}")), &fileBytes)
+ .map_err(|e| format!("could not write the photo: {e}"))?;
+ // Caption sidecar: trigger word comes first — musubi has no trigger
+ // mechanism of its own, the token must live in every caption.
+ fs::write(img_dir.join(format!("{stem}.txt")), caption.trim())
+ .map_err(|e| format!("could not write the caption: {e}"))?;
+ Ok(serde_json::json!({"staged": format!("{stem}.{ext}")}))
+}
+
+#[allow(non_snake_case)]
+#[tauri::command]
+pub fn clear_training_set(app: tauri::AppHandle, setId: String) -> Result<(), String> {
+ let set = sanitize_component(&setId);
+ if set.is_empty() {
+ return Err("invalid set id".to_string());
+ }
+ let dir = trainer_root(&app).join("train").join(&set);
+ if dir.exists() {
+ fs::remove_dir_all(&dir).map_err(|e| format!("could not clear the set: {e}"))?;
+ }
+ Ok(())
+}
+
+// ── the training run ─────────────────────────────────────────────────────────
+
+#[allow(non_snake_case)]
+#[tauri::command]
+pub fn start_character_training(
+ app: tauri::AppHandle,
+ state: State<'_, AppState>,
+ setId: String,
+ name: String,
+ triggerWord: String,
+ steps: Option<u32>,
+) -> Result<serde_json::Value, String> {
+ {
+ let mut run = state.trainer_run.lock().unwrap();
+ if run.status == "running" {
+ return Ok(serde_json::json!({"status": "already_running"}));
+ }
+ run.status = "running".to_string();
+ run.logs.clear();
+ run.download_progress = 0;
+ run.download_total = 0;
+ run.logs.push("Preparing the training run...".to_string());
+ }
+ info!("character training start");
+
+ let set = sanitize_component(&setId);
+ let lora_name = sanitize_component(&name);
+ let trigger = sanitize_component(&triggerWord);
+ if set.is_empty() || lora_name.is_empty() || trigger.is_empty() {
+ set_status(&state.trainer_run, "error", "Set, name and trigger word are required.");
+ return Err("invalid arguments".to_string());
+ }
+ let steps = steps.unwrap_or(1200).clamp(100, 4000);
+
+ let root = trainer_root(&app);
+ let comfy = active_comfy_dir(state.inner());
+ let vpy = venv_python(&root);
+ if !vpy.exists() {
+ set_status(&state.trainer_run, "error", "Trainer environment is missing. Run the trainer install first.");
+ return Err("trainer_not_installed".to_string());
+ }
+ let (Some(dit), Some(te), Some(vae)) = (
+ resolve_base_file(&root, comfy.as_deref(), DIT_CANDIDATES, "diffusion_models"),
+ resolve_base_file(&root, comfy.as_deref(), TE_CANDIDATES, "text_encoders"),
+ resolve_base_file(&root, comfy.as_deref(), VAE_CANDIDATES, "vae"),
+ ) else {
+ set_status(
+ &state.trainer_run,
+ "error",
+ "The Z-Image training base files are missing (z_image_bf16 / qwen_3_4b / ae). Get them from the Model Manager, then train again.",
+ );
+ return Err("bases_missing".to_string());
+ };
+ let img_dir = root.join("train").join(&set).join("img");
+ let photo_count = fs::read_dir(&img_dir)
+ .map(|it| it.filter_map(Result::ok).filter(|e| {
+ e.path().extension().and_then(|x| x.to_str())
+ .map(|x| ["png", "jpg", "jpeg", "webp"].contains(&x.to_ascii_lowercase().as_str()))
+ .unwrap_or(false)
+ }).count())
+ .unwrap_or(0);
+ if photo_count < 4 {
+ set_status(&state.trainer_run, "error", "Need at least 4 staged photos to train.");
+ return Err("not_enough_photos".to_string());
+ }
+
+ // Copy the finished LoRA next to the other local LoRAs so the existing
+ // chain picks it up. Fall back to the trainer root when ComfyUI is absent.
+ let loras_dir = comfy
+ .as_deref()
+ .map(|c| c.join("models").join("loras"))
+ .unwrap_or_else(|| root.join("out"));
+
+ let run = state.trainer_run.clone();
+ let cancel = state.trainer_cancel.clone();
+ let pid_slot = state.trainer_process.clone();
+ cancel.store(false, Ordering::SeqCst);
+
+ std::thread::spawn(move || {
+ let set_dir = root.join("train").join(&set);
+ let cache_dir = set_dir.join("cache");
+ let out_dir = set_dir.join("out");
+ let _ = fs::create_dir_all(&cache_dir);
+ let _ = fs::create_dir_all(&out_dir);
+
+ // Repeats sized so photos x repeats x epochs lands near the step goal
+ // with batch 1 (steps/epoch = photos x repeats).
+ let repeats = (steps as usize / photo_count / 8).clamp(2, 40);
+ let toml = format!(
+ "[general]\nresolution = [768, 768]\ncaption_extension = \".txt\"\nbatch_size = 1\nenable_bucket = true\nbucket_no_upscale = false\n\n[[datasets]]\nimage_directory = '{}'\ncache_directory = '{}'\nnum_repeats = {}\n",
+ img_dir.to_string_lossy().replace('\\', "/"),
+ cache_dir.to_string_lossy().replace('\\', "/"),
+ repeats,
+ );
+ let toml_path = set_dir.join("dataset.toml");
+ if let Err(e) = fs::write(&toml_path, toml) {
+ set_status(&run, "error", &format!("could not write dataset config: {e}"));
+ return;
+ }
+
+ let vpy_s = vpy.to_string_lossy().to_string();
+ let repo = repo_dir(&root);
+ let toml_s = toml_path.to_string_lossy().to_string();
+ let dit_s = dit.to_string_lossy().to_string();
+ let te_s = te.to_string_lossy().to_string();
+ let vae_s = vae.to_string_lossy().to_string();
+
+ // 1) latent cache
+ set_status(&run, "running", "Step 1/4: Caching image latents...");
+ let mut c1 = Command::new(&vpy_s);
+ c1.current_dir(&repo).args([
+ "src/musubi_tuner/zimage_cache_latents.py",
+ "--dataset_config", &toml_s,
+ "--vae", &vae_s,
+ ]);
+ if let Err(e) = run_streamed(c1, "latent cache", &run, &cancel, &pid_slot) {
+ set_status(&run, if e == "cancelled" { "cancelled" } else { "error" }, &e);
+ return;
+ }
+
+ // 2) text-encoder cache (fp8 keeps the 4B Qwen TE inside 12 GB)
+ set_status(&run, "running", "Step 2/4: Caching text encoder outputs...");
+ let mut c2 = Command::new(&vpy_s);
+ c2.current_dir(&repo).args([
+ "src/musubi_tuner/zimage_cache_text_encoder_outputs.py",
+ "--dataset_config", &toml_s,
+ "--text_encoder", &te_s,
+ "--batch_size", "8",
+ "--fp8_llm",
+ ]);
+ if let Err(e) = run_streamed(c2, "text encoder cache", &run, &cancel, &pid_slot) {
+ set_status(&run, if e == "cancelled" { "cancelled" } else { "error" }, &e);
+ return;
+ }
+
+ // 3) the train itself — documented 12 GB combo: fp8 base + block swap
+ // + gradient checkpointing + 8-bit optimizer. ComfyUI's model cache
+ // would eat the same VRAM the trainer needs — ask it to let go first.
+ if crate::commands::process::free_comfyui_memory() {
+ push_log(&run, "Freed ComfyUI's cached models to make room for training.");
+ }
+ set_status(&run, "running", &format!("Step 3/4: Training ({steps} steps). This runs for a while, live log below..."));
+ let accelerate = {
+ #[cfg(target_os = "windows")]
+ { root.join("venv").join("Scripts").join("accelerate.exe") }
+ #[cfg(not(target_os = "windows"))]
+ { root.join("venv").join("bin").join("accelerate") }
+ };
+ let steps_s = steps.to_string();
+ let out_name = format!("char_{lora_name}_zimage");
+ let mut c3 = Command::new(accelerate);
+ c3.current_dir(&repo).args([
+ "launch", "--num_cpu_threads_per_process", "1", "--mixed_precision", "bf16",
+ "src/musubi_tuner/zimage_train_network.py",
+ "--dit", &dit_s,
+ "--vae", &vae_s,
+ "--text_encoder", &te_s,
+ "--dataset_config", &toml_s,
+ "--sdpa", "--mixed_precision", "bf16",
+ "--fp8_base", "--fp8_scaled",
+ "--blocks_to_swap", "16",
+ "--timestep_sampling", "shift", "--weighting_scheme", "none", "--discrete_flow_shift", "2.0",
+ "--optimizer_type", "adamw8bit", "--learning_rate", "1e-4", "--gradient_checkpointing",
+ "--max_data_loader_n_workers", "2", "--persistent_data_loader_workers",
+ "--network_module", "networks.lora_zimage", "--network_dim", "32",
+ "--max_train_steps", &steps_s,
+ "--save_precision", "bf16",
+ "--seed", "42",
+ "--output_dir", &out_dir.to_string_lossy(),
+ "--output_name", &out_name,
+ ]);
+ if let Err(e) = run_streamed(c3, "training", &run, &cancel, &pid_slot) {
+ set_status(&run, if e == "cancelled" { "cancelled" } else { "error" }, &e);
+ return;
+ }
+
+ // 4) convert to the Diffusers key layout ComfyUI loads, straight into
+ // the loras dir (musubi's documented `--target other` conversion).
+ set_status(&run, "running", "Step 4/4: Converting the LoRA for ComfyUI...");
+ let trained = out_dir.join(format!("{out_name}.safetensors"));
+ if !trained.exists() {
+ set_status(&run, "error", "Training finished but the LoRA file was not written.");
+ return;
+ }
+ let _ = fs::create_dir_all(&loras_dir);
+ let final_path = loras_dir.join(format!("{out_name}.safetensors"));
+ let mut c4 = Command::new(&vpy_s);
+ c4.current_dir(&repo).args([
+ "src/musubi_tuner/convert_lora.py",
+ "--input", &trained.to_string_lossy(),
+ "--output", &final_path.to_string_lossy(),
+ "--target", "other",
+ ]);
+ if let Err(e) = run_streamed(c4, "lora convert", &run, &cancel, &pid_slot) {
+ set_status(&run, if e == "cancelled" { "cancelled" } else { "error" }, &e);
+ return;
+ }
+
+ set_status(
+ &run,
+ "complete",
+ &format!(
+ "Character ready: {out_name}.safetensors is in your loras. Put '{trigger}' in a prompt on the Image tab with the LoRA active.",
+ ),
+ );
+ info!("character training complete");
+ });
+
+ Ok(serde_json::json!({"status": "running"}))
+}
+
+#[tauri::command]
+pub fn character_training_status(state: State<'_, AppState>) -> Result<serde_json::Value, String> {
+ let run = state.trainer_run.lock().unwrap();
+ Ok(serde_json::json!({
+ "status": run.status,
+ "logs": run.logs.iter().rev().take(30).rev().collect::<Vec<_>>(),
+ "step": run.download_progress,
+ "totalSteps": run.download_total,
+ }))
+}
+
+#[tauri::command]
+pub fn cancel_character_training(state: State<'_, AppState>) -> Result<(), String> {
+ state.trainer_cancel.store(true, Ordering::SeqCst);
+ // Kill the live child directly too — pip/accelerate ignore the flag.
+ if let Ok(slot) = state.trainer_process.lock() {
+ if let Some(pid) = *slot {
+ #[cfg(target_os = "windows")]
+ {
+ let mut kill = Command::new("taskkill");
+ kill.args(["/PID", &pid.to_string(), "/T", "/F"]);
+ kill.creation_flags(CREATE_NO_WINDOW);
+ let _ = kill.output();
+ }
+ #[cfg(not(target_os = "windows"))]
+ {
+ let _ = Command::new("kill").args(["-9", &pid.to_string()]).output();
+ }
+ }
+ }
+ Ok(())
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn step_counter_parses_tqdm_lines() {
+ assert_eq!(parse_step_counter("steps: 8%|▊| 123/1600 [02:10<26:04]"), Some((123, 1600)));
+ assert_eq!(parse_step_counter("epoch 1/16"), Some((1, 16)));
+ assert_eq!(parse_step_counter("no counter here"), None);
+ // version-ish fragments with tiny totals are ignored
+ assert_eq!(parse_step_counter("python 3/4 things"), None);
+ }
+
+ #[test]
+ fn sanitize_component_strips_path_syntax() {
+ assert_eq!(sanitize_component("../../evil"), "evil");
+ assert_eq!(sanitize_component("my char!"), "my_char");
+ assert_eq!(sanitize_component("lumi"), "lumi");
+ }
+}
diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs
index d8c9b91e..fe46964b 100644
--- a/src-tauri/src/main.rs
+++ b/src-tauri/src/main.rs
@@ -116,6 +116,7 @@ fn main() {
// Installation
commands::install::install_comfyui,
commands::install::install_comfyui_status,
+ commands::install::update_comfyui,
commands::install::cancel_comfyui_install,
commands::install::install_ollama,
commands::install::install_ollama_status,
@@ -136,6 +137,14 @@ fn main() {
commands::install::install_tts,
commands::install::install_tts_status,
commands::install::check_git_installed,
+ // Local character trainer (musubi-tuner)
+ commands::trainer::install_character_trainer,
+ commands::trainer::character_trainer_status,
+ commands::trainer::stage_training_image,
+ commands::trainer::clear_training_set,
+ commands::trainer::start_character_training,
+ commands::trainer::character_training_status,
+ commands::trainer::cancel_character_training,
// Whisper STT
commands::whisper::whisper_status,
commands::whisper::transcribe,
diff --git a/src-tauri/src/state.rs b/src-tauri/src/state.rs
index de433d30..384d389e 100644
--- a/src-tauri/src/state.rs
+++ b/src-tauri/src/state.rs
@@ -200,6 +200,15 @@ pub struct AppState {
/// Progress/log state for the in-app Piper neural-TTS installer (mirrors
/// `whisper_install`); `install_tts` writes it, `install_tts_status` reads it.
pub tts_install: Arc<Mutex<InstallState>>,
+ /// 2.5.8 local character trainer (musubi-tuner in its own venv, outside
+ /// the ComfyUI dir). `trainer_install` streams the one-time environment
+ /// setup; `trainer_run` streams an actual training run (status
+ /// idle/running/complete/error, logs, step counters in
+ /// download_progress/total). Commands live in `commands::trainer`.
+ pub trainer_install: Arc<Mutex<InstallState>>,
+ pub trainer_run: Arc<Mutex<InstallState>>,
+ pub trainer_cancel: Arc<AtomicBool>,
+ pub trainer_process: Arc<Mutex<Option<u32>>>,
pub searxng_install: Mutex<InstallState>,
pub searxng_available: AtomicBool,
/// Resolved Python binary path. Empty string means "no real Python on
@@ -300,6 +309,10 @@ impl AppState {
python_install: Arc::new(Mutex::new(InstallState::default())),
whisper_install: Arc::new(Mutex::new(InstallState::default())),
tts_install: Arc::new(Mutex::new(InstallState::default())),
+ trainer_install: Arc::new(Mutex::new(InstallState::default())),
+ trainer_run: Arc::new(Mutex::new(InstallState::default())),
+ trainer_cancel: Arc::new(AtomicBool::new(false)),
+ trainer_process: Arc::new(Mutex::new(None)),
searxng_install: Mutex::new(InstallState::default()),
searxng_available: AtomicBool::new(false),
python_bin: Arc::new(Mutex::new(python_bin)),
diff --git a/src/api/trainer.ts b/src/api/trainer.ts
new file mode 100644
index 00000000..8799d3ab
--- /dev/null
+++ b/src/api/trainer.ts
@@ -0,0 +1,102 @@
+// 2.5.8 — typed wrappers for the local character trainer (musubi-tuner).
+// The Rust side (src-tauri/src/commands/trainer.rs) owns the pinned repo,
+// the dedicated venv and the whole train -> convert -> loras/ pipeline; this
+// module is only the IPC surface the Create page talks to.
+
+import { backendCall } from './backend'
+
+export interface TrainerInstallState {
+ status: string
+ logs: string[]
+}
+
+export interface TrainerStatus {
+ envReady: boolean
+ basesReady: boolean
+ dit: string | null
+ textEncoder: string | null
+ vae: string | null
+ root: string
+ install: TrainerInstallState
+}
+
+export interface TrainingRunStatus {
+ status: 'idle' | 'running' | 'complete' | 'error' | 'cancelled' | string
+ logs: string[]
+ step: number
+ totalSteps: number
+}
+
+/** The Z-Image training bases the trainer resolves by exact filename.
+ * Subfolders match the ComfyUI models tree so the regular download
+ * pipeline (download_model) drops them where both sides find them. */
+export const TRAINER_BASE_FILES = [
+ {
+ filename: 'z_image_bf16.safetensors',
+ subfolder: 'diffusion_models',
+ url: 'https://huggingface.co/Comfy-Org/z_image/resolve/main/split_files/diffusion_models/z_image_bf16.safetensors',
+ label: 'Z Image base model',
+ },
+ {
+ filename: 'qwen_3_4b.safetensors',
+ subfolder: 'text_encoders',
+ url: 'https://huggingface.co/Comfy-Org/z_image/resolve/main/split_files/text_encoders/qwen_3_4b.safetensors',
+ label: 'Text encoder',
+ },
+ {
+ filename: 'ae.safetensors',
+ subfolder: 'vae',
+ url: 'https://huggingface.co/Comfy-Org/z_image/resolve/main/split_files/vae/ae.safetensors',
+ label: 'VAE',
+ },
+] as const
+
+export async function installCharacterTrainer(installPath?: string): Promise<{ status: string }> {
+ return backendCall('install_character_trainer', { installPath: installPath ?? null })
+}
+
+export async function characterTrainerStatus(): Promise<TrainerStatus> {
+ return backendCall('character_trainer_status')
+}
+
+export async function stageTrainingImage(
+ setId: string,
+ filename: string,
+ fileBytes: number[],
+ caption: string,
+): Promise<{ staged: string }> {
+ return backendCall('stage_training_image', { setId, filename, fileBytes, caption })
+}
+
+export async function clearTrainingSet(setId: string): Promise<void> {
+ await backendCall('clear_training_set', { setId })
+}
+
+export async function startCharacterTraining(
+ setId: string,
+ name: string,
+ triggerWord: string,
+ steps?: number,
+): Promise<{ status: string }> {
+ return backendCall('start_character_training', { setId, name, triggerWord, steps: steps ?? null })
+}
+
+export async function characterTrainingStatus(): Promise<TrainingRunStatus> {
+ return backendCall('character_training_status')
+}
+
+export async function cancelCharacterTraining(): Promise<void> {
+ await backendCall('cancel_character_training')
+}
+
+/** Local character LoRAs are recognized by the trainer's own naming
+ * (`char_<name>_zimage.safetensors`); the trigger word IS the name part —
+ * start_character_training writes it that way on purpose so the Use shelf
+ * can recover it without a sidecar database. */
+export function parseLocalCharacterLora(file: string): { file: string; trigger: string } | null {
+ // ComfyUI lists loras with their subfolder prefix — keep the full enum
+ // string as `file` (LoraLoader needs it verbatim), match on the basename.
+ const m = /(?:^|[\\/])char_(.+)_zimage\.safetensors$/i.exec(file.trim())
+ if (!m) return null
+ return { file: file.trim(), trigger: m[1] }
+}