Spaces:
Sleeping
Sleeping
Update web/past_data.html (#18)
Browse files- Update web/past_data.html (f35059427cd2b141ccd28862eee3a6311d5b1939)
Co-authored-by: Vihaan Jyothiswaroop <meamgodyay@users.noreply.huggingface.co>
- web/past_data.html +95 -1
web/past_data.html
CHANGED
|
@@ -27,6 +27,28 @@
|
|
| 27 |
<div id="recs-container" class="space-y-4">
|
| 28 |
<p class="text-sm text-gray-500">Sign in to view your saved analyzes.</p>
|
| 29 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
</main>
|
| 31 |
|
| 32 |
<script type="module">
|
|
@@ -47,7 +69,17 @@
|
|
| 47 |
const app = initializeApp(firebaseConfig);
|
| 48 |
const auth = getAuth(app);
|
| 49 |
const db = getFirestore(app);
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
onAuthStateChanged(auth, (user) => {
|
| 52 |
const authNavItem = document.getElementById('authNavItem');
|
| 53 |
if (user) {
|
|
@@ -210,6 +242,68 @@
|
|
| 210 |
statusEl.textContent = "Not signed in.";
|
| 211 |
recsEl.innerHTML = '<p class="text-sm text-gray-500">Please sign in to see your analyses.</p>';
|
| 212 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
});
|
| 214 |
</script>
|
| 215 |
</body>
|
|
|
|
| 27 |
<div id="recs-container" class="space-y-4">
|
| 28 |
<p class="text-sm text-gray-500">Sign in to view your saved analyzes.</p>
|
| 29 |
</div>
|
| 30 |
+
<div
|
| 31 |
+
class="bg-[var(--latte-cream)] border border-[var(--wisteria)] rounded-lg p-6 mb-8"
|
| 32 |
+
>
|
| 33 |
+
<h3 class="text-lg font-semibold mb-3">Ask Chatbot</h3>
|
| 34 |
+
<div
|
| 35 |
+
id="chat-output"
|
| 36 |
+
class="space-y-2 h-48 overflow-auto text-sm rounded p-4"
|
| 37 |
+
>
|
| 38 |
+
<p><strong>Chatbot:</strong> Ask me something about your report</p>
|
| 39 |
+
</div>
|
| 40 |
+
<div class="flex mt-4 gap-2">
|
| 41 |
+
<input
|
| 42 |
+
type="text"
|
| 43 |
+
id="user-question"
|
| 44 |
+
placeholder="Ask a question..."
|
| 45 |
+
class="flex-1 rounded px-3 py-2 focus:outline-none"
|
| 46 |
+
/>
|
| 47 |
+
<button id="ask-btn" class="btn-primary px-4 py-2 rounded">
|
| 48 |
+
Ask
|
| 49 |
+
</button>
|
| 50 |
+
</div>
|
| 51 |
+
</div>
|
| 52 |
</main>
|
| 53 |
|
| 54 |
<script type="module">
|
|
|
|
| 69 |
const app = initializeApp(firebaseConfig);
|
| 70 |
const auth = getAuth(app);
|
| 71 |
const db = getFirestore(app);
|
| 72 |
+
window.currentUser = null;
|
| 73 |
+
window.chatContext = null;
|
| 74 |
+
window.setChatContextFromId = function (id) {
|
| 75 |
+
const pre = document.querySelector(`#content-${id} .ocr-pre`);
|
| 76 |
+
window.chatContext = pre ? pre.textContent.trim() : null;
|
| 77 |
+
const chatOutput = document.getElementById('chat-output');
|
| 78 |
+
if (chatOutput) {
|
| 79 |
+
chatOutput.innerHTML += `<p class="text-xs text-gray-500"><em>Context set from selected report.</em></p>`;
|
| 80 |
+
chatOutput.scrollTop = chatOutput.scrollHeight;
|
| 81 |
+
}
|
| 82 |
+
};
|
| 83 |
onAuthStateChanged(auth, (user) => {
|
| 84 |
const authNavItem = document.getElementById('authNavItem');
|
| 85 |
if (user) {
|
|
|
|
| 242 |
statusEl.textContent = "Not signed in.";
|
| 243 |
recsEl.innerHTML = '<p class="text-sm text-gray-500">Please sign in to see your analyses.</p>';
|
| 244 |
}
|
| 245 |
+
document.getElementById("ask-btn").onclick = async () => {
|
| 246 |
+
const q = document.getElementById("user-question").value.trim();
|
| 247 |
+
if (!q) return;
|
| 248 |
+
|
| 249 |
+
// require a selected report as context
|
| 250 |
+
if (!window.chatContext) {
|
| 251 |
+
alert("Please click 'Chat about this report' on a saved report first to set context.");
|
| 252 |
+
return;
|
| 253 |
+
}
|
| 254 |
+
|
| 255 |
+
document.getElementById("ask-btn").onclick = async () => {
|
| 256 |
+
const q = document.getElementById("user-question").value.trim();
|
| 257 |
+
if (!q) return;
|
| 258 |
+
|
| 259 |
+
// require a selected report as context
|
| 260 |
+
if (!window.chatContext) {
|
| 261 |
+
alert("Please click 'Chat about this report' on a saved report first to set context.");
|
| 262 |
+
return;
|
| 263 |
+
}
|
| 264 |
+
|
| 265 |
+
const chat = document.getElementById("chat-output");
|
| 266 |
+
chat.innerHTML += `<p><strong>You:</strong> ${q}</p>`;
|
| 267 |
+
chat.scrollTop = chat.scrollHeight;
|
| 268 |
+
|
| 269 |
+
chat.innerHTML += `<p><strong>Chatbot:</strong> <em>Thinking...</em></p>`;
|
| 270 |
+
chat.scrollTop = chat.scrollHeight;
|
| 271 |
+
|
| 272 |
+
try {
|
| 273 |
+
const response = await fetch(api('chat/'), {
|
| 274 |
+
method: "POST",
|
| 275 |
+
headers: {
|
| 276 |
+
"Content-Type": "application/json",
|
| 277 |
+
},
|
| 278 |
+
body: JSON.stringify({
|
| 279 |
+
question: q,
|
| 280 |
+
user_id: auth && auth.currentUser ? (auth.currentUser.uid || auth.currentUser.email) : "anonymous",
|
| 281 |
+
context: window.chatContext
|
| 282 |
+
}),
|
| 283 |
+
});
|
| 284 |
+
|
| 285 |
+
if (!response.ok) {
|
| 286 |
+
throw new Error(`HTTP error! status: ${response.status}`);
|
| 287 |
+
}
|
| 288 |
+
|
| 289 |
+
const data = await response.json();
|
| 290 |
+
|
| 291 |
+
//now addign acctual reposnse
|
| 292 |
+
const messages = chat.querySelectorAll('p');
|
| 293 |
+
const lastMessage = messages[messages.length - 1];
|
| 294 |
+
lastMessage.innerHTML = `<strong>Chatbot:</strong> ${data.answer}`;
|
| 295 |
+
|
| 296 |
+
} catch (error) {
|
| 297 |
+
console.error("Error:", error);
|
| 298 |
+
const messages = chat.querySelectorAll('p');
|
| 299 |
+
const lastMessage = messages[messages.length - 1];
|
| 300 |
+
lastMessage.innerHTML = `<strong>Chatbot:</strong> Sorry, I encountered an error: ${error.message}`;
|
| 301 |
+
}
|
| 302 |
+
|
| 303 |
+
document.getElementById("user-question").value = "";
|
| 304 |
+
chat.scrollTop = chat.scrollHeight;
|
| 305 |
+
};
|
| 306 |
+
};
|
| 307 |
});
|
| 308 |
</script>
|
| 309 |
</body>
|