Spaces:
Build error
Build error
Create chatbot.html
Browse files- templates/chatbot.html +109 -0
templates/chatbot.html
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{% extends "layout.html" %}
|
| 2 |
+
|
| 3 |
+
{% block content %}
|
| 4 |
+
<div class="text-2xl font-bold text-gray-800">
|
| 5 |
+
Welcome to NeuroML 🚀
|
| 6 |
+
</div>
|
| 7 |
+
|
| 8 |
+
<p class="mt-4 text-gray-600">
|
| 9 |
+
Ask anything about Machine Learning using the chat button.
|
| 10 |
+
</p>
|
| 11 |
+
|
| 12 |
+
<!-- ================= CHAT BUTTON ================= -->
|
| 13 |
+
<button id="chat-btn"
|
| 14 |
+
class="fixed bottom-6 right-6 bg-indigo-600 text-white w-14 h-14 rounded-full shadow-xl text-xl z-50">
|
| 15 |
+
🤖
|
| 16 |
+
</button>
|
| 17 |
+
|
| 18 |
+
<!-- ================= CHAT BOX ================= -->
|
| 19 |
+
<div id="chat-box"
|
| 20 |
+
class="hidden fixed bottom-24 right-6 w-80 bg-white rounded-xl shadow-2xl border z-50">
|
| 21 |
+
|
| 22 |
+
<div class="p-3 bg-indigo-600 text-white rounded-t-xl font-semibold">
|
| 23 |
+
Qwen ML Tutor
|
| 24 |
+
</div>
|
| 25 |
+
|
| 26 |
+
<div id="chat-messages"
|
| 27 |
+
class="p-3 h-64 overflow-y-auto text-sm space-y-2">
|
| 28 |
+
</div>
|
| 29 |
+
|
| 30 |
+
<div class="p-3 flex gap-2 border-t">
|
| 31 |
+
<input id="chat-input"
|
| 32 |
+
class="border flex-1 p-2 rounded text-sm"
|
| 33 |
+
placeholder="Ask ML question..." />
|
| 34 |
+
<button id="send-btn"
|
| 35 |
+
class="bg-indigo-600 text-white px-4 rounded">
|
| 36 |
+
Send
|
| 37 |
+
</button>
|
| 38 |
+
</div>
|
| 39 |
+
</div>
|
| 40 |
+
|
| 41 |
+
<!-- ================= CHAT SCRIPT ================= -->
|
| 42 |
+
<script>
|
| 43 |
+
const chatBtn = document.getElementById("chat-btn");
|
| 44 |
+
const chatBox = document.getElementById("chat-box");
|
| 45 |
+
const sendBtn = document.getElementById("send-btn");
|
| 46 |
+
const input = document.getElementById("chat-input");
|
| 47 |
+
const messages = document.getElementById("chat-messages");
|
| 48 |
+
|
| 49 |
+
chatBtn.addEventListener("click", () => {
|
| 50 |
+
chatBox.classList.toggle("hidden");
|
| 51 |
+
});
|
| 52 |
+
|
| 53 |
+
sendBtn.addEventListener("click", sendMessage);
|
| 54 |
+
|
| 55 |
+
input.addEventListener("keydown", (e) => {
|
| 56 |
+
if (e.key === "Enter") {
|
| 57 |
+
sendMessage();
|
| 58 |
+
}
|
| 59 |
+
});
|
| 60 |
+
|
| 61 |
+
function addMessage(text, sender) {
|
| 62 |
+
const div = document.createElement("div");
|
| 63 |
+
|
| 64 |
+
if (sender === "user") {
|
| 65 |
+
div.className = "text-right";
|
| 66 |
+
div.innerHTML = `
|
| 67 |
+
<span class="inline-block bg-indigo-500 text-white px-3 py-2 rounded-lg max-w-[85%]">
|
| 68 |
+
${text}
|
| 69 |
+
</span>
|
| 70 |
+
`;
|
| 71 |
+
} else {
|
| 72 |
+
div.className = "text-left";
|
| 73 |
+
div.innerHTML = `
|
| 74 |
+
<span class="inline-block bg-gray-200 text-gray-800 px-3 py-2 rounded-lg max-w-[85%]">
|
| 75 |
+
${text}
|
| 76 |
+
</span>
|
| 77 |
+
`;
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
messages.appendChild(div);
|
| 81 |
+
messages.scrollTop = messages.scrollHeight;
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
async function sendMessage() {
|
| 85 |
+
const text = input.value.trim();
|
| 86 |
+
if (!text) return;
|
| 87 |
+
|
| 88 |
+
addMessage(text, "user");
|
| 89 |
+
input.value = "";
|
| 90 |
+
|
| 91 |
+
try {
|
| 92 |
+
const response = await fetch("/chat", {
|
| 93 |
+
method: "POST",
|
| 94 |
+
headers: {
|
| 95 |
+
"Content-Type": "application/json"
|
| 96 |
+
},
|
| 97 |
+
body: JSON.stringify({ message: text })
|
| 98 |
+
});
|
| 99 |
+
|
| 100 |
+
const data = await response.json();
|
| 101 |
+
addMessage(data.reply, "bot");
|
| 102 |
+
|
| 103 |
+
} catch (error) {
|
| 104 |
+
addMessage("Server error. Please try again.", "bot");
|
| 105 |
+
}
|
| 106 |
+
}
|
| 107 |
+
</script>
|
| 108 |
+
|
| 109 |
+
{% endblock %}
|