Spaces:
Running
Running
File size: 2,167 Bytes
0cfacc3 e2becb6 0cfacc3 e2becb6 0cfacc3 e2becb6 0cfacc3 e2becb6 0cfacc3 e2becb6 0cfacc3 e2becb6 0cfacc3 e2becb6 0cfacc3 e2becb6 0cfacc3 e2becb6 0cfacc3 e2becb6 0cfacc3 e2becb6 0cfacc3 e2becb6 0cfacc3 e2becb6 0cfacc3 e2becb6 0cfacc3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 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 46 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 80 81 |
import { Client } from "https://cdn.jsdelivr.net/npm/@gradio/client/dist/index.min.js";
(async () => {
const chatWindow = document.getElementById("chat-window");
const userInput = document.getElementById("userMessage");
const sendBtn = document.getElementById("sendBtn");
let client;
let isConnected = false;
function pushMessage(role, text) {
const wrap = document.createElement("div");
wrap.className = `message ${role}`;
const bubble = document.createElement("div");
bubble.className = "bubble";
bubble.textContent = text;
wrap.appendChild(bubble);
chatWindow.appendChild(wrap);
chatWindow.scrollTop = chatWindow.scrollHeight;
}
async function connectClient() {
pushMessage("assistant", "Connecting to model...");
try {
client = await Client.connect("amd/gpt-oss-120b-chatbot");
isConnected = true;
pushMessage("assistant", "已連線,開始對話吧!");
} catch (err) {
pushMessage("assistant", "連線失敗: " + String(err));
console.error(err);
}
}
async function sendChat() {
if (!isConnected) {
await connectClient();
if (!isConnected) return;
}
const message = userInput.value.trim();
if (!message) return;
sendBtn.disabled = true;
pushMessage("user", message);
try {
// 如果你有固定的 system_prompt 可以直接填這裡
const system_prompt = "You are a helpful assistant.";
const result = await client.predict("/chat", {
message,
system_prompt,
temperature: 0,
});
const assistantText =
result && result.data ? String(result.data) : "No response";
pushMessage("assistant", assistantText);
} catch (err) {
console.error(err);
pushMessage("assistant", "Error: " + String(err));
} finally {
sendBtn.disabled = false;
userInput.value = "";
userInput.focus();
}
}
sendBtn.addEventListener("click", sendChat);
userInput.addEventListener("keydown", (e) => {
if (e.key === "Enter" && !e.shiftKey) {
e.preventDefault();
sendChat();
}
});
connectClient();
})();
|