Spaces:
Running
Running
Update index.html
Browse files- index.html +95 -65
index.html
CHANGED
@@ -3,99 +3,139 @@
|
|
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: #
|
|
|
|
|
|
|
|
|
|
|
17 |
}
|
18 |
|
19 |
#chat-container {
|
20 |
-
width:
|
21 |
-
max-width:
|
22 |
-
border:
|
23 |
-
padding:
|
24 |
-
border-radius:
|
25 |
background-color: #fff;
|
26 |
-
box-shadow: 0
|
|
|
27 |
}
|
28 |
|
29 |
#chat-messages {
|
30 |
-
height:
|
31 |
overflow-y: auto;
|
32 |
-
margin-bottom:
|
33 |
-
padding:
|
34 |
-
border: 1px solid #
|
35 |
-
border-radius:
|
|
|
36 |
}
|
37 |
|
38 |
.message {
|
39 |
-
margin-bottom:
|
40 |
-
padding:
|
41 |
-
border-radius:
|
|
|
|
|
|
|
42 |
}
|
43 |
-
|
44 |
.user-message {
|
45 |
-
background-color: #
|
|
|
46 |
text-align: right;
|
47 |
-
|
48 |
-
|
|
|
49 |
}
|
50 |
|
|
|
51 |
.bot-message {
|
52 |
-
background-color: #
|
|
|
53 |
text-align: left;
|
54 |
-
|
|
|
|
|
55 |
}
|
56 |
|
57 |
#input-container {
|
58 |
display: flex;
|
59 |
gap: 10px;
|
|
|
|
|
60 |
}
|
61 |
|
62 |
#user-input {
|
63 |
flex-grow: 1;
|
64 |
-
padding:
|
65 |
border: 1px solid #ccc;
|
66 |
-
border-radius:
|
|
|
|
|
|
|
67 |
}
|
68 |
|
|
|
|
|
|
|
|
|
|
|
69 |
#send-button {
|
70 |
-
padding:
|
71 |
-
background-color: #
|
72 |
color: white;
|
73 |
border: none;
|
74 |
-
border-radius:
|
75 |
cursor: pointer;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
}
|
77 |
|
78 |
#loading-indicator {
|
79 |
display: none; /* Hidden by default */
|
80 |
-
margin-top:
|
81 |
text-align: center;
|
82 |
color: #888;
|
|
|
83 |
}
|
|
|
84 |
.system-message{
|
85 |
-
display: none;
|
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 |
-
|
|
|
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>
|
@@ -103,7 +143,6 @@
|
|
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');
|
@@ -113,83 +152,74 @@
|
|
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 |
-
|
124 |
-
|
125 |
-
|
126 |
);
|
127 |
-
}catch(error){
|
128 |
console.error("Error loading model:", error);
|
129 |
-
loadingIndicator.textContent = "Error loading model.
|
130 |
loadingIndicator.style.color = "red";
|
131 |
-
sendButton.disabled = true;
|
132 |
return;
|
133 |
}
|
134 |
loadingIndicator.style.display = 'none';
|
135 |
-
addMessage("bot", "Hello! I'm ready to chat.");
|
136 |
}
|
137 |
|
138 |
-
initializePipeline();
|
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;
|
146 |
|
147 |
-
|
148 |
-
|
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;
|
157 |
|
158 |
addMessage('user', message);
|
159 |
-
userInput.value = '';
|
160 |
-
|
161 |
-
|
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,
|
168 |
-
do_sample: false
|
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,
|
179 |
} finally {
|
180 |
-
loadingIndicator.style.display = 'none';
|
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>
|
|
|
3 |
<head>
|
4 |
<meta charset="UTF-8">
|
5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
+
<title>Gemma 3 Chatbot</title>
|
7 |
<style>
|
8 |
body {
|
9 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; /* More modern font */
|
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: #f4f7f6; /* Light, neutral background */
|
17 |
+
}
|
18 |
+
|
19 |
+
h1 {
|
20 |
+
color: #333; /* Darker heading color */
|
21 |
+
margin-bottom: 0.5em;
|
22 |
}
|
23 |
|
24 |
#chat-container {
|
25 |
+
width: 90%; /* Responsive width */
|
26 |
+
max-width: 700px; /* Max width for larger screens */
|
27 |
+
border: none; /* Remove border */
|
28 |
+
padding: 25px; /* More padding */
|
29 |
+
border-radius: 15px; /* Rounded corners */
|
30 |
background-color: #fff;
|
31 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); /* Subtle shadow */
|
32 |
+
overflow: hidden; /* Hide overflowing content */
|
33 |
}
|
34 |
|
35 |
#chat-messages {
|
36 |
+
height: 400px; /* Taller message area */
|
37 |
overflow-y: auto;
|
38 |
+
margin-bottom: 15px;
|
39 |
+
padding: 15px;
|
40 |
+
border: 1px solid #e0e0e0; /* Lighter border */
|
41 |
+
border-radius: 10px;
|
42 |
+
background-color: #fafafa; /* Very light gray background */
|
43 |
}
|
44 |
|
45 |
.message {
|
46 |
+
margin-bottom: 10px;
|
47 |
+
padding: 12px;
|
48 |
+
border-radius: 20px; /* More rounded messages */
|
49 |
+
max-width: 70%; /* Limit message width */
|
50 |
+
word-wrap: break-word; /* Wrap long words */
|
51 |
+
box-shadow: 0 1px 2px rgba(0,0,0,0.1);
|
52 |
}
|
53 |
+
/* User message styling */
|
54 |
.user-message {
|
55 |
+
background-color: #d4e5ff; /* Light blue */
|
56 |
+
color: #000;
|
57 |
text-align: right;
|
58 |
+
align-self: flex-end;
|
59 |
+
margin-left: auto; /* Push to the right */
|
60 |
+
margin-right: 10px; /* Consistent margin */
|
61 |
}
|
62 |
|
63 |
+
/* Bot message styling */
|
64 |
.bot-message {
|
65 |
+
background-color: #e5e5ea; /* Light grey */
|
66 |
+
color: #000;
|
67 |
text-align: left;
|
68 |
+
align-self: flex-start;
|
69 |
+
margin-right: auto; /* Push to the left */
|
70 |
+
margin-left: 10px;
|
71 |
}
|
72 |
|
73 |
#input-container {
|
74 |
display: flex;
|
75 |
gap: 10px;
|
76 |
+
padding-top: 15px;
|
77 |
+
border-top: 1px solid #e0e0e0;
|
78 |
}
|
79 |
|
80 |
#user-input {
|
81 |
flex-grow: 1;
|
82 |
+
padding: 12px;
|
83 |
border: 1px solid #ccc;
|
84 |
+
border-radius: 20px; /* Rounded input field */
|
85 |
+
font-size: 16px; /* Larger font size */
|
86 |
+
outline: none; /* Remove default outline */
|
87 |
+
transition: border-color 0.2s ease-in-out; /* Smooth transition */
|
88 |
}
|
89 |
|
90 |
+
#user-input:focus {
|
91 |
+
border-color: #007bff; /* Highlight on focus */
|
92 |
+
}
|
93 |
+
|
94 |
+
|
95 |
#send-button {
|
96 |
+
padding: 12px 25px;
|
97 |
+
background-color: #007bff; /* Blue button */
|
98 |
color: white;
|
99 |
border: none;
|
100 |
+
border-radius: 20px;
|
101 |
cursor: pointer;
|
102 |
+
font-size: 16px;
|
103 |
+
transition: background-color 0.2s ease-in-out;
|
104 |
+
}
|
105 |
+
|
106 |
+
#send-button:hover {
|
107 |
+
background-color: #0056b3; /* Darker blue on hover */
|
108 |
+
}
|
109 |
+
|
110 |
+
#send-button:disabled {
|
111 |
+
background-color: #cccccc;
|
112 |
+
cursor: not-allowed;
|
113 |
}
|
114 |
|
115 |
#loading-indicator {
|
116 |
display: none; /* Hidden by default */
|
117 |
+
margin-top: 15px;
|
118 |
text-align: center;
|
119 |
color: #888;
|
120 |
+
font-style: italic;
|
121 |
}
|
122 |
+
|
123 |
.system-message{
|
124 |
+
display: none;
|
125 |
}
|
126 |
|
127 |
</style>
|
128 |
</head>
|
129 |
<body>
|
130 |
+
<h1>Gemma 3 Chatbot</h1>
|
131 |
|
132 |
<div id="chat-container">
|
133 |
<div id="chat-messages">
|
134 |
+
<!-- Messages will appear here -->
|
135 |
+
</div>
|
136 |
<div id="input-container">
|
137 |
+
<input type="text" id="user-input" placeholder="Type your message..." aria-label="Your message">
|
138 |
+
<button id="send-button" aria-label="Send message">Send</button>
|
139 |
</div>
|
140 |
<div id="loading-indicator">Loading...</div>
|
141 |
</div>
|
|
|
143 |
<script type="module">
|
144 |
import { pipeline } from "https://cdn.jsdelivr.net/npm/@huggingface/[email protected]/dist/transformers.js";
|
145 |
|
|
|
146 |
const chatMessagesDiv = document.getElementById('chat-messages');
|
147 |
const userInput = document.getElementById('user-input');
|
148 |
const sendButton = document.getElementById('send-button');
|
|
|
152 |
{ role: "system", content: "You are a helpful assistant." },
|
153 |
];
|
154 |
|
|
|
|
|
155 |
let generator;
|
156 |
+
|
157 |
async function initializePipeline() {
|
158 |
loadingIndicator.style.display = 'block';
|
159 |
+
try {
|
160 |
generator = await pipeline(
|
161 |
+
"text-generation",
|
162 |
+
"onnx-community/gemma-3-1b-it-ONNX",
|
163 |
+
{ dtype: "q4" }
|
164 |
);
|
165 |
+
} catch (error) {
|
166 |
console.error("Error loading model:", error);
|
167 |
+
loadingIndicator.textContent = "Error loading model. Check console and ensure you're using a compatible browser/environment.";
|
168 |
loadingIndicator.style.color = "red";
|
169 |
+
sendButton.disabled = true;
|
170 |
return;
|
171 |
}
|
172 |
loadingIndicator.style.display = 'none';
|
173 |
+
addMessage("bot", "Hello! I'm ready to chat. Ask me anything!"); // More welcoming message
|
174 |
}
|
175 |
|
176 |
+
initializePipeline();
|
177 |
|
178 |
function addMessage(role, content) {
|
179 |
const messageDiv = document.createElement('div');
|
180 |
messageDiv.classList.add('message', `${role}-message`);
|
181 |
messageDiv.textContent = content;
|
182 |
chatMessagesDiv.appendChild(messageDiv);
|
183 |
+
chatMessagesDiv.scrollTop = chatMessagesDiv.scrollHeight;
|
184 |
|
185 |
+
if (role === 'user' || role === 'assistant') {
|
186 |
+
chatHistory.push({ role: role, content: content });
|
|
|
187 |
}
|
188 |
}
|
189 |
|
|
|
190 |
async function sendMessage() {
|
191 |
const message = userInput.value.trim();
|
192 |
+
if (!message || !generator) return;
|
193 |
|
194 |
addMessage('user', message);
|
195 |
+
userInput.value = '';
|
196 |
+
loadingIndicator.style.display = 'block';
|
197 |
+
sendButton.disabled = true;
|
|
|
198 |
|
199 |
try {
|
|
|
200 |
const output = await generator(chatHistory, {
|
201 |
+
max_new_tokens: 512,
|
202 |
+
do_sample: false // Consider setting to true for more varied responses.
|
203 |
});
|
204 |
|
|
|
205 |
const botResponse = output[0].generated_text.at(-1).content;
|
206 |
addMessage('assistant', botResponse);
|
207 |
|
|
|
208 |
} catch (error) {
|
209 |
console.error("Error generating response:", error);
|
210 |
+
addMessage('bot', "Sorry, I encountered an error. Please try again.");
|
211 |
} finally {
|
212 |
+
loadingIndicator.style.display = 'none';
|
213 |
sendButton.disabled = false;
|
214 |
}
|
215 |
}
|
216 |
|
|
|
217 |
sendButton.addEventListener('click', sendMessage);
|
218 |
userInput.addEventListener('keypress', (event) => {
|
219 |
if (event.key === 'Enter') {
|
220 |
sendMessage();
|
221 |
}
|
222 |
});
|
|
|
223 |
</script>
|
224 |
</body>
|
225 |
</html>
|