Spaces:
Running
Running
Upload index.js with huggingface_hub
Browse files
index.js
CHANGED
|
@@ -1,76 +1,41 @@
|
|
| 1 |
-
import { pipeline } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.7.
|
| 2 |
-
|
| 3 |
-
//
|
| 4 |
-
const
|
| 5 |
-
const
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
}
|
| 26 |
-
|
| 27 |
-
const reader = new FileReader();
|
| 28 |
-
|
| 29 |
-
// Set up a callback when the file is loaded
|
| 30 |
-
reader.onload = e2 => detect(e2.target.result);
|
| 31 |
-
|
| 32 |
-
reader.readAsDataURL(file);
|
| 33 |
-
});
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
// Detect objects in the image
|
| 37 |
-
async function detect(img) {
|
| 38 |
-
imageContainer.innerHTML = '';
|
| 39 |
-
imageContainer.style.backgroundImage = `url(${img})`;
|
| 40 |
-
|
| 41 |
-
status.textContent = 'Analysing...';
|
| 42 |
-
const output = await detector(img, {
|
| 43 |
-
threshold: 0.5,
|
| 44 |
-
percentage: true,
|
| 45 |
-
});
|
| 46 |
-
status.textContent = '';
|
| 47 |
-
output.forEach(renderBox);
|
| 48 |
-
}
|
| 49 |
-
|
| 50 |
-
// Render a bounding box and label on the image
|
| 51 |
-
function renderBox({ box, label }) {
|
| 52 |
-
const { xmax, xmin, ymax, ymin } = box;
|
| 53 |
-
|
| 54 |
-
// Generate a random color for the box
|
| 55 |
-
const color = '#' + Math.floor(Math.random() * 0xFFFFFF).toString(16).padStart(6, 0);
|
| 56 |
-
|
| 57 |
-
// Draw the box
|
| 58 |
-
const boxElement = document.createElement('div');
|
| 59 |
-
boxElement.className = 'bounding-box';
|
| 60 |
-
Object.assign(boxElement.style, {
|
| 61 |
-
borderColor: color,
|
| 62 |
-
left: 100 * xmin + '%',
|
| 63 |
-
top: 100 * ymin + '%',
|
| 64 |
-
width: 100 * (xmax - xmin) + '%',
|
| 65 |
-
height: 100 * (ymax - ymin) + '%',
|
| 66 |
-
})
|
| 67 |
-
|
| 68 |
-
// Draw label
|
| 69 |
-
const labelElement = document.createElement('span');
|
| 70 |
-
labelElement.textContent = label;
|
| 71 |
-
labelElement.className = 'bounding-box-label';
|
| 72 |
-
labelElement.style.backgroundColor = color;
|
| 73 |
-
|
| 74 |
-
boxElement.appendChild(labelElement);
|
| 75 |
-
imageContainer.appendChild(boxElement);
|
| 76 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { pipeline, TextStreamer } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.7.3';
|
| 2 |
+
|
| 3 |
+
// --- Configuration ---
|
| 4 |
+
const MODEL_ID = "onnx-community/gemma-3-270m-it-ONNX";
|
| 5 |
+
const SYSTEM_PROMPT = { role: "system", content: "You are a helpful assistant." };
|
| 6 |
+
|
| 7 |
+
// --- State ---
|
| 8 |
+
let generator = null;
|
| 9 |
+
let messages = [SYSTEM_PROMPT];
|
| 10 |
+
let isGenerating = false;
|
| 11 |
+
let currentDevice = 'cpu';
|
| 12 |
+
|
| 13 |
+
// --- DOM Elements ---
|
| 14 |
+
const chatContainer = document.getElementById('chat-container');
|
| 15 |
+
const chatForm = document.getElementById('chat-form');
|
| 16 |
+
const userInput = document.getElementById('user-input');
|
| 17 |
+
const sendBtn = document.getElementById('send-btn');
|
| 18 |
+
const statusBar = document.getElementById('status-bar');
|
| 19 |
+
const statusText = document.querySelector('.status-text');
|
| 20 |
+
const progressBar = document.getElementById('progress-bar');
|
| 21 |
+
const deviceSelect = document.getElementById('device-select');
|
| 22 |
+
const resetBtn = document.getElementById('reset-btn');
|
| 23 |
+
|
| 24 |
+
// --- Initialization ---
|
| 25 |
+
function checkWebGPU() {
|
| 26 |
+
if (!navigator.gpu) {
|
| 27 |
+
const gpuOption = deviceSelect.querySelector('option[value="webgpu"]');
|
| 28 |
+
if (gpuOption) {
|
| 29 |
+
gpuOption.disabled = true;
|
| 30 |
+
gpuOption.textContent = "GPU (WebGPU not available)";
|
| 31 |
+
}
|
| 32 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
}
|
| 34 |
+
checkWebGPU();
|
| 35 |
+
|
| 36 |
+
// --- Pipeline Management ---
|
| 37 |
+
async function loadGenerator() {
|
| 38 |
+
const selectedDevice = deviceSelect.value;
|
| 39 |
+
|
| 40 |
+
// If we already have a generator and the device hasn't changed, return it
|
| 41 |
+
if (generator && currentDevice
|