HARISARAVANANM commited on
Commit
b0bc8fa
·
verified ·
1 Parent(s): 6cb08b3

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +346 -21
index.html CHANGED
@@ -1,29 +1,354 @@
1
  <!DOCTYPE html>
2
  <html lang="en">
3
-
4
  <head>
5
- <meta charset="UTF-8" />
6
- <link rel="stylesheet" href="style.css" />
 
 
 
 
 
 
 
 
7
 
8
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
9
- <title>Transformers.js - Object Detection</title>
10
- </head>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  <body>
13
- <h1>Object Detection w/ 🤗 Transformers.js</h1>
14
- <label id="container" for="upload">
15
- <svg width="25" height="25" viewBox="0 0 25 25" fill="none" xmlns="http://www.w3.org/2000/svg">
16
- <path fill="#000"
17
- d="M3.5 24.3a3 3 0 0 1-1.9-.8c-.5-.5-.8-1.2-.8-1.9V2.9c0-.7.3-1.3.8-1.9.6-.5 1.2-.7 2-.7h18.6c.7 0 1.3.2 1.9.7.5.6.7 1.2.7 2v18.6c0 .7-.2 1.4-.7 1.9a3 3 0 0 1-2 .8H3.6Zm0-2.7h18.7V2.9H3.5v18.7Zm2.7-2.7h13.3c.3 0 .5 0 .6-.3v-.7l-3.7-5a.6.6 0 0 0-.6-.2c-.2 0-.4 0-.5.3l-3.5 4.6-2.4-3.3a.6.6 0 0 0-.6-.3c-.2 0-.4.1-.5.3l-2.7 3.6c-.1.2-.2.4 0 .7.1.2.3.3.6.3Z">
18
- </path>
19
- </svg>
20
- Click to upload image
21
- <label id="example">(or try example)</label>
22
- </label>
23
- <label id="status">Loading model...</label>
24
- <input id="upload" type="file" accept="image/*" />
25
-
26
- <script src="index.js" type="module"></script>
27
- </body>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  </html>
 
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>AI Chatbot</title>
7
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/iframe-resizer/4.3.6/iframeResizer.min.js"></script>
8
+ <style>
9
+ * {
10
+ margin: 0;
11
+ padding: 0;
12
+ box-sizing: border-box;
13
+ }
14
 
