Update templates/chat.html
Browse files- templates/chat.html +66 -19
templates/chat.html
CHANGED
@@ -1,23 +1,70 @@
|
|
1 |
-
{% extends
|
|
|
2 |
{% block content %}
|
|
|
|
|
3 |
<h1>Chat with Little Krishna</h1>
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
<div id="comic-strip"></div>
|
21 |
</div>
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
{% endblock %}
|
|
|
1 |
+
{% extends "base.html" %}
|
2 |
+
|
3 |
{% block content %}
|
4 |
+
<div class="container">
|
5 |
+
<div class="chat-container">
|
6 |
<h1>Chat with Little Krishna</h1>
|
7 |
+
|
8 |
+
{% if chat_history %}
|
9 |
+
<div class="chat-history">
|
10 |
+
{% for message in chat_history %}
|
11 |
+
<p><strong>You:</strong> {{ message.user_input }}</p>
|
12 |
+
<p><strong>Krishna:</strong> {{ message.reply }}</p>
|
13 |
+
{% endfor %}
|
14 |
+
</div>
|
15 |
+
{% else %}
|
16 |
+
<p class="chat-error">Error loading chat—try again</p>
|
17 |
+
{% endif %}
|
18 |
+
|
19 |
+
<div class="chat-options">
|
20 |
+
<button onclick="sendMessage('Say something to Krishna...')">Say Something</button>
|
21 |
+
<button onclick="sendMessage('Tell me a story!')">Tell a Story</button>
|
22 |
+
<button onclick="sendMessage('Tell me a riddle!')">Tell a Riddle</button>
|
|
|
23 |
</div>
|
24 |
+
|
25 |
+
<div class="chat-input">
|
26 |
+
<input type="text" id="message-input" placeholder="Type your message here..." />
|
27 |
+
<button onclick="sendMessage(document.getElementById('message-input').value)">Send</button>
|
28 |
+
</div>
|
29 |
+
</div>
|
30 |
+
</div>
|
31 |
+
{% endblock %}
|
32 |
+
|
33 |
+
{% block scripts %}
|
34 |
+
<script>
|
35 |
+
async function sendMessage(message) {
|
36 |
+
if (!message) return;
|
37 |
+
|
38 |
+
const response = await fetch('/chat', {
|
39 |
+
method: 'POST',
|
40 |
+
headers: {
|
41 |
+
'Content-Type': 'application/x-www-form-urlencoded',
|
42 |
+
},
|
43 |
+
body: new URLSearchParams({ 'message': message })
|
44 |
+
});
|
45 |
+
|
46 |
+
const data = await response.json();
|
47 |
+
if (data.reply) {
|
48 |
+
const chatHistory = document.querySelector('.chat-history') || document.createElement('div');
|
49 |
+
if (!chatHistory.classList.contains('chat-history')) {
|
50 |
+
chatHistory.classList.add('chat-history');
|
51 |
+
document.querySelector('.chat-container').insertBefore(chatHistory, document.querySelector('.chat-options'));
|
52 |
+
}
|
53 |
+
chatHistory.innerHTML += `<p><strong>You:</strong> ${message}</p>`;
|
54 |
+
chatHistory.innerHTML += `<p><strong>Krishna:</strong> ${data.reply}</p>`;
|
55 |
+
chatHistory.scrollTop = chatHistory.scrollHeight;
|
56 |
+
|
57 |
+
// Remove error message if present
|
58 |
+
const errorMessage = document.querySelector('.chat-error');
|
59 |
+
if (errorMessage) {
|
60 |
+
errorMessage.remove();
|
61 |
+
}
|
62 |
+
} else {
|
63 |
+
alert('Error: Could not get a response from Krishna. Please try again.');
|
64 |
+
}
|
65 |
+
|
66 |
+
// Clear the input field
|
67 |
+
document.getElementById('message-input').value = '';
|
68 |
+
}
|
69 |
+
</script>
|
70 |
{% endblock %}
|