Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

39.2 KB
Raw Blame
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

"""Export API routes: checkpoint discovery and model export operations."""

from core.training.account_jobs import (
    account_event_stream,
    account_path,
    job_busy,
    job_is_foreign,
    require_job_owner,
    validate_job_paths,
)
import asyncio
import json
import os
import sys
import time
from pathlib import Path
from typing import Any, AsyncGenerator, Dict, List, Optional, Tuple

from fastapi import APIRouter, Depends, Header, HTTPException, Query, Request
from fastapi.responses import StreamingResponse
import structlog
from loggers import get_logger

backend_path = Path(__file__).parent.parent.parent
if str(backend_path) not in sys.path:
    sys.path.insert(0, str(backend_path))

from auth.authentication import allow_ambient_hf_token, get_current_subject
from hub.services.models import account_access
from hub.utils.hf_tokens import HfTokenArg, hf_token_arg

from utils.security.consent import managed_remote_code_refused
from utils.utils import safe_error_detail

try:
    from core.export import get_export_backend
except ImportError:
    parent_backend = backend_path.parent / "backend"
    if str(parent_backend) not in sys.path:
        sys.path.insert(0, str(parent_backend))
    from core.export import get_export_backend

from models import (
    LoadCheckpointRequest,
    ExportCancelRequest,
    ExportStatusResponse,
    ExportOperationResponse,
    ExportMergedModelRequest,
    ExportBaseModelRequest,
    ExportGGUFRequest,
    ExportLoRAAdapterRequest,
    ConvertQ4NXRequest,
    ExportDiffusionLoRARequest,
    LlmCompressorExportProbeResponse,
    ExportDecisionInfoResponse,
    MoeInfoResponse,
)

router = APIRouter()
logger = get_logger(__name__)


async def _ensure_export_supported() -> None:
    """Reject mutating exports server-side (400 if unsupported; 409 during a .venv_t5_latest install, else half-swapped sidecar). Read-only endpoints stay ungated."""
    from utils.transformers_latest import is_install_in_progress

    if is_install_in_progress():
        raise HTTPException(
            status_code = 409,
            detail = "A transformers installation is in progress. Retry when it completes.",
        )

    from utils.hardware import export_capability

    # Off-loop: detection is deferred past bind, so the first call can wait on a cold import.
    cap = await asyncio.to_thread(export_capability)
    if not cap.get("export_supported", True):
        raise HTTPException(
            status_code = 400,
            detail = cap.get("export_unsupported_message")
            or "Export is not supported on this platform.",
        )


def _resolve_export_hf_token(
    raw_token: Optional[str],
    *,
    push_to_hub: bool = False,
    allow_ambient: bool = True,
) -> HfTokenArg:
    """The credential this export runs under, as the anonymous-aware sentinel. ``None`` reads
    downstream as "go and find a credential" (``if token is None: get_token()``), so a caller denied
    the ambient token is spelled ``False``."""
    token = raw_token.strip() if isinstance(raw_token, str) and raw_token.strip() else None
    if push_to_hub and token is None and not allow_ambient:
        raise HTTPException(
            status_code = 400,
            detail = "Hugging Face token is required to push to Hub when authenticated via API key.",
        )
    # An empty token pushes with the operator's own login (hf auth login), never a managed account's.
    if push_to_hub and token is None and account_access.managed_account():
        return False
    return hf_token_arg(token, allow_ambient_token = allow_ambient)


def _authorized_adapter_base(checkpoint_path: str) -> Optional[str]:
    """The adapter's config names the base the worker loads, so it is authorized like the checkpoint."""
    if not account_access.managed_account():
        return None
    from utils.models import get_base_model_from_lora
    from core.export.decision import adapter_load_bases

    bases = adapter_load_bases(checkpoint_path)
    base = get_base_model_from_lora(checkpoint_path) or next(iter(bases), None)
    # The decision loader prefers its own config's base, so every base named is checked.
    for name in dict.fromkeys([base, *bases]):
        if name:
            account_access.require_model_access(name)
    return base or None