15
+ body {
16
+ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
17
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
18
+ height: 100vh;
19
+ display: flex;
20
+ justify-content: center;
21
+ align-items: center;
22
+ padding: 20px;
23
+ }
24
+
25
+ .container {
26
+ width: 100%;
27
+ max-width: 600px;
28
+ height: 600px;
29
+ background: white;
30
+ border-radius: 15px;
31
+ box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
32
+ display: flex;
33
+ flex-direction: column;
34
+ overflow: hidden;
35
+ }
36
+
37
+ .header {
38
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
39
+ color: white;
40
+ padding: 20px;
41
+ text-align: center;
42
+ }
43
+
44
+ .header h1 {
45
+ font-size: 24px;
46
+ margin-bottom: 5px;
47
+ }
48
+
49
+ .header p {
50
+ font-size: 12px;
51
+ opacity: 0.9;
52
+ }
53
+
54
+ .chat-area {
55
+ flex: 1;
56
+ overflow-y: auto;
57
+ padding: 20px;
58
+ background: #f5f5f5;
59
+ display: flex;
60
+ flex-direction: column;
61
+ gap: 10px;
62
+ }
63
+
64
+ .message {
65
+ display: flex;
66
+ margin-bottom: 10px;
67
+ animation: slideIn 0.3s ease-in-out;
68
+ }
69
+
70
+ @keyframes slideIn {
71
+ from {
72
+ opacity: 0;
73
+ transform: translateY(10px);
74
+ }
75
+ to {
76
+ opacity: 1;
77
+ transform: translateY(0);
78
+ }
79
+ }
80
+
81
+ .user-message {
82
+ justify-content: flex-end;
83
+ }
84
+
85
+ .user-message .text {
86
+ background: #667eea;
87
+ color: white;
88
+ padding: 12px 15px;
89
+ border-radius: 15px 15px 0 15px;
90
+ max-width: 80%;
91
+ word-wrap: break-word;
92
+ }
93
+
94
+ .bot-message {
95
+ justify-content: flex-start;
96
+ }
97
+
98
+ .bot-message .text {
99
+ background: white;
100
+ color: #333;
101
+ padding: 12px 15px;
102
+ border-radius: 15px 15px 15px 0;
103
+ max-width: 80%;
104
+ word-wrap: break-word;
105
+ border: 1px solid #e0e0e0;
106
+ }
107
+
108
+ .loading {
109
+ display: flex;
110
+ gap: 5px;
111
+ align-items: center;
112
+ }
113
+
114
+ .loading span {
115
+ width: 8px;
116
+ height: 8px;
117
+ background: #667eea;
118
+ border-radius: 50%;
119
+ animation: bounce 1.4s infinite;
120
+ }
121
+
122
+ .loading span:nth-child(2) {
123
+ animation-delay: 0.2s;
124
+ }
125
+
126
+ .loading span:nth-child(3) {
127
+ animation-delay: 0.4s;
128
+ }
129
+
130
+ @keyframes bounce {
131
+ 0%, 80%, 100% {
132
+ opacity: 0.3;
133
+ transform: translateY(0);
134
+ }
135
+ 40% {
136
+ opacity: 1;
137
+ transform: translateY(-10px);
138
+ }
139
+ }
140
+
141
+ .input-area {
142
+ padding: 15px;
143
+ background: white;
144
+ border-top: 1px solid #e0e0e0;
145
+ display: flex;
146
+ gap: 10px;
147
+ }
148
+
149
+ input {
150
+ flex: 1;
151
+ border: 1px solid #e0e0e0;
152
+ border-radius: 25px;
153
+ padding: 10px 15px;
154
+ font-size: 14px;
155
+ outline: none;
156
+ transition: border-color 0.3s;
157
+ }
158
+
159
+ input:focus {
160
+ border-color: #667eea;
161
+ }
162
+
163
+ button {
164
+ background: #667eea;
165
+ color: white;
166
+ border: none;
167
+ border-radius: 25px;
168
+ padding: 10px 25px;
169
+ cursor: pointer;
170
+ font-weight: 600;
171
+ transition: background 0.3s;
172
+ }
173
+
174
+ button:hover {
175
+ background: #764ba2;
176
+ }
177
+
178
+ button:disabled {
179
+ background: #ccc;
180
+ cursor: not-allowed;
181
+ }
182
 
183
+ .empty-state {
184
+ display: flex;
185
+ flex-direction: column;
186
+ justify-content: center;
187
+ align-items: center;
188
+ height: 100%;
189
+ color: #999;
190
+ text-align: center;
191
+ }
192
+
193
+ .empty-state p {
194
+ font-size: 14px;
195
+ }
196
+ </style>
197
+ </head>
198
  <body>
