Spaces:
Running
Running
Update index.js
Browse files
index.js
CHANGED
|
@@ -1,79 +1,85 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
pushMessage(
|
| 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 |
-
message
|
| 46 |
-
system_prompt
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
}
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// index.js
|
| 2 |
+
import { Client } from "https://cdn.jsdelivr.net/npm/@gradio/client/dist/index.min.js";
|
| 3 |
+
|
| 4 |
+
(async () => {
|
| 5 |
+
const sendBtn = document.getElementById('sendBtn');
|
| 6 |
+
const userInput = document.getElementById('userMessage');
|
| 7 |
+
const systemInput = document.getElementById('systemPrompt');
|
| 8 |
+
const chatDisplay = document.getElementById('chatDisplay');
|
| 9 |
+
|
| 10 |
+
let client;
|
| 11 |
+
let isConnected = false;
|
| 12 |
+
|
| 13 |
+
function pushMessage(role, text){
|
| 14 |
+
const wrap = document.createElement('div');
|
| 15 |
+
wrap.className = `message ${role}`;
|
| 16 |
+
|
| 17 |
+
const bubble = document.createElement('div');
|
| 18 |
+
bubble.className = 'bubble';
|
| 19 |
+
bubble.textContent = text;
|
| 20 |
+
|
| 21 |
+
wrap.appendChild(bubble);
|
| 22 |
+
chatDisplay.appendChild(wrap);
|
| 23 |
+
chatDisplay.scrollTop = chatDisplay.scrollHeight;
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
async function connectClient(){
|
| 27 |
+
pushMessage('assistant','Connecting to model...');
|
| 28 |
+
try{
|
| 29 |
+
client = await Client.connect("amd/gpt-oss-120b-chatbot");
|
| 30 |
+
isConnected = true;
|
| 31 |
+
pushMessage('assistant','Connected. You can send messages.');
|
| 32 |
+
console.log("✅ Connected to Gradio Space");
|
| 33 |
+
}catch(err){
|
| 34 |
+
pushMessage('assistant','Failed to connect: ' + String(err));
|
| 35 |
+
console.error("❌ Connect error",err);
|
| 36 |
+
}
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
async function sendChat(){
|
| 40 |
+
if(!isConnected){
|
| 41 |
+
await connectClient();
|
| 42 |
+
if(!isConnected) return;
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
const message = userInput.value.trim();
|
| 46 |
+
const system_prompt = systemInput.value.trim();
|
| 47 |
+
if(!message) return;
|
| 48 |
+
|
| 49 |
+
sendBtn.disabled = true;
|
| 50 |
+
|
| 51 |
+
pushMessage('user', message);
|
| 52 |
+
|
| 53 |
+
try{
|
| 54 |
+
console.log("Sending to /chat",{message,system_prompt});
|
| 55 |
+
const result = await client.predict("/chat", {
|
| 56 |
+
message,
|
| 57 |
+
system_prompt,
|
| 58 |
+
temperature: 0,
|
| 59 |
+
});
|
| 60 |
+
|
| 61 |
+
console.log("Received result",result);
|
| 62 |
+
|
| 63 |
+
const assistantText = (result && result.data) ? String(result.data) : 'No response';
|
| 64 |
+
pushMessage('assistant', assistantText);
|
| 65 |
+
}catch(err){
|
| 66 |
+
console.error("❌ Predict error",err);
|
| 67 |
+
pushMessage('assistant','Error: ' + String(err));
|
| 68 |
+
}finally{
|
| 69 |
+
sendBtn.disabled = false;
|
| 70 |
+
userInput.value = '';
|
| 71 |
+
userInput.focus();
|
| 72 |
+
}
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
sendBtn.addEventListener('click', sendChat);
|
| 76 |
+
userInput.addEventListener('keydown', (e) => {
|
| 77 |
+
if(e.key === 'Enter'){
|
| 78 |
+
e.preventDefault();
|
| 79 |
+
sendChat();
|
| 80 |
+
}
|
| 81 |
+
});
|
| 82 |
+
|
| 83 |
+
// connect on load
|
| 84 |
+
connectClient();
|
| 85 |
+
})();
|