Spaces:
Running
Running
Upload index.js with huggingface_hub
Browse files
index.js
CHANGED
|
@@ -1,76 +1,152 @@
|
|
| 1 |
-
import { pipeline } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
status.textContent = 'Loading model...';
|
| 13 |
-
const detector = await pipeline('object-detection', 'Xenova/detr-resnet-50');
|
| 14 |
-
status.textContent = 'Ready';
|
| 15 |
-
|
| 16 |
-
example.addEventListener('click', (e) => {
|
| 17 |
-
e.preventDefault();
|
| 18 |
-
detect(EXAMPLE_URL);
|
| 19 |
-
});
|
| 20 |
-
|
| 21 |
-
fileUpload.addEventListener('change', function (e) {
|
| 22 |
-
const file = e.target.files[0];
|
| 23 |
-
if (!file) {
|
| 24 |
-
return;
|
| 25 |
}
|
| 26 |
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
imageContainer.style.backgroundImage = `url(${img})`;
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 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 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.7.1';
|
| 2 |
+
|
| 3 |
+
class GemmaChatbot {
|
| 4 |
+
constructor() {
|
| 5 |
+
this.generator = null;
|
| 6 |
+
this.messages = [
|
| 7 |
+
{ role: "system", content: "You are a helpful, friendly AI assistant. Keep your responses concise and helpful." }
|
| 8 |
+
];
|
| 9 |
+
this.isGenerating = false;
|
| 10 |
+
this.initializeElements();
|
| 11 |
+
this.initializeModel();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
}
|
| 13 |
|
| 14 |
+
initializeElements() {
|
| 15 |
+
this.chatMessages = document.getElementById('chatMessages');
|
| 16 |
+
this.userInput = document.getElementById('userInput');
|
| 17 |
+
this.sendButton = document.getElementById('sendButton');
|
| 18 |
+
this.loadingIndicator = document.getElementById('loadingIndicator');
|
| 19 |
+
|
| 20 |
+
this.sendButton.addEventListener('click', () => this.sendMessage());
|
| 21 |
+
this.userInput.addEventListener('keypress', (e) => {
|
| 22 |
+
if (e.key === 'Enter' && !e.shiftKey) {
|
| 23 |
+
e.preventDefault();
|
| 24 |
+
this.sendMessage();
|
| 25 |
+
}
|
| 26 |
+
});
|
| 27 |
+
}
|
| 28 |
|
| 29 |
+
async initializeModel() {
|
| 30 |
+
try {
|
| 31 |
+
console.log('Initializing Gemma model...');
|
| 32 |
+
this.generator = await pipeline(
|
| 33 |
+
'text-generation',
|
| 34 |
+
'onnx-community/gemma-3-270m-it-ONNX',
|
| 35 |
+
{ dtype: 'fp32' }
|
| 36 |
+
);
|
| 37 |
+
console.log('Model loaded successfully');
|
| 38 |
+
this.enableChat();
|
| 39 |
+
} catch (error) {
|
| 40 |
+
console.error('Failed to initialize model:', error);
|
| 41 |
+
this.showError('Failed to load AI model. Please refresh the page to try again.');
|
| 42 |
+
}
|
| 43 |
+
}
|
| 44 |
|
| 45 |
+
enableChat() {
|
| 46 |
+
this.loadingIndicator.style.display = 'none';
|
| 47 |
+
this.userInput.disabled = false;
|
| 48 |
+
this.sendButton.disabled = false;
|
| 49 |
+
this.userInput.focus();
|
| 50 |
+
}
|
| 51 |
|
| 52 |
+
addMessage(content, isUser = false) {
|
| 53 |
+
const messageDiv = document.createElement('div');
|
| 54 |
+
messageDiv.className = `message ${isUser ? 'user' : 'assistant'}`;
|
| 55 |
+
|
| 56 |
+
const contentDiv = document.createElement('div');
|
| 57 |
+
contentDiv.className = 'message-content';
|
| 58 |
+
contentDiv.textContent = content;
|
| 59 |
+
|
| 60 |
+
messageDiv.appendChild(contentDiv);
|
| 61 |
+
this.chatMessages.appendChild(messageDiv);
|
| 62 |
+
this.scrollToBottom();
|
| 63 |
+
|
| 64 |
+
return contentDiv;
|
| 65 |
+
}
|
| 66 |
|
| 67 |
+
scrollToBottom() {
|
| 68 |
+
this.chatMessages.scrollTop = this.chatMessages.scrollHeight;
|
| 69 |
+
}
|
|
|
|
| 70 |
|
| 71 |
+
async sendMessage() {
|
| 72 |
+
const message = this.userInput.value.trim();
|
| 73 |
+
if (!message || this.isGenerating || !this.generator) return;
|
| 74 |
+
|
| 75 |
+
this.isGenerating = true;
|
| 76 |
+
this.userInput.value = '';
|
| 77 |
+
this.userInput.disabled = true;
|
| 78 |
+
this.sendButton.disabled = true;
|
| 79 |
+
|
| 80 |
+
// Add user message
|
| 81 |
+
this.addMessage(message, true);
|
| 82 |
+
this.messages.push({ role: "user", content: message });
|
| 83 |
+
|
| 84 |
+
// Add assistant message placeholder
|
| 85 |
+
const assistantMessageDiv = document.createElement('div');
|
| 86 |
+
assistantMessageDiv.className = 'message assistant';
|
| 87 |
+
|
| 88 |
+
const contentDiv = document.createElement('div');
|
| 89 |
+
contentDiv.className = 'message-content';
|
| 90 |
+
|
| 91 |
+
const typingIndicator = document.createElement('span');
|
| 92 |
+
typingIndicator.className = 'typing-indicator';
|
| 93 |
+
typingIndicator.innerHTML = '<span></span><span></span><span></span>';
|
| 94 |
+
contentDiv.appendChild(typingIndicator);
|
| 95 |
+
|
| 96 |
+
assistantMessageDiv.appendChild(contentDiv);
|
| 97 |
+
this.chatMessages.appendChild(assistantMessageDiv);
|
| 98 |
+
this.scrollToBottom();
|
| 99 |
+
|
| 100 |
+
try {
|
| 101 |
+
let fullResponse = '';
|
| 102 |
+
|
| 103 |
+
// Create custom streamer
|
| 104 |
+
const streamer = new TextStreamer(this.generator.tokenizer, {
|
| 105 |
+
skip_prompt: true,
|
| 106 |
+
skip_special_tokens: true,
|
| 107 |
+
callback_function: (text) => {
|
| 108 |
+
fullResponse += text;
|
| 109 |
+
contentDiv.textContent = fullResponse;
|
| 110 |
+
this.scrollToBottom();
|
| 111 |
+
}
|
| 112 |
+
});
|
| 113 |
+
|
| 114 |
+
// Generate response
|
| 115 |
+
const output = await this.generator(this.messages, {
|
| 116 |
+
max_new_tokens: 256,
|
| 117 |
+
do_sample: true,
|
| 118 |
+
temperature: 0.7,
|
| 119 |
+
top_p: 0.9,
|
| 120 |
+
streamer: streamer
|
| 121 |
+
});
|
| 122 |
+
|
| 123 |
+
const generatedContent = output[0].generated_text.at(-1).content;
|
| 124 |
+
this.messages.push({ role: "assistant", content: generatedContent });
|
| 125 |
+
|
| 126 |
+
// Ensure final content is displayed
|
| 127 |
+
contentDiv.textContent = generatedContent;
|
| 128 |
+
|
| 129 |
+
} catch (error) {
|
| 130 |
+
console.error('Generation error:', error);
|
| 131 |
+
contentDiv.textContent = 'Sorry, I encountered an error. Please try again.';
|
| 132 |
+
} finally {
|
| 133 |
+
this.isGenerating = false;
|
| 134 |
+
this.userInput.disabled = false;
|
| 135 |
+
this.sendButton.disabled = false;
|
| 136 |
+
this.userInput.focus();
|
| 137 |
+
}
|
| 138 |
+
}
|
| 139 |
|
| 140 |
+
showError(message) {
|
| 141 |
+
this.loadingIndicator.innerHTML = `
|
| 142 |
+
<div class="error-message">
|
| 143 |
+
<p>⚠️ ${message}</p>
|
| 144 |
+
</div>
|
| 145 |
+
`;
|
| 146 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
}
|
| 148 |
+
|
| 149 |
+
// Initialize chatbot when DOM is ready
|
| 150 |
+
document.addEventListener('DOMContentLoaded', () => {
|
| 151 |
+
new GemmaChatbot();
|
| 152 |
+
});
|