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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
"""Factories for the training, diffusion dataset, scan folder and data recipe object routes."""
from .factory_base import Factory, seeder
SENTINEL = "Alice's training resource: café / 日本語"
EDITED = "training-matrix-edited"
START_REQUEST_ID = "training-matrix-start-request"
START_JOB_ID = "training-matrix-job"
DIFFUSION_JOB_ID = "a1b2c3d4e5f607182930a4b5c6d7e8f9"
SAMPLE_JOB_ID = "b2c3d4e5f607182930a4b5c6d7e8f9a1"
SAMPLE_PATH = "samples/20260101-000000-abcdef/step-0-0.png"
DATASET_NAME = "training-matrix-dataset"
IMAGE_NAME = "training-matrix-image.png"
RECIPE_JOB_ID = "f9e8d7c6b5a40312092a8b7c6d5e4f30"
BLOCK_ID = "0123456789abcdef0123456789abcdef"
FILE_ID = "fedcba9876543210fedcba9876543210"
SCAN_FOLDER_DIRNAME = "training-matrix-scan-folder"
SAVED_RECIPE_ID = "training-matrix-recipe"
RECIPE_EXECUTION_ID = "training-matrix-execution"
SAVED_RECIPE_BODY = {
"id": SAVED_RECIPE_ID,
"name": EDITED,
"payload": {"recipe": {}},
"createdAt": 1000,
"updatedAt": 2000,
}
RECIPE_EXECUTION_BODY = {
"id": RECIPE_EXECUTION_ID,
"recipeId": SAVED_RECIPE_ID,
"createdAt": 1000,
"status": EDITED,
}
PNG_1X1 = (
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmM"
"IQAAAABJRU5ErkJggg=="
)
_SCAN_FOLDER_REASON = (
"Removing a scan folder id is an idempotent delete in the caller's own studio.db, so a "
"foreign account is told ok without seeing or touching alice's row."
)
_PUBLISH_REASON = (
"Publishing uploads to the Hub, so the owning account is taken only as far as the 409 that "
"proves it resolved its own preview job, while a foreign account resolves no job at all and "
"is refused for a missing artifact path."
)
_BLOCK_REASON = (
"Removing an upload block is an idempotent delete under the caller's own uploads root, so a "
"foreign account is told ok with deleted false."
)
_SAVED_RECIPE_UPSERT_REASON = (
"PUT is an upsert keyed only by id inside the caller's own studio.db, so a foreign caller "
"writes its own row and never sees alice's; the snapshot proves hers is untouched."
)
_SAVED_RECIPE_DELETE_REASON = (
"DELETE is idempotent in the caller's own studio.db, so a foreign caller gets 204 after "
"tombstoning an id it never had; the snapshot proves alice's recipe survived."
)
_RECIPE_EXECUTIONS_READ_REASON = (
"Run history is read from the caller's own studio.db, so a foreign caller gets an empty list."
)
_EVENTS_REASON = (
"The owning account's SSE stream ends only when the client disconnects, which an in-process "
"TestClient request cannot do, so the case would hang rather than return."
)
def _claim_in_process_job(service) -> None:
from utils.account_context import current_account
with service._account_job_lock:
service._result_account = service.job_account = current_account()
@seeder("training-start-request")
def seed_training_start_request(account) -> dict[str, str]:
from core.training import get_training_backend
from core.training.training import TrainingStartRequestRecord
from utils.account_context import run_as
backend = get_training_backend()
def install() -> None:
_claim_in_process_job(backend)
record = TrainingStartRequestRecord(
start_request_id = START_REQUEST_ID,
job_id = START_JOB_ID,
state = "rejected",
message = SENTINEL,
error = "Training start failed validation",
)
with backend._lock:
backend._start_requests[START_REQUEST_ID] = record
backend._start_cancel_tombstones.pop(START_REQUEST_ID, None)
backend._start_cancel_tombstone_reservations.pop(START_REQUEST_ID, None)
backend.current_start_request_id = None
backend.current_job_id = None
backend._pending_start_request_id = None
backend._status_start_request_id = None
run_as(account, install)
return {"start_request_id": START_REQUEST_ID}
@seeder("training-diffusion-run")
def seed_diffusion_run(account) -> dict[str, str]:
import json
from utils.account_context import run_as
from utils.paths.storage_roots import tensorboard_root
def install() -> None:
folder = tensorboard_root() / "diffusion"
folder.mkdir(parents = True, exist_ok = True)
(folder / f"{DIFFUSION_JOB_ID}.json").write_text(
json.dumps(
{
"job_id": DIFFUSION_JOB_ID,
"status": "completed",
"message": SENTINEL,
"step": 100,
"total_steps": 100,
"saved": True,
"config": {},
}
),
encoding = "utf-8",
)
run_as(account, install)
return {"job_id": DIFFUSION_JOB_ID}
@seeder("training-diffusion-sample")
def seed_diffusion_sample(account) -> dict[str, str]:
import base64
import json
from utils.account_context import run_as
from utils.paths import outputs_root
from utils.paths.storage_roots import tensorboard_root
def install() -> None:
out_dir = outputs_root() / "training-matrix-diffusion"
image = out_dir / SAMPLE_PATH
image.parent.mkdir(parents = True, exist_ok = True)
image.write_bytes(base64.b64decode(PNG_1X1))
folder = tensorboard_root() / "diffusion"
folder.mkdir(parents = True, exist_ok = True)
(folder / f"{SAMPLE_JOB_ID}.json").write_text(
json.dumps(
{
"job_id": SAMPLE_JOB_ID,
"status": "completed",
"output_dir": str(out_dir),
"samples": [{"step": 0, "index": 0, "path": SAMPLE_PATH}],
"config": {},
}
),
encoding = "utf-8",
)
run_as(account, install)
return {"job_id": SAMPLE_JOB_ID}
@seeder("training-diffusion-dataset")
def seed_diffusion_dataset(account) -> dict[str, str]:
import base64
from utils.account_context import run_as
from utils.paths import datasets_root
def install() -> None:
folder = datasets_root() / DATASET_NAME
folder.mkdir(parents = True, exist_ok = True)
image = folder / IMAGE_NAME
image.write_bytes(base64.b64decode(PNG_1X1))
image.with_suffix(".txt").write_text(SENTINEL, encoding = "utf-8")
run_as(account, install)
return {"name": DATASET_NAME, "filename": IMAGE_NAME}
@seeder("training-scan-folder")
def seed_scan_folder(account) -> dict[str, str]:
from storage.studio_db import add_scan_folder_with_status
from utils.account_context import run_as
from utils.paths import workspace_root
def install() -> str:
folder = workspace_root() / SCAN_FOLDER_DIRNAME
folder.mkdir(parents = True, exist_ok = True)
row, _ = add_scan_folder_with_status(str(folder))
return str(row["id"])
return {"folder_id": run_as(account, install)}
@seeder("training-saved-recipe")
def seed_saved_recipe(account) -> dict[str, str]:
from storage import data_recipes_db
from utils.account_context import run_as
def install() -> None:
data_recipes_db.upsert_recipe({**SAVED_RECIPE_BODY, "name": SENTINEL})
data_recipes_db.upsert_execution({**RECIPE_EXECUTION_BODY, "status": SENTINEL})
run_as(account, install)
return {"recipe_id": SAVED_RECIPE_ID, "execution_id": RECIPE_EXECUTION_ID}
@seeder("training-recipe-job")
def seed_recipe_job(account) -> dict[str, str]:
from core.data_recipe.jobs import get_job_manager
from core.data_recipe.jobs.types import Job
from utils.account_context import run_as
manager = get_job_manager()
def install() -> None:
_claim_in_process_job(manager)
job = Job(job_id = RECIPE_JOB_ID, status = "completed", started_at = 1000.0)
job.finished_at = 2000.0
job.execution_type = "preview"
job.analysis = {"summary": SENTINEL}
job.dataset = [{"text": SENTINEL}]
job.rows = 1
job.cols = 1
with manager._lock:
manager._job = job
manager._proc = None
manager._mp_q = None
manager._events.clear()
manager._subs.clear()
manager._seq = 0
run_as(account, install)
return {"job_id": RECIPE_JOB_ID}
@seeder("training-unstructured-upload")
def seed_unstructured_upload(account) -> dict[str, str]:
import json
from utils.account_context import run_as
from utils.paths import unstructured_uploads_root
def install() -> None:
block = unstructured_uploads_root() / BLOCK_ID
block.mkdir(parents = True, exist_ok = True)
(block / f"{FILE_ID}.txt").write_text(SENTINEL, encoding = "utf-8")
(block / f"{FILE_ID}.extracted.txt").write_text(SENTINEL, encoding = "utf-8")
(block / f"{FILE_ID}.meta.json").write_text(
json.dumps({"original_filename": "training-matrix.txt", "size_bytes": 1}),
encoding = "utf-8",
)
run_as(account, install)
return {"block_id": BLOCK_ID, "file_id": FILE_ID}
OPENAI_RUN_ID = "training-matrix-openai-run"
OPENAI_UPLOAD_ID = "0f1e2d3c4b5a69788796a5b4c3d2e1f0"
@seeder("training-openai-job")
def seed_openai_job(account) -> dict[str, str]:
"""A finished run in this account, which the OpenAI fine-tuning routes read as a job."""
import json
from storage import studio_db
from utils.account_context import run_as
def install() -> None:
studio_db.create_run(
id = OPENAI_RUN_ID,
model_name = "local/model",
dataset_name = "local/dataset",
config_json = json.dumps({"model_name": "local/model", "project_name": SENTINEL}),
started_at = "2026-01-01T00:00:00+00:00",
total_steps = 1,
)
studio_db.finish_run(
id = OPENAI_RUN_ID,
status = "completed",
ended_at = "2026-01-01T00:01:00+00:00",
final_step = 1,
final_loss = 1.0,
duration_seconds = 60.0,
)
run_as(account, install)
return {"job_id": OPENAI_RUN_ID}
@seeder("training-openai-file")
def seed_openai_file(account) -> dict[str, str]:
from utils.account_context import run_as
from utils.paths.storage_roots import dataset_uploads_root
def install() -> None:
root = dataset_uploads_root()
root.mkdir(parents = True, exist_ok = True)
(root / f"{OPENAI_UPLOAD_ID}_matrix.jsonl").write_text(
'{"messages": []}\n', encoding = "utf-8"
)
run_as(account, install)
return {"file_id": f"file-{OPENAI_UPLOAD_ID}"}
FACTORIES = {
"routes.training_history:DELETE:/runs/{run_id}": Factory("training", fragment = "deleted"),
"routes.openai_fine_tuning:GET:/files/{file_id}": Factory(
"training-openai-file", fragment = "matrix.jsonl"
),
"routes.openai_fine_tuning:GET:/fine_tuning/jobs/{job_id}": Factory(
"training-openai-job", fragment = SENTINEL
),
"routes.openai_fine_tuning:GET:/fine_tuning/jobs/{job_id}/events": Factory(
"training-openai-job", fragment = "successfully completed"
),
"routes.openai_fine_tuning:GET:/fine_tuning/jobs/{job_id}/checkpoints": Factory(
"training-openai-job", fragment = "list"
),
# A finished job cannot be cancelled: its own account gets 400, every other account 404.
"routes.openai_fine_tuning:POST:/fine_tuning/jobs/{job_id}/cancel": Factory(
"training-openai-job", success = 400
),
"routes.openai_fine_tuning:POST:/fine_tuning/jobs/{job_id}/pause": Factory(
"training-openai-job",
success = 400,
owner = (400,),
wrong = (400,),
reason = "unsupported for every job: refused before any lookup, so nothing is revealed",
),
"routes.openai_fine_tuning:POST:/fine_tuning/jobs/{job_id}/resume": Factory(
"training-openai-job",
success = 400,
owner = (400,),
wrong = (400,),
reason = "unsupported for every job: refused before any lookup, so nothing is revealed",
),
"routes.training:GET:/start-requests/{start_request_id}": Factory(
"training-start-request", fragment = SENTINEL
),
"routes.training:POST:/start-requests/{start_request_id}/acknowledge": Factory(
"training-start-request", fragment = "ok"
),
"routes.training:POST:/start-requests/{start_request_id}/cancel": Factory(
"training-start-request", fragment = SENTINEL
),
"routes.training:GET:/diffusion/runs/{job_id}": Factory(
"training-diffusion-run", fragment = SENTINEL
),
"routes.training:GET:/diffusion/runs/{job_id}/sample": Factory(
"training-diffusion-sample", fragment = "IHDR", query = {"path": SAMPLE_PATH}
),
"routes.training:GET:/diffusion/dataset/{name}/images": Factory(
"training-diffusion-dataset", fragment = SENTINEL
),
"routes.training:GET:/diffusion/dataset/{name}/image/{filename}": Factory(
"training-diffusion-dataset", fragment = "IHDR"
),
"routes.training:DELETE:/diffusion/dataset/{name}/image/{filename}": Factory(
"training-diffusion-dataset", fragment = IMAGE_NAME
),
"routes.training:PUT:/diffusion/dataset/{name}/caption/{filename}": Factory(
"training-diffusion-dataset", {"caption": EDITED}, fragment = EDITED
),
"routes.models:DELETE:/scan-folders/{folder_id}": Factory(
"training-scan-folder",
owner = (200,),
wrong = (200,),
reason = _SCAN_FOLDER_REASON,
),
"routes.data_recipe.jobs:GET:/jobs/{job_id}/status": Factory(
"training-recipe-job", fragment = RECIPE_JOB_ID
),
"routes.data_recipe.jobs:GET:/jobs/{job_id}/analysis": Factory(
"training-recipe-job", fragment = SENTINEL
),
"routes.data_recipe.jobs:GET:/jobs/{job_id}/dataset": Factory(
"training-recipe-job", fragment = SENTINEL
),
"routes.data_recipe.jobs:GET:/jobs/{job_id}/download-url": Factory(
"training-recipe-job", fragment = "/download?"
),
"routes.data_recipe.jobs:GET:/jobs/{job_id}/download": Factory(
"training-recipe-job", fragment = SENTINEL
),
"routes.data_recipe.jobs:POST:/jobs/{job_id}/cancel": Factory(
"training-recipe-job", fragment = RECIPE_JOB_ID
),
"routes.data_recipe.jobs:POST:/jobs/{job_id}/publish": Factory(
"training-recipe-job",
{"repo_id": "training-matrix/dataset", "description": SENTINEL, "hf_token": "hf_matrix"},
success = 409,
fragment = "Only completed full runs can be published.",
owner = (400,),
wrong = (400,),
reason = _PUBLISH_REASON,
),
"routes.data_recipe.library:GET:/recipes/{recipe_id}": Factory(
"training-saved-recipe", fragment = SENTINEL
),
"routes.data_recipe.library:PUT:/recipes/{recipe_id}": Factory(
"training-saved-recipe",
SAVED_RECIPE_BODY,
fragment = EDITED,
owner = (200,),
wrong = (200,),
reason = _SAVED_RECIPE_UPSERT_REASON,
),
"routes.data_recipe.library:DELETE:/recipes/{recipe_id}": Factory(
"training-saved-recipe",
success = 204,
owner = (204,),
wrong = (204,),
reason = _SAVED_RECIPE_DELETE_REASON,
),
"routes.data_recipe.library:GET:/recipes/{recipe_id}/executions": Factory(
"training-saved-recipe",
fragment = SENTINEL,
absent = SENTINEL,
owner = (200,),
wrong = (200,),
reason = _RECIPE_EXECUTIONS_READ_REASON,
),
"routes.data_recipe.library:PUT:/recipes/{recipe_id}/executions/{execution_id}": Factory(
"training-saved-recipe",
RECIPE_EXECUTION_BODY,
success = 204,
),
"routes.data_recipe.seed:DELETE:/seed/unstructured-block/{block_id}": Factory(
"training-unstructured-upload",
fragment = "true",
absent = "true",
owner = (200,),
wrong = (200,),
reason = _BLOCK_REASON,
),
"routes.data_recipe.seed:DELETE:/seed/unstructured-file/{block_id}/{file_id}": Factory(
"training-unstructured-upload", fragment = "ok"
),
}
SKIPPED = {
"routes.data_recipe.jobs:GET:/jobs/{job_id}/events": _EVENTS_REASON,
"routes.data_recipe.jobs:POST:/jobs/{job_id}/events": _EVENTS_REASON,
}