import { pipeline, TextStreamer } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.7.3'; // --- Configuration --- const MODEL_ID = "onnx-community/gemma-3-270m-it-ONNX"; const SYSTEM_PROMPT = { role: "system", content: "You are a helpful assistant." }; // --- State --- let generator = null; let messages = [SYSTEM_PROMPT]; let isGenerating = false; let currentDevice = 'cpu'; // --- DOM Elements --- const chatContainer = document.getElementById('chat-container'); const chatForm = document.getElementById('chat-form'); const userInput = document.getElementById('user-input'); const sendBtn = document.getElementById('send-btn'); const statusBar = document.getElementById('status-bar'); const statusText = document.querySelector('.status-text'); const progressBar = document.getElementById('progress-bar'); const deviceSelect = document.getElementById('device-select'); const resetBtn = document.getElementById('reset-btn'); // --- Initialization --- function checkWebGPU() { if (!navigator.gpu) { const gpuOption = deviceSelect.querySelector('option[value="webgpu"]'); if (gpuOption) { gpuOption.disabled = true; gpuOption.textContent = "GPU (WebGPU not available)"; } } } checkWebGPU(); // --- Pipeline Management --- async function loadGenerator() { const selectedDevice = deviceSelect.value; // If we already have a generator and the device hasn't changed, return it if (generator && currentDevice