Spaces:
Running
Running
Upload index.js with huggingface_hub
Browse files
index.js
CHANGED
@@ -1,76 +1,92 @@
|
|
1 |
-
import { pipeline } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.
|
2 |
-
|
3 |
-
|
4 |
-
const
|
5 |
-
const
|
6 |
-
const
|
7 |
-
const
|
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 |
-
status.textContent = '';
|
47 |
-
output.forEach(renderBox);
|
48 |
-
}
|
49 |
-
|
50 |
-
// Render a bounding box and label on the image
|
51 |
-
function renderBox({ box, label }) {
|
52 |
-
const { xmax, xmin, ymax, ymin } = box;
|
53 |
-
|
54 |
-
// Generate a random color for the box
|
55 |
-
const color = '#' + Math.floor(Math.random() * 0xFFFFFF).toString(16).padStart(6, 0);
|
56 |
-
|
57 |
-
// Draw the box
|
58 |
-
const boxElement = document.createElement('div');
|
59 |
-
boxElement.className = 'bounding-box';
|
60 |
-
Object.assign(boxElement.style, {
|
61 |
-
borderColor: color,
|
62 |
-
left: 100 * xmin + '%',
|
63 |
-
top: 100 * ymin + '%',
|
64 |
-
width: 100 * (xmax - xmin) + '%',
|
65 |
-
height: 100 * (ymax - ymin) + '%',
|
66 |
-
})
|
67 |
-
|
68 |
-
// Draw label
|
69 |
-
const labelElement = document.createElement('span');
|
70 |
-
labelElement.textContent = label;
|
71 |
-
labelElement.className = 'bounding-box-label';
|
72 |
-
labelElement.style.backgroundColor = color;
|
73 |
|
74 |
-
|
75 |
-
|
76 |
-
}
|
|
|
1 |
+
import { pipeline, TextStreamer } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.7.1';
|
2 |
+
|
3 |
+
document.addEventListener('DOMContentLoaded', () => {
|
4 |
+
const chatContainer = document.getElementById('chat-container');
|
5 |
+
const chatForm = document.getElementById('chat-form');
|
6 |
+
const userInput = document.getElementById('user-input');
|
7 |
+
const loading = document.getElementById('loading');
|
8 |
+
const errorDiv = document.getElementById('error');
|
9 |
+
let generator = null;
|
10 |
+
let messages = [{ role: "system", content: "You are a helpful assistant." }];
|
11 |
+
|
12 |
+
async function initGenerator() {
|
13 |
+
try {
|
14 |
+
loading.classList.remove('hidden');
|
15 |
+
errorDiv.classList.add('hidden');
|
16 |
+
generator = await pipeline(
|
17 |
+
"text-generation",
|
18 |
+
"onnx-community/gemma-3-270m-it-ONNX",
|
19 |
+
{ dtype: "fp32" }
|
20 |
+
);
|
21 |
+
loading.classList.add('hidden');
|
22 |
+
addMessage('system', 'Chatbot is ready! How can I help you?');
|
23 |
+
} catch (err) {
|
24 |
+
loading.classList.add('hidden');
|
25 |
+
errorDiv.textContent = 'Failed to load the model. Please try refreshing the page.';
|
26 |
+
errorDiv.classList.remove('hidden');
|
27 |
+
console.error(err);
|
28 |
+
}
|
29 |
}
|
30 |
|
31 |
+
function addMessage(role, content) {
|
32 |
+
const messageDiv = document.createElement('div');
|
33 |
+
messageDiv.classList.add('message', role === 'user' ? 'user-message' : 'bot-message');
|
34 |
+
messageDiv.textContent = content;
|
35 |
+
chatContainer.appendChild(messageDiv);
|
36 |
+
chatContainer.scrollTop = chatContainer.scrollHeight;
|
37 |
+
}
|
38 |
|
39 |
+
async function generateResponse() {
|
40 |
+
if (!generator) return;
|
41 |
+
|
42 |
+
loading.classList.remove('hidden');
|
43 |
+
document.getElementById('send-btn').disabled = true;
|
44 |
+
userInput.disabled = true;
|
45 |
+
|
46 |
+
const botMessageDiv = document.createElement('div');
|
47 |
+
botMessageDiv.classList.add('message', 'bot-message');
|
48 |
+
chatContainer.appendChild(botMessageDiv);
|
49 |
+
chatContainer.scrollTop = chatContainer.scrollHeight;
|
50 |
+
|
51 |
+
try {
|
52 |
+
const streamer = new TextStreamer(generator.tokenizer, {
|
53 |
+
skip_prompt: true,
|
54 |
+
skip_special_tokens: true,
|
55 |
+
callback_function: (text) => {
|
56 |
+
botMessageDiv.textContent += text;
|
57 |
+
chatContainer.scrollTop = chatContainer.scrollHeight;
|
58 |
+
}
|
59 |
+
});
|
60 |
+
|
61 |
+
const output = await generator(messages, {
|
62 |
+
max_new_tokens: 512,
|
63 |
+
do_sample: false,
|
64 |
+
streamer: streamer
|
65 |
+
});
|
66 |
+
|
67 |
+
const response = output[0].generated_text.at(-1).content;
|
68 |
+
messages.push({ role: "assistant", content: response });
|
69 |
+
} catch (err) {
|
70 |
+
botMessageDiv.textContent = 'Error generating response.';
|
71 |
+
console.error(err);
|
72 |
+
} finally {
|
73 |
+
loading.classList.add('hidden');
|
74 |
+
document.getElementById('send-btn').disabled = false;
|
75 |
+
userInput.disabled = false;
|
76 |
+
userInput.focus();
|
77 |
+
}
|
78 |
+
}
|
79 |
|
80 |
+
chatForm.addEventListener('submit', (e) => {
|
81 |
+
e.preventDefault();
|
82 |
+
const userText = userInput.value.trim();
|
83 |
+
if (!userText) return;
|
84 |
|
85 |
+
addMessage('user', userText);
|
86 |
+
messages.push({ role: "user", content: userText });
|
87 |
+
userInput.value = '';
|
88 |
+
generateResponse();
|
89 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
|
91 |
+
initGenerator();
|
92 |
+
});
|
|