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