Shresthh03 commited on
Commit
c429d8c
Β·
verified Β·
1 Parent(s): bb82646

Update it.

Browse files
Files changed (1) hide show
  1. index.html +171 -149
index.html CHANGED
@@ -1,163 +1,185 @@
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.0">
6
- <title>Empathy Chatbot πŸ’¬</title>
7
  <script src="https://cdn.tailwindcss.com"></script>
8
  <style>
9
- body {
10
- transition: background 1.5s ease;
 
 
 
 
 
11
  }
12
- #chat-box {
13
- max-height: 60vh;
14
- overflow-y: auto;
15
- scroll-behavior: smooth;
 
 
 
 
 
 
 
16
  }
17
  </style>
18
  </head>
19
- <body class="bg-gradient-to-br from-green-100 to-green-200 min-h-screen flex flex-col items-center justify-center p-4">
20
- <div class="bg-white rounded-lg shadow-xl w-full max-w-md p-6 flex flex-col">
21
- <h1 class="text-2xl font-bold text-center mb-4 text-gray-700">πŸ’› Emotional Support Assistant</h1>
22
 
23
- <div id="chat-box" class="flex-1 mb-4 overflow-y-auto space-y-3"></div>
24
 
25
- <div class="flex gap-2">
26
- <input id="message-input" type="text" placeholder="Type your message..." class="flex-1 border border-gray-300 rounded px-3 py-2 focus:outline-none focus:ring focus:ring-blue-200">
27
- <button id="send-btn" class="bg-blue-500 text-white rounded px-4 py-2 hover:bg-blue-600">Send</button>
28
- </div>
 
 
 
 
 
 
 
29
 
30
- <div class="flex justify-center gap-2 mt-4">
31
- <button id="start-btn" class="bg-green-500 text-white px-3 py-2 rounded hover:bg-green-600">πŸŽ™οΈ Start Listening</button>
32
- <button id="stop-btn" class="bg-red-500 text-white px-3 py-2 rounded hover:bg-red-600">⏹️ Stop</button>
33
- </div>
34
  </div>
35
- <script>
36
- const chatBox = document.createElement("div");
37
- const input = document.createElement("input");
38
- const sendBtn = document.createElement("button");
39
- const startBtn = document.createElement("button");
40
- const stopBtn = document.createElement("button");
41
-
42
- chatBox.id = "chat-box";
43
- input.placeholder = "Type your message...";
44
- sendBtn.textContent = "Send";
45
- startBtn.textContent = "πŸŽ™οΈ Start Listening";
46
- stopBtn.textContent = "⏹️ Stop";
47
-
48
- document.body.append(chatBox, input, sendBtn, startBtn, stopBtn);
49
-
50
- let recognition;
51
- let isListening = false;
52
- let synth = window.speechSynthesis;
53
- let currentUtterance = null;
54
-
55
- // βœ… Initialize SpeechRecognition
56
- if ("webkitSpeechRecognition" in window || "SpeechRecognition" in window) {
57
- const SpeechRecognition =
58
- window.SpeechRecognition || window.webkitSpeechRecognition;
59
- recognition = new SpeechRecognition();
60
- recognition.continuous = true;
61
- recognition.interimResults = false;
62
- recognition.lang = "en-US";
63
-
64
- recognition.onresult = (event) => {
65
- const text = event.results[event.results.length - 1][0].transcript.trim();
66
- input.value = text;
67
- sendMessage();
68
- };
69
-
70
- recognition.onerror = (event) => {
71
- console.error("Speech recognition error:", event.error);
72
- };
73
-
74
- recognition.onend = () => {
75
- isListening = false;
76
- startBtn.disabled = false;
77
- };
78
- } else {
79
- alert("Speech recognition not supported on this browser.");
80
- }
81
-
82
- // βœ… Send message to backend
83
- async function sendMessage() {
84
- const text = input.value.trim();
85
- if (!text) return;
86
- addMessage("πŸ§‘ You", text);
87
- input.value = "";
88
-
89
- const res = await fetch("/chat", {
90
- method: "POST",
91
- headers: { "Content-Type": "application/json" },
92
- body: JSON.stringify({ message: text }),
93
- });
94
-
95
- const data = await res.json();
96
- addMessage("πŸ€– Bot", data.text);
97
- speakText(data.text);
98
- updateBackground(data.emotion);
99
- }
100
-
101
- // βœ… Speak bot response
102
- function speakText(text) {
103
- if (synth.speaking) synth.cancel();
104
- currentUtterance = new SpeechSynthesisUtterance(text);
105
- currentUtterance.lang = "en-US";
106
- synth.speak(currentUtterance);
107
- }
108
-
109
- // βœ… Stop everything cleanly
110
- stopBtn.onclick = () => {
111
- if (isListening && recognition) recognition.stop();
112
- if (synth.speaking) synth.cancel();
113
- isListening = false;
114
- startBtn.disabled = false;
115
- };
116
-
117
- // βœ… Start listening
118
- startBtn.onclick = () => {
119
- if (!isListening && recognition) {
120
- recognition.start();
121
- isListening = true;
122
- startBtn.disabled = true;
123
- }
124
- };
125
-
126
- // βœ… Chatbox display
127
- function addMessage(sender, text) {
128
- const msg = document.createElement("p");
129
- msg.textContent = `${sender}: ${text}`;
130
- chatBox.appendChild(msg);
131
- chatBox.scrollTop = chatBox.scrollHeight;
132
- }
133
-
134
- // βœ… Emotion-based background
135
- function updateBackground(emotion) {
136
- let color;
137
- switch (emotion.toLowerCase()) {
138
- case "happy":
139
- color = "linear-gradient(135deg, #fff176, #ffd54f)";
140
- break;
141
- case "sad":
142
- color = "linear-gradient(135deg, #64b5f6, #1976d2)";
143
- break;
144
- case "angry":
145
- color = "linear-gradient(135deg, #ff7043, #f44336)";
146
- break;
147
- case "calm":
148
- color = "linear-gradient(135deg, #a5d6a7, #66bb6a)";
149
- break;
150
- case "motivated":
151
- color = "linear-gradient(135deg, #ffb74d, #fb8c00)";
152
- break;
153
- default:
154
- color = "linear-gradient(135deg, #e0e0e0, #bdbdbd)";
155
- }
156
- document.body.style.background = color;
157
- document.body.style.transition = "background 1s ease";
158
- }
159
-
160
- sendBtn.onclick = sendMessage;
161
- </script>
 
 
 
 
 
 
162
  </body>