@router.post("/load-checkpoint", response_model = ExportOperationResponse)
async def load_checkpoint(
    request: LoadCheckpointRequest,
    current_subject: str = Depends(get_current_subject),
    allow_ambient: bool = Depends(allow_ambient_hf_token),
):
    """Load a checkpoint into the export backend (ExportBackend.load_checkpoint).

    Export runs in its own subprocess and is allowed to run in parallel with
    training and inference. We deliberately do NOT stop training or unload the
    chat model here -- if the GPU runs out of memory the load/export fails with
    a clear error instead of tearing down the user's other running workloads.
    """
    validate_job_paths(request.model_dump())
    base_model = await asyncio.to_thread(_authorized_adapter_base, request.checkpoint_path)
    try:
        await _ensure_export_supported()
        export_hf_token = _resolve_export_hf_token(request.hf_token, allow_ambient = allow_ambient)
        # Unset lets the backend pick 16-bit for a full fine-tune.
        load_in_4bit = request.load_in_4bit if "load_in_4bit" in request.model_fields_set else None
        backend = get_export_backend()
        # Worker thread: it waits on a subprocess that can take minutes, so the loop stays free for SSE.
        success, message = await asyncio.to_thread(
            backend.load_checkpoint,
            checkpoint_path = request.checkpoint_path,
            base_model = base_model,
            max_seq_length = request.max_seq_length,
            load_in_4bit = load_in_4bit,
            offload_layers = request.offload_layers,
            # Downgraded, not refused: the Export tab always sends true for local models.
            trust_remote_code = request.trust_remote_code and not managed_remote_code_refused(),
            approved_remote_code_fingerprint = request.approved_remote_code_fingerprint,
            hf_token = export_hf_token,
            # A supplied token cannot say whether it came from a session or an API key.
            allow_ambient = allow_ambient,
            subject = current_subject,
            op_id = request.op_id,
        )

        if not success:
            raise HTTPException(status_code = 400, detail = message)

        return ExportOperationResponse(success = True, message = message)
    except HTTPException:
        raise
    except Exception as e:
        from utils.transformers_version import SidecarSwapInProgress

        if isinstance(e, SidecarSwapInProgress):
            # Expected loss of the race against a sidecar install: retryable 409.
            raise HTTPException(status_code = 409, detail = str(e))
        logger.error(f"Error loading checkpoint: {e}", exc_info = True)
        raise HTTPException(
            status_code = 500,
            detail = "Failed to load checkpoint",
        )


@router.post("/cleanup", response_model = ExportOperationResponse)
async def cleanup_export_memory(current_subject: str = Depends(get_current_subject)):
    """Cleanup export-related models from memory (ExportBackend.cleanup_memory)."""
    try:
        backend = get_export_backend()
        success = await asyncio.to_thread(backend.cleanup_memory)

        if not success:
            raise HTTPException(
                status_code = 500,
                detail = "Memory cleanup failed. See server logs for details.",
            )

        return ExportOperationResponse(
            success = True,
            message = "Memory cleanup completed successfully",
        )
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"Error during export memory cleanup: {e}", exc_info = True)
        raise HTTPException(
            status_code = 500,
            detail = "Failed to cleanup export memory",
        )


@router.post("/cancel", response_model = ExportOperationResponse)
async def cancel_export(
    body: Optional[ExportCancelRequest] = None, current_subject: str = Depends(get_current_subject)
):
    """Cancel the in-flight export by terminating its worker subprocess.

    Only the export subprocess is killed; training and inference run in their
    own subprocesses and keep going.
    """
    require_job_owner(get_export_backend())
    try:
        backend = get_export_backend()
        expected = body.expected_op_id if body is not None else None
        if expected is None:
            cancelled = await asyncio.to_thread(backend.cancel_export)
        else:
            cancelled = await asyncio.to_thread(backend.cancel_export, expected_op_id = expected)
        return ExportOperationResponse(
            success = True,
            message = "Export cancelled" if cancelled else "No active export to cancel",
        )
    except Exception as e:
        logger.error(f"Error cancelling export: {e}", exc_info = True)
        raise HTTPException(
            status_code = 500,
            detail = "Failed to cancel export",
        )


