Spaces:
Running
Running
Update index.html
Browse files- index.html +74 -125
index.html
CHANGED
@@ -3,7 +3,7 @@
|
|
3 |
<head>
|
4 |
<meta charset="UTF-8">
|
5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
|
6 |
-
<title>AI Assistant (Gemma 3 1B -
|
7 |
<style>
|
8 |
/* CSS๋ ์ด์ ๊ณผ ๋์ผ */
|
9 |
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700&display=swap');
|
@@ -23,21 +23,16 @@
|
|
23 |
* { box-sizing: border-box; margin: 0; padding: 0; }
|
24 |
html { height: 100%; }
|
25 |
body { font-family: 'Roboto', sans-serif; display: flex; flex-direction: column; align-items: center; justify-content: flex-start; min-height: 100vh; background-color: var(--bg-color); color: var(--text-color); padding: 10px; overscroll-behavior: none; }
|
26 |
-
#control-panel { background: var(--header-bg); padding: 15px; border-radius: 8px; margin-bottom: 10px; box-shadow: var(--header-shadow); width: 100%; max-width: 600px; border: 1px solid var(--border-color); }
|
27 |
-
#
|
28 |
-
.option-group { margin-bottom: 10px; text-align: center; }
|
29 |
-
.option-group label { margin-right: 15px; font-size: 0.95em; }
|
30 |
-
.option-group select { padding: 5px 8px; border-radius: 4px; border: 1px solid var(--input-border); font-size: 0.95em; }
|
31 |
-
#loadModelButton { padding: 10px 20px; font-size: 1em; background-color: var(--primary-color); color: white; border: none; border-radius: 5px; cursor: pointer; transition: background-color 0.2s; margin-top: 10px; display: block; margin-left: auto; margin-right: auto; }
|
32 |
#loadModelButton:hover:not(:disabled) { background-color: var(--button-hover-bg); }
|
33 |
#loadModelButton:disabled { background-color: var(--button-disabled-bg); cursor: not-allowed; }
|
34 |
-
#model-status { font-size: 0.9em; padding: 10px; border-radius: 4px; text-align: center; min-height: 40px; line-height: 1.4;
|
35 |
#model-status.info { background-color: #e2e3e5; border: 1px solid #d6d8db; color: #383d41; }
|
36 |
#model-status.loading { background-color: var(--warning-bg); border: 1px solid var(--warning-border); color: var(--warning-color); }
|
37 |
#model-status.success { background-color: var(--success-bg); border: 1px solid var(--success-border); color: var(--success-color); }
|
38 |
#model-status.error { background-color: var(--error-bg); border: 1px solid var(--error-border); color: var(--error-color); }
|
39 |
-
|
40 |
-
#chat-container { width: 100%; max-width: 600px; height: 70vh; /* Adjusted height */ max-height: 650px; background-color: #ffffff; border-radius: 12px; box-shadow: var(--container-shadow); display: flex; flex-direction: column; overflow: hidden; border: 1px solid var(--border-color); }
|
41 |
h1 { text-align: center; color: var(--primary-color); padding: 15px; background-color: var(--header-bg); border-bottom: 1px solid var(--border-color); font-size: 1.2em; font-weight: 500; flex-shrink: 0; box-shadow: var(--header-shadow); position: relative; z-index: 10; }
|
42 |
#chatbox { flex-grow: 1; overflow-y: auto; padding: 15px; display: flex; flex-direction: column; gap: 12px; scrollbar-width: thin; scrollbar-color: var(--scrollbar-thumb) var(--scrollbar-track); background-color: var(--bg-color); }
|
43 |
#chatbox::-webkit-scrollbar { width: 6px; } #chatbox::-webkit-scrollbar-track { background: var(--scrollbar-track); border-radius: 3px; } #chatbox::-webkit-scrollbar-thumb { background-color: var(--scrollbar-thumb); border-radius: 3px; }
|
@@ -58,7 +53,6 @@
|
|
58 |
#toggleSpeakerButton.muted { background-color: #aaa; }
|
59 |
@media (max-width: 600px) { /* Responsive styles */
|
60 |
body { padding: 5px; justify-content: flex-start; } #control-panel { margin-bottom: 5px; padding: 12px; }
|
61 |
-
.option-group { text-align: left; } .option-group label { display: block; margin-bottom: 8px; margin-right: 0; }
|
62 |
#chat-container { width: 100%; height: auto; flex-grow: 1; border-radius: 12px; max-height: none; margin-bottom: 5px; }
|
63 |
h1 { font-size: 1.1em; padding: 12px; } #chatbox { padding: 12px 8px; gap: 10px; }
|
64 |
#messages div { max-width: 90%; font-size: 0.95em; padding: 9px 14px;}
|
@@ -66,34 +60,31 @@
|
|
66 |
.control-button { width: 40px; height: 40px; font-size: 1.2em; }
|
67 |
}
|
68 |
</style>
|
|
|
69 |
<script type="importmap">
|
70 |
-
{
|
|
|
|
|
|
|
|
|
71 |
</script>
|
72 |
</head>
|
73 |
<body>
|
74 |
<div id="control-panel">
|
75 |
<h2>Model Loader</h2>
|
76 |
-
<
|
77 |
-
|
78 |
-
<select id="quantization-select">
|
79 |
-
<option value="q4" selected>Q4 (Recommended by Doc)</option>
|
80 |
-
<option value="fp16">FP16 (Half Precision)</option>
|
81 |
-
<option value="int8">INT8 (Integer Quantized)</option>
|
82 |
-
<!-- Add other dtypes if supported/tested, e.g., 'fp32' -->
|
83 |
-
<option value="">Default (No dtype specified)</option>
|
84 |
-
</select>
|
85 |
-
</div>
|
86 |
-
<button id="loadModelButton">Load Gemma 3 1B Model</button>
|
87 |
-
<div id="model-status" class="info">Select quantization and click load. **Warning:** Loading `gemma3_text` models is expected to fail due to library incompatibility.</div>
|
88 |
</div>
|
89 |
|
90 |
<div id="chat-container">
|
91 |
<h1 id="chatbot-name">AI Assistant</h1>
|
92 |
<div id="chatbox">
|
93 |
-
<div id="messages"
|
|
|
|
|
94 |
</div>
|
95 |
<div id="input-area">
|
96 |
-
<textarea id="userInput" placeholder="Please load
|
97 |
<button id="speechButton" class="control-button" title="Speak message" disabled>๐ค</button>
|
98 |
<button id="toggleSpeakerButton" class="control-button" title="Toggle AI speech output" disabled>๐</button>
|
99 |
<button id="sendButton" class="control-button" title="Send message" disabled>โค</button>
|
@@ -101,20 +92,22 @@
|
|
101 |
</div>
|
102 |
|
103 |
<script type="module">
|
|
|
104 |
import { pipeline, env } from '@xenova/transformers';
|
105 |
|
106 |
-
//
|
107 |
-
const MODEL_NAME = 'onnx-community/gemma-3-1b-it-ONNX-GQA';
|
108 |
const TASK = 'text-generation';
|
|
|
109 |
|
110 |
-
// Environment
|
111 |
env.allowRemoteModels = true;
|
112 |
env.useBrowserCache = true;
|
113 |
env.backends.onnx.executionProviders = ['webgpu', 'wasm'];
|
114 |
console.log('Using Execution Providers:', env.backends.onnx.executionProviders);
|
115 |
env.backends.onnx.prefer_alternative_execution_providers = true;
|
116 |
|
117 |
-
// DOM Elements
|
118 |
const chatbox = document.getElementById('messages');
|
119 |
const userInput = document.getElementById('userInput');
|
120 |
const sendButton = document.getElementById('sendButton');
|
@@ -123,17 +116,16 @@
|
|
123 |
const toggleSpeakerButton = document.getElementById('toggleSpeakerButton');
|
124 |
const modelStatus = document.getElementById('model-status');
|
125 |
const loadModelButton = document.getElementById('loadModelButton');
|
126 |
-
const quantizationSelect = document.getElementById('quantization-select'); // Dropdown
|
127 |
|
128 |
-
// State
|
129 |
let generator = null;
|
130 |
let isLoadingModel = false;
|
131 |
-
let conversationHistory = []; // Stores { role: 'user' | 'assistant', content: '...' }
|
132 |
let botState = { botName: "AI Assistant", userName: "User", botSettings: { useSpeechOutput: true } };
|
133 |
-
const stateKey = '
|
134 |
-
const historyKey = '
|
135 |
|
136 |
-
// Speech API
|
137 |
let recognition = null;
|
138 |
let synthesis = window.speechSynthesis;
|
139 |
let targetVoice = null;
|
@@ -141,12 +133,17 @@
|
|
141 |
|
142 |
// --- Initialization ---
|
143 |
window.addEventListener('load', () => {
|
144 |
-
loadState();
|
145 |
-
|
146 |
-
|
|
|
|
|
|
|
|
|
|
|
147 |
loadModelButton.addEventListener('click', handleLoadModelClick);
|
148 |
-
console.log("Using
|
149 |
-
displayMessage('system', `Ready to load ${MODEL_NAME}
|
150 |
});
|
151 |
|
152 |
// --- State Persistence ---
|
@@ -169,7 +166,7 @@
|
|
169 |
modelStatus.textContent = message; modelStatus.className = 'model-status ' + type; console.log(`Model Status (${type}): ${message}`);
|
170 |
}
|
171 |
function updateChatUIState(isModelLoadedSuccessfully) { /* No changes */
|
172 |
-
userInput.disabled = !isModelLoadedSuccessfully || isLoadingModel; sendButton.disabled = !isModelLoadedSuccessfully || isLoadingModel || userInput.value.trim() === ''; speechButton.disabled = !isModelLoadedSuccessfully || isLoadingModel || isListening || !recognition; toggleSpeakerButton.disabled = !isModelLoadedSuccessfully || isLoadingModel || !synthesis; loadModelButton.disabled = isLoadingModel || isModelLoadedSuccessfully;
|
173 |
}
|
174 |
function updateSpeakerButtonUI() { /* No changes */
|
175 |
toggleSpeakerButton.textContent = botState.botSettings.useSpeechOutput ? '๐' : '๐'; toggleSpeakerButton.title = botState.botSettings.useSpeechOutput ? 'Turn off AI speech' : 'Turn on AI speech'; toggleSpeakerButton.classList.toggle('muted', !botState.botSettings.useSpeechOutput);
|
@@ -178,117 +175,69 @@
|
|
178 |
function setupInputAutosize() { userInput.addEventListener('input', () => { userInput.style.height = 'auto'; userInput.style.height = userInput.scrollHeight + 'px'; updateChatUIState(generator !== null); }); }
|
179 |
|
180 |
// --- Model & AI Logic ---
|
181 |
-
async function handleLoadModelClick() {
|
182 |
-
if (isLoadingModel || generator) return;
|
183 |
-
isLoadingModel = true; generator = null;
|
184 |
-
const selectedQuantization = quantizationSelect.value || null; // Get selected dtype, null if empty string
|
185 |
-
updateChatUIState(false);
|
186 |
-
await initializeModel(MODEL_NAME, selectedQuantization); // Pass selected dtype
|
187 |
-
isLoadingModel = false;
|
188 |
-
updateChatUIState(generator !== null);
|
189 |
}
|
190 |
|
191 |
-
// Initialize model
|
192 |
-
async function initializeModel(modelId
|
193 |
-
|
194 |
-
|
195 |
-
const msg = `[Loading ${quantizationType || 'Default'}]: ${progress.status}] ${progress.file ? progress.file.split('/').pop() : ''} (${Math.round(progress.progress || 0)}%)`;
|
196 |
-
updateModelStatus(msg, 'loading');
|
197 |
-
}
|
198 |
-
};
|
199 |
-
if (quantizationType) {
|
200 |
-
options.dtype = quantizationType; // Set dtype only if selected
|
201 |
-
}
|
202 |
-
|
203 |
-
const loadingMsg = `Loading ${modelId} ${quantizationType ? `with dtype: "${quantizationType}"` : '(default dtype)'}... (Still expected to fail)`;
|
204 |
-
updateModelStatus(loadingMsg, 'loading');
|
205 |
-
displayMessage('system', `Attempting to load ${modelId} with ${quantizationType ? `dtype: ${quantizationType}` : 'default dtype'}...`, false);
|
206 |
|
207 |
try {
|
208 |
-
//
|
209 |
-
generator = await pipeline(TASK, modelId,
|
|
|
|
|
|
|
|
|
|
|
|
|
210 |
|
211 |
-
//
|
212 |
-
updateModelStatus(`${modelId}
|
213 |
-
displayMessage('system', `[SUCCESS] ${modelId} loaded
|
214 |
|
215 |
} catch (error) {
|
216 |
-
|
|
|
217 |
let errorMsg = `Failed to load ${modelId}: ${error.message}.`;
|
218 |
if (error.message.includes("Unsupported model type") || error.message.includes("gemma3_text")) {
|
219 |
-
errorMsg += "
|
220 |
} else if (error.message.includes("split is not a function")) {
|
221 |
-
errorMsg += " TypeError during config parsing
|
222 |
} else {
|
223 |
-
errorMsg += " Check console/network/memory.";
|
224 |
}
|
225 |
updateModelStatus(errorMsg, 'error');
|
226 |
displayMessage('system', `[ERROR] ${errorMsg}`, true, true);
|
227 |
-
generator = null;
|
228 |
}
|
229 |
}
|
230 |
|
231 |
-
// Build messages array
|
232 |
-
function buildMessages(newUserMessage) {
|
233 |
-
let messages = [{ role: "system", content: "You are a helpful assistant." }];
|
234 |
-
messages = messages.concat(conversationHistory);
|
235 |
-
messages.push({ role: "user", content: newUserMessage });
|
236 |
-
console.log("Input Messages:", messages); return messages;
|
237 |
}
|
238 |
|
239 |
-
// Cleanup response
|
240 |
-
function cleanupResponse(output) {
|
241 |
-
try {
|
242 |
-
if (output && output.length > 0 && output[0].generated_text && Array.isArray(output[0].generated_text)) {
|
243 |
-
const lastMessage = output[0].generated_text.at(-1);
|
244 |
-
if (lastMessage && (lastMessage.role === 'assistant' || lastMessage.role === 'model') && typeof lastMessage.content === 'string') {
|
245 |
-
let content = lastMessage.content.trim(); content = content.replace(/<end_of_turn>/g, '').trim(); if (content.length > 0) return content;
|
246 |
-
}
|
247 |
-
}
|
248 |
-
} catch (e) { console.error("Error parsing output:", e, output); }
|
249 |
-
console.warn("Could not extract response using standard structure.", output);
|
250 |
-
const fallbacks = [ "Sorry, response format was unexpected.", "My response might be garbled.", "Error processing the AI answer." ];
|
251 |
-
return fallbacks[Math.floor(Math.random() * fallbacks.length)];
|
252 |
}
|
253 |
|
254 |
|
255 |
// --- Main Interaction Logic ---
|
256 |
-
async function handleUserMessage() {
|
257 |
-
const userText = userInput.value.trim();
|
258 |
-
if (!userText || !generator || isLoadingModel) return; // Check if generator is ready
|
259 |
-
userInput.value = ''; userInput.style.height = 'auto'; updateChatUIState(true);
|
260 |
-
displayMessage('user', userText); conversationHistory.push({ role: 'user', content: userText });
|
261 |
-
updateModelStatus("AI thinking...", "loading");
|
262 |
-
const messages = buildMessages(userText); // Use messages format
|
263 |
-
|
264 |
-
try {
|
265 |
-
// Call generator with messages array and parameters
|
266 |
-
const outputs = await generator(messages, {
|
267 |
-
max_new_tokens: 512,
|
268 |
-
do_sample: true, // Example: enable sampling
|
269 |
-
temperature: 0.7,
|
270 |
-
top_k: 50
|
271 |
-
});
|
272 |
-
const replyText = cleanupResponse(outputs);
|
273 |
-
console.log("Cleaned AI Output:", replyText);
|
274 |
-
displayMessage('bot', replyText); conversationHistory.push({ role: 'assistant', content: replyText });
|
275 |
-
if (botState.botSettings.useSpeechOutput && synthesis && targetVoice) { speakText(replyText); }
|
276 |
-
saveState();
|
277 |
-
} catch (error) {
|
278 |
-
console.error("AI response generation error:", error); displayMessage('system', `[ERROR] Failed to generate response: ${error.message}`, true, true);
|
279 |
-
} finally {
|
280 |
-
if(generator) updateModelStatus(`${MODEL_NAME} ready.`, "success"); // Assuming MODEL_NAME hasn't changed
|
281 |
-
updateChatUIState(generator !== null); userInput.focus();
|
282 |
-
}
|
283 |
}
|
284 |
|
285 |
// --- Speech API Functions ---
|
286 |
-
function initializeSpeechAPI() { /* No changes */
|
287 |
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition; if (SpeechRecognition) { recognition = new SpeechRecognition(); recognition.lang = 'en-US'; recognition.continuous = false; recognition.interimResults = false; recognition.onstart = () => { isListening = true; updateChatUIState(generator !== null); console.log('Listening...'); }; recognition.onresult = (event) => { userInput.value = event.results[0][0].transcript; userInput.dispatchEvent(new Event('input')); handleUserMessage(); }; recognition.onerror = (event) => { console.error("Speech error:", event.error); updateModelStatus(`Speech recognition error (${event.error})`, 'error'); setTimeout(() => updateModelStatus(generator ? `${MODEL_NAME} ready.` : 'Model not loaded.', generator ? 'success' : 'error'), 3000); }; recognition.onend = () => { isListening = false; updateChatUIState(generator !== null); console.log('Stopped listening.'); }; } else { console.warn("Speech Recognition not supported."); } if (!synthesis) { console.warn("Speech Synthesis not supported."); } else { toggleSpeakerButton.addEventListener('click', () => { botState.botSettings.useSpeechOutput = !botState.botSettings.useSpeechOutput; updateSpeakerButtonUI(); saveState(); if (!botState.botSettings.useSpeechOutput) synthesis.cancel(); }); } updateChatUIState(false);
|
288 |
}
|
289 |
-
function loadVoices() { /* No changes */ if (!synthesis) return; let voices = synthesis.getVoices(); if (voices.length === 0) { synthesis.onvoiceschanged = () => { voices = synthesis.getVoices(); findAndSetVoice(voices); }; } else { findAndSetVoice(voices); } }
|
290 |
-
function findAndSetVoice(voices) { /* No changes */ targetVoice = voices.find(v => v.lang === 'en-US') || voices.find(v => v.lang.startsWith('en-')); if (targetVoice) { console.log("Using English voice:", targetVoice.name, targetVoice.lang); } else { console.warn("No suitable English voice found."); } }
|
291 |
-
function speakText(text) { /* No changes */ if (!synthesis || !botState.botSettings.useSpeechOutput || !targetVoice) return; synthesis.cancel(); const utterance = new SpeechSynthesisUtterance(text); utterance.voice = targetVoice; utterance.lang = targetVoice.lang; utterance.rate = 1.0; utterance.pitch = 1.0; synthesis.speak(utterance); }
|
292 |
|
293 |
// --- Event Listeners ---
|
294 |
sendButton.addEventListener('click', handleUserMessage);
|
|
|
3 |
<head>
|
4 |
<meta charset="UTF-8">
|
5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
|
6 |
+
<title>AI Assistant (Gemma 3 1B - Final HTML Attempt)</title>
|
7 |
<style>
|
8 |
/* CSS๋ ์ด์ ๊ณผ ๋์ผ */
|
9 |
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700&display=swap');
|
|
|
23 |
* { box-sizing: border-box; margin: 0; padding: 0; }
|
24 |
html { height: 100%; }
|
25 |
body { font-family: 'Roboto', sans-serif; display: flex; flex-direction: column; align-items: center; justify-content: flex-start; min-height: 100vh; background-color: var(--bg-color); color: var(--text-color); padding: 10px; overscroll-behavior: none; }
|
26 |
+
#control-panel { background: var(--header-bg); padding: 15px; border-radius: 8px; margin-bottom: 10px; box-shadow: var(--header-shadow); width: 100%; max-width: 600px; border: 1px solid var(--border-color); text-align: center; }
|
27 |
+
#loadModelButton { padding: 10px 20px; font-size: 1em; background-color: var(--primary-color); color: white; border: none; border-radius: 5px; cursor: pointer; transition: background-color 0.2s; margin-bottom: 10px; }
|
|
|
|
|
|
|
|
|
28 |
#loadModelButton:hover:not(:disabled) { background-color: var(--button-hover-bg); }
|
29 |
#loadModelButton:disabled { background-color: var(--button-disabled-bg); cursor: not-allowed; }
|
30 |
+
#model-status { font-size: 0.9em; padding: 10px; border-radius: 4px; text-align: center; min-height: 40px; line-height: 1.4; }
|
31 |
#model-status.info { background-color: #e2e3e5; border: 1px solid #d6d8db; color: #383d41; }
|
32 |
#model-status.loading { background-color: var(--warning-bg); border: 1px solid var(--warning-border); color: var(--warning-color); }
|
33 |
#model-status.success { background-color: var(--success-bg); border: 1px solid var(--success-border); color: var(--success-color); }
|
34 |
#model-status.error { background-color: var(--error-bg); border: 1px solid var(--error-border); color: var(--error-color); }
|
35 |
+
#chat-container { width: 100%; max-width: 600px; height: 75vh; max-height: 700px; background-color: #ffffff; border-radius: 12px; box-shadow: var(--container-shadow); display: flex; flex-direction: column; overflow: hidden; border: 1px solid var(--border-color); }
|
|
|
36 |
h1 { text-align: center; color: var(--primary-color); padding: 15px; background-color: var(--header-bg); border-bottom: 1px solid var(--border-color); font-size: 1.2em; font-weight: 500; flex-shrink: 0; box-shadow: var(--header-shadow); position: relative; z-index: 10; }
|
37 |
#chatbox { flex-grow: 1; overflow-y: auto; padding: 15px; display: flex; flex-direction: column; gap: 12px; scrollbar-width: thin; scrollbar-color: var(--scrollbar-thumb) var(--scrollbar-track); background-color: var(--bg-color); }
|
38 |
#chatbox::-webkit-scrollbar { width: 6px; } #chatbox::-webkit-scrollbar-track { background: var(--scrollbar-track); border-radius: 3px; } #chatbox::-webkit-scrollbar-thumb { background-color: var(--scrollbar-thumb); border-radius: 3px; }
|
|
|
53 |
#toggleSpeakerButton.muted { background-color: #aaa; }
|
54 |
@media (max-width: 600px) { /* Responsive styles */
|
55 |
body { padding: 5px; justify-content: flex-start; } #control-panel { margin-bottom: 5px; padding: 12px; }
|
|
|
56 |
#chat-container { width: 100%; height: auto; flex-grow: 1; border-radius: 12px; max-height: none; margin-bottom: 5px; }
|
57 |
h1 { font-size: 1.1em; padding: 12px; } #chatbox { padding: 12px 8px; gap: 10px; }
|
58 |
#messages div { max-width: 90%; font-size: 0.95em; padding: 9px 14px;}
|
|
|
60 |
.control-button { width: 40px; height: 40px; font-size: 1.2em; }
|
61 |
}
|
62 |
</style>
|
63 |
+
<!-- Using LATEST version of Transformers.js via CDN -->
|
64 |
<script type="importmap">
|
65 |
+
{
|
66 |
+
"imports": {
|
67 |
+
"@xenova/transformers": "https://cdn.jsdelivr.net/npm/@xenova/transformers@latest"
|
68 |
+
}
|
69 |
+
}
|
70 |
</script>
|
71 |
</head>
|
72 |
<body>
|
73 |
<div id="control-panel">
|
74 |
<h2>Model Loader</h2>
|
75 |
+
<button id="loadModelButton">Load Gemma 3 1B Model (Q4)</button>
|
76 |
+
<div id="model-status" class="info">Click button to load `onnx-community/gemma-3-1b-it-ONNX-GQA` (Q4). **Warning:** This model is known to be incompatible with Transformers.js via CDN and loading is expected to fail.</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
</div>
|
78 |
|
79 |
<div id="chat-container">
|
80 |
<h1 id="chatbot-name">AI Assistant</h1>
|
81 |
<div id="chatbox">
|
82 |
+
<div id="messages">
|
83 |
+
<!-- Chat messages appear here -->
|
84 |
+
</div>
|
85 |
</div>
|
86 |
<div id="input-area">
|
87 |
+
<textarea id="userInput" placeholder="Please attempt to load the model first..." rows="1" disabled></textarea>
|
88 |
<button id="speechButton" class="control-button" title="Speak message" disabled>๐ค</button>
|
89 |
<button id="toggleSpeakerButton" class="control-button" title="Toggle AI speech output" disabled>๐</button>
|
90 |
<button id="sendButton" class="control-button" title="Send message" disabled>โค</button>
|
|
|
92 |
</div>
|
93 |
|
94 |
<script type="module">
|
95 |
+
// Import necessary functions from the loaded library.
|
96 |
import { pipeline, env } from '@xenova/transformers';
|
97 |
|
98 |
+
// --- Configuration ---
|
99 |
+
const MODEL_NAME = 'onnx-community/gemma-3-1b-it-ONNX-GQA'; // The specific model requested
|
100 |
const TASK = 'text-generation';
|
101 |
+
const QUANTIZATION = 'q4'; // As specified in the model card example
|
102 |
|
103 |
+
// --- Environment Setup ---
|
104 |
env.allowRemoteModels = true;
|
105 |
env.useBrowserCache = true;
|
106 |
env.backends.onnx.executionProviders = ['webgpu', 'wasm'];
|
107 |
console.log('Using Execution Providers:', env.backends.onnx.executionProviders);
|
108 |
env.backends.onnx.prefer_alternative_execution_providers = true;
|
109 |
|
110 |
+
// --- DOM Elements ---
|
111 |
const chatbox = document.getElementById('messages');
|
112 |
const userInput = document.getElementById('userInput');
|
113 |
const sendButton = document.getElementById('sendButton');
|
|
|
116 |
const toggleSpeakerButton = document.getElementById('toggleSpeakerButton');
|
117 |
const modelStatus = document.getElementById('model-status');
|
118 |
const loadModelButton = document.getElementById('loadModelButton');
|
|
|
119 |
|
120 |
+
// --- State Management ---
|
121 |
let generator = null;
|
122 |
let isLoadingModel = false;
|
123 |
+
let conversationHistory = []; // Stores { role: 'user' | 'assistant' | 'system', content: '...' }
|
124 |
let botState = { botName: "AI Assistant", userName: "User", botSettings: { useSpeechOutput: true } };
|
125 |
+
const stateKey = 'gemma3_1b_strict_state_v2'; // Adjusted key
|
126 |
+
const historyKey = 'gemma3_1b_strict_history_v2';
|
127 |
|
128 |
+
// --- Web Speech API ---
|
129 |
let recognition = null;
|
130 |
let synthesis = window.speechSynthesis;
|
131 |
let targetVoice = null;
|
|
|
133 |
|
134 |
// --- Initialization ---
|
135 |
window.addEventListener('load', () => {
|
136 |
+
loadState();
|
137 |
+
chatbotNameElement.textContent = botState.botName;
|
138 |
+
updateSpeakerButtonUI();
|
139 |
+
initializeSpeechAPI();
|
140 |
+
setupInputAutosize();
|
141 |
+
updateChatUIState(false);
|
142 |
+
displayHistory();
|
143 |
+
setTimeout(loadVoices, 500);
|
144 |
loadModelButton.addEventListener('click', handleLoadModelClick);
|
145 |
+
console.log("Using Transformers.js (latest) loaded via import map.");
|
146 |
+
displayMessage('system', `Using latest Transformers.js. Ready to load ${MODEL_NAME}.`, false);
|
147 |
});
|
148 |
|
149 |
// --- State Persistence ---
|
|
|
166 |
modelStatus.textContent = message; modelStatus.className = 'model-status ' + type; console.log(`Model Status (${type}): ${message}`);
|
167 |
}
|
168 |
function updateChatUIState(isModelLoadedSuccessfully) { /* No changes */
|
169 |
+
userInput.disabled = !isModelLoadedSuccessfully || isLoadingModel; sendButton.disabled = !isModelLoadedSuccessfully || isLoadingModel || userInput.value.trim() === ''; speechButton.disabled = !isModelLoadedSuccessfully || isLoadingModel || isListening || !recognition; toggleSpeakerButton.disabled = !isModelLoadedSuccessfully || isLoadingModel || !synthesis; loadModelButton.disabled = isLoadingModel || isModelLoadedSuccessfully; if (isModelLoadedSuccessfully) { userInput.placeholder = "How can I help you today?"; } else if (isLoadingModel) { userInput.placeholder = "Model loading..."; } else { userInput.placeholder = "Please attempt to load the model first..."; }
|
170 |
}
|
171 |
function updateSpeakerButtonUI() { /* No changes */
|
172 |
toggleSpeakerButton.textContent = botState.botSettings.useSpeechOutput ? '๐' : '๐'; toggleSpeakerButton.title = botState.botSettings.useSpeechOutput ? 'Turn off AI speech' : 'Turn on AI speech'; toggleSpeakerButton.classList.toggle('muted', !botState.botSettings.useSpeechOutput);
|
|
|
175 |
function setupInputAutosize() { userInput.addEventListener('input', () => { userInput.style.height = 'auto'; userInput.style.height = userInput.scrollHeight + 'px'; updateChatUIState(generator !== null); }); }
|
176 |
|
177 |
// --- Model & AI Logic ---
|
178 |
+
async function handleLoadModelClick() { /* No changes */
|
179 |
+
if (isLoadingModel || generator) return; isLoadingModel = true; generator = null; updateChatUIState(false); await initializeModel(MODEL_NAME); isLoadingModel = false; updateChatUIState(generator !== null);
|
|
|
|
|
|
|
|
|
|
|
|
|
180 |
}
|
181 |
|
182 |
+
// Initialize model EXACTLY as per the documentation example
|
183 |
+
async function initializeModel(modelId) {
|
184 |
+
updateModelStatus(`Loading ${modelId} with { dtype: "${QUANTIZATION}" }... (Strict doc example)`, 'loading');
|
185 |
+
displayMessage('system', `Attempting to load ${modelId} using documented method (dtype: ${QUANTIZATION})...`, false);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
186 |
|
187 |
try {
|
188 |
+
// Pipeline creation EXACTLY as in the example
|
189 |
+
generator = await pipeline(TASK, modelId, {
|
190 |
+
dtype: QUANTIZATION, // Use q4 as requested
|
191 |
+
progress_callback: (progress) => {
|
192 |
+
const msg = `[Loading: ${progress.status}] ${progress.file ? progress.file.split('/').pop() : ''} (${Math.round(progress.progress || 0)}%)`;
|
193 |
+
updateModelStatus(msg, 'loading');
|
194 |
+
}
|
195 |
+
});
|
196 |
|
197 |
+
// If successful (HIGHLY UNLIKELY)
|
198 |
+
updateModelStatus(`${modelId} loaded successfully!`, 'success');
|
199 |
+
displayMessage('system', `[SUCCESS] ${modelId} loaded.`, false);
|
200 |
|
201 |
} catch (error) {
|
202 |
+
// Catch and report the expected error
|
203 |
+
console.error(`Model loading failed for ${modelId} (Strict Attempt):`, error);
|
204 |
let errorMsg = `Failed to load ${modelId}: ${error.message}.`;
|
205 |
if (error.message.includes("Unsupported model type") || error.message.includes("gemma3_text")) {
|
206 |
+
errorMsg += " As expected, the 'gemma3_text' model type is unsupported by this library version.";
|
207 |
} else if (error.message.includes("split is not a function")) {
|
208 |
+
errorMsg += " As expected, a TypeError occurred during config parsing (incompatibility).";
|
209 |
} else {
|
210 |
+
errorMsg += " Unknown error. Check console/network/memory.";
|
211 |
}
|
212 |
updateModelStatus(errorMsg, 'error');
|
213 |
displayMessage('system', `[ERROR] ${errorMsg}`, true, true);
|
214 |
+
generator = null; // Ensure generator is null on failure
|
215 |
}
|
216 |
}
|
217 |
|
218 |
+
// Build messages array EXACTLY as per documentation example
|
219 |
+
function buildMessages(newUserMessage) { /* No changes */
|
220 |
+
let messages = [{ role: "system", content: "You are a helpful assistant." }]; messages = messages.concat(conversationHistory); messages.push({ role: "user", content: newUserMessage }); console.log("Input Messages for Pipeline:", messages); return messages;
|
|
|
|
|
|
|
221 |
}
|
222 |
|
223 |
+
// Cleanup response EXACTLY as per documentation example (with safety checks)
|
224 |
+
function cleanupResponse(output) { /* No changes */
|
225 |
+
try { if (output && output.length > 0 && output[0].generated_text && Array.isArray(output[0].generated_text)) { const lastMessage = output[0].generated_text.at(-1); if (lastMessage && (lastMessage.role === 'assistant' || lastMessage.role === 'model') && typeof lastMessage.content === 'string') { let content = lastMessage.content.trim(); content = content.replace(/<end_of_turn>/g, '').trim(); if (content.length > 0) return content; } } } catch (e) { console.error("Error parsing generator output with .at(-1):", e, "Output:", output); } console.warn("Could not extract response using output[0].generated_text.at(-1).content. Output structure might differ.", output); const fallbacks = [ "Sorry, response format was unexpected.", "My response might be garbled.", "Error processing the AI answer." ]; return fallbacks[Math.floor(Math.random() * fallbacks.length)];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
226 |
}
|
227 |
|
228 |
|
229 |
// --- Main Interaction Logic ---
|
230 |
+
async function handleUserMessage() { /* No changes needed, uses messages format */
|
231 |
+
const userText = userInput.value.trim(); if (!userText || !generator || isLoadingModel) return; userInput.value = ''; userInput.style.height = 'auto'; updateChatUIState(true); displayMessage('user', userText); conversationHistory.push({ role: 'user', content: userText }); updateModelStatus("AI thinking...", "loading"); const messages = buildMessages(userText); try { const outputs = await generator(messages, { max_new_tokens: 512, do_sample: false }); const replyText = cleanupResponse(outputs); console.log("Cleaned AI Output:", replyText); displayMessage('bot', replyText); conversationHistory.push({ role: 'assistant', content: replyText }); if (botState.botSettings.useSpeechOutput && synthesis && targetVoice) { speakText(replyText); } saveState(); } catch (error) { console.error("AI response generation error:", error); displayMessage('system', `[ERROR] Failed to generate response: ${error.message}`, true, true); } finally { if(generator) updateModelStatus(`${MODEL_NAME} ready.`, "success"); updateChatUIState(generator !== null); userInput.focus(); }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
232 |
}
|
233 |
|
234 |
// --- Speech API Functions ---
|
235 |
+
function initializeSpeechAPI() { /* No changes needed */
|
236 |
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition; if (SpeechRecognition) { recognition = new SpeechRecognition(); recognition.lang = 'en-US'; recognition.continuous = false; recognition.interimResults = false; recognition.onstart = () => { isListening = true; updateChatUIState(generator !== null); console.log('Listening...'); }; recognition.onresult = (event) => { userInput.value = event.results[0][0].transcript; userInput.dispatchEvent(new Event('input')); handleUserMessage(); }; recognition.onerror = (event) => { console.error("Speech error:", event.error); updateModelStatus(`Speech recognition error (${event.error})`, 'error'); setTimeout(() => updateModelStatus(generator ? `${MODEL_NAME} ready.` : 'Model not loaded.', generator ? 'success' : 'error'), 3000); }; recognition.onend = () => { isListening = false; updateChatUIState(generator !== null); console.log('Stopped listening.'); }; } else { console.warn("Speech Recognition not supported."); } if (!synthesis) { console.warn("Speech Synthesis not supported."); } else { toggleSpeakerButton.addEventListener('click', () => { botState.botSettings.useSpeechOutput = !botState.botSettings.useSpeechOutput; updateSpeakerButtonUI(); saveState(); if (!botState.botSettings.useSpeechOutput) synthesis.cancel(); }); } updateChatUIState(false);
|
237 |
}
|
238 |
+
function loadVoices() { /* No changes needed */ if (!synthesis) return; let voices = synthesis.getVoices(); if (voices.length === 0) { synthesis.onvoiceschanged = () => { voices = synthesis.getVoices(); findAndSetVoice(voices); }; } else { findAndSetVoice(voices); } }
|
239 |
+
function findAndSetVoice(voices) { /* No changes needed */ targetVoice = voices.find(v => v.lang === 'en-US') || voices.find(v => v.lang.startsWith('en-')); if (targetVoice) { console.log("Using English voice:", targetVoice.name, targetVoice.lang); } else { console.warn("No suitable English voice found."); } }
|
240 |
+
function speakText(text) { /* No changes needed */ if (!synthesis || !botState.botSettings.useSpeechOutput || !targetVoice) return; synthesis.cancel(); const utterance = new SpeechSynthesisUtterance(text); utterance.voice = targetVoice; utterance.lang = targetVoice.lang; utterance.rate = 1.0; utterance.pitch = 1.0; synthesis.speak(utterance); }
|
241 |
|
242 |
// --- Event Listeners ---
|
243 |
sendButton.addEventListener('click', handleUserMessage);
|