triflix commited on
Commit
0222dab
·
verified ·
1 Parent(s): b287a99

Create chat.html

Browse files
Files changed (1) hide show
  1. templates/chat.html +126 -0
templates/chat.html ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ <style>
9
+ .message-enter {
10
+ animation: fadeIn 0.3s ease-in-out;
11
+ }
12
+ @keyframes fadeIn {
13
+ from { opacity: 0; transform: translateY(4px); }
14
+ to { opacity: 1; transform: translateY(0); }
15
+ }
16
+ </style>
17
+ </head>
18
+ <body class="bg-gradient-to-br from-blue-100 to-white min-h-screen font-sans">
19
+ <div class="max-w-md mx-auto py-8 px-4">
20
+ <div id="chatbox"
21
+ class="bg-white p-4 rounded-lg shadow-xl h-[500px] overflow-y-auto space-y-3 border border-gray-200">
22
+ </div>
23
+
24
+ <div class="mt-3 flex sticky bottom-0 bg-white rounded-md shadow-md">
25
+ <input id="userInput" type="text"
26
+ class="flex-1 p-3 border border-r-0 border-gray-300 rounded-l-md focus:outline-none focus:ring-2 focus:ring-blue-400"
27
+ placeholder="Type your answer..." autocomplete="off" />
28
+ <button id="sendBtn"
29
+ class="bg-blue-600 hover:bg-blue-700 transition-colors px-5 text-white font-semibold rounded-r-md">
30
+ Send
31
+ </button>
32
+ </div>
33
+ </div>
34
+
35
+ <script>
36
+ const fields = {{ fields | tojson }};
37
+ let idx = -1;
38
+ const answers = {};
39
+ const chatbox = document.getElementById("chatbox");
40
+ const input = document.getElementById("userInput");
41
+ const btn = document.getElementById("sendBtn");
42
+
43
+ function addMessage(txt, who = "bot") {
44
+ const div = document.createElement("div");
45
+ div.className = `message-enter px-3 py-2 rounded-lg max-w-[80%] ${
46
+ who === "bot"
47
+ ? "bg-gray-100 text-gray-900 self-start"
48
+ : "bg-blue-600 text-white self-end ml-auto"
49
+ }`;
50
+ div.innerText = txt;
51
+
52
+ const wrapper = document.createElement("div");
53
+ wrapper.className = "flex flex-col";
54
+ wrapper.appendChild(div);
55
+
56
+ chatbox.appendChild(wrapper);
57
+ chatbox.scrollTop = chatbox.scrollHeight;
58
+ }
59
+
60
+ function askNext() {
61
+ idx++;
62
+ if (idx === 0) {
63
+ addMessage("👋 Hello! I’ll ask 18 quick questions to predict your exam score—and then give personalized improvement tips.");
64
+ setTimeout(askNext, 800);
65
+ return;
66
+ }
67
+ const i = idx - 1;
68
+ if (i < fields.length) {
69
+ let q = fields[i].question;
70
+ if (fields[i].type === "select") {
71
+ q += " Options: " + fields[i].options.join(", ");
72
+ }
73
+ if (fields[i].type === "number" && fields[i].validation) {
74
+ q += ` (enter ${fields[i].validation.min}–${fields[i].validation.max})`;
75
+ }
76
+ addMessage(q);
77
+ } else {
78
+ addMessage("🔄 Computing your predicted score and fetching advice…");
79
+ fetch("/predict_json", {
80
+ method: "POST",
81
+ headers: { "Content-Type": "application/json" },
82
+ body: JSON.stringify(answers)
83
+ })
84
+ .then(r => r.json())
85
+ .then(d => {
86
+ addMessage(`🎯 Predicted Score: ${d.predicted}`);
87
+ setTimeout(() => addMessage(`💡 Improvement Tips:\n${d.advice}`), 500);
88
+ })
89
+ .catch(() => {
90
+ addMessage("⚠️ Something went wrong. Please refresh and try again.");
91
+ });
92
+ }
93
+ }
94
+
95
+ btn.onclick = () => {
96
+ const text = input.value.trim();
97
+ if (!text) return;
98
+ const i = idx - 1;
99
+ if (i >= 0 && i < fields.length) {
100
+ const f = fields[i];
101
+ if (f.type === "number") {
102
+ const n = parseFloat(text);
103
+ if (isNaN(n) || (f.validation && (n < f.validation.min || n > f.validation.max))) {
104
+ addMessage(`⚠️ Please enter a number between ${f.validation.min} and ${f.validation.max}.`, "bot");
105
+ input.value = "";
106
+ return;
107
+ }
108
+ answers[f.name] = n;
109
+ } else if (f.type === "select") {
110
+ if (!f.options.includes(text)) {
111
+ addMessage(`⚠️ Choose one of: ${f.options.join(", ")}`, "bot");
112
+ input.value = "";
113
+ return;
114
+ }
115
+ answers[f.name] = text;
116
+ }
117
+ }
118
+ addMessage(text, "user");
119
+ input.value = "";
120
+ askNext();
121
+ };
122
+
123
+ askNext();
124
+ </script>
125
+ </body>
126
+ </html>