@router.get("/status", response_model = ExportStatusResponse)
async def get_export_status(current_subject: str = Depends(get_current_subject)):
    """Get export backend status (loaded checkpoint, model type, PEFT flag)."""
    if job_is_foreign(get_export_backend()):
        return ExportStatusResponse(
            current_checkpoint = None,
            is_vision = False,
            is_peft = False,
            is_export_active = bool(get_export_backend().is_export_active()),
            active_op_kind = "busy" if job_busy(get_export_backend()) else None,
        )
    try:
        backend = get_export_backend()
        last_op = backend.get_last_op()
        # Relativise the recovered path like the per-op POST response, so both routes show the same path.
        last_op_output_path = None
        if last_op and last_op.get("output_path"):
            if getattr(backend, "decision", None):
                last_op_output_path = last_op["output_path"]
            else:
                details = await asyncio.to_thread(_export_details, last_op["output_path"])
                last_op_output_path = (details or {}).get("output_path")
        return ExportStatusResponse(
            current_checkpoint = backend.current_checkpoint,
            is_vision = bool(getattr(backend, "is_vision", False)),
            is_peft = bool(getattr(backend, "is_peft", False)),
            is_export_active = bool(backend.is_export_active()),
            active_op_kind = backend.get_active_op_kind(),
            last_op_seq = int(last_op["seq"]) if last_op else 0,
            last_op_kind = last_op.get("kind") if last_op else None,
            last_op_status = last_op.get("status") if last_op else None,
            last_op_output_path = last_op_output_path,
            last_op_error = last_op.get("error") if last_op else None,
            decision = getattr(backend, "decision", None),
        )
    except Exception as e:
        logger.error(f"Error getting export status: {e}", exc_info = True)
        raise HTTPException(
            status_code = 500,
            detail = "Failed to get export status",
        )


@router.get("/logs")
async def get_export_logs(
    since: Optional[int] = Query(
        None,
        description = "Return log entries with seq strictly greater than this cursor.",
    ),
    current_subject: str = Depends(get_current_subject),
):
    """Tunnel-safe JSON fallback for the live export log stream.

    The SSE endpoint (`/logs/stream`) is the low-latency path, but some reverse
    proxies -- notably Cloudflare quick tunnels (`*.trycloudflare.com`) used by
    `--secure` mode -- buffer streamed GET responses until the stream closes.
    This endpoint returns the same ring-buffer lines as a short, complete JSON
    response that no proxy buffers, so the frontend can poll it and still show
    logs in near real time even where the stream itself does not arrive.

    Shares the orchestrator's monotonic `seq` cursor with the SSE stream, so the
    two transports can run together and the client de-dupes by seq.
    """
    try:
        backend = get_export_backend()
        # First poll starts at the run-start snapshot, not the whole ring buffer.
        if since is None:
            cursor = backend.get_run_start_seq()
        else:
            cursor = max(0, int(since))

        entries, new_cursor = backend.get_logs_since(cursor)
        return {
            "entries": [
                {
                    "seq": int(entry.get("seq", 0)),
                    "stream": entry.get("stream", "stdout"),
                    "line": entry.get("line", ""),
                    "ts": entry.get("ts"),
                }
                for entry in entries
            ],
            "cursor": new_cursor,
            "active": bool(backend.is_export_active()),
        }
    except Exception as e:
        logger.error(f"Error getting export logs: {e}", exc_info = True)
        raise HTTPException(
            status_code = 500,
            detail = "Failed to get export logs",
        )


def _try_register_external_export(
    path: Path, *, refresh_index: bool = False
) -> tuple[bool, Optional[str]]:
    """Best-effort registration so absolute exports show up in local scans."""
    try:
        from storage.studio_db import add_scan_folder_with_status

        folder, inserted = add_scan_folder_with_status(str(path))
        if inserted or refresh_index:
            from core.inference.local_model_resolver import invalidate_index, warm_index_soon
            invalidate_index()
            warm_index_soon()
        return True, str(folder.get("path") or path)
    except Exception as exc:
        logger.warning("Could not register export scan folder %s: %s", path, exc)
        return False, None


def _export_details(
    output_path: Optional[str], *, refresh_index: bool = False
) -> Optional[Dict[str, Any]]:
    """Return relative export paths, keeping external absolute paths visible."""
    if not output_path:
        return None
    try:
        from utils.paths.storage_roots import exports_root

        path = Path(output_path)
        # Outside exports_root, so return the full absolute path and users can find their files on another drive.
        if path.is_absolute():
            try:
                path.resolve().relative_to(exports_root().resolve())
            except ValueError:
                registered, registered_path = _try_register_external_export(
                    path, refresh_index = refresh_index
                )
                return {
                    "output_path": str(path),
                    "scan_folder_registered": registered,
                    "scan_folder_path": registered_path,
                }
        rel = os.path.relpath(output_path, exports_root())
        return {"output_path": rel}
    except Exception:
        return {"output_path": output_path}


