Spaces:
Running
Running
File size: 15,536 Bytes
1afc395 d9e145a 1afc395 2d26b26 1afc395 c16097d 1afc395 f45f033 1afc395 9772eed c30fe23 1afc395 50ea48d 1afc395 f37c00e 1afc395 6566197 617c91e 1afc395 81a9a45 66d0e91 1afc395 c2e0fdf 1afc395 2d26b26 1afc395 092c377 1afc395 092c377 1afc395 dd2578b 1afc395 ca48a7f 092c377 1afc395 61895cb 1afc395 092b1bf 1afc395 092b1bf 1afc395 d9e145a 05a44a1 d9e145a c280318 1afc395 |
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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 |
// Copyright 2025 The MediaPipe Authors.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// -------------------------------------------------------------------------- //
import {oauthLoginUrl, oauthHandleRedirectIfPresent} from "@huggingface/hub";
import {FilesetResolver, LlmInference} from '@mediapipe/tasks-genai';
// --- DOM Element References ---
const webcamElement = document.getElementById('webcam');
const statusMessageElement = document.getElementById(
'status-message',
);
const responseContainer = document.getElementById(
'response-container',
);
const promptInputElement = document.getElementById(
'prompt-input',
);
const recordButton = document.getElementById(
'record-button',
);
const sendButton = document.getElementById('send-button');
const clearCacheButton = document.getElementById('clear-cache-button');
const recordButtonIcon = recordButton.querySelector('i');
const loaderOverlay = document.getElementById('loader-overlay');
const progressBarFill = document.getElementById('progress-bar-fill');
const signInMessage = document.getElementById('sign-in-message');
const loaderMessage = document.getElementById('loader-message');
const versionText = document.getElementById('version-text');
const toggleVersionButton = document.getElementById('toggle-version-button');
// --- State Management ---
let isRecording = false;
let isLoading = false;
let mediaRecorder = null;
let audioChunks = [];
// --- Model-specific constants ---
// If the user wants to try running on a more limited device, they can switch
// the demo from default E4B to E2B by appending '?e2b' to the URL.
const thisUrl = new URL(window.location.href);
const use_e4b = !thisUrl.searchParams.has('e2b');
const cacheFileName = use_e4b ? "3n_e4b" : "3n_e2b";
const remoteFileUrl = use_e4b ? 'https://huggingface.co/google/gemma-3n-E4B-it-litert-lm/resolve/main/gemma-3n-E4B-it-int4-Web.litertlm'
: 'https://huggingface.co/google/gemma-3n-E2B-it-litert-lm/resolve/main/gemma-3n-E2B-it-int4-Web.litertlm';
// Model size in bytes for reliable progress indication; just hard-coded for now
const modelSize = use_e4b ? 4275044352 : 3038117888;
// --- Core Functions ---
/**
* Updates the progress bar's width.
* @param {number} percentage The progress percentage (0-100).
*/
function updateProgressBar(percentage) {
if (progressBarFill) {
progressBarFill.style.width = `${percentage}%`;
}
}
/**
* Initializes our local LLM from a StreamReader.
*/
let llmInference;
async function initLlm(modelReader) {
console.log('Initializing LLM');
loaderMessage.textContent = "Initializing model...";
// We have no actual progress updates for this last initialization step, but
// it's relatively short (<10s on a decent laptop). So we just set to 90%.
// TODO: It'd look nicer to have this go from 0 to 100% instead.
updateProgressBar(90);
const genaiFileset = await FilesetResolver.forGenAiTasks(
'https://cdn.jsdelivr.net/npm/@mediapipe/tasks-genai/wasm');
try {
llmInference = await LlmInference.createFromOptions(genaiFileset, {
baseOptions: {modelAssetBuffer: modelReader},
maxTokens: 2048,
maxNumImages: 1,
supportAudio: true,
});
// Enable demo now that loading has fully finished.
loaderOverlay.style.opacity = '0';
setTimeout(() => {
loaderOverlay.style.display = 'none';
promptInputElement.disabled = false;
sendButton.disabled = false;
recordButton.disabled = false;
}, 300);
} catch (error) {
console.error('Failed to initialize the LLM', error);
loaderOverlay.style.display = 'none'; // Hide loader on error
}
}
/**
* Replaces our demo with a sign-in button for HuggingFace.
*/
function requireSignIn() {
document.getElementById('loader-overlay').style = "display:none";
document.getElementById('main-container').style = "display:none";
document.body.parentElement.prepend(document.getElementById('title-container'));
document.body.appendChild(document.getElementById('version-info'));
document.getElementById("signin").style.removeProperty("display");
document.getElementById('sign-in-message').style.removeProperty("display");
document.getElementById("signin").onclick = async function() {
// prompt=consent to re-trigger the consent screen instead of silently redirecting
window.location.href = (await oauthLoginUrl({scopes: window.huggingface.variables.OAUTH_SCOPES})) + "&prompt=consent";
};
// clear old oauth, if any
localStorage.removeItem("oauth");
}
/**
* Utility function to show progress while we load from remote file into local
* cache.
*/
async function pipeStreamAndReportProgress(readableStream, writableStream) {
// Alert the user if browser expects caching to run out of memory.
const cacheEstimate = await navigator.storage.estimate();
if (modelSize > (cacheEstimate.quota - cacheEstimate.usage)) {
alert(`The browser reports it does not have enough space in cache for this model. Ensure you are not running in incognito mode, or else try to free up some space. Model size: ${modelSize}. Cache quota: ${cacheEstimate.quota}. Cache usage: ${cacheEstimate.usage}.`);
}
// Effectively "await responseStream.pipeTo(writeStream)", but with progress
// reporting.
const reader = readableStream.getReader();
const writer = writableStream.getWriter();
let bytesCount = 0;
let progressBarPercent = 0;
let wasAborted = false;
try {
while (true) {
const {done, value} = await reader.read();
if (done) {
break;
}
if (value) {
bytesCount += value.length;
const percentage = Math.round(bytesCount / modelSize * 90);
if (percentage > progressBarPercent) {
progressBarPercent = percentage;
updateProgressBar(progressBarPercent);
const downloadedMB = (bytesCount / 1e6).toFixed(2);
const totalMB = (modelSize / 1e6).toFixed(2);
loaderMessage.textContent =
`Downloading model: ${downloadedMB}MB / ${totalMB}MB`;
}
await writer.write(value);
}
}
} catch (error) {
console.error('Error while piping stream:', error);
// Abort the writer if there's an error
wasAborted = true;
await writer.abort(error);
throw error;
} finally {
// Release the reader lock
reader.releaseLock();
// Close the writer only if the stream wasn't aborted
if (!wasAborted) {
console.log('Closing the writer, and hence the stream');
await writer.close();
}
}
}
/**
* Loads the LLM file from either cache or OAuth-guarded remote download.
*/
async function loadLlm() {
// For browsers other than Chrome and Edge, we just show an alert and error out.
const isChrome = navigator.userAgent.includes('Chrome');
const isEdge = navigator.userAgent.includes('Edg');
if (!isChrome && !isEdge) {
alert('Non-Chromium browsers are not supported yet. Please run demo on Chrome for the best experience.');
loaderMessage.textContent = "Non-Chromium browsers are not supported yet. Please run on Chrome for best experience.";
return;
}
let opfs = await navigator.storage.getDirectory();
// If we can load the model from cache, then do so.
try {
const fileHandle = await opfs.getFileHandle(cacheFileName);
// Check to make sure size is as expected, and not a partially-downloaded
// or corrupt file.
console.log('Model found in cache; checking size.');
const file = await fileHandle.getFile();
console.log('File size is: ', file.size);
if (file.size !== modelSize) {
console.error('Cached model had unexpected size. Redownloading.');
throw new Error('Unexpected cached model size');
}
console.log('Model found in cache of expected size, reusing.');
const fileReader = file.stream().getReader();
await initLlm(fileReader);
} catch {
// Otherwise, we need to be download remotely, which requires oauth.
console.log('Model not found in cache: oauth and download required.');
// We first remove from cache, in case model file is corrupted/partial.
try {
await opfs.removeEntry(cacheFileName);
} catch {}
let oauthResult = localStorage.getItem("oauth");
if (oauthResult) {
try {
oauthResult = JSON.parse(oauthResult);
} catch {
oauthResult = null;
}
}
oauthResult ||= await oauthHandleRedirectIfPresent();
// If we have successful oauth from one of the methods above, download from
// remote.
if (oauthResult?.accessToken) {
localStorage.setItem("oauth", JSON.stringify(oauthResult));
const modelUrl = remoteFileUrl;
const oauthHeaders = {
"Authorization": `Bearer ${oauthResult.accessToken}`
};
const response = await fetch(modelUrl, {headers: oauthHeaders});
if (response.ok) {
const responseStream = await response.body;
// Cache locally, so we can avoid this next time.
const fileHandle =
await opfs.getFileHandle(cacheFileName, {create: true});
const writeStream = await fileHandle.createWritable();
await pipeStreamAndReportProgress(responseStream, writeStream);
console.log('Model written to cache!');
const file = await fileHandle.getFile();
const fileReader = file.stream().getReader();
await initLlm(fileReader);
} else {
console.error('Model fetch encountered error. Likely requires sign-in or Gemma license acknowledgement.');
requireSignIn();
}
} else {
// No successful oauth, so replace our demo with a HuggingFace sign-in button.
console.log('No oauth detected. Requiring sign-in.');
requireSignIn();
}
}
}
/**
* Initializes the webcam and microphone.
*/
let audioUrl = undefined;
async function initMedia() {
versionText.textContent = use_e4b ? 'E4B' : 'E2B';
toggleVersionButton.textContent = use_e4b ? 'Switch to E2B' : 'Switch to E4B';
// Disable controls on startup
promptInputElement.disabled = true;
sendButton.disabled = true;
recordButton.disabled = true;
try {
const videoStream = await navigator.mediaDevices.getUserMedia({video: true});
const audioStream = await navigator.mediaDevices.getUserMedia({audio: true});
webcamElement.srcObject = videoStream;
statusMessageElement.style.display = 'none';
webcamElement.style.display = 'block';
await loadLlm();
// Set up MediaRecorder for audio
mediaRecorder = new MediaRecorder(audioStream);
mediaRecorder.ondataavailable = (event) => {
console.log('ondataavailable event: ', event);
audioChunks.push(event.data);
};
mediaRecorder.onstop = () => {
// We process the audio here, and free the previous one, if any
const blob = new Blob(audioChunks, {type: 'audio/webm'});
if (audioUrl) window.URL.revokeObjectURL(audioUrl);
audioUrl = window.URL.createObjectURL(blob);
audioChunks = [];
sendQuery({audioSource: audioUrl});
};
} catch (error) {
console.error('Error accessing media devices.', error);
audioUrl = undefined;
statusMessageElement.textContent =
'Error: Could not access camera or microphone. Please check permissions.';
loaderOverlay.style.display = 'none'; // Hide loader on error
}
}
/**
* Toggles the audio recording state.
*/
function toggleRecording() {
isRecording = !isRecording;
if (isRecording) {
if (mediaRecorder && mediaRecorder.state === 'inactive') {
mediaRecorder.start();
}
recordButton.classList.add('recording');
if (recordButtonIcon) {
recordButtonIcon.className = 'fa-solid fa-stop';
}
promptInputElement.placeholder = 'Recording... Press stop when done.';
} else {
if (mediaRecorder && mediaRecorder.state === 'recording') {
mediaRecorder.stop();
}
recordButton.classList.remove('recording');
if (recordButtonIcon) {
recordButtonIcon.className = 'fa-solid fa-microphone';
}
promptInputElement.placeholder = 'Ask a question about what you see...';
}
}
/**
* Sends a text prompt with webcam frame to the Gemma 3n model.
*/
async function sendTextQuery() {
const prompt = promptInputElement.value.trim();
sendQuery(prompt);
}
/**
* Sends the user's prompt (text or audio) with webcam frame to the Gemma 3n model.
*/
async function sendQuery(prompt) {
if (!prompt || isLoading) {
return;
}
setLoading(true);
try {
const query = [
'<start_of_turn>user\n',
prompt, // audio or text
{imageSource: webcam},
'<end_of_turn>\n<start_of_turn>model\n',
];
let resultSoFar = '';
await llmInference.generateResponse(query, (newText, isDone) => {
resultSoFar += newText;
updateResponse(resultSoFar);
});
promptInputElement.value = '';
} catch (error) {
console.error('Error running Gemma 3n on query.', error);
updateResponse(
`Error: Could not get a response. ${error instanceof Error ? error.message : String(error)}`,
);
} finally {
setLoading(false);
}
}
/**
* Updates the response container with new content.
* @param {string} text The text to display.
*/
function updateResponse(text) {
responseContainer.classList.remove('thinking');
responseContainer.innerHTML = '';
const p = document.createElement('p');
p.textContent = text;
responseContainer.appendChild(p);
}
/**
* Sets the loading state of the UI.
* @param {boolean} loading - True if loading, false otherwise.
*/
function setLoading(loading) {
isLoading = loading;
promptInputElement.disabled = loading;
sendButton.disabled = loading;
recordButton.disabled = loading;
if (loading) {
responseContainer.classList.add('thinking');
responseContainer.innerHTML = '<p>Processing...</p>';
}
}
// --- Event Listeners ---
recordButton.addEventListener('click', toggleRecording);
sendButton.addEventListener('click', sendTextQuery);
promptInputElement.addEventListener('keydown', (event) => {
if (event.key === 'Enter') {
sendTextQuery();
}
});
clearCacheButton.addEventListener('click', async () => {
const userConfirmed = confirm(
'Are you sure you want to clear the cached model? ' +
'This will require re-downloading the model on the next visit.'
);
if (userConfirmed) {
try {
const opfs = await navigator.storage.getDirectory();
await opfs.removeEntry(cacheFileName);
console.log('Cache cleared successfully.');
clearCacheButton.style.display = 'none';
} catch (error) {
console.error('Error clearing cache:', error);
}
}
});
toggleVersionButton.addEventListener('click', () => {
const url = new URL(window.location.href);
if (use_e4b) {
url.searchParams.set('e2b', 'true');
} else {
url.searchParams.delete('e2b');
}
window.location.href = url.href;
});
// --- Initialization ---
document.addEventListener('DOMContentLoaded', initMedia);
|