Download frontend/src/components/common/ComputeDeviceSelector.tsx from sumandas/ostra: direct link, hf CLI and curl.
- Browser
- Download file 14.2 kB
-
https://huggingface.co/spaces/sumandas/ostra/resolve/main/frontend/src/components/common/ComputeDeviceSelector.tsx
- Command line
-
hf download hf://spaces/sumandas/ostra/frontend/src/components/common/ComputeDeviceSelector.tsx
-
curl -L -o ComputeDeviceSelector.tsx https://huggingface.co/spaces/sumandas/ostra/resolve/main/frontend/src/components/common/ComputeDeviceSelector.tsx
14.2 kB
| import { useEffect, useRef, useState } from "react"; | |
| import { | |
| Check, | |
| ChevronDown, | |
| Cpu, | |
| ExternalLink, | |
| RefreshCw, | |
| Server, | |
| Sparkles, | |
| Zap, | |
| } from "lucide-react"; | |
| import { useShallow } from "zustand/react/shallow"; | |
| import { useOstraStore } from "../../store/useOstraStore"; | |
| import { | |
| resolveComputeTarget, | |
| type ComputePreference, | |
| } from "../../utils/browserCompute"; | |
| interface ComputeDeviceSelectorProps { | |
| compact?: boolean; | |
| disabled?: boolean; | |
| } | |
| /** | |
| * Unified Compute / GPU Selector Dropdown listing ALL available compute targets: | |
| * 1. Auto Cascade (WebGPU Metal/GPU -> Browser CPU WASM -> HF Space Server) | |
| * 2. Every detected client WebGPU GPU adapter (Apple Metal, NVIDIA, AMD, Intel) | |
| * 3. Client Browser CPU (WASM) | |
| * 4. Every available Server backend (CUDA GPU, Apple MLX, PyTorch MPS, HF Space CPU) | |
| * | |
| * When running inside a Hugging Face Space iframe where the browser blocks WebGPU, | |
| * surfaces a 1-click "Open Direct .hf.space Tab" link to unlock on-device Metal/WebGPU. | |
| */ | |
| export function ComputeDeviceSelector({ | |
| compact = false, | |
| disabled = false, | |
| }: ComputeDeviceSelectorProps) { | |
| const { | |
| hardware, | |
| browserCompute, | |
| computePref, | |
| setComputePref, | |
| refreshBrowserCompute, | |
| } = useOstraStore( | |
| useShallow((s) => ({ | |
| hardware: s.hardware, | |
| browserCompute: s.browserCompute, | |
| computePref: s.computePref, | |
| setComputePref: s.setComputePref, | |
| refreshBrowserCompute: s.refreshBrowserCompute, | |
| })) | |
| ); | |
| const [open, setOpen] = useState(false); | |
| const [refreshing, setRefreshing] = useState(false); | |
| const containerRef = useRef<HTMLDivElement>(null); | |
| useEffect(() => { | |
| if (!open) return; | |
| const onDocMouseDown = (ev: MouseEvent) => { | |
| if ( | |
| containerRef.current && | |
| !containerRef.current.contains(ev.target as Node) | |
| ) { | |
| setOpen(false); | |
| } | |
| }; | |
| document.addEventListener("mousedown", onDocMouseDown); | |
| return () => document.removeEventListener("mousedown", onDocMouseDown); | |
| }, [open]); | |
| const resolved = resolveComputeTarget(computePref, browserCompute, hardware); | |
| const isHfSpace = Boolean(hardware?.is_hf_space); | |
| const toneClasses = | |
| resolved.tone === "emerald" | |
| ? "bg-emerald-50 border-emerald-700 text-emerald-950 hover:bg-emerald-100" | |
| : resolved.tone === "sky" | |
| ? "bg-sky-50 border-sky-700 text-sky-950 hover:bg-sky-100" | |
| : "bg-[#f1f4f8] border-[#0f172a] text-[#0f172a] hover:bg-[#e8edf3]"; | |
| const pick = (next: ComputePreference) => { | |
| setComputePref(next); | |
| setOpen(false); | |
| }; | |
| const handleRefresh = async () => { | |
| setRefreshing(true); | |
| try { | |
| await refreshBrowserCompute(); | |
| } finally { | |
| setRefreshing(false); | |
| } | |
| }; | |
| const serverOptions: Array<{ | |
| id: ComputePreference; | |
| label: string; | |
| detail: string; | |
| isGpu: boolean; | |
| }> = []; | |
| if (hardware?.cuda_available) { | |
| serverOptions.push({ | |
| id: "server:cuda", | |
| label: isHfSpace ? "HF Space · CUDA GPU" : "Server · CUDA GPU", | |
| detail: "PyTorch CUDA hardware acceleration on server", | |
| isGpu: true, | |
| }); | |
| } | |
| if (hardware?.mlx_embeddings_available) { | |
| serverOptions.push({ | |
| id: "server:mlx", | |
| label: hardware.device_label || "Apple Silicon MLX", | |
| detail: "Native local Apple Silicon MLX backend", | |
| isGpu: true, | |
| }); | |
| } | |
| if (hardware?.mps_available) { | |
| serverOptions.push({ | |
| id: "server:mps", | |
| label: "Server · PyTorch MPS", | |
| detail: "Local Apple Metal via PyTorch MPS", | |
| isGpu: true, | |
| }); | |
| } | |
| serverOptions.push({ | |
| id: "server:cpu", | |
| label: isHfSpace | |
| ? `HF Space · CPU (${hardware?.cpu_count ?? 2} vCPU)` | |
| : `Server · CPU (${hardware?.cpu_count ?? 2} cores)`, | |
| detail: isHfSpace | |
| ? "Hugging Face Space server fallback (works with all PyTorch models)" | |
| : "PyTorch CPU backend on server", | |
| isGpu: false, | |
| }); | |
| const directSpaceUrl = | |
| typeof window !== "undefined" ? window.location.href : ""; | |
| return ( | |
| <div ref={containerRef} className="relative"> | |
| <button | |
| type="button" | |
| disabled={disabled} | |
| onClick={() => setOpen((o) => !o)} | |
| title="Select Compute Device / GPU (WebGPU Metal, Browser WASM CPU, or HF Space Server)" | |
| className={`inline-flex items-center justify-between gap-1.5 border-[1.5px] font-mono font-semibold cursor-pointer transition-colors disabled:opacity-60 ${toneClasses} ${ | |
| compact | |
| ? "h-7 px-2 text-[10px] max-w-[195px]" | |
| : "w-full px-2.5 py-1.5 text-xs" | |
| }`} | |
| > | |
| <span className="inline-flex items-center gap-1.5 min-w-0"> | |
| {resolved.isWebGPU ? ( | |
| <Zap className="w-3 h-3 shrink-0 text-emerald-700" /> | |
| ) : resolved.isClient ? ( | |
| <Cpu className="w-3 h-3 shrink-0 text-sky-700" /> | |
| ) : ( | |
| <Server className="w-3 h-3 shrink-0 text-[#475569]" /> | |
| )} | |
| <span className="truncate"> | |
| {compact ? resolved.shortLabel : resolved.label} | |
| </span> | |
| </span> | |
| <ChevronDown className="w-3 h-3 shrink-0 opacity-75" /> | |
| </button> | |
| {open && ( | |
| <div | |
| className={`absolute top-full mt-1 z-50 bg-white border-[1.5px] border-[#0f172a] shadow-[4px_4px_0_rgba(15,23,42,0.2)] flex flex-col text-left ${ | |
| compact ? "right-0 w-[330px]" : "left-0 right-0 min-w-[310px]" | |
| }`} | |
| > | |
| {/* Header */} | |
| <div className="px-2.5 py-1.5 bg-[#f1f4f8] border-b border-[#cbd5e1] flex items-center justify-between gap-2"> | |
| <span className="font-mono text-[9px] font-bold uppercase tracking-wider text-[#334155]"> | |
| Available GPUs & Compute Engines | |
| </span> | |
| <button | |
| type="button" | |
| onClick={() => void handleRefresh()} | |
| disabled={refreshing} | |
| title="Re-scan browser WebGPU adapters" | |
| className="inline-flex items-center gap-1 px-1.5 py-0.5 bg-white border border-[#94a3b8] hover:border-[#0f172a] font-mono text-[9px] text-[#334155] cursor-pointer" | |
| > | |
| <RefreshCw | |
| className={`w-2.5 h-2.5 ${refreshing ? "animate-spin" : ""}`} | |
| /> | |
| <span>Scan GPUs</span> | |
| </button> | |
| </div> | |
| <div className="max-h-80 overflow-y-auto divide-y divide-[#e2e8f0]"> | |
| {/* 1. Auto Cascade */} | |
| <button | |
| type="button" | |
| onClick={() => pick("auto")} | |
| className={`w-full px-2.5 py-2 text-left cursor-pointer transition-colors ${ | |
| computePref === "auto" ? "bg-emerald-50" : "hover:bg-[#f8fafc]" | |
| }`} | |
| > | |
| <div className="flex items-center justify-between gap-2"> | |
| <span className="font-mono text-[11px] font-bold text-[#0f172a] inline-flex items-center gap-1.5"> | |
| <Sparkles className="w-3 h-3 text-emerald-700" /> | |
| <span>Auto Cascade (Recommended)</span> | |
| </span> | |
| {computePref === "auto" && ( | |
| <Check className="w-3.5 h-3.5 text-emerald-700 shrink-0" /> | |
| )} | |
| </div> | |
| <div className="font-mono text-[9px] text-[#475569] mt-0.5"> | |
| {browserCompute?.webgpuAvailable | |
| ? `WebGPU (${browserCompute.isAppleMetal ? "Apple Metal" : "GPU"}) → Browser CPU → ${isHfSpace ? "HF Space" : "Server"}` | |
| : browserCompute?.wasmAvailable | |
| ? `Browser CPU (WASM) → ${isHfSpace ? "HF Space" : "Server"}` | |
| : hardware?.device_label || "Server Backend"} | |
| </div> | |
| </button> | |
| {/* 2. Detected Consumer GPUs (WebGPU) */} | |
| <div className="bg-[#f8fafc] px-2.5 py-1 font-mono text-[9px] font-bold uppercase tracking-wider text-emerald-900"> | |
| Your Device GPUs (WebGPU / Metal) | |
| </div> | |
| {browserCompute && browserCompute.adapters.length > 0 ? ( | |
| browserCompute.adapters.map((adapter) => { | |
| const selected = computePref === adapter.id; | |
| return ( | |
| <button | |
| key={adapter.id} | |
| type="button" | |
| onClick={() => pick(adapter.id)} | |
| className={`w-full px-2.5 py-2 text-left cursor-pointer transition-colors ${ | |
| selected ? "bg-emerald-50" : "hover:bg-[#f8fafc]" | |
| }`} | |
| > | |
| <div className="flex items-center justify-between gap-2"> | |
| <span className="font-mono text-[11px] font-bold text-emerald-950 inline-flex items-center gap-1.5 min-w-0"> | |
| <Zap className="w-3 h-3 text-emerald-700 shrink-0" /> | |
| <span className="truncate">{adapter.label}</span> | |
| </span> | |
| {selected && ( | |
| <Check className="w-3.5 h-3.5 text-emerald-700 shrink-0" /> | |
| )} | |
| </div> | |
| <div className="font-mono text-[9px] text-[#475569] mt-0.5 flex items-center gap-1.5 flex-wrap"> | |
| <span>{adapter.vendor}</span> | |
| {adapter.architecture && ( | |
| <span>· {adapter.architecture}</span> | |
| )} | |
| {adapter.isAppleMetal && ( | |
| <span className="px-1 bg-emerald-100 border border-emerald-700 text-emerald-900 font-bold"> | |
| Metal | |
| </span> | |
| )} | |
| <span>· Runs in Web Worker (0% Space CPU)</span> | |
| </div> | |
| </button> | |
| ); | |
| }) | |
| ) : ( | |
| <div className="px-2.5 py-2 space-y-1.5 bg-amber-50/70"> | |
| <div className="font-mono text-[10px] font-semibold text-amber-950"> | |
| No WebGPU adapter detected in this browser view | |
| </div> | |
| <div className="text-[10px] text-amber-900 leading-snug"> | |
| {browserCompute?.webgpuUnavailableReason || | |
| "Checking browser GPU capabilities…"} | |
| </div> | |
| {browserCompute?.webglRenderer && ( | |
| <div className="font-mono text-[9px] text-[#475569] truncate"> | |
| Detected WebGL GPU: {browserCompute.webglRenderer} | |
| </div> | |
| )} | |
| {browserCompute?.inIframe && directSpaceUrl && ( | |
| <a | |
| href={directSpaceUrl} | |
| target="_blank" | |
| rel="noopener noreferrer" | |
| className="inline-flex items-center gap-1 px-2 py-1 bg-white border border-[#0f172a] font-mono text-[10px] font-bold text-[#0f172a] hover:bg-emerald-50" | |
| > | |
| <ExternalLink className="w-3 h-3 text-emerald-700" /> | |
| <span>Open Direct .hf.space Tab to Unlock WebGPU</span> | |
| </a> | |
| )} | |
| </div> | |
| )} | |
| {/* 3. Consumer Browser CPU (WASM) */} | |
| <div className="bg-[#f8fafc] px-2.5 py-1 font-mono text-[9px] font-bold uppercase tracking-wider text-sky-900"> | |
| Your Device CPU (Browser WASM) | |
| </div> | |
| <button | |
| type="button" | |
| disabled={!browserCompute?.wasmAvailable} | |
| onClick={() => pick("wasm")} | |
| className={`w-full px-2.5 py-2 text-left cursor-pointer transition-colors disabled:opacity-50 ${ | |
| computePref === "wasm" ? "bg-sky-50" : "hover:bg-[#f8fafc]" | |
| }`} | |
| > | |
| <div className="flex items-center justify-between gap-2"> | |
| <span className="font-mono text-[11px] font-bold text-sky-950 inline-flex items-center gap-1.5"> | |
| <Cpu className="w-3 h-3 text-sky-700" /> | |
| <span>Browser CPU (ONNX WASM SIMD)</span> | |
| </span> | |
| {computePref === "wasm" && ( | |
| <Check className="w-3.5 h-3.5 text-sky-700 shrink-0" /> | |
| )} | |
| </div> | |
| <div className="font-mono text-[9px] text-[#475569] mt-0.5"> | |
| Runs on your machine's CPU in a Web Worker · works inside HF Space iframes | |
| </div> | |
| </button> | |
| {/* 4. Server GPUs & CPU */} | |
| <div className="bg-[#f8fafc] px-2.5 py-1 font-mono text-[9px] font-bold uppercase tracking-wider text-[#334155]"> | |
| {isHfSpace ? "Hugging Face Space Server" : "Local Backend Server"} | |
| </div> | |
| {serverOptions.map((opt) => { | |
| const selected = computePref === opt.id; | |
| return ( | |
| <button | |
| key={opt.id} | |
| type="button" | |
| onClick={() => pick(opt.id)} | |
| className={`w-full px-2.5 py-2 text-left cursor-pointer transition-colors ${ | |
| selected ? "bg-[#f1f4f8]" : "hover:bg-[#f8fafc]" | |
| }`} | |
| > | |
| <div className="flex items-center justify-between gap-2"> | |
| <span className="font-mono text-[11px] font-bold text-[#0f172a] inline-flex items-center gap-1.5"> | |
| {opt.isGpu ? ( | |
| <Zap className="w-3 h-3 text-emerald-700" /> | |
| ) : ( | |
| <Server className="w-3 h-3 text-[#475569]" /> | |
| )} | |
| <span>{opt.label}</span> | |
| </span> | |
| {selected && ( | |
| <Check className="w-3.5 h-3.5 text-[#0f172a] shrink-0" /> | |
| )} | |
| </div> | |
| <div className="font-mono text-[9px] text-[#475569] mt-0.5"> | |
| {opt.detail} | |
| </div> | |
| </button> | |
| ); | |
| })} | |
| </div> | |
| </div> | |
| )} | |
| </div> | |
| ); | |
| } | |