def _with_drafter(
    details: Optional[Dict[str, Any]], output_path: Optional[str]
) -> Optional[Dict[str, Any]]:
    """Adds the trained drafter an export carries (drafter.json), when there is one."""
    try:
        from core.export.drafter_export import export_drafter_details
        drafter = export_drafter_details(output_path)
    except Exception:
        drafter = None
    if drafter is None or details is None:
        return details
    return {**details, "drafter": drafter}


def _decision_export_details(output_path: Optional[str]) -> Optional[Dict[str, Any]]:
    """A decision GGUF lands in the run folder's gguf/; not registered as a chat model folder."""
    if not output_path:
        return None
    from core.export.decision import read_existing_export

    # output_path is <run folder>/gguf; the contract reads from the run folder.
    export = read_existing_export(Path(output_path).parent)
    return {
        "output_path": str(output_path),
        "decision_export": export,
        "quantizations": (export or {}).get("quantizations") or [],
    }


@router.get("/decision-info", response_model = ExportDecisionInfoResponse)
async def get_decision_export_info(
    checkpoint_path: str = Query(..., description = "Local checkpoint folder"),
    current_subject: str = Depends(get_current_subject),
    allow_ambient: bool = Depends(allow_ambient_hf_token),
):
    """GGUF export options for a decision model checkpoint (Clef / Laya); decision is null otherwise."""
    validate_job_paths({"checkpoint_path": checkpoint_path})
    from core.export.decision import decision_preview

    try:
        decision = await asyncio.to_thread(decision_preview, checkpoint_path)
    except Exception as e:
        logger.warning(f"Could not inspect {checkpoint_path} for decision export: {e}")
        decision = None
    from utils.models.diffusion_lm import diffusion_lm_export_limits

    diffusion_lm = (
        None
        if decision is not None
        else await asyncio.to_thread(
            diffusion_lm_export_limits,
            checkpoint_path,
            hf_token_arg(None, allow_ambient_token = allow_ambient),
        )
    )
    return ExportDecisionInfoResponse(
        checkpoint_path = checkpoint_path, decision = decision, diffusion_lm = diffusion_lm
    )


@router.get("/moe-info", response_model = MoeInfoResponse)
async def get_moe_info(
    model: str = Query(
        ..., max_length = 4096, description = "Local checkpoint / model folder or Hub id"
    ),
    hf_token: Optional[str] = Header(None, alias = "X-HF-Token"),
    current_subject: str = Depends(get_current_subject),
    allow_ambient: bool = Depends(allow_ambient_hf_token),
):
    """Routed-expert counts for the REAP option on the export page; null counts for a dense model.
    The token (gated repos) comes in the X-HF-Token header, as for /api/models/export-size."""
    from core.export.reap import (
        MoeProbeError,
        hub_adapter_has_mtp_head,
        hub_file_exists,
        moe_export_info,
    )
    from routes.models import _normalize_hf_token, _require_model_access_or_caller_token
    from utils.paths import is_local_path

    local = is_local_path(model)
    token = _normalize_hf_token(hf_token)
    adapter_base = None
    if local:
        validate_job_paths({"checkpoint_path": model})
        # The probe follows a LoRA folder to its base, so that base is authorized as a load is.
        adapter_base = await asyncio.to_thread(_authorized_adapter_base, model)
        if adapter_base:
            validate_job_paths({"base_model": adapter_base, "hf_token": token})
    else:
        await _require_model_access_or_caller_token(model, token)
    probe_token = token or hf_token_arg(None, allow_ambient_token = allow_ambient)
    # The export loader follows a Hub repo's adapter_config.json even when it also ships a
    # config.json, so an adapter repo is probed through its base (and its MTP head) like a local one.
    try:
        hub_adapter = not local and await asyncio.to_thread(
            hub_file_exists, model, "adapter_config.json", probe_token
        )
        # A local folder never reaches the network; a Hub id reads only its config.json.
        info = (
            None
            if hub_adapter
            else await asyncio.to_thread(
                moe_export_info,
                model,
                probe_token,
                local,
                adapter_base = adapter_base,
            )
        )
        if info is None and not local:
            # A Hub LoRA repo publishes adapter_config.json, not config.json: probe its base, which is
            # authorized first like the adapter.
            from utils.models.model_config import get_base_model_from_lora_identifier

            base = await asyncio.to_thread(get_base_model_from_lora_identifier, model, probe_token)
            if hub_adapter and not base:
                # A confirmed adapter whose base could not be read (a failed download after its
                # retries): unknown, not dense.
                raise MoeProbeError(f"Could not resolve the base model of the adapter '{model}'")
            if (
                base
                and base != model
                and await asyncio.to_thread(hub_adapter_has_mtp_head, model, probe_token)
            ):
                # Unsloth's loader re-attaches this adapter's trained MTP head, which REAP refuses.
                base = None
            if base and base != model:
                base_local = is_local_path(base)
                if base_local:
                    validate_job_paths({"base_model": base, "hf_token": token})
                else:
                    await _require_model_access_or_caller_token(base, token)
                info = await asyncio.to_thread(moe_export_info, base, probe_token, base_local)
    except MoeProbeError as exc:
        # Unknown is not dense: the page keeps a REAP export blocked and offers a retry.
        raise HTTPException(status_code = 503, detail = str(exc)) from exc
    return MoeInfoResponse(model = model, **(info or {}))


