File size: 8,073 Bytes
34c7121 b352433 34c7121 32922f8 34c7121 b352433 34c7121 b352433 8e1842a b352433 8e1842a 34c7121 b352433 34c7121 b352433 34c7121 b352433 34c7121 8e1842a b352433 8e1842a b352433 34c7121 8e1842a b352433 8e1842a 34c7121 b352433 34c7121 b352433 8e1842a 34c7121 8e1842a b352433 34c7121 b352433 8e1842a 34c7121 8e1842a b352433 8e1842a 34c7121 8e1842a 34c7121 8e1842a 34c7121 b352433 34c7121 8e1842a b352433 8e1842a 34c7121 8e1842a b352433 34c7121 8e1842a 34c7121 8e1842a b352433 8e1842a 34c7121 b352433 8e1842a 34c7121 8e1842a 34c7121 8e1842a b352433 8e1842a 34c7121 9c24e6f 34c7121 8e1842a 34c7121 8e1842a 34c7121 e7706f8 9c24e6f 2cec2e5 34c7121 e7706f8 34c7121 8e1842a 34c7121 8e1842a 32922f8 34c7121 8e1842a 32922f8 e7706f8 32922f8 e7706f8 2cec2e5 32922f8 e7706f8 2cec2e5 e7706f8 34c7121 e7706f8 8e1842a 34c7121 32922f8 34c7121 9c24e6f 34c7121 32922f8 b352433 34c7121 8e1842a 34c7121 2cec2e5 34c7121 e7706f8 34c7121 8e1842a 34c7121 8e1842a 2cec2e5 8e1842a 34c7121 e7706f8 8e1842a 32922f8 34c7121 32922f8 34c7121 8e1842a 34c7121 8e1842a 34c7121 |
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 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Gemma Chatbot</title>
<style>
/* (Same good CSS as before) */
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
margin: 0;
background-color: #f4f7f6;
}
h1 {
color: #333;
margin-bottom: 0.5em;
}
#chat-container {
width: 90%;
max-width: 700px;
border: none;
padding: 25px;
border-radius: 15px;
background-color: #fff;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
overflow: hidden;
}
#chat-messages {
height: 400px;
overflow-y: auto;
margin-bottom: 15px;
padding: 15px;
border: 1px solid #e0e0e0;
border-radius: 10px;
background-color: #fafafa;
}
.message {
margin-bottom: 10px;
padding: 12px;
border-radius: 20px;
max-width: 70%;
word-wrap: break-word;
box-shadow: 0 1px 2px rgba(0,0,0,0.1);
}
.user-message {
background-color: #d4e5ff;
color: #000;
text-align: right;
align-self: flex-end;
margin-left: auto;
margin-right: 10px;
}
.bot-message {
background-color: #e5e5ea;
color: #000;
text-align: left;
align-self: flex-start;
margin-right: auto;
margin-left: 10px;
}
#input-container {
display: flex;
gap: 10px;
padding-top: 15px;
border-top: 1px solid #e0e0e0;
}
#user-input {
flex-grow: 1;
padding: 12px;
border: 1px solid #ccc;
border-radius: 20px;
font-size: 16px;
outline: none;
transition: border-color 0.2s ease-in-out;
}
#user-input:focus {
border-color: #007bff;
}
#send-button {
padding: 12px 25px;
background-color: #007bff;
color: white;
border: none;
border-radius: 20px;
cursor: pointer;
font-size: 16px;
transition: background-color 0.2s ease-in-out;
}
#send-button:hover {
background-color: #0056b3;
}
#send-button:disabled {
background-color: #cccccc;
cursor: not-allowed;
}
#loading-indicator {
display: none;
margin-top: 15px;
text-align: center;
color: #888;
font-style: italic;
}
.system-message {
display: none;
}
</style>
</head>
<body>
<h1>Gemma 3 Chatbot</h1>
<div id="chat-container">
<div id="chat-messages">
<!-- Messages will appear here -->
</div>
<div id="input-container">
<input type="text" id="user-input" placeholder="Type your message..." aria-label="Your message">
<button id="send-button" aria-label="Send message">Send</button>
</div>
<div id="loading-indicator">Loading...</div>
</div>
<script type="module">
import { pipeline, env } from "https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.4.0/dist/transformers.js";
// Allow remote models and set a cache directory.
env.allowRemoteModels = true;
env.cacheDir = './cache';
const chatMessagesDiv = document.getElementById('chat-messages');
const userInput = document.getElementById('user-input');
const sendButton = document.getElementById('send-button');
const loadingIndicator = document.getElementById('loading-indicator');
let chatHistory = [
{ role: "system", content: "You are a helpful assistant." },
];
let generator;
async function initializePipeline() {
loadingIndicator.style.display = 'block';
try {
// Correct Gemma 3 model name and text-generation pipeline
generator = await pipeline(
"text-generation",
"onnx-community/gemma-3-1b-it-ONNX",
{
dtype: "q4", // Quantization
progress_callback: (progress) => {
if (progress.status === 'progress') {
loadingIndicator.textContent = `Loading... ${progress.file} - ${Math.round(progress.loaded / 1000000)}MB/${Math.round(progress.total / 1000000)}MB`;
}
if (progress.status === 'loaded') {
loadingIndicator.textContent = 'Model Loaded!';
// Optional: Hide after a short delay, so the user sees it.
setTimeout(() => {
loadingIndicator.style.display = 'none';
}, 1500); // 1.5 seconds
}
},
}
);
} catch (error) {
console.error("Error loading model:", error);
loadingIndicator.textContent = "Error loading model. Check console, ensure a modern browser, and internet connection.";
loadingIndicator.style.color = "red";
sendButton.disabled = true;
return;
}
// Don't hide it here anymore: let the progress_callback and timeout handle it.
addMessage("bot", "Hello! I'm ready to chat. Ask me anything!");
}
initializePipeline();
function addMessage(role, content) {
const messageDiv = document.createElement('div');
messageDiv.classList.add('message', `${role}-message`);
messageDiv.textContent = content;
chatMessagesDiv.appendChild(messageDiv);
chatMessagesDiv.scrollTop = chatMessagesDiv.scrollHeight; // Scroll to bottom
if (role === 'user' || role === 'assistant') {
chatHistory.push({ role: role, content: content });
}
}
async function sendMessage() {
const message = userInput.value.trim();
if (!message || !generator) return;
addMessage('user', message);
userInput.value = '';
loadingIndicator.style.display = 'block';
loadingIndicator.textContent = 'Generating response...'; // Indicate response generation
sendButton.disabled = true;
try {
const output = await generator(chatHistory, {
max_new_tokens: 512,
do_sample: false // Try setting to `true` for varied responses.
});
const botResponse = output[0].generated_text.at(-1).content; // Correctly access generated text
addMessage('assistant', botResponse);
} catch (error) {
console.error("Error generating response:", error);
addMessage('bot', "Sorry, I encountered an error. Please try again.");
} finally {
loadingIndicator.style.display = 'none';
sendButton.disabled = false;
}
}
sendButton.addEventListener('click', sendMessage);
userInput.addEventListener('keypress', (event) => {
if (event.key === 'Enter') {
sendMessage();
}
});
</script>
</body>
</html> |