ayush2917 commited on
Commit
2a477dc
·
verified ·
1 Parent(s): 29d4eb8

Update templates/chat.html

Browse files
Files changed (1) hide show
  1. templates/chat.html +66 -19
templates/chat.html CHANGED
@@ -1,23 +1,70 @@
1
- {% extends 'base.html' %}
 
2
  {% block content %}
 
 
3
  <h1>Chat with Little Krishna</h1>
4
- <div id="chat-container">
5
- <div id="chat-messages"></div>
6
- <form id="chat-form">
7
- <input type="text" id="message-input" placeholder="Say something to Krishna..." required>
8
- <button type="submit">Send</button>
9
- </form>
10
- <div id="story-section">
11
- <input type="text" id="story-theme" placeholder="Enter a theme (e.g., butter, peacock)..." value="Vrindavan">
12
- <button id="tell-story-button">Tell me a story!</button>
13
- </div>
14
- <div id="story-output"></div>
15
- <audio id="story-audio" controls style="display: none; margin-top: 10px;"></audio>
16
- <div id="riddle-section">
17
- <button id="tell-riddle-button">Tell me a riddle!</button>
18
- </div>
19
- <div id="riddle-output"></div>
20
- <div id="comic-strip"></div>
21
  </div>
22
- <script src="/static/js/chat.js"></script>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 %}