Spaces:
Paused

assistant / ui /web /src /components /PyTorchInstallDialog.svelte
localailb's picture
Deploy Full LocalAiLab Application (FastAPI + Svelte + Agents)
bc9f3e6 verified
Raw History Blame Contribute Delete
6.3 kB
<script>
import { onMount } from "svelte";
import { t } from "../lib/stores.svelte.js";
import { pytorchInfo } from "../lib/api.js";
import "./PyTorchInstallDialog.css";
/** PyTorch install popup β€” the Settings tab's "Detect & Install GPU
* PyTorch" button opens this instead of silently installing the
* recommended path. Fetches the detection report
* (/api/settings/pytorch-info), shows every build that CAN run on this PC
* (CPU / NVIDIA CUDA / Intel XPU / AMD ROCm) with its reason, and lets the
* user pick. `oninstall(choice)` fires with the chosen id (auto / cpu /
* cuda / xpu / rocm) when the user confirms. */
let { onclose, oninstall } = $props();
let plan = $state(null);
let loadErr = $state("");
let selected = $state("auto");
let rootEl;
let boxEl = $state(null);
// ── Draggable ──────────────────────────────────────────────────
let dragOffsetX = 0;
let dragOffsetY = 0;
let posX = $state(null);
let posY = $state(null);
let dragging = $state(false);
function onDragStart(e) {
if (!boxEl) return;
if (e.button !== 0) return;
e.preventDefault();
const rect = boxEl.getBoundingClientRect();
dragOffsetX = e.clientX - rect.left;
dragOffsetY = e.clientY - rect.top;
if (posX === null) posX = rect.left;
if (posY === null) posY = rect.top;
dragging = true;
}
function onDragMove(e) {
if (!dragging) return;
const w = window.innerWidth;
const h = window.innerHeight;
let x = e.clientX - dragOffsetX;
let y = e.clientY - dragOffsetY;
const bw = boxEl ? boxEl.offsetWidth : 480;
const bh = boxEl ? boxEl.offsetHeight : 350;
x = Math.max(0, Math.min(x, w - bw));
y = Math.max(0, Math.min(y, h - bh));
posX = x;
posY = y;
}
function onDragEnd() {
dragging = false;
}
function fmt(key, params) {
const s = t(key);
if (!params) return s;
return s.replace(/\{(\w+)\}/g, (m, k) => (params[k] !== undefined ? String(params[k]) : m));
}
function close() {
onclose?.();
}
function confirm() {
oninstall?.(selected);
onclose?.();
}
function pick(id, available) {
if (available) selected = id;
}
onMount(async () => {
try {
const r = await pytorchInfo();
plan = r.plan || r;
const rec = (plan.options || []).find((o) => o.recommended);
if (rec) selected = rec.id;
} catch (e) {
loadErr = e.message || String(e);
}
rootEl?.querySelector(".ptch-ok")?.focus();
const onKey = (e) => {
if (e.key === "Escape") {
e.preventDefault();
close();
}
};
document.addEventListener("keydown", onKey, true);
return () => document.removeEventListener("keydown", onKey, true);
});
const detected = $derived(plan?.detected || {});
const options = $derived(plan?.options || []);
</script>
<!-- svelte-ignore a11y_no_static_element_interactions -->
<svelte:window
onmousemove={onDragMove}
onmouseup={onDragEnd}
/>
<div
class="ptch-overlay"
role="presentation"
bind:this={rootEl}
onmousedown={(e) => {
if (e.target === rootEl) close();
}}
>
<div
class="ptch-box"
class:dragging
role="dialog"
aria-modal="true"
aria-labelledby="ptch-title"
bind:this={boxEl}
style="{posX !== null ? `left:${posX}px;top:${posY}px;transform:none` : ''}"
>
<!-- svelte-ignore a11y_no_static_element_interactions -->
<div
class="ptch-title ptch-drag-handle"
id="ptch-title"
role="none"
onmousedown={onDragStart}
>{t("ptch_dialog_title")}</div>
{#if loadErr}
<div class="ptch-err">{loadErr}</div>
<div class="ptch-actions">
<button type="button" class="ptch-ok" data-tooltip={t("ptch_dialog_ok")} onclick={close}>
{t("ptch_dialog_ok")}
</button>
</div>
{:else if !plan}
<div class="ptch-loading">{t("web_loading", "…")}</div>
{:else}
<div class="ptch-detect">
{t("ptch_detected_gpu")}: <b>{detected.gpu_name || detected.gpu_brand || "β€”"}</b>
<br />
{t("ptch_detected_cpu")}: <b>{detected.cpu_name || "β€”"}</b>
{#if detected.cuda_version}
<br />{t("ptch_detected_driver")}: <b>{detected.cuda_version}</b>
{/if}
{#if detected.compute_capability}
<br />{t("ptch_detected_cc")}: <b>{detected.compute_capability}</b>
{/if}
<br />{t("ptch_detected_device")}: <b>{detected.current_device || "cpu"}</b>
</div>
<div class="ptch-pick">{t("ptch_dialog_ask")}</div>
<div class="ptch-opts">
{#each options as o (o.id)}
{@const avail = !!o.available}
<label
class="ptch-opt"
class:unavail={!avail}
class:sel={avail && selected === o.id}
data-tooltip={t("ptch_opt_tip")}
>
<input
type="radio"
name="ptch-choice"
value={o.id}
checked={avail && selected === o.id}
disabled={!avail}
onchange={() => pick(o.id, avail)}
/>
<span class="ptch-opt-main">
<span class="ptch-opt-label">
{t(o.label_key)}
{#if o.recommended && avail}<span class="ptch-rec">β˜… {t("ptch_recommended")}</span>{/if}
{#if !avail}<span class="ptch-na">β€” {t("ptch_not_available")}</span>{/if}
</span>
<span class="ptch-opt-reason">{fmt(o.reason_key, o.params)}</span>
</span>
</label>
{/each}
</div>
<div class="ptch-actions">
<button type="button" class="ptch-cancel" data-tooltip={t("ptch_dialog_cancel")} onclick={close}>
{t("ptch_dialog_cancel")}
</button>
<button type="button" class="ptch-ok" data-tooltip={t("ptch_dialog_install")} onclick={confirm}>
{t("ptch_dialog_install")}
</button>
</div>
{/if}
</div>
</div>