@router.post("/export/merged", response_model = ExportOperationResponse)
async def export_merged_model(
    request: ExportMergedModelRequest,
    current_subject: str = Depends(get_current_subject),
    allow_ambient: bool = Depends(allow_ambient_hf_token),
):
    """Export a merged PEFT model (16-bit or 4-bit), optionally pushing to Hub.

    Wraps ExportBackend.export_merged_model.
    """
    validate_job_paths(request.model_dump())
    if request.reap and request.reap.dataset:
        validate_job_paths({"hf_dataset": request.reap.dataset, "hf_token": request.hf_token})
    try:
        await _ensure_export_supported()
        backend = get_export_backend()
        success, message, output_path = await asyncio.to_thread(
            backend.export_merged_model,
            save_directory = request.save_directory,
            format_type = request.format_type,
            push_to_hub = request.push_to_hub,
            repo_id = request.repo_id,
            hf_token = _resolve_export_hf_token(
                request.hf_token,
                push_to_hub = request.push_to_hub,
                allow_ambient = allow_ambient,
            ),
            private = request.private,
            compressed_method = request.compressed_method,
            install_missing_dependencies = request.install_missing_dependencies,
            expected_checkpoint = request.expected_checkpoint,
            op_id = request.op_id,
            reap = request.reap.model_dump() if request.reap else None,
        )

        if not success:
            raise HTTPException(status_code = 400, detail = message)

        return ExportOperationResponse(
            success = True,
            message = message,
            details = _with_drafter(
                await asyncio.to_thread(_export_details, output_path, refresh_index = True),
                output_path,
            ),
        )
    except HTTPException:
        raise
    except Exception as e:
        from utils.transformers_version import SidecarSwapInProgress

        if isinstance(e, SidecarSwapInProgress):
            raise HTTPException(status_code = 409, detail = str(e))
        logger.error(f"Error exporting merged model: {e}", exc_info = True)
        raise HTTPException(
            status_code = 500,
            detail = "Failed to export merged model",
        )


@router.get("/llm-compressor-probe", response_model = LlmCompressorExportProbeResponse)
async def probe_llm_compressor_export(current_subject: str = Depends(get_current_subject)):
    del current_subject
    from utils.llm_compressor_export import probe_llm_compressor_for_compressed_export
    return LlmCompressorExportProbeResponse(
        **probe_llm_compressor_for_compressed_export(),
    )


