Spaces:
Sleeping
Sleeping
Create templates/chat.py
Browse files- templates/chat.py +110 -0
templates/chat.py
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8" />
|
| 5 |
+
<meta name="viewport" content="width=device-width,initial-scale=1" />
|
| 6 |
+
<title>Score Predictor + Groq Chatbot</title>
|
| 7 |
+
<script src="https://cdn.tailwindcss.com"></script>
|
| 8 |
+
</head>
|
| 9 |
+
<body class="bg-gray-100">
|
| 10 |
+
<div class="max-w-md mx-auto py-6">
|
| 11 |
+
<div id="chatbox"
|
| 12 |
+
class="bg-white p-4 rounded-lg shadow-lg h-[500px] overflow-y-auto space-y-3">
|
| 13 |
+
</div>
|
| 14 |
+
<div class="mt-3 flex">
|
| 15 |
+
<input id="userInput" type="text"
|
| 16 |
+
class="flex-1 p-2 border border-gray-300 rounded-l-md"
|
| 17 |
+
placeholder="Type here…" autocomplete="off" />
|
| 18 |
+
<button id="sendBtn"
|
| 19 |
+
class="bg-blue-600 px-4 text-white rounded-r-md">
|
| 20 |
+
Send
|
| 21 |
+
</button>
|
| 22 |
+
</div>
|
| 23 |
+
</div>
|
| 24 |
+
|
| 25 |
+
<script>
|
| 26 |
+
const fields = {{ fields | tojson }};
|
| 27 |
+
let idx = -1; // before welcome
|
| 28 |
+
const answers = {};
|
| 29 |
+
const chatbox = document.getElementById("chatbox");
|
| 30 |
+
const input = document.getElementById("userInput");
|
| 31 |
+
const btn = document.getElementById("sendBtn");
|
| 32 |
+
|
| 33 |
+
function addMessage(txt, who="bot") {
|
| 34 |
+
const div = document.createElement("div");
|
| 35 |
+
div.className = who==="bot"
|
| 36 |
+
? "text-left text-gray-800"
|
| 37 |
+
: "text-right text-blue-700";
|
| 38 |
+
div.innerText = txt;
|
| 39 |
+
chatbox.appendChild(div);
|
| 40 |
+
chatbox.scrollTop = chatbox.scrollHeight;
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
function askNext() {
|
| 44 |
+
idx++;
|
| 45 |
+
if (idx === 0) {
|
| 46 |
+
addMessage("👋 Hello! I’ll ask 18 quick questions to predict your exam score—and then give personalized improvement tips.");
|
| 47 |
+
setTimeout(askNext, 800);
|
| 48 |
+
return;
|
| 49 |
+
}
|
| 50 |
+
const i = idx - 1;
|
| 51 |
+
if (i < fields.length) {
|
| 52 |
+
let q = fields[i].question;
|
| 53 |
+
if (fields[i].type==="select") {
|
| 54 |
+
q += " Options: " + fields[i].options.join(", ");
|
| 55 |
+
}
|
| 56 |
+
if (fields[i].type==="number" && fields[i].validation) {
|
| 57 |
+
q += ` (enter ${fields[i].validation.min}–${fields[i].validation.max})`;
|
| 58 |
+
}
|
| 59 |
+
addMessage(q);
|
| 60 |
+
} else {
|
| 61 |
+
addMessage("🔄 Computing your predicted score and fetching advice…");
|
| 62 |
+
fetch("/predict_json", {
|
| 63 |
+
method: "POST",
|
| 64 |
+
headers: {"Content-Type":"application/json"},
|
| 65 |
+
body: JSON.stringify(answers)
|
| 66 |
+
})
|
| 67 |
+
.then(r=>r.json())
|
| 68 |
+
.then(d=>{
|
| 69 |
+
addMessage(`🎯 Predicted Score: ${d.predicted}`);
|
| 70 |
+
setTimeout(()=> addMessage(`💡 Improvement Tips:\n${d.advice}`), 500);
|
| 71 |
+
})
|
| 72 |
+
.catch(e=>{
|
| 73 |
+
addMessage("⚠️ Something went wrong. Please refresh and try again.");
|
| 74 |
+
});
|
| 75 |
+
}
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
btn.onclick = () => {
|
| 79 |
+
const text = input.value.trim();
|
| 80 |
+
if (!text) return;
|
| 81 |
+
const i = idx - 1;
|
| 82 |
+
if (i >= 0 && i < fields.length) {
|
| 83 |
+
const f = fields[i];
|
| 84 |
+
if (f.type==="number") {
|
| 85 |
+
const n = parseFloat(text);
|
| 86 |
+
if (isNaN(n) || (f.validation && (n < f.validation.min || n > f.validation.max))) {
|
| 87 |
+
addMessage(`⚠️ Please enter a number between ${f.validation.min} and ${f.validation.max}.`, "bot");
|
| 88 |
+
input.value = "";
|
| 89 |
+
return;
|
| 90 |
+
}
|
| 91 |
+
answers[f.name] = n;
|
| 92 |
+
} else if (f.type==="select") {
|
| 93 |
+
if (!f.options.includes(text)) {
|
| 94 |
+
addMessage(`⚠️ Choose one of: ${f.options.join(", ")}`, "bot");
|
| 95 |
+
input.value = "";
|
| 96 |
+
return;
|
| 97 |
+
}
|
| 98 |
+
answers[f.name] = text;
|
| 99 |
+
}
|
| 100 |
+
}
|
| 101 |
+
addMessage(text, "user");
|
| 102 |
+
input.value = "";
|
| 103 |
+
askNext();
|
| 104 |
+
};
|
| 105 |
+
|
| 106 |
+
// kick things off
|
| 107 |
+
askNext();
|
| 108 |
+
</script>
|
| 109 |
+
</body>
|
| 110 |
+
</html>
|