Spaces:
Running
Running
File size: 10,732 Bytes
c4221c8 | 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 | import { pipeline, env } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.8.0';
// ============================================
// CONFIGURATION
// ============================================
const CONFIG = {
model: 'Xenova/Llama-3.2-3B-Instruct', // Quantized version for faster loading
maxTokens: 512,
temperature: 0.7,
topP: 0.9,
doSample: true,
repetitionPenalty: 1.1
};
// ============================================
// DOM ELEMENTS
// ============================================
const elements = {
chatContainer: document.getElementById('chat-container'),
messagesList: document.getElementById('messages-list'),
userInput: document.getElementById('user-input'),
sendBtn: document.getElementById('send-btn'),
loadingIndicator: document.getElementById('loading-indicator'),
loadingMessage: document.getElementById('loading-message'),
progressBar: document.getElementById('progress-bar'),
progressFill: document.getElementById('progress-fill'),
welcomeScreen: document.getElementById('welcome-screen'),
statusDot: document.querySelector('.status-dot'),
statusText: document.querySelector('.status-text'),
exampleBtns: document.querySelectorAll('.example-btn')
};
// ============================================
// APPLICATION STATE
// ============================================
let appState = {
modelLoaded: false,
isGenerating: false,
messages: []
};
// ============================================
// WORKER HANDLERS
// ============================================
let currentResponse = '';
let isWorkerBusy = false;
// Handle worker messages
worker.onmessage = (e) => {
const { type, payload } = e.data;
switch (type) {
case 'loading':
updateModelStatus('loading');
break;
case 'progress':
updateProgress(payload.progress, payload.message);
break;
case 'loaded':
appState.modelLoaded = true;
updateModelStatus('ready');
hideLoading();
hideWelcomeScreen();
break;
case 'result':
handleGenerationComplete(payload.result);
break;
case 'error':
showError(payload.error);
break;
case 'cancelled':
stopGeneration();
break;
}
};
// ============================================
// MODEL INITIALIZATION
// ============================================
async function initializeModel() {
try {
showLoading('Initializing model...');
worker.postMessage({
type: 'load',
payload: { model: CONFIG.model }
});
} catch (error) {
showError(`Failed to initialize model: ${error.message}`);
}
}
// ============================================
// GENERATION FUNCTIONS
// ============================================
async function sendMessage(message) {
if (!appState.modelLoaded || isWorkerBusy) return;
// Add user message to chat
addMessage('user', message);
appState.messages.push({ role: 'user', content: message });
// Clear input
elements.userInput.value = '';
elements.userInput.style.height = 'auto';
updateSendButton();
// Start generation
isWorkerBusy = true;
currentResponse = '';
showLoading('Generating response...');
try {
// Create prompt for the model
const prompt = createPrompt();
worker.postMessage({
type: 'generate',
payload: {
prompt: prompt,
maxTokens: CONFIG.maxTokens,
temperature: CONFIG.temperature,
topP: CONFIG.topP,
doSample: CONFIG.doSample,
repetitionPenalty: CONFIG.repetitionPenalty
}
});
} catch (error) {
showError(`Generation failed: ${error.message}`);
isWorkerBusy = false;
}
}
function createPrompt() {
// Convert messages to chat format
if (appState.messages.length === 0) {
return 'You are a helpful AI assistant. Please answer the user\'s question helpfully and accurately.';
}
return appState.messages
.map(msg => `${msg.role}: ${msg.content}`)
.join('\n\n') + '\n\nassistant:';
}
// ============================================
// MESSAGE HANDLING
// ============================================
function addMessage(role, content) {
const messageDiv = document.createElement('div');
messageDiv.className = `message ${role}`;
const avatar = role === 'user' ? '👤' : '🤖';
messageDiv.innerHTML = `
<div class="message-avatar">${avatar}</div>
<div class="message-content">${formatContent(content)}</div>
`;
elements.messagesList.appendChild(messageDiv);
scrollToBottom();
return messageDiv;
}
function handleGenerationComplete(generatedText) {
// Add AI response to chat
const messageDiv = addMessage('ai', '');
// Create content element for streaming
const contentElement = messageDiv.querySelector('.message-content');
contentElement.classList.add('typing-cursor');
// Stream the response
streamText(contentElement, generatedText)
.then(() => {
contentElement.classList.remove('typing-cursor');
appState.messages.push({ role: 'assistant', content: generatedText });
isWorkerBusy = false;
hideLoading();
})
.catch(error => {
showError(`Streaming failed: ${error.message}`);
isWorkerBusy = false;
hideLoading();
});
}
async function streamText(element, text) {
const words = text.split(' ');
let currentText = '';
for (let i = 0; i < words.length; i++) {
currentText += words[i] + ' ';
element.innerHTML = formatContent(currentText);
scrollToBottom();
// Add a small delay for more natural typing effect
await new Promise(resolve => setTimeout(resolve, 10 + Math.random() * 20));
}
}
function formatContent(text) {
// Simple text formatting
// Escape HTML
let formatted = text
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>');
// Format code blocks
formatted = formatted.replace(/```(\w*)\n([\s\S]*?)```/g, (match, lang, code) => {
return `<pre><code class="language-${lang}">${escapeHtml(code.trim())}</code></pre>`;
});
// Format inline code
formatted = formatted.replace(/`([^`]+)`/g, '<code>$1</code>');
// Format bold text
formatted = formatted.replace(/\*\*([^*]+)\*\*/g, '<strong>$1</strong>');
// Format line breaks
formatted = formatted.replace(/\n/g, '<br>');
return formatted;
}
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
// ============================================
// UI FUNCTIONS
// ============================================
function showLoading(message) {
elements.loadingIndicator.classList.remove('hidden');
elements.loadingMessage.textContent = message;
elements.progressBar.classList.remove('hidden');
elements.progressFill.style.width = '0%';
}
function hideLoading() {
elements.loadingIndicator.classList.add('hidden');
elements.progressBar.classList.add('hidden');
}
function updateProgress(progress, message) {
elements.progressFill.style.width = `${progress}%`;
if (message) {
elements.loadingMessage.textContent = message;
}
}
function updateModelStatus(status) {
elements.statusDot.className = 'status-dot';
if (status === 'loading') {
elements.statusDot.classList.add('loading');
elements.statusText.textContent = 'Loading model...';
} else if (status === 'ready') {
elements.statusDot.classList.add('ready');
elements.statusText.textContent = 'Ready';
}
}
function showError(error) {
console.error('Error:', error);
hideLoading();
// Add error message to chat
const messageDiv = document.createElement('div');
messageDiv.className = `message ai`;
messageDiv.innerHTML = `
<div class="message-avatar">⚠️</div>
<div class="message-content" style="color: var(--error-color);">
<strong>Error:</strong> ${error}
</div>
`;
elements.messagesList.appendChild(messageDiv);
isWorkerBusy = false;
}
function scrollToBottom() {
elements.chatContainer.scrollTop = elements.chatContainer.scrollHeight;
}
function hideWelcomeScreen() {
elements.welcomeScreen.style.display = 'none';
}
// ============================================
// EVENT LISTENERS
// ============================================
function updateSendButton() {
const hasText = elements.userInput.value.trim().length > 0;
elements.sendBtn.disabled = !hasText || isWorkerBusy;
}
async function handleInput() {
updateSendButton();
// Auto-resize textarea
elements.userInput.style.height = 'auto';
elements.userInput.style.height = Math.min(elements.userInput.scrollHeight, 200) + 'px';
}
async function handleSend() {
const message = elements.userInput.value.trim();
if (message && !isWorkerBusy) {
await sendMessage(message);
}
}
// ============================================
// INITIALIZATION
// ============================================
async function init() {
// Configure environment
env.allowLocalModels = false;
env.useBrowserCache = true;
// Initialize model
await initializeModel();
// Setup event listeners
elements.userInput.addEventListener('input', handleInput);
elements.userInput.addEventListener('keydown', (e) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
handleSend();
}
});
elements.sendBtn.addEventListener('click', handleSend);
// Setup example prompts
elements.exampleBtns.forEach(btn => {
btn.addEventListener('click', () => {
elements.userInput.value = btn.dataset.prompt;
handleInput();
elements.userInput.focus();
});
});
// Focus input on load
elements.userInput.focus();
}
// Start the application
init();
</arg_value>=== worker.js ===
// This file is handled as an inline blob in index.html for single-file deployment
// The worker code is provided as a string in the index.html script tag |