File size: 10,587 Bytes
cb8f522 |
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 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>On-Device AI Chat</title>
<style>
:root {
--background-color: #f0f2f5;
--container-bg-color: #ffffff;
--text-color: #050505;
--secondary-text-color: #65676b;
--border-color: #ced0d4;
--accent-color: #007bff;
--accent-text-color: #ffffff;
--user-msg-bg: #007bff;
--ai-msg-bg: #e4e6eb;
}
html,
body {
margin: 0;
padding: 0;
height: 100%;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
background-color: var(--background-color);
color: var(--text-color);
}
#app-container {
display: flex;
flex-direction: column;
height: 100vh;
/* Full viewport height */
max-width: 800px;
margin: 0 auto;
background-color: var(--container-bg-color);
box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
}
header {
padding: 10px 15px;
border-bottom: 1px solid var(--border-color);
background-color: #f7f7f7;
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 15px;
}
header h1 {
font-size: 1.2em;
margin: 0;
flex-grow: 1;
}
#model-selector {
padding: 8px 12px;
border-radius: 8px;
border: 1px solid var(--border-color);
font-size: 0.9em;
background-color: white;
cursor: pointer;
}
#chat-history {
flex-grow: 1;
overflow-y: auto;
padding: 15px;
display: flex;
flex-direction: column;
}
.message {
max-width: 85%;
padding: 10px 14px;
border-radius: 18px;
margin-bottom: 10px;
line-height: 1.4;
word-wrap: break-word;
}
.user-message {
background-color: var(--user-msg-bg);
color: var(--accent-text-color);
align-self: flex-end;
border-bottom-right-radius: 4px;
}
.ai-message {
background-color: var(--ai-msg-bg);
color: var(--text-color);
align-self: flex-start;
border-bottom-left-radius: 4px;
}
#status {
padding: 5px 15px;
font-size: 0.85em;
color: var(--secondary-text-color);
text-align: center;
min-height: 22px;
transition: all 0.3s ease;
}
#input-area {
display: flex;
padding: 10px;
border-top: 1px solid var(--border-color);
gap: 10px;
}
#user-input {
flex-grow: 1;
border: 1px solid var(--border-color);
border-radius: 20px;
padding: 10px 15px;
font-size: 1em;
outline: none;
}
#user-input:focus {
border-color: var(--accent-color);
box-shadow: 0 0 0 2px rgba(0, 123, 255, 0.25);
}
#send-button {
padding: 10px 20px;
border: none;
background-color: var(--accent-color);
color: var(--accent-text-color);
border-radius: 20px;
font-size: 1em;
cursor: pointer;
transition: background-color 0.2s ease;
}
#send-button:hover:not(:disabled) {
background-color: #0056b3;
}
#send-button:disabled {
background-color: #a0a0a0;
cursor: not-allowed;
}
</style>
</head>
<body>
<div id="app-container">
<header>
<h1>Local Chat AI 🤖</h1>
<select id="model-selector" title="Select a model"></select>
</header>
<main id="chat-history"></main>
<div id="status">Select a model and start chatting!</div>
<footer id="input-area">
<input type="text" id="user-input" placeholder="Type your message..." autocomplete="off">
<button id="send-button">Send</button>
</footer>
</div>
<script type="module">
import { pipeline, env } from 'https://cdn.jsdelivr.net/npm/@xenova/transformers@2.17.1';
// Configuration
// Prevent Transformers.js from looking for local models
env.allowLocalModels = false;
// --- DOM Elements ---
const statusDiv = document.getElementById('status');
const modelSelector = document.getElementById('model-selector');
const chatHistoryDiv = document.getElementById('chat-history');
const userInput = document.getElementById('user-input');
const sendButton = document.getElementById('send-button');
// --- State Management ---
let generator = null; // This will hold the loaded model pipeline
let isBusy = false;
let currentModel = '';
let chatHistory = []; // Array to store conversation [{role: 'user'|'assistant', content: '...'}, ...]
// --- Models ---
const MODELS = [
"Xenova/phi-3-mini-4k-instruct", // Good default, fast and capable
"Xenova/distilgpt2",
"Xenova/gemma-2b-it",
"Xenova/t5-small",
"Xenova/Mistral-7B-Instruct-v0.2",
"Xenova/llama-3-8b-instruct", // Note: Large model, may be slow/unstable on some devices
];
// --- Core Functions ---
/**
* Populates the model selector dropdown with the available models.
*/
function initializeModelSelector() {
MODELS.forEach(modelName => {
const option = document.createElement('option');
option.value = modelName;
option.textContent = modelName.split('/')[1]; // Display only the model name
modelSelector.appendChild(option);
});
currentModel = modelSelector.value;
}
/**
* Updates the UI to reflect the application's busy state.
* @param {boolean} busy - Whether the application is busy.
* @param {string} message - The message to display in the status div.
*/
function setBusyState(busy, message = '') {
isBusy = busy;
userInput.disabled = busy;
sendButton.disabled = busy;
modelSelector.disabled = busy;
statusDiv.textContent = message;
if (busy) {
sendButton.textContent = '...';
} else {
sendButton.textContent = 'Send';
userInput.focus();
}
}
/**
* Appends a message to the chat history div and scrolls to the bottom.
* @param {string} sender - 'user' or 'ai'.
* @param {string} text - The message content.
*/
function appendMessage(sender, text) {
const messageDiv = document.createElement('div');
messageDiv.classList.add('message', sender === 'user' ? 'user-message' : 'ai-message');
messageDiv.textContent = text;
chatHistoryDiv.appendChild(messageDiv);
chatHistoryDiv.scrollTop = chatHistoryDiv.scrollHeight; // Auto-scroll to the latest message
}
/**
* Main function to handle sending a message and generating a response.
*/
async function sendMessage() {
const userText = userInput.value.trim();
if (!userText || isBusy) return;
setBusyState(true, 'Preparing...');
const selectedModel = modelSelector.value;
userInput.value = '';
// 1. Add user message to UI and history
appendMessage('user', userText);
chatHistory.push({ role: 'user', content: userText });
try {
// 2. Load model if it has changed or hasn't been loaded yet
if (!generator || currentModel !== selectedModel) {
currentModel = selectedModel;
// Dispose of the old generator if it exists
if (generator) {
await generator.dispose();
}
setBusyState(true, `Loading ${currentModel.split('/')[1]}...`);
generator = await pipeline('text-generation', currentModel, {
progress_callback: (data) => {
if (data.status === 'progress') {
const loaded = (data.loaded / 1024 / 1024).toFixed(2);
const total = (data.total / 1024 / 1024).toFixed(2);
setBusyState(true, `Downloading: ${data.file} (${loaded} / ${total} MB)`);
} else {
setBusyState(true, `Status: ${data.status.replace(/_/g, ' ')}`);
}
}
});
}
// 3. Generate AI response
setBusyState(true, 'Generating response...');
// Format the conversation history for the model.
// `apply_chat_template` is the recommended way to format prompts for chat models.
const prompt = generator.tokenizer.apply_chat_template(chatHistory, {
tokenize: false,
add_generation_prompt: true,
});
const result = await generator(prompt, {
max_new_tokens: 512,
temperature: 0.7,
top_k: 50,
do_sample: true,
});
// 4. Process and display AI response
// The output contains the full conversation history; we extract only the newly generated part.
const rawResponse = result[0].generated_text;
const aiResponse = rawResponse.substring(prompt.length).trim();
appendMessage('ai', aiResponse);
chatHistory.push({ role: 'assistant', content: aiResponse });
} catch (error) {
console.error('An error occurred:', error);
appendMessage('ai', `Sorry, an error occurred: ${error.message}`);
} finally {
// 5. Reset UI state
setBusyState(false, 'Select a model and start chatting!');
}
}
// --- Event Listeners ---
sendButton.addEventListener('click', sendMessage);
userInput.addEventListener('keydown', (event) => {
if (event.key === 'Enter') {
event.preventDefault(); // Prevent form submission
sendMessage();
}
});
// Initialize the app
initializeModelSelector();
</script>
</body>
</html> |