Spaces:
Running
Running
Upload index.js with huggingface_hub
Browse files
index.js
CHANGED
@@ -1,76 +1,125 @@
|
|
1 |
-
import { pipeline } from '
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
//
|
4 |
-
|
5 |
-
const
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
|
|
|
|
|
|
|
|
|
10 |
|
11 |
-
//
|
12 |
-
|
13 |
-
const
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
26 |
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
|
30 |
-
|
31 |
|
32 |
-
|
|
|
|
|
|
|
|
|
33 |
});
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
async function detect(img) {
|
38 |
-
imageContainer.innerHTML = '';
|
39 |
-
imageContainer.style.backgroundImage = `url(${img})`;
|
40 |
-
|
41 |
-
status.textContent = 'Analysing...';
|
42 |
-
const output = await detector(img, {
|
43 |
-
threshold: 0.5,
|
44 |
-
percentage: true,
|
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 |
-
boxElement.appendChild(labelElement);
|
75 |
-
imageContainer.appendChild(boxElement);
|
76 |
-
}
|
|
|
1 |
+
import { pipeline, TextStreamer } from '@huggingface/transformers';
|
2 |
+
|
3 |
+
// DOM Elements
|
4 |
+
const chatHistory = document.getElementById('chat-history');
|
5 |
+
const userInput = document.getElementById('user-input');
|
6 |
+
const sendButton = document.getElementById('send-button');
|
7 |
+
|
8 |
+
// Global pipeline variable
|
9 |
+
let generator = null;
|
10 |
+
|
11 |
+
// Initialize the model when the page loads
|
12 |
+
async function initializeModel() {
|
13 |
+
try {
|
14 |
+
showLoader('Loading model...');
|
15 |
+
generator = await pipeline('text-generation', 'onnx-community/Qwen3-0.6B-ONNX', { dtype: 'q4f16' });
|
16 |
+
hideLoader();
|
17 |
+
addMessage('System', 'Model loaded successfully! You can now start chatting.', 'system');
|
18 |
+
} catch (error) {
|
19 |
+
hideLoader();
|
20 |
+
addMessage('System', `Error loading model: ${error.message}`, 'error');
|
21 |
+
}
|
22 |
+
}
|
23 |
|
24 |
+
// Show loading message
|
25 |
+
function showLoader(message) {
|
26 |
+
const loaderElement = document.createElement('div');
|
27 |
+
loaderElement.className = 'chat-message system';
|
28 |
+
loaderElement.id = 'loader';
|
29 |
+
loaderElement.innerHTML = `
|
30 |
+
<div class="message-content">
|
31 |
+
<div class="loader"></div>
|
32 |
+
<span>${message}</span>
|
33 |
+
</div>
|
34 |
+
`;
|
35 |
+
chatHistory.appendChild(loaderElement);
|
36 |
+
chatHistory.scrollTop = chatHistory.scrollHeight;
|
37 |
+
}
|
38 |
|
39 |
+
// Hide loading message
|
40 |
+
function hideLoader() {
|
41 |
+
const loaderElement = document.getElementById('loader');
|
42 |
+
if (loaderElement) loaderElement.remove();
|
43 |
+
}
|
44 |
|
45 |
+
// Add message to chat display
|
46 |
+
function addMessage(role, content, type = 'user') {
|
47 |
+
const messageElement = document.createElement('div');
|
48 |
+
messageElement.className = `chat-message ${type}`;
|
49 |
+
|
50 |
+
if (type === 'user' || type === 'assistant') {
|
51 |
+
messageElement.innerHTML = `<strong>${role}:</strong> ${content}`;
|
52 |
+
} else {
|
53 |
+
messageElement.innerHTML = `<em>${content}</em>`;
|
54 |
+
}
|
55 |
+
|
56 |
+
chatHistory.appendChild(messageElement);
|
57 |
+
chatHistory.scrollTop = chatHistory.scrollHeight;
|
58 |
+
}
|
59 |
|
60 |
+
// Handle sending messages
|
61 |
+
async function handleSendMessage() {
|
62 |
+
if (!generator) {
|
63 |
+
addMessage('System', 'Model is still loading. Please wait...', 'error');
|
64 |
+
return;
|
65 |
+
}
|
66 |
+
|
67 |
+
const input = userInput.value.trim();
|
68 |
+
if (!input) return;
|
69 |
+
|
70 |
+
// Add user message to chat
|
71 |
+
addMessage('You', input, 'user');
|
72 |
+
userInput.value = '';
|
73 |
+
|
74 |
+
try {
|
75 |
+
// Prepare messages for model
|
76 |
+
const messages = [
|
77 |
+
{ role: 'system', content: 'You are a helpful assistant.' },
|
78 |
+
{ role: 'user', content: input }
|
79 |
+
];
|
80 |
+
|
81 |
+
// Create temporary element for streaming response
|
82 |
+
const assistantMessageElement = document.createElement('div');
|
83 |
+
assistantMessageElement.className = 'chat-message assistant';
|
84 |
+
assistantMessageElement.innerHTML = '<strong>Assistant:</strong> ';
|
85 |
+
const contentSpan = document.createElement('span');
|
86 |
+
assistantMessageElement.appendChild(contentSpan);
|
87 |
+
chatHistory.appendChild(assistantMessageElement);
|
88 |
+
|
89 |
+
// Create streamer to handle response tokens
|
90 |
+
const streamer = new TextStreamer(generator.tokenizer, {
|
91 |
+
skip_prompt: true,
|
92 |
+
skip_special_tokens: true,
|
93 |
+
callback_function: (token) => {
|
94 |
+
contentSpan.textContent += token;
|
95 |
+
chatHistory.scrollTop = chatHistory.scrollHeight;
|
96 |
+
}
|
97 |
+
});
|
98 |
|
99 |
+
// Generate response
|
100 |
+
await generator(messages, {
|
101 |
+
max_new_tokens: 256,
|
102 |
+
do_sample: false,
|
103 |
+
streamer: streamer
|
104 |
+
});
|
105 |
|
106 |
+
// Final response element update
|
107 |
+
contentSpan.textContent = contentSpan.textContent.trim();
|
108 |
+
chatHistory.scrollTop = chatHistory.scrollHeight;
|
109 |
+
} catch (error) {
|
110 |
+
addMessage('System', `Error generating response: ${error.message}`, 'error');
|
111 |
+
}
|
112 |
+
}
|
113 |
|
114 |
+
// Event Listeners
|
115 |
+
sendButton.addEventListener('click', handleSendMessage);
|
116 |
|
117 |
+
userInput.addEventListener('keydown', (e) => {
|
118 |
+
if (e.key === 'Enter' && !e.shiftKey) {
|
119 |
+
e.preventDefault();
|
120 |
+
handleSendMessage();
|
121 |
+
}
|
122 |
});
|
123 |
|
124 |
+
// Initialize the model on page load
|
125 |
+
document.addEventListener('DOMContentLoaded', initializeModel);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|