Commit
·
d2ef7f6
1
Parent(s):
84dfa29
update_space
Browse files- README.md +6 -5
- index.html +489 -19
- style.css +0 -28
README.md
CHANGED
|
@@ -1,11 +1,12 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
colorTo: yellow
|
| 6 |
sdk: static
|
| 7 |
pinned: false
|
| 8 |
-
|
|
|
|
| 9 |
---
|
| 10 |
|
| 11 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Realtime_LLM_Camera_Interaction_App
|
| 3 |
+
emoji: ⚡️
|
| 4 |
+
colorFrom: blue
|
| 5 |
colorTo: yellow
|
| 6 |
sdk: static
|
| 7 |
pinned: false
|
| 8 |
+
models:
|
| 9 |
+
- HuggingFaceTB/SmolVLM-500M-Instruct
|
| 10 |
---
|
| 11 |
|
| 12 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
index.html
CHANGED
|
@@ -1,19 +1,489 @@
|
|
| 1 |
-
<!
|
| 2 |
-
<html>
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8" />
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
| 6 |
+
<title>Vision AI Assistant</title>
|
| 7 |
+
<style>
|
| 8 |
+
:root {
|
| 9 |
+
--primary: #4f46e5;
|
| 10 |
+
--primary-light: #6366f1;
|
| 11 |
+
--success: #10b981;
|
| 12 |
+
--danger: #ef4444;
|
| 13 |
+
--dark: #1e293b;
|
| 14 |
+
--light: #f8fafc;
|
| 15 |
+
--gray: #94a3b8;
|
| 16 |
+
--border: #e2e8f0;
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
* {
|
| 20 |
+
margin: 0;
|
| 21 |
+
padding: 0;
|
| 22 |
+
box-sizing: border-box;
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
body {
|
| 26 |
+
font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
|
| 27 |
+
background-color: #f1f5f9;
|
| 28 |
+
color: var(--dark);
|
| 29 |
+
min-height: 100vh;
|
| 30 |
+
display: flex;
|
| 31 |
+
flex-direction: column;
|
| 32 |
+
align-items: center;
|
| 33 |
+
padding: 2rem;
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
.app-container {
|
| 37 |
+
width: 100%;
|
| 38 |
+
max-width: 800px;
|
| 39 |
+
display: flex;
|
| 40 |
+
flex-direction: column;
|
| 41 |
+
gap: 1.5rem;
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
h1 {
|
| 45 |
+
color: var(--primary);
|
| 46 |
+
margin-bottom: 0.5rem;
|
| 47 |
+
text-align: center;
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
.subtitle {
|
| 51 |
+
color: var(--gray);
|
| 52 |
+
text-align: center;
|
| 53 |
+
margin-bottom: 2rem;
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
.video-container {
|
| 57 |
+
position: relative;
|
| 58 |
+
width: 100%;
|
| 59 |
+
aspect-ratio: 4/3;
|
| 60 |
+
background-color: #000;
|
| 61 |
+
border-radius: 12px;
|
| 62 |
+
overflow: hidden;
|
| 63 |
+
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
#videoFeed {
|
| 67 |
+
width: 100%;
|
| 68 |
+
height: 100%;
|
| 69 |
+
object-fit: cover;
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
.loading-overlay {
|
| 73 |
+
position: absolute;
|
| 74 |
+
top: 0;
|
| 75 |
+
left: 0;
|
| 76 |
+
width: 100%;
|
| 77 |
+
height: 100%;
|
| 78 |
+
display: none;
|
| 79 |
+
flex-direction: column;
|
| 80 |
+
justify-content: center;
|
| 81 |
+
align-items: center;
|
| 82 |
+
background-color: rgba(0, 0, 0, 0.7);
|
| 83 |
+
color: white;
|
| 84 |
+
font-weight: 600;
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
.loading-spinner {
|
| 88 |
+
width: 40px;
|
| 89 |
+
height: 40px;
|
| 90 |
+
border: 4px solid rgba(255, 255, 255, 0.3);
|
| 91 |
+
border-radius: 50%;
|
| 92 |
+
border-top-color: white;
|
| 93 |
+
animation: spin 1s ease-in-out infinite;
|
| 94 |
+
margin-bottom: 1rem;
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
@keyframes spin {
|
| 98 |
+
to { transform: rotate(360deg); }
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
.control-panel {
|
| 102 |
+
background: white;
|
| 103 |
+
border-radius: 12px;
|
| 104 |
+
padding: 1.5rem;
|
| 105 |
+
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
.input-group {
|
| 109 |
+
display: flex;
|
| 110 |
+
flex-direction: column;
|
| 111 |
+
gap: 0.5rem;
|
| 112 |
+
margin-bottom: 1.5rem;
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
label {
|
| 116 |
+
font-weight: 600;
|
| 117 |
+
color: var(--dark);
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
textarea {
|
| 121 |
+
width: 100%;
|
| 122 |
+
padding: 0.75rem;
|
| 123 |
+
border: 1px solid var(--border);
|
| 124 |
+
border-radius: 8px;
|
| 125 |
+
font-family: inherit;
|
| 126 |
+
resize: vertical;
|
| 127 |
+
min-height: 60px;
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
textarea:focus {
|
| 131 |
+
outline: none;
|
| 132 |
+
border-color: var(--primary-light);
|
| 133 |
+
box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.2);
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
.controls {
|
| 137 |
+
display: flex;
|
| 138 |
+
gap: 1rem;
|
| 139 |
+
align-items: center;
|
| 140 |
+
flex-wrap: wrap;
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
.interval-select {
|
| 144 |
+
flex-grow: 1;
|
| 145 |
+
display: flex;
|
| 146 |
+
align-items: center;
|
| 147 |
+
gap: 0.5rem;
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
select {
|
| 151 |
+
padding: 0.5rem;
|
| 152 |
+
border-radius: 8px;
|
| 153 |
+
border: 1px solid var(--border);
|
| 154 |
+
background-color: white;
|
| 155 |
+
flex-grow: 1;
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
.btn {
|
| 159 |
+
padding: 0.75rem 1.5rem;
|
| 160 |
+
border-radius: 8px;
|
| 161 |
+
font-weight: 600;
|
| 162 |
+
cursor: pointer;
|
| 163 |
+
border: none;
|
| 164 |
+
transition: all 0.2s;
|
| 165 |
+
}
|
| 166 |
+
|
| 167 |
+
.btn-primary {
|
| 168 |
+
background-color: var(--primary);
|
| 169 |
+
color: white;
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
.btn-primary:hover {
|
| 173 |
+
background-color: var(--primary-light);
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
.btn-danger {
|
| 177 |
+
background-color: var(--danger);
|
| 178 |
+
color: white;
|
| 179 |
+
}
|
| 180 |
+
|
| 181 |
+
.btn-danger:hover {
|
| 182 |
+
background-color: #dc2626;
|
| 183 |
+
}
|
| 184 |
+
|
| 185 |
+
.status-indicator {
|
| 186 |
+
display: flex;
|
| 187 |
+
align-items: center;
|
| 188 |
+
gap: 0.5rem;
|
| 189 |
+
margin-top: 1rem;
|
| 190 |
+
font-size: 0.875rem;
|
| 191 |
+
color: var(--gray);
|
| 192 |
+
}
|
| 193 |
+
|
| 194 |
+
.status-dot {
|
| 195 |
+
width: 10px;
|
| 196 |
+
height: 10px;
|
| 197 |
+
border-radius: 50%;
|
| 198 |
+
background-color: var(--gray);
|
| 199 |
+
}
|
| 200 |
+
|
| 201 |
+
.status-dot.active {
|
| 202 |
+
background-color: var(--success);
|
| 203 |
+
}
|
| 204 |
+
|
| 205 |
+
.status-dot.processing {
|
| 206 |
+
background-color: var(--primary);
|
| 207 |
+
animation: pulse 1.5s infinite;
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
@keyframes pulse {
|
| 211 |
+
0% { opacity: 1; }
|
| 212 |
+
50% { opacity: 0.5; }
|
| 213 |
+
100% { opacity: 1; }
|
| 214 |
+
}
|
| 215 |
+
|
| 216 |
+
.hidden {
|
| 217 |
+
display: none;
|
| 218 |
+
}
|
| 219 |
+
|
| 220 |
+
canvas {
|
| 221 |
+
display: none;
|
| 222 |
+
}
|
| 223 |
+
</style>
|
| 224 |
+
</head>
|
| 225 |
+
<body>
|
| 226 |
+
<div class="app-container">
|
| 227 |
+
<h1>Vision AI Assistant</h1>
|
| 228 |
+
<p class="subtitle">Ask questions about what your camera sees</p>
|
| 229 |
+
|
| 230 |
+
<div class="video-container">
|
| 231 |
+
<video id="videoFeed" autoplay playsinline></video>
|
| 232 |
+
<div class="loading-overlay" id="loadingOverlay">
|
| 233 |
+
<div class="loading-spinner"></div>
|
| 234 |
+
<span id="loadingText">Loading AI Model</span>
|
| 235 |
+
</div>
|
| 236 |
+
</div>
|
| 237 |
+
|
| 238 |
+
<div class="control-panel">
|
| 239 |
+
<div class="input-group">
|
| 240 |
+
<label for="instructionText">Your Question</label>
|
| 241 |
+
<textarea
|
| 242 |
+
id="instructionText"
|
| 243 |
+
placeholder="Ask me about what you see..."
|
| 244 |
+
>What do you see?</textarea>
|
| 245 |
+
</div>
|
| 246 |
+
|
| 247 |
+
<div class="input-group">
|
| 248 |
+
<label for="responseText">AI Response</label>
|
| 249 |
+
<textarea
|
| 250 |
+
id="responseText"
|
| 251 |
+
readonly
|
| 252 |
+
placeholder="The AI response will appear here..."
|
| 253 |
+
></textarea>
|
| 254 |
+
</div>
|
| 255 |
+
|
| 256 |
+
<div class="controls">
|
| 257 |
+
<div class="interval-select">
|
| 258 |
+
<label for="intervalSelect">Interval:</label>
|
| 259 |
+
<select id="intervalSelect">
|
| 260 |
+
<option value="0" selected>Manual</option>
|
| 261 |
+
<option value="100">100ms</option>
|
| 262 |
+
<option value="250">250ms</option>
|
| 263 |
+
<option value="500">500ms</option>
|
| 264 |
+
<option value="1000">1s</option>
|
| 265 |
+
<option value="2000">2s</option>
|
| 266 |
+
</select>
|
| 267 |
+
</div>
|
| 268 |
+
|
| 269 |
+
<button id="startButton" class="btn btn-primary">Start</button>
|
| 270 |
+
</div>
|
| 271 |
+
|
| 272 |
+
<div class="status-indicator">
|
| 273 |
+
<div class="status-dot" id="statusDot"></div>
|
| 274 |
+
<span id="statusText">Initializing</span>
|
| 275 |
+
</div>
|
| 276 |
+
</div>
|
| 277 |
+
</div>
|
| 278 |
+
|
| 279 |
+
<canvas id="canvas" class="hidden"></canvas>
|
| 280 |
+
|
| 281 |
+
<script type="module">
|
| 282 |
+
import {
|
| 283 |
+
AutoProcessor,
|
| 284 |
+
AutoModelForVision2Seq,
|
| 285 |
+
RawImage,
|
| 286 |
+
} from "https://cdn.jsdelivr.net/npm/@huggingface/transformers/dist/transformers.min.js";
|
| 287 |
+
|
| 288 |
+
const video = document.getElementById("videoFeed");
|
| 289 |
+
const canvas = document.getElementById("canvas");
|
| 290 |
+
const instructionText = document.getElementById("instructionText");
|
| 291 |
+
const responseText = document.getElementById("responseText");
|
| 292 |
+
const intervalSelect = document.getElementById("intervalSelect");
|
| 293 |
+
const startButton = document.getElementById("startButton");
|
| 294 |
+
const loadingOverlay = document.getElementById("loadingOverlay");
|
| 295 |
+
const loadingText = document.getElementById("loadingText");
|
| 296 |
+
const statusDot = document.getElementById("statusDot");
|
| 297 |
+
const statusText = document.getElementById("statusText");
|
| 298 |
+
|
| 299 |
+
let stream;
|
| 300 |
+
let isProcessing = false;
|
| 301 |
+
let processor, model;
|
| 302 |
+
|
| 303 |
+
function updateStatus(text, state) {
|
| 304 |
+
statusText.textContent = text;
|
| 305 |
+
statusDot.className = 'status-dot';
|
| 306 |
+
if (state === 'active') {
|
| 307 |
+
statusDot.classList.add('active');
|
| 308 |
+
} else if (state === 'processing') {
|
| 309 |
+
statusDot.classList.add('processing');
|
| 310 |
+
}
|
| 311 |
+
}
|
| 312 |
+
|
| 313 |
+
async function initModel() {
|
| 314 |
+
const modelId = "HuggingFaceTB/SmolVLM-500M-Instruct";
|
| 315 |
+
loadingOverlay.style.display = "flex";
|
| 316 |
+
loadingText.textContent = "Loading processor...";
|
| 317 |
+
updateStatus("Loading processor...", "processing");
|
| 318 |
+
|
| 319 |
+
try {
|
| 320 |
+
processor = await AutoProcessor.from_pretrained(modelId);
|
| 321 |
+
loadingText.textContent = "Loading model...";
|
| 322 |
+
updateStatus("Loading model...", "processing");
|
| 323 |
+
|
| 324 |
+
// Modified model configuration to use FP32 instead of FP16
|
| 325 |
+
model = await AutoModelForVision2Seq.from_pretrained(modelId, {
|
| 326 |
+
dtype: {
|
| 327 |
+
embed_tokens: "fp32", // Changed from fp16 to fp32
|
| 328 |
+
vision_encoder: "q4",
|
| 329 |
+
decoder_model_merged: "q4",
|
| 330 |
+
},
|
| 331 |
+
device: "webgpu",
|
| 332 |
+
});
|
| 333 |
+
|
| 334 |
+
loadingOverlay.style.display = "none";
|
| 335 |
+
updateStatus("Ready", "active");
|
| 336 |
+
responseText.value = "Model loaded successfully. Ready to start.";
|
| 337 |
+
} catch (error) {
|
| 338 |
+
console.error("Model loading error:", error);
|
| 339 |
+
loadingText.textContent = "Model loading failed";
|
| 340 |
+
responseText.value = `Error loading model: ${error.message}`;
|
| 341 |
+
updateStatus("Error loading model", "");
|
| 342 |
+
throw error;
|
| 343 |
+
}
|
| 344 |
+
}
|
| 345 |
+
|
| 346 |
+
async function initCamera() {
|
| 347 |
+
try {
|
| 348 |
+
updateStatus("Accessing camera...", "processing");
|
| 349 |
+
stream = await navigator.mediaDevices.getUserMedia({
|
| 350 |
+
video: true,
|
| 351 |
+
audio: false,
|
| 352 |
+
});
|
| 353 |
+
video.srcObject = stream;
|
| 354 |
+
responseText.value = "Camera access granted. Ready to start.";
|
| 355 |
+
} catch (err) {
|
| 356 |
+
console.error("Error accessing camera:", err);
|
| 357 |
+
responseText.value = `Error accessing camera: ${err.name} - ${err.message}. Please ensure permissions are granted and you are on HTTPS or localhost.`;
|
| 358 |
+
updateStatus("Camera error", "");
|
| 359 |
+
alert(
|
| 360 |
+
`Error accessing camera: ${err.name}. Make sure you've granted permission and are on HTTPS or localhost.`
|
| 361 |
+
);
|
| 362 |
+
}
|
| 363 |
+
}
|
| 364 |
+
|
| 365 |
+
function captureImage() {
|
| 366 |
+
if (!stream || !video.videoWidth) {
|
| 367 |
+
console.warn("Video stream not ready for capture.");
|
| 368 |
+
return null;
|
| 369 |
+
}
|
| 370 |
+
canvas.width = video.videoWidth;
|
| 371 |
+
canvas.height = video.videoHeight;
|
| 372 |
+
const context = canvas.getContext("2d", { willReadFrequently: true });
|
| 373 |
+
context.drawImage(video, 0, 0, canvas.width, canvas.height);
|
| 374 |
+
const frame = context.getImageData(0, 0, canvas.width, canvas.height);
|
| 375 |
+
return new RawImage(frame.data, frame.width, frame.height, 4);
|
| 376 |
+
}
|
| 377 |
+
|
| 378 |
+
async function runLocalVisionInference(imgElement, instruction) {
|
| 379 |
+
const messages = [
|
| 380 |
+
{
|
| 381 |
+
role: "user",
|
| 382 |
+
content: [{ type: "image" }, { type: "text", text: instruction }],
|
| 383 |
+
},
|
| 384 |
+
];
|
| 385 |
+
const text = processor.apply_chat_template(messages, {
|
| 386 |
+
add_generation_prompt: true,
|
| 387 |
+
});
|
| 388 |
+
const inputs = await processor(text, [imgElement], {
|
| 389 |
+
do_image_splitting: false,
|
| 390 |
+
});
|
| 391 |
+
const generatedIds = await model.generate({
|
| 392 |
+
...inputs,
|
| 393 |
+
max_new_tokens: 100,
|
| 394 |
+
});
|
| 395 |
+
const output = processor.batch_decode(
|
| 396 |
+
generatedIds.slice(null, [inputs.input_ids.dims.at(-1), null]),
|
| 397 |
+
{ skip_special_tokens: true }
|
| 398 |
+
);
|
| 399 |
+
return output[0].trim();
|
| 400 |
+
}
|
| 401 |
+
|
| 402 |
+
async function sendData() {
|
| 403 |
+
if (!isProcessing) return;
|
| 404 |
+
const instruction = instructionText.value;
|
| 405 |
+
const rawImg = captureImage();
|
| 406 |
+
if (!rawImg) {
|
| 407 |
+
responseText.value = "Capture failed";
|
| 408 |
+
return;
|
| 409 |
+
}
|
| 410 |
+
try {
|
| 411 |
+
const reply = await runLocalVisionInference(rawImg, instruction);
|
| 412 |
+
responseText.value = reply;
|
| 413 |
+
} catch (e) {
|
| 414 |
+
console.error(e);
|
| 415 |
+
responseText.value = `Error: ${e.message}`;
|
| 416 |
+
}
|
| 417 |
+
}
|
| 418 |
+
|
| 419 |
+
function sleep(ms) {
|
| 420 |
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
| 421 |
+
}
|
| 422 |
+
|
| 423 |
+
async function processingLoop() {
|
| 424 |
+
const intervalMs = parseInt(intervalSelect.value, 10);
|
| 425 |
+
while (isProcessing) {
|
| 426 |
+
await sendData();
|
| 427 |
+
if (!isProcessing) break;
|
| 428 |
+
await sleep(intervalMs);
|
| 429 |
+
}
|
| 430 |
+
}
|
| 431 |
+
|
| 432 |
+
function handleStart() {
|
| 433 |
+
if (!stream) {
|
| 434 |
+
responseText.value = "Camera not available. Cannot start.";
|
| 435 |
+
alert("Camera not available. Please grant permission first.");
|
| 436 |
+
return;
|
| 437 |
+
}
|
| 438 |
+
isProcessing = true;
|
| 439 |
+
startButton.textContent = "Stop";
|
| 440 |
+
startButton.classList.remove("btn-primary");
|
| 441 |
+
startButton.classList.add("btn-danger");
|
| 442 |
+
instructionText.disabled = true;
|
| 443 |
+
intervalSelect.disabled = true;
|
| 444 |
+
responseText.value = "Processing started...";
|
| 445 |
+
updateStatus("Processing...", "processing");
|
| 446 |
+
processingLoop();
|
| 447 |
+
}
|
| 448 |
+
|
| 449 |
+
function handleStop() {
|
| 450 |
+
isProcessing = false;
|
| 451 |
+
startButton.textContent = "Start";
|
| 452 |
+
startButton.classList.remove("btn-danger");
|
| 453 |
+
startButton.classList.add("btn-primary");
|
| 454 |
+
instructionText.disabled = false;
|
| 455 |
+
intervalSelect.disabled = false;
|
| 456 |
+
if (responseText.value.startsWith("Processing started...")) {
|
| 457 |
+
responseText.value = "Processing stopped.";
|
| 458 |
+
}
|
| 459 |
+
updateStatus("Ready", "active");
|
| 460 |
+
}
|
| 461 |
+
|
| 462 |
+
startButton.addEventListener("click", () => {
|
| 463 |
+
if (isProcessing) {
|
| 464 |
+
handleStop();
|
| 465 |
+
} else {
|
| 466 |
+
handleStart();
|
| 467 |
+
}
|
| 468 |
+
});
|
| 469 |
+
|
| 470 |
+
window.addEventListener("DOMContentLoaded", async () => {
|
| 471 |
+
if (!navigator.gpu) {
|
| 472 |
+
responseText.value = "WebGPU is not available in this browser. Please try Chrome or Edge with WebGPU enabled.";
|
| 473 |
+
updateStatus("WebGPU not available", "");
|
| 474 |
+
startButton.disabled = true;
|
| 475 |
+
return;
|
| 476 |
+
}
|
| 477 |
+
|
| 478 |
+
await initModel();
|
| 479 |
+
await initCamera();
|
| 480 |
+
});
|
| 481 |
+
|
| 482 |
+
window.addEventListener("beforeunload", () => {
|
| 483 |
+
if (stream) {
|
| 484 |
+
stream.getTracks().forEach((track) => track.stop());
|
| 485 |
+
}
|
| 486 |
+
});
|
| 487 |
+
</script>
|
| 488 |
+
</body>
|
| 489 |
+
</html>
|
style.css
DELETED
|
@@ -1,28 +0,0 @@
|
|
| 1 |
-
body {
|
| 2 |
-
padding: 2rem;
|
| 3 |
-
font-family: -apple-system, BlinkMacSystemFont, "Arial", sans-serif;
|
| 4 |
-
}
|
| 5 |
-
|
| 6 |
-
h1 {
|
| 7 |
-
font-size: 16px;
|
| 8 |
-
margin-top: 0;
|
| 9 |
-
}
|
| 10 |
-
|
| 11 |
-
p {
|
| 12 |
-
color: rgb(107, 114, 128);
|
| 13 |
-
font-size: 15px;
|
| 14 |
-
margin-bottom: 10px;
|
| 15 |
-
margin-top: 5px;
|
| 16 |
-
}
|
| 17 |
-
|
| 18 |
-
.card {
|
| 19 |
-
max-width: 620px;
|
| 20 |
-
margin: 0 auto;
|
| 21 |
-
padding: 16px;
|
| 22 |
-
border: 1px solid lightgray;
|
| 23 |
-
border-radius: 16px;
|
| 24 |
-
}
|
| 25 |
-
|
| 26 |
-
.card p:last-child {
|
| 27 |
-
margin-bottom: 0;
|
| 28 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|