199
+ <div class="container">
200
+ <div class="header">
201
+ <h1>🤖 AI Chatbot</h1>
202
+ <p>Powered by Transformers.js</p>
203
+ </div>
204
+
205
+ <div class="chat-area" id="chatArea">
206
+ <div class="empty-state">
207
+ <p>Start a conversation!</p>
208
+ </div>
209
+ </div>
210
+
211
+ <div class="input-area">
212
+ <input
213
+ type="text"
214
+ id="userInput"
215
+ placeholder="Type your message..."
216
+ autocomplete="off"
217
+ >
218
+ <button id="sendBtn" onclick="sendMessage()">Send</button>
219
+ </div>
220
+ </div>
221
+
222
+ <script type="module">
223
+ import { pipeline } from 'https://cdn.jsdelivr.net/npm/@xenova/transformers@2.6.0';
224
+
225
+ let chatHistory = [];
226
+ let isLoading = false;
227
+ let generator = null;
228
+
229
+ const chatArea = document.getElementById('chatArea');
230
+ const userInput = document.getElementById('userInput');
231
+ const sendBtn = document.getElementById('sendBtn');
232
+
233
+ // Initialize the model
234
+ async function initModel() {
235
+ console.log('Loading model...');
236
+ sendBtn.disabled = true;
237
+ sendBtn.textContent = 'Loading...';
238
+
239
+ try {
240
+ generator = await pipeline(
241
+ 'text-generation',
242
+ 'Xenova/distilgpt2'
243
+ );
244
+ sendBtn.disabled = false;
245
+ sendBtn.textContent = 'Send';
246
+ console.log('Model loaded successfully!');
247
+ } catch (error) {
248
+ console.error('Error loading model:', error);
249
+ sendBtn.textContent = 'Error Loading Model';
250
+ }
251
+ }
252
+
253
+ // Send message function
254
+ window.sendMessage = async function() {
255
+ const message = userInput.value.trim();
256
+
257
+ if (!message || isLoading || !generator) return;
258
+
259
+ // Add user message to chat
260
+ addMessage(message, 'user');
261
+ userInput.value = '';
262
+ isLoading = true;
263
+ sendBtn.disabled = true;
264
+
265
+ // Add loading indicator
266
+ const loadingDiv = document.createElement('div');
267
+ loadingDiv.className = 'message bot-message';
268
+ loadingDiv.innerHTML = '<div class="text"><div class="loading"><span></span><span></span><span></span></div></div>';
269
+ chatArea.appendChild(loadingDiv);
270
+ chatArea.scrollTop = chatArea.scrollHeight;
271
 
272
+ try {
273
+ // Prepare input with conversation history
274
+ let inputText = message;
275
+ if (chatHistory.length > 0) {
276
+ const recentHistory = chatHistory.slice(-4).map(m => `${m.role}: ${m.content}`).join('\n');
277
+ inputText = `${recentHistory}\nUser: ${message}\nAssistant:`;
278
+ } else {
279
+ inputText = `User: ${message}\nAssistant:`;
280
+ }
281
+
282
+ // Generate response
283
+ const result = await generator(inputText, {
284
+ max_new_tokens: 100,
285
+ temperature: 0.7,
286
+ top_p: 0.9,
287
+ });
288
+
289
+ let response = result[0].generated_text;
290
+
291
+ // Extract only the assistant's response
292
+ const assistantIndex = response.lastIndexOf('Assistant:');
293
+ if (assistantIndex !== -1) {
294
+ response = response.substring(assistantIndex + 10).trim();
295
+ }
296
+
297
+ // Clean up response
298
+ response = response.split('\n')[0].trim();
299
+ if (!response) {
300
+ response = "I'm not sure how to respond to that. Could you rephrase?";
301
+ }
302
+
303
+ // Remove loading indicator
304
+ loadingDiv.remove();
305
+
306
+ // Add bot response
307
+ addMessage(response, 'bot');
308
+
309
+ } catch (error) {
310
+ console.error('Error generating response:', error);
311
+ loadingDiv.remove();
312
+ addMessage('Sorry, I encountered an error. Please try again.', 'bot');
313
+ }
314
+
315
+ isLoading = false;
316
+ sendBtn.disabled = false;
317
+ };
318
+
319
+ // Add message to chat
320
+ function addMessage(text, role) {
321
+ chatHistory.push({ role, content: text });
322
+
323
+ const messageDiv = document.createElement('div');
324
+ messageDiv.className = `message ${role === 'user' ? 'user-message' : 'bot-message'}`;
325
+ messageDiv.innerHTML = `<div class="text">${escapeHtml(text)}</div>`;
326
+
327
+ chatArea.appendChild(messageDiv);
328
+ chatArea.scrollTop = chatArea.scrollHeight;
329
+ }
330
+
331
+ // Escape HTML to prevent XSS
332
+ function escapeHtml(text) {
333
+ const map = {
334
+ '&': '&amp;',
335
+ '<': '&lt;',
336
+ '>': '&gt;',
337
+ '"': '&quot;',
338
+ "'": '&#039;'
339
+ };
340
+ return text.replace(/[&<>"']/g, m => map[m]);
341
+ }
342
+
343
+ // Handle Enter key
344
+ userInput.addEventListener('keypress', (e) => {
345
+ if (e.key === 'Enter' && !isLoading) {
346
+ sendMessage();
347
+ }
348
+ });
349
+
350
+ // Initialize on load
351
+ window.addEventListener('load', initModel);
352
+ </script>
353
+ </body>
354
  </html>