Spaces:
Running
Running
Update index.html
Browse files- index.html +195 -19
index.html
CHANGED
@@ -1,19 +1,195 @@
|
|
1 |
-
<!
|
2 |
-
<html>
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html lang="en">
|
3 |
+
<head>
|
4 |
+
<meta charset="UTF-8">
|
5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
+
<title>Gemma Chatbot</title>
|
7 |
+
<style>
|
8 |
+
body {
|
9 |
+
font-family: sans-serif;
|
10 |
+
display: flex;
|
11 |
+
flex-direction: column;
|
12 |
+
align-items: center;
|
13 |
+
justify-content: center;
|
14 |
+
min-height: 100vh;
|
15 |
+
margin: 0;
|
16 |
+
background-color: #f0f0f0;
|
17 |
+
}
|
18 |
+
|
19 |
+
#chat-container {
|
20 |
+
width: 80%;
|
21 |
+
max-width: 600px;
|
22 |
+
border: 1px solid #ccc;
|
23 |
+
padding: 20px;
|
24 |
+
border-radius: 8px;
|
25 |
+
background-color: #fff;
|
26 |
+
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
|
27 |
+
}
|
28 |
+
|
29 |
+
#chat-messages {
|
30 |
+
height: 300px;
|
31 |
+
overflow-y: auto;
|
32 |
+
margin-bottom: 10px;
|
33 |
+
padding: 10px;
|
34 |
+
border: 1px solid #eee;
|
35 |
+
border-radius: 4px;
|
36 |
+
}
|
37 |
+
|
38 |
+
.message {
|
39 |
+
margin-bottom: 8px;
|
40 |
+
padding: 8px;
|
41 |
+
border-radius: 4px;
|
42 |
+
}
|
43 |
+
|
44 |
+
.user-message {
|
45 |
+
background-color: #dcf8c6; /* Light green */
|
46 |
+
text-align: right;
|
47 |
+
margin-left: 20%; /* Push to the right */
|
48 |
+
|
49 |
+
}
|
50 |
+
|
51 |
+
.bot-message {
|
52 |
+
background-color: #f0f0f0; /* Light gray */
|
53 |
+
text-align: left;
|
54 |
+
margin-right: 20%; /* Push to the left */
|
55 |
+
}
|
56 |
+
|
57 |
+
#input-container {
|
58 |
+
display: flex;
|
59 |
+
gap: 10px;
|
60 |
+
}
|
61 |
+
|
62 |
+
#user-input {
|
63 |
+
flex-grow: 1;
|
64 |
+
padding: 10px;
|
65 |
+
border: 1px solid #ccc;
|
66 |
+
border-radius: 4px;
|
67 |
+
}
|
68 |
+
|
69 |
+
#send-button {
|
70 |
+
padding: 10px 20px;
|
71 |
+
background-color: #4CAF50;
|
72 |
+
color: white;
|
73 |
+
border: none;
|
74 |
+
border-radius: 4px;
|
75 |
+
cursor: pointer;
|
76 |
+
}
|
77 |
+
|
78 |
+
#loading-indicator {
|
79 |
+
display: none; /* Hidden by default */
|
80 |
+
margin-top: 10px;
|
81 |
+
text-align: center;
|
82 |
+
color: #888;
|
83 |
+
}
|
84 |
+
.system-message{
|
85 |
+
display: none; /* we don't display system messages */
|
86 |
+
}
|
87 |
+
|
88 |
+
</style>
|
89 |
+
</head>
|
90 |
+
<body>
|
91 |
+
<h1>Gemma Chatbot</h1>
|
92 |
+
|
93 |
+
<div id="chat-container">
|
94 |
+
<div id="chat-messages">
|
95 |
+
</div>
|
96 |
+
<div id="input-container">
|
97 |
+
<input type="text" id="user-input" placeholder="Type your message...">
|
98 |
+
<button id="send-button">Send</button>
|
99 |
+
</div>
|
100 |
+
<div id="loading-indicator">Loading...</div>
|
101 |
+
</div>
|
102 |
+
|
103 |
+
<script type="module">
|
104 |
+
import { pipeline } from "https://cdn.jsdelivr.net/npm/@huggingface/[email protected]/dist/transformers.js";
|
105 |
+
|
106 |
+
|
107 |
+
const chatMessagesDiv = document.getElementById('chat-messages');
|
108 |
+
const userInput = document.getElementById('user-input');
|
109 |
+
const sendButton = document.getElementById('send-button');
|
110 |
+
const loadingIndicator = document.getElementById('loading-indicator');
|
111 |
+
|
112 |
+
let chatHistory = [
|
113 |
+
{ role: "system", content: "You are a helpful assistant." },
|
114 |
+
];
|
115 |
+
|
116 |
+
|
117 |
+
// Create a text generation pipeline
|
118 |
+
let generator;
|
119 |
+
async function initializePipeline() {
|
120 |
+
loadingIndicator.style.display = 'block';
|
121 |
+
try{
|
122 |
+
generator = await pipeline(
|
123 |
+
"text-generation",
|
124 |
+
"onnx-community/gemma-3-1b-it-ONNX", // "Xenova/gemma-1b-it-quantized" is an alternative
|
125 |
+
{ dtype: "q4" }, // Use quantized weights (smaller, faster, but slightly less accurate)
|
126 |
+
);
|
127 |
+
}catch(error){
|
128 |
+
console.error("Error loading model:", error);
|
129 |
+
loadingIndicator.textContent = "Error loading model. Check console for details, and ensure you're using a compatible browser/environment.";
|
130 |
+
loadingIndicator.style.color = "red";
|
131 |
+
sendButton.disabled = true; // Disable sending until loaded.
|
132 |
+
return;
|
133 |
+
}
|
134 |
+
loadingIndicator.style.display = 'none';
|
135 |
+
addMessage("bot", "Hello! I'm ready to chat.");
|
136 |
+
}
|
137 |
+
|
138 |
+
initializePipeline(); // Call on page load
|
139 |
+
|
140 |
+
function addMessage(role, content) {
|
141 |
+
const messageDiv = document.createElement('div');
|
142 |
+
messageDiv.classList.add('message', `${role}-message`);
|
143 |
+
messageDiv.textContent = content;
|
144 |
+
chatMessagesDiv.appendChild(messageDiv);
|
145 |
+
chatMessagesDiv.scrollTop = chatMessagesDiv.scrollHeight; // Scroll to bottom
|
146 |
+
|
147 |
+
// Only add user and assistant messages to history.
|
148 |
+
if (role === 'user' || role === 'assistant'){
|
149 |
+
chatHistory.push({ role: role, content: content });
|
150 |
+
}
|
151 |
+
}
|
152 |
+
|
153 |
+
|
154 |
+
async function sendMessage() {
|
155 |
+
const message = userInput.value.trim();
|
156 |
+
if (!message || !generator) return; // Don't send empty messages or before loading
|
157 |
+
|
158 |
+
addMessage('user', message);
|
159 |
+
userInput.value = ''; // Clear input
|
160 |
+
|
161 |
+
loadingIndicator.style.display = 'block'; // Show loading indicator
|
162 |
+
sendButton.disabled = true; // Prevent rapid clicks
|
163 |
+
|
164 |
+
try {
|
165 |
+
// Call the chatbot pipeline
|
166 |
+
const output = await generator(chatHistory, {
|
167 |
+
max_new_tokens: 512, // Adjust as needed
|
168 |
+
do_sample: false // Set to true for more creative responses
|
169 |
+
});
|
170 |
+
|
171 |
+
// Extract and add the bot's response
|
172 |
+
const botResponse = output[0].generated_text.at(-1).content;
|
173 |
+
addMessage('assistant', botResponse);
|
174 |
+
|
175 |
+
|
176 |
+
} catch (error) {
|
177 |
+
console.error("Error generating response:", error);
|
178 |
+
addMessage('bot', "Sorry, there was an error generating a response.");
|
179 |
+
} finally {
|
180 |
+
loadingIndicator.style.display = 'none'; // Hide loading indicator
|
181 |
+
sendButton.disabled = false;
|
182 |
+
}
|
183 |
+
}
|
184 |
+
|
185 |
+
|
186 |
+
sendButton.addEventListener('click', sendMessage);
|
187 |
+
userInput.addEventListener('keypress', (event) => {
|
188 |
+
if (event.key === 'Enter') {
|
189 |
+
sendMessage();
|
190 |
+
}
|
191 |
+
});
|
192 |
+
|
193 |
+
</script>
|
194 |
+
</body>
|
195 |
+
</html>
|