@router.post("/export/base", response_model = ExportOperationResponse)
async def export_base_model(
    request: ExportBaseModelRequest,
    current_subject: str = Depends(get_current_subject),
    allow_ambient: bool = Depends(allow_ambient_hf_token),
):
    """Export a non-PEFT base model, optionally pushing to Hub.

    Wraps ExportBackend.export_base_model.
    """
    validate_job_paths(request.model_dump())
    if request.reap and request.reap.dataset:
        validate_job_paths({"hf_dataset": request.reap.dataset, "hf_token": request.hf_token})
    try:
        await _ensure_export_supported()
        backend = get_export_backend()
        success, message, output_path = await asyncio.to_thread(
            backend.export_base_model,
            save_directory = request.save_directory,
            push_to_hub = request.push_to_hub,
            repo_id = request.repo_id,
            hf_token = _resolve_export_hf_token(
                request.hf_token,
                push_to_hub = request.push_to_hub,
                allow_ambient = allow_ambient,
            ),
            private = request.private,
            base_model_id = request.base_model_id,
            expected_checkpoint = request.expected_checkpoint,
            op_id = request.op_id,
            reap = request.reap.model_dump() if request.reap else None,
        )

        if not success:
            raise HTTPException(status_code = 400, detail = message)

        return ExportOperationResponse(
            success = True,
            message = message,
            details = _with_drafter(
                await asyncio.to_thread(_export_details, output_path, refresh_index = True),
                output_path,
            ),
        )
    except HTTPException:
        raise
    except Exception as e:
        from utils.transformers_version import SidecarSwapInProgress

        if isinstance(e, SidecarSwapInProgress):
            raise HTTPException(status_code = 409, detail = str(e))
        logger.error(f"Error exporting base model: {e}", exc_info = True)
        raise HTTPException(
            status_code = 500,
            detail = "Failed to export base model",
        )


@router.post("/export/gguf", response_model = ExportOperationResponse)
async def export_gguf(
    request: ExportGGUFRequest,
    current_subject: str = Depends(get_current_subject),
    allow_ambient: bool = Depends(allow_ambient_hf_token),
):
    """Export the current model to GGUF format, optionally pushing to Hub.

    Wraps ExportBackend.export_gguf.
    """
    validate_job_paths(request.model_dump())
    if request.reap and request.reap.dataset:
        validate_job_paths({"hf_dataset": request.reap.dataset, "hf_token": request.hf_token})
    try:
        await _ensure_export_supported()
        backend = get_export_backend()
        # A custom path wins; otherwise the imatrix toggle requests the upstream auto-download.
        imatrix_file = request.imatrix_path or (True if request.imatrix else None)
        success, message, output_path = await asyncio.to_thread(
            backend.export_gguf,
            save_directory = request.save_directory,
            quantization_method = request.quantization_method,
            push_to_hub = request.push_to_hub,
            repo_id = request.repo_id,
            hf_token = _resolve_export_hf_token(
                request.hf_token,
                push_to_hub = request.push_to_hub,
                allow_ambient = allow_ambient,
            ),
            imatrix_file = imatrix_file,
            private = request.private,
            npu_q4nx = request.npu_q4nx,
            expected_checkpoint = request.expected_checkpoint,
            op_id = request.op_id,
            reap = request.reap.model_dump() if request.reap else None,
        )

        if not success:
            raise HTTPException(status_code = 400, detail = message)

        if getattr(backend, "decision", None):
            details = await asyncio.to_thread(_decision_export_details, output_path)
        else:
            details = _with_drafter(
                await asyncio.to_thread(_export_details, output_path, refresh_index = True),
                output_path,
            )
        return ExportOperationResponse(success = True, message = message, details = details)
    except HTTPException:
        raise
    except Exception as e:
        from utils.transformers_version import SidecarSwapInProgress

        if isinstance(e, SidecarSwapInProgress):
            raise HTTPException(status_code = 409, detail = str(e))
        logger.error(f"Error exporting GGUF model: {e}", exc_info = True)
        raise HTTPException(
            status_code = 500,
            detail = "Failed to export GGUF model",
        )


