zenkamind-web / script.js
Zenkad's picture
Update script.js
59da560 verified
const chatBox = document.querySelector(".chat-box");
const sendButton = document.querySelector(".send-btn");
const input = document.querySelector("#userInput");
const MODEL = "mistralai/Mixtral-8x7B-Instruct-v0.1";
const API_URL = `https://huggingface.co/api/inference-proxy/tokens/${MODEL}`;
function addMessage(sender, text) {
const msg = document.createElement("div");
msg.classList.add("message", sender);
msg.innerHTML = `<strong>${sender === "user" ? "🧍‍♂️ Siz:" : "🤖 ZenkaMind:"}</strong> ${text}`;
chatBox.appendChild(msg);
chatBox.scrollTop = chatBox.scrollHeight;
}
async function sendMessage() {
const userInput = input.value.trim();
if (!userInput) return;
addMessage("user", userInput);
input.value = "";
addMessage("bot", "💭 Düşünüyorum...");
try {
const res = await fetch(API_URL, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
inputs: userInput,
options: { wait_for_model: true },
parameters: { temperature: 0.7, max_new_tokens: 300 }
}),
});
const data = await res.json();
document.querySelector(".message.bot:last-child").remove();
if (data?.error) {
addMessage("bot", "⚠️ Model şu anda meşgul veya hatalı: " + data.error);
} else {
addMessage("bot", data?.[0]?.generated_text || "Yanıt alınamadı.");
}
} catch (err) {
document.querySelector(".message.bot:last-child").remove();
addMessage("bot", "❌ Bağlantı hatası oluştu.");
}
}
sendButton.addEventListener("click", sendMessage);
input.addEventListener("keypress", e => {
if (e.key === "Enter") sendMessage();
});
addMessage("bot", "Merhaba 👋 Ben ZenkaMind! Size nasıl yardımcı olabilirim?");