Lumi/plugins/lumi_transcription/public/transcription.js
2026-07-22 11:39:05 +02:00

45 lines
2.6 KiB
JavaScript

(() => {
const root = document.querySelector("[data-transcription-admin]");
if (!root) return;
const button = root.querySelector("[data-create-pairing]");
const status = root.querySelector("[data-status]");
button?.addEventListener("click", async () => {
button.disabled = true;
status.textContent = "Creating a one-time pairing package…";
try {
const response = await fetch("/plugins/lumi_transcription/api/pairing-package", { method: "POST", headers: { Accept: "application/json" } });
if (!response.ok) throw new Error((await response.json()).error || "Pairing package could not be created.");
const blob = await response.blob();
const disposition = response.headers.get("content-disposition") || "";
const filename = /filename="?([^";]+)"?/i.exec(disposition)?.[1] || "lumi-companion.lumi-pairing.json";
const link = document.createElement("a");
link.href = URL.createObjectURL(blob); link.download = filename; link.click();
setTimeout(() => URL.revokeObjectURL(link.href), 1000);
status.textContent = "Pairing package created. It expires in 15 minutes and works once.";
} catch (error) { status.textContent = error.message; }
finally { button.disabled = false; }
});
root.addEventListener("click", async (event) => {
const download = event.target.closest("[data-download-model]");
const load = event.target.closest("[data-load-model]");
if (!download && !load) return;
const modelId = download?.dataset.downloadModel || load?.dataset.loadModel;
if (download && !window.confirm(`Download ${download.dataset.modelLabel} (${download.dataset.modelSize} MiB) to the Lumi host?`)) return;
const action = download ? "download" : "load";
const target = download || load;
target.disabled = true;
status.textContent = download ? "Downloading and verifying the model…" : "Loading the model into whisper.cpp…";
try {
const response = await fetch(`/plugins/lumi_transcription/api/models/${encodeURIComponent(modelId)}/${action}`, {
method: "POST", headers: { "Content-Type": "application/json", Accept: "application/json" },
body: download ? JSON.stringify({ confirmed: true }) : "{}"
});
const result = await response.json();
if (!response.ok) throw new Error(result.error || `Model ${action} failed.`);
status.textContent = download ? "Model verified. You can load it now." : "Model loaded. Run the host benchmark before live use.";
setTimeout(() => window.location.reload(), 700);
} catch (error) { status.textContent = error.message; target.disabled = false; }
});
})();