Spaces:
Running
Running
Update index.js
Browse files
index.js
CHANGED
|
@@ -1,76 +1,79 @@
|
|
| 1 |
-
import {
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
// Reference the elements that we will need
|
| 4 |
-
const status = document.getElementById('status');
|
| 5 |
-
const fileUpload = document.getElementById('upload');
|
| 6 |
-
const imageContainer = document.getElementById('container');
|
| 7 |
-
const example = document.getElementById('example');
|
| 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 |
-
|
| 17 |
-
|
| 18 |
-
|
| 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 |
-
// Set up a callback when the file is loaded
|
| 30 |
-
reader.onload = e2 => detect(e2.target.result);
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
|
| 35 |
|
| 36 |
-
//
|
| 37 |
-
|
| 38 |
-
imageContainer.innerHTML = '';
|
| 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 { Client } from "@gradio/client";
|
| 2 |
+
chatDisplay.appendChild(wrap);
|
| 3 |
+
chatDisplay.scrollTop = chatDisplay.scrollHeight;
|
| 4 |
+
}
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
// connect to gradio back-end
|
| 8 |
+
async function connectClient(){
|
| 9 |
+
pushMessage('assistant','Connecting to model...');
|
| 10 |
+
try{
|
| 11 |
+
client = await Client.connect('amd/gpt-oss-120b-chatbot');
|
| 12 |
+
isConnected = true;
|
| 13 |
+
pushMessage('assistant','Connected. You can send messages.');
|
| 14 |
+
}catch(err){
|
| 15 |
+
pushMessage('assistant','Failed to connect: ' + String(err));
|
| 16 |
+
console.error(err);
|
| 17 |
+
}
|
| 18 |
+
}
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
// send a chat request
|
| 22 |
+
async function sendChat(){
|
| 23 |
+
if(!isConnected){
|
| 24 |
+
await connectClient();
|
| 25 |
+
if(!isConnected) return;
|
| 26 |
+
}
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
const message = userInput.value.trim();
|
| 30 |
+
const system_prompt = systemInput.value;
|
| 31 |
+
if(!message) return;
|
| 32 |
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
// UI lock
|
| 35 |
+
sendBtn.disabled = true;
|
| 36 |
|
| 37 |
|
| 38 |
+
// show user message immediately
|
| 39 |
+
pushMessage('user', message);
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
|
| 42 |
+
try{
|
| 43 |
+
// call gradio predict endpoint
|
| 44 |
+
const result = await client.predict('/chat', {
|
| 45 |
+
message,
|
| 46 |
+
system_prompt,
|
| 47 |
+
temperature: 0,
|
| 48 |
+
});
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
// gradio client returns result.data per example
|
| 52 |
+
// Some backends may return streaming or different structure — adjust if needed
|
| 53 |
+
const assistantText = (result && result.data) ? String(result.data) : 'No response';
|
| 54 |
+
pushMessage('assistant', assistantText);
|
| 55 |
+
}catch(err){
|
| 56 |
+
console.error(err);
|
| 57 |
+
pushMessage('assistant','Error: ' + String(err));
|
| 58 |
+
}finally{
|
| 59 |
+
sendBtn.disabled = false;
|
| 60 |
+
userInput.value = '';
|
| 61 |
+
userInput.focus();
|
| 62 |
}
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
|
| 66 |
+
// wire events
|
| 67 |
+
sendBtn.addEventListener('click', sendChat);
|
| 68 |
+
userInput.addEventListener('keydown', (e) => {
|
| 69 |
+
// Enter to send
|
| 70 |
+
if(e.key === 'Enter'){
|
| 71 |
+
e.preventDefault();
|
| 72 |
+
sendChat();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
}
|
| 74 |
+
});
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
// initial connect in background
|
| 78 |
+
connectClient();
|
| 79 |
+
})();
|