Spaces:
Paused

File size: 6,301 Bytes
bc9f3e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<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>