Spaces:
Running
Running
Upload index.js with huggingface_hub
Browse files
index.js
CHANGED
|
@@ -1,76 +1,39 @@
|
|
| 1 |
-
import { pipeline } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
// Create a new object detection pipeline
|
| 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 |
-
const reader = new FileReader();
|
| 28 |
-
|
| 29 |
-
// Set up a callback when the file is loaded
|
| 30 |
-
reader.onload = e2 => detect(e2.target.result);
|
| 31 |
-
|
| 32 |
-
reader.readAsDataURL(file);
|
| 33 |
-
});
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
// Detect objects in the image
|
| 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 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 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.8.0';
|
| 2 |
+
|
| 3 |
+
class AppleStyleChatbot {
|
| 4 |
+
constructor() {
|
| 5 |
+
this.generator = null;
|
| 6 |
+
this.messages = [];
|
| 7 |
+
this.isGenerating = false;
|
| 8 |
+
this.initElements();
|
| 9 |
+
this.initEventListeners();
|
| 10 |
+
this.loadModel();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
}
|
| 12 |
|
| 13 |
+
initElements() {
|
| 14 |
+
// Loading elements
|
| 15 |
+
this.loadingScreen = document.getElementById('loading-screen');
|
| 16 |
+
this.loadingText = document.getElementById('loading-text');
|
| 17 |
+
this.progressFill = document.getElementById('progress-fill');
|
| 18 |
+
this.progressText = document.getElementById('progress-text');
|
| 19 |
+
|
| 20 |
+
// Chat elements
|
| 21 |
+
this.chatContainer = document.getElementById('chat-container');
|
| 22 |
+
this.chatMessages = document.getElementById('chat-messages');
|
| 23 |
+
this.inputArea = document.getElementById('input-area');
|
| 24 |
+
this.messageInput = document.getElementById('message-input');
|
| 25 |
+
this.sendButton = document.getElementById('send-button');
|
| 26 |
+
this.charCount = document.getElementById('char-count');
|
| 27 |
+
this.modelStatus = document.getElementById('model-status');
|
| 28 |
+
|
| 29 |
+
// Remove welcome message when first user message is sent
|
| 30 |
+
this.welcomeMessage = null;
|
| 31 |
+
}
|
| 32 |
|
| 33 |
+
initEventListeners() {
|
| 34 |
+
// Send button click
|
| 35 |
+
this.sendButton.addEventListener('click', () => this.sendMessage());
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
// Enter key to send (Shift+Enter for new line)
|
| 38 |
+
this.messageInput.addEventListener('keydown', (e) => {
|
| 39 |
+
if (e.key
|