163
- </html>accesskey
 
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.0" />
6
+ <title>πŸ’› Emotional Support Assistant</title>
7
  <script src="https://cdn.tailwindcss.com"></script>
8
  <style>
9
+ body { transition: background 1s ease; }
10
+ #mic-indicator {
11
+ width: 20px; height: 20px; border-radius: 50%;
12
+ margin-left: 10px; display: inline-block;
13
+ background-color: gray;
14
+ box-shadow: 0 0 5px rgba(0,0,0,0.2);
15
+ transition: background-color 0.3s ease, box-shadow 0.3s ease;
16
  }
17
+ .listening {
18
+ background-color: #22c55e;
19
+ box-shadow: 0 0 15px #22c55e;
20
+ }
21
+ .speaking {
22
+ animation: pulse 1s infinite;
23
+ }
24
+ @keyframes pulse {
25
+ 0% { opacity: 0.3; }
26
+ 50% { opacity: 1; }
27
+ 100% { opacity: 0.3; }
28
  }
29
  </style>
30
  </head>
31
+ <body class="min-h-screen flex flex-col items-center justify-center bg-gradient-to-br from-yellow-100 to-yellow-300 p-4">
32
+ <h1 class="text-2xl font-bold mb-4 text-center">πŸ’› Emotional Support Assistant</h1>
 
33
 
34
+ <div id="chat-box" class="w-full max-w-2xl h-96 overflow-y-auto bg-white p-4 rounded shadow mb-4 border"></div>
35
 
36
+ <div class="flex gap-2 w-full max-w-2xl">
37
+ <input id="user-input" type="text" placeholder="Type your message..."
38
+ class="flex-1 border p-2 rounded" />
39
+ <button id="send-btn" class="bg-blue-500 text-white px-4 py-2 rounded">Send</button>
40
+ </div>
41
+
42
+ <div class="mt-4 flex items-center gap-3">
43
+ <button id="start-btn" class="bg-green-500 text-white px-4 py-2 rounded">πŸŽ™οΈ Start Listening</button>
44
+ <button id="stop-btn" class="bg-red-500 text-white px-4 py-2 rounded">⏹️ Stop</button>
45
+ <div id="mic-indicator" title="Mic status"></div>
46
+ </div>
47
 
48
+ <div id="speaking-status" class="text-gray-600 mt-3 hidden">
49
+ πŸ€– Bot is speaking...
 
 
50
  </div>