@router.post("/convert/q4nx", response_model = ExportOperationResponse)
async def convert_q4nx(
    request: ConvertQ4NXRequest,
    current_subject: str = Depends(get_current_subject),
    allow_ambient: bool = Depends(allow_ambient_hf_token),
):
    """Convert an existing GGUF (local file or Hub repo) to FastFlowLM Q4NX for the AMD NPU.

    Needs no loaded checkpoint or GPU, so it runs here rather than in the export worker.
    """
    validate_job_paths(request.model_dump())
    # A managed account may only read a GGUF inside its own workspace.
    account_path(request.gguf_path)
    if bool(request.gguf_path) == bool(request.repo_id and request.filename):
        raise HTTPException(
            status_code = 400,
            detail = "Give either a local gguf_path or a repo_id with a filename.",
        )
    token = _resolve_export_hf_token(request.hf_token, allow_ambient = allow_ambient)
    base_model = request.base_model.strip()
    # The shared HF cache can hold another account's private repo: check this caller's access.
    hub_repos = [request.repo_id] if request.repo_id else []
    if not Path(base_model).expanduser().is_dir():
        hub_repos.append(base_model)
    for repo in hub_repos:
        await asyncio.to_thread(account_access.authorize_download, repo, "model", token)

    def run() -> str:
        from core.export import q4nx
        from utils.paths import resolve_export_write_dir

        q4nx.require_converter_deps()
        if request.gguf_path:
            source = Path(request.gguf_path)
            if not source.is_file() or source.suffix.lower() != ".gguf":
                raise ValueError(f"{source} is not a .gguf file.")
        else:
            from huggingface_hub import hf_hub_download
            source = Path(hf_hub_download(request.repo_id, request.filename, token = token))
        out = q4nx.convert_existing_gguf(
            source,
            base_model,
            Path(resolve_export_write_dir(request.save_directory)),
            token = token,
        )
        return str(out.resolve())

    try:
        output_path = await asyncio.to_thread(run)
    except Exception as e:
        logger.error(f"Q4NX conversion failed: {e}", exc_info = True)
        raise HTTPException(status_code = 400, detail = f"Q4NX conversion failed: {e}")
    return ExportOperationResponse(
        success = True,
        message = "Converted to Q4NX for the AMD NPU",
        details = await asyncio.to_thread(_export_details, output_path, refresh_index = True),
    )


@router.post("/export/diffusion-lora", response_model = ExportOperationResponse)
async def export_diffusion_lora(
    request: ExportDiffusionLoRARequest, current_subject: str = Depends(get_current_subject)
):
    """Save a copy of an image-generation LoRA (weights plus metadata sidecar) to a folder.

    A plain file copy out of the Images LoRA catalog: no loaded model, GPU or export worker.
    """
    validate_job_paths(request.model_dump())

    def run() -> str:
        from core.inference.diffusion_lora import export_local_lora
        from utils.paths import resolve_export_write_dir

        out = export_local_lora(
            request.lora_id, Path(resolve_export_write_dir(request.save_directory))
        )
        return str(out.resolve())

    try:
        output_path = await asyncio.to_thread(run)
    except FileNotFoundError as e:
        raise HTTPException(status_code = 404, detail = str(e))
    except ValueError as e:
        raise HTTPException(status_code = 400, detail = str(e))
    except OSError as e:
        logger.error(f"Image LoRA export failed: {e}", exc_info = True)
        raise HTTPException(status_code = 500, detail = "Failed to export image LoRA")

    # Not routed through _export_details: a LoRA folder is not a chat model scan folder.
    from utils.paths.storage_roots import exports_root

    try:
        shown = os.path.relpath(output_path, exports_root().resolve())
        if shown.startswith(".."):
            shown = output_path
    except ValueError:
        shown = output_path
    return ExportOperationResponse(
        success = True,
        message = "Image LoRA exported",
        details = {"output_path": shown},
    )


@router.post("/export/lora", response_model = ExportOperationResponse)
async def export_lora_adapter(
    request: ExportLoRAAdapterRequest,
    current_subject: str = Depends(get_current_subject),
    allow_ambient: bool = Depends(allow_ambient_hf_token),
):
    """Export only the LoRA adapter (if the loaded model is PEFT).

    Wraps ExportBackend.export_lora_adapter.
    """
    validate_job_paths(request.model_dump())
    try:
        await _ensure_export_supported()
        backend = get_export_backend()
        success, message, output_path = await asyncio.to_thread(
            backend.export_lora_adapter,
            save_directory = request.save_directory,
            push_to_hub = request.push_to_hub,
            repo_id = request.repo_id,
            hf_token = _resolve_export_hf_token(
                request.hf_token,
                push_to_hub = request.push_to_hub,
                allow_ambient = allow_ambient,
            ),
            private = request.private,
            gguf = request.gguf,
            gguf_outtype = request.gguf_outtype,
            adapter_format = request.adapter_format,
            expected_checkpoint = request.expected_checkpoint,
            op_id = request.op_id,
        )

        if not success:
            raise HTTPException(status_code = 400, detail = message)

        return ExportOperationResponse(
            success = True,
            message = message,
            details = _with_drafter(
                await asyncio.to_thread(_export_details, output_path, refresh_index = True),
                output_path,
            ),
        )
    except HTTPException:
        raise
    except Exception as e:
        from utils.transformers_version import SidecarSwapInProgress

        if isinstance(e, SidecarSwapInProgress):
            raise HTTPException(status_code = 409, detail = str(e))
        logger.error(f"Error exporting LoRA adapter: {e}", exc_info = True)
        raise HTTPException(
            status_code = 500,
            detail = "Failed to export LoRA adapter",
        )


