File size: 10,697 Bytes
e1c6e19 0274d40 e1c6e19 0274d40 e1c6e19 0274d40 e1c6e19 0274d40 e1c6e19 0274d40 e1c6e19 ede0814 e1c6e19 ede0814 e1c6e19 210f09e c5387c8 e1c6e19 c5387c8 e1c6e19 0274d40 e1c6e19 0274d40 e1c6e19 0274d40 e1c6e19 0274d40 e1c6e19 0274d40 e1c6e19 0274d40 e1c6e19 0274d40 e1c6e19 0274d40 e1c6e19 0274d40 e1c6e19 9d4ac95 e1c6e19 0274d40 e1c6e19 c5387c8 210f09e e1c6e19 c5387c8 5aa9e3e c5387c8 e1c6e19 bdba789 | 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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 | const API_URL = "/ask";
// 1. Generate a random Session ID
const sessionId = "session_" + Math.random().toString(36).substr(2, 9);
console.log("Your Session ID:", sessionId);
// --- VOICE SETUP ---
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
const recognition = new SpeechRecognition();
recognition.lang = 'en-US';
recognition.interimResults = false;
let isVoiceActive = false;
function startVoice() {
const micBtn = document.getElementById("mic-btn");
if (isVoiceActive) {
recognition.stop();
return;
}
try {
recognition.start();
micBtn.classList.add("listening");
isVoiceActive = true;
} catch (error) {
console.error("Speech recognition error:", error);
isVoiceActive = false;
}
}
recognition.onend = () => {
document.getElementById("mic-btn").classList.remove("listening");
};
recognition.onresult = (event) => {
const transcript = event.results[0][0].transcript;
document.getElementById("user-input").value = transcript;
sendMessage();
};
// 3. Text-to-Speech
function speakText(text) {
window.speechSynthesis.cancel();
const cleanText = text.replace(/\*/g, "").replace(/<[^>]*>/g, "");
const utterance = new SpeechSynthesisUtterance(cleanText);
window.speechSynthesis.speak(utterance);
}
function appendMessage(text, sender) {
const box = document.getElementById("chat-box");
const msg = document.createElement("div");
msg.classList.add("message", sender);
// If it's the bot, render Markdown & Math
if (sender === "bot") {
// 1. Convert Markdown to HTML
let htmlContent = marked.parse(text);
msg.innerHTML = htmlContent;
} else {
// User messages stay simple text
msg.textContent = text;
}
box.appendChild(msg);
box.scrollTop = box.scrollHeight;
// 2. Render Math Formulas (if any)
if (sender === "bot" && window.MathJax) {
MathJax.typesetPromise([msg]).catch((err) => console.log(err));
}
}
let currentQuizData = [];
let currentQuestionIndex = 0;
let score = 0;
function renderQuiz(jsonString) {
const box = document.getElementById("chat-box");
try {
currentQuizData = JSON.parse(jsonString);
} catch (e) {
appendMessage("β οΈ Error loading quiz.", "bot");
return;
}
currentQuestionIndex = 0;
score = 0;
showNextQuestion();
}
function showNextQuestion() {
const box = document.getElementById("chat-box");
// Check if quiz is finished
if (currentQuestionIndex >= currentQuizData.length) {
const resultDiv = document.createElement("div");
resultDiv.className = "message bot";
resultDiv.innerHTML = `
<div class="quiz-container">
<div class="quiz-score">
π Quiz Complete!<br>
You scored ${score} / ${currentQuizData.length}
</div>
</div>
`;
box.appendChild(resultDiv);
box.scrollTop = box.scrollHeight;
return;
}
const qData = currentQuizData[currentQuestionIndex];
// Create Question Container
const div = document.createElement("div");
div.className = "message bot";
div.innerHTML = `
<div class="quiz-container" id="question-${currentQuestionIndex}">
<div class="quiz-question">Q${currentQuestionIndex + 1}: ${qData.question}</div>
<div class="quiz-options">
${qData.options.map(opt =>
`<button class="quiz-btn" onclick="checkAnswer(this, '${opt.replace(/'/g, "\\'")}')">${opt}</button>`
).join('')}
</div>
</div>
`;
box.appendChild(div);
box.scrollTop = box.scrollHeight;
}
function checkAnswer(btn, selectedOption) {
const qData = currentQuizData[currentQuestionIndex];
const correctOption = qData.correct_answer;
const parent = btn.parentElement;
// Disable all buttons to prevent double clicking
const allBtns = parent.querySelectorAll('.quiz-btn');
allBtns.forEach(b => b.disabled = true);
if (selectedOption === correctOption) {
btn.classList.add("correct");
score++;
} else {
btn.classList.add("wrong");
// Highlight the correct one
allBtns.forEach(b => {
if (b.innerText === correctOption) b.classList.add("correct");
});
}
// Wait 1.5 seconds then show next question
currentQuestionIndex++;
setTimeout(showNextQuestion, 1500);
}
function appendFlashcards(jsonString) {
const box = document.getElementById("chat-box");
// Parse the JSON string from Python
let cardsData;
try {
cardsData = JSON.parse(jsonString);
} catch (e) {
console.error("JSON Error:", e);
appendMessage("β οΈ Error generating cards.", "bot");
return;
}
// Create Container
const container = document.createElement("div");
container.className = "message bot";
container.style.background = "transparent";
container.style.padding = "0";
const scrollBox = document.createElement("div");
scrollBox.className = "flashcard-container";
// Create Cards
cardsData.forEach(card => {
const cardDiv = document.createElement("div");
cardDiv.className = "flashcard";
// Flip on click
cardDiv.onclick = function() { this.classList.toggle('flipped'); };
cardDiv.innerHTML = `
<div class="flashcard-inner">
<div class="flashcard-front">${card.question}</div>
<div class="flashcard-back">${card.answer}</div>
</div>
`;
scrollBox.appendChild(cardDiv);
});
container.appendChild(scrollBox);
box.appendChild(container);
box.scrollTop = box.scrollHeight;
}
function showTyping() {
const box = document.getElementById("chat-box");
const typing = document.createElement("div");
typing.classList.add("message", "bot");
typing.id = "typing";
typing.innerHTML = `<i class="fa-solid fa-circle-notch fa-spin"></i> Thinking...`;
box.appendChild(typing);
box.scrollTop = box.scrollHeight;
}
function removeTyping() {
const el = document.getElementById("typing");
if (el) el.remove();
}
// --- MIND MAP RENDERER ---
async function appendMindMap(mermaidCode) {
const box = document.getElementById("chat-box");
const div = document.createElement("div");
div.classList.add("message", "bot");
div.style.background = "white";
div.style.border = "1px solid #ddd";
const id = "mermaid-" + Math.floor(Math.random() * 10000);
div.id = id;
let cleanCode = mermaidCode.replace(/```mermaid/g, "").replace(/```/g, "").trim();
div.textContent = cleanCode;
box.appendChild(div);
box.scrollTop = box.scrollHeight;
try {
await mermaid.init(undefined, div);
} catch (error) {
div.innerHTML = "β οΈ Could not render diagram.";
}
}
async function sendMessage() {
const input = document.getElementById("user-input");
const message = input.value.trim();
if (!message) return;
appendMessage(message, "user");
input.value = "";
showTyping();
try {
const res = await fetch(API_URL, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
question: message,
session_id: sessionId
})
});
const data = await res.json();
removeTyping();
appendMessage(data.answer, "bot");
if (isVoiceActive) {
speakText(data.answer);
isVoiceActive = false;
}
} catch (error) {
removeTyping();
appendMessage("β οΈ Error contacting server.", "bot");
isVoiceActive = false;
}
}
// --- FEATURE REQUESTS ---
async function requestFeature(type) {
let topic = "";
let endpoint = "";
let body = {};
let method = "POST";
if (type === 'quiz') {
topic = prompt("Enter the topic for the quiz:");
if (!topic) return;
appendMessage(`π Generating quiz for: ${topic}...`, "user");
endpoint = "/quiz";
body = JSON.stringify({ topic: topic });
}
else if (type === 'summary') {
topic = prompt("Enter topic to summarize:");
if (!topic) return;
appendMessage(`π Summarizing: ${topic}...`, "user");
endpoint = "/summary";
body = JSON.stringify({ topic: topic });
}
else if (type === 'countdown') {
appendMessage("β³ Checking exam schedule...", "user");
endpoint = "/countdown";
method = "GET";
}
else if (type === 'plan') {
const input = prompt("Enter Subject and Days (e.g., 'Science, 3'):");
if (!input) return;
const parts = input.split(",");
topic = parts[0].trim();
let days = parts.length > 1 ? parts[1].trim() : "5";
appendMessage(`π
Creating ${days}-day plan for ${topic}...`, "user");
endpoint = "/study_plan";
body = JSON.stringify({ subject: topic, days: days });
}
else if (type === 'mindmap') {
topic = prompt("Enter topic for Mind Map:");
if (!topic) return;
appendMessage(`π§ Drawing Mind Map for: ${topic}...`, "user");
endpoint = "/mindmap";
body = JSON.stringify({ topic: topic });
}
else if (type === 'flashcards') {
topic = prompt("Enter topic for Flashcards:");
if (!topic) return;
appendMessage(`π Creating flashcards for: ${topic}...`, "user");
endpoint = "/flashcards";
body = JSON.stringify({ topic: topic });
}
else if (type === 'quiz') {
renderQuiz(data.answer);
}
if (!endpoint) return;
showTyping();
try {
const res = await fetch(`${endpoint}`, {
method: method,
headers: { "Content-Type": "application/json" },
body: method === "POST" ? body : null
});
const data = await res.json();
removeTyping();
if (type === 'mindmap') {
appendMindMap(data.answer);
}
else if (type === 'flashcards') {
appendFlashcards(data.answer);
}
else if (type === 'quiz') {
renderQuiz(data.answer);
}
else {
appendMessage(data.answer, "bot");
}
} catch (error) {
removeTyping();
appendMessage("β οΈ Error fetching data.", "bot");
}
} |