51
+
52
+ <script>
53
+ const chatBox = document.getElementById("chat-box");
54
+ const userInput = document.getElementById("user-input");
55
+ const sendBtn = document.getElementById("send-btn");
56
+ const startBtn = document.getElementById("start-btn");
57
+ const stopBtn = document.getElementById("stop-btn");
58
+ const micIndicator = document.getElementById("mic-indicator");
59
+ const speakingStatus = document.getElementById("speaking-status");
60
+
61
+ let recognition;
62
+ let isListening = false;
63
+ let synth = window.speechSynthesis;
64
+ let currentUtterance = null;
65
+
66
+ // 🎀 Initialize SpeechRecognition
67
+ if ("webkitSpeechRecognition" in window || "SpeechRecognition" in window) {
68
+ const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
69
+ recognition = new SpeechRecognition();
70
+ recognition.continuous = true;
71
+ recognition.interimResults = false;
72
+ recognition.lang = "en-US";
73
+
74
+ recognition.onstart = () => {
75
+ micIndicator.classList.add("listening");
76
+ isListening = true;
77
+ startBtn.disabled = true;
78
+ };
79
+
80
+ recognition.onend = () => {
81
+ micIndicator.classList.remove("listening");
82
+ isListening = false;
83
+ startBtn.disabled = false;
84
+ };
85
+
86
+ recognition.onresult = (event) => {
87
+ const text = event.results[event.results.length - 1][0].transcript.trim();
88
+ userInput.value = text;
89
+ sendMessage();
90
+ };
91
+
92
+ recognition.onerror = (event) => {
93
+ console.error("Speech recognition error:", event.error);
94
+ micIndicator.classList.remove("listening");
95
+ isListening = false;
96
+ startBtn.disabled = false;
97
+ };
98
+ } else {
99
+ alert("Speech recognition not supported in this browser. Try using Chrome.");
100
+ }
101
+
102
+ // πŸ’¬ Send user message to Flask backend
103
+ async function sendMessage() {
104
+ const text = userInput.value.trim();
105
+ if (!text) return;
106
+ addMessage("πŸ§‘ You", text);
107
+ userInput.value = "";
108
+
109
+ try {
110
+ const res = await fetch("/chat", {
111
+ method: "POST",
112
+ headers: { "Content-Type": "application/json" },
113
+ body: JSON.stringify({ message: text }),
114
+ });
115
+
116
+ const data = await res.json();
117
+ addMessage("πŸ€– Bot", data.text);
118
+ speakText(data.text);
119
+ updateBackground(data.emotion);
120
+ } catch (err) {
121
+ console.error("Error:", err);
122
+ }
123
+ }
124
+
125
+ // πŸ—£οΈ Speak text aloud and show animation
126
+ function speakText(text) {
127
+ if (synth.speaking) synth.cancel();
128
+
129
+ currentUtterance = new SpeechSynthesisUtterance(text);
130
+ currentUtterance.lang = "en-US";
131
+
132
+ speakingStatus.classList.remove("hidden");
133
+ speakingStatus.classList.add("speaking");
134
+
135
+ currentUtterance.onend = () => {
136
+ speakingStatus.classList.add("hidden");
137
+ speakingStatus.classList.remove("speaking");
138
+ };
139
+
140
+ synth.speak(currentUtterance);
141
+ }
142
+
143
+ // 🧠 Button handlers
144
+ startBtn.onclick = () => {
145
+ if (recognition && !isListening) recognition.start();
146
+ };
147
+
148
+ stopBtn.onclick = () => {
149
+ if (isListening && recognition) recognition.stop();
150
+ if (synth.speaking) synth.cancel();
151
+ isListening = false;
152
+ micIndicator.classList.remove("listening");
153
+ speakingStatus.classList.add("hidden");
154
+ startBtn.disabled = false;
155
+ };
156
+
157
+ sendBtn.onclick = sendMessage;
158
+
159
+ // πŸ’¬ Display messages in chat
160
+ function addMessage(sender, text) {
161
+ const bubble = document.createElement("div");
162
+ bubble.className = sender.includes("Bot")
163
+ ? "text-left mb-2 bg-yellow-100 p-2 rounded"
164
+ : "text-right mb-2 bg-blue-100 p-2 rounded";
165
+ bubble.innerHTML = `<strong>${sender}:</strong> ${text}`;
166
+ chatBox.appendChild(bubble);
167
+ chatBox.scrollTop = chatBox.scrollHeight;
168
+ }
169
+
170
+ // 🎨 Change background smoothly by emotion
171
+ function updateBackground(emotion) {
172
+ let color;
173
+ switch (emotion?.toLowerCase()) {
174
+ case "happy": color = "linear-gradient(135deg, #fff176, #ffd54f)"; break;
175
+ case "sad": color = "linear-gradient(135deg, #64b5f6, #1976d2)"; break;
176
+ case "angry": color = "linear-gradient(135deg, #ff7043, #f44336)"; break;
177
+ case "calm": color = "linear-gradient(135deg, #a5d6a7, #66bb6a)"; break;
178
+ case "motivated": color = "linear-gradient(135deg, #ffb74d, #fb8c00)"; break;
179
+ default: color = "linear-gradient(135deg, #e0e0e0, #bdbdbd)";
180
+ }
181
+ document.body.style.background = color;
182
+ }
183
+ </script>
184
  </body>
185
+ </html>