# SSE shape mirrors routes/training.py: id/event/data, leading retry:, Last-Event-ID.
def _format_sse(
    data: str,
    event: str,
    event_id: Optional[int] = None,
) -> str:
    """Format a single SSE message with id/event/data fields."""
    lines = []
    if event_id is not None:
        lines.append(f"id: {event_id}")
    lines.append(f"event: {event}")
    lines.append(f"data: {data}")
    lines.append("")
    lines.append("")
    return "\n".join(lines)


# POST too: quick tunnels hold a streamed GET until it closes. The hidden GET keeps old clients.
@router.post("/logs/stream")
@router.get("/logs/stream", include_in_schema = False)
async def stream_export_logs(
    request: Request,
    since: Optional[int] = Query(
        None,
        description = "Return log entries with seq strictly greater than this cursor.",
    ),
    current_subject: str = Depends(get_current_subject),
):
    """
    Stream live stdout/stderr from the export worker subprocess as
    Server-Sent Events.

    Events:
      - `log`      : a single log line (data: {"stream","line","ts"})
      - `heartbeat`: periodic keepalive when no new lines are available
      - `complete` : once the worker is idle and no new lines arrived for
                     ~1 second. Clients should close.
      - `error`    : unrecoverable server-side error

    Each event's `id:` field is the log entry's monotonic seq number so the
    browser can resume via `Last-Event-ID` on reconnect.
    """
    backend = get_export_backend()

    # Cursor: since, then Last-Event-ID, else the run-start snapshot, so a late SSE misses nothing.
    last_event_id = request.headers.get("last-event-id")
    if since is None and last_event_id is not None:
        try:
            since = int(last_event_id)
        except ValueError:
            pass

    if since is None:
        cursor = backend.get_run_start_seq()
    else:
        cursor = max(0, int(since))

    async def event_generator() -> AsyncGenerator[str, None]:
        nonlocal cursor
        # Reconnect after 3 seconds if the connection drops mid-export.
        yield "retry: 3000\n\n"

        last_yield = time.monotonic()
        idle_since: Optional[float] = None
        try:
            while True:
                if job_is_foreign(backend):
                    yield _format_sse('{"status":"busy"}', event = "busy")
                    return
                if await request.is_disconnected():
                    return

                entries, new_cursor = backend.get_logs_since(cursor)
                if entries:
                    for entry in entries:
                        payload = json.dumps(
                            {
                                "stream": entry.get("stream", "stdout"),
                                "line": entry.get("line", ""),
                                "ts": entry.get("ts"),
                            }
                        )
                        yield _format_sse(
                            payload,
                            event = "log",
                            event_id = int(entry.get("seq", 0)),
                        )
                    cursor = new_cursor
                    last_yield = time.monotonic()
                    idle_since = None
                else:
                    now = time.monotonic()
                    if now - last_yield > 10.0:
                        yield _format_sse("{}", event = "heartbeat")
                        last_yield = now
                    if not backend.is_export_active():
                        # Reader drains lines printed just before the worker signalled done.
                        if idle_since is None:
                            idle_since = now
                        elif now - idle_since > 1.0:
                            yield _format_sse(
                                "{}",
                                event = "complete",
                                event_id = cursor,
                            )
                            return
                    else:
                        idle_since = None

                await asyncio.sleep(0.1)
        except asyncio.CancelledError:
            # Client disconnected mid-yield: end cleanly so StreamingResponse finalizes.
            return
        except Exception as exc:
            logger.error("Export log stream failed: %s", exc, exc_info = True)
            try:
                yield _format_sse(
                    json.dumps({"error": safe_error_detail(exc)}),
                    event = "error",
                )
            except Exception:
                pass

    return StreamingResponse(
        account_event_stream(get_export_backend(), event_generator()),
        media_type = "text/event-stream",
        headers = {
            "Cache-Control": "no-cache",
            "Connection": "keep-alive",
            "X-Accel-Buffering": "no",
        },
    )