-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
177 lines (158 loc) ยท 4.98 KB
/
index.html
File metadata and controls
177 lines (158 loc) ยท 4.98 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>RASA Voice Assistant</title>
<style>
body {
font-family: sans-serif;
background: #f0f0f0;
text-align: center;
padding: 30px;
}
#chat {
width: 90%;
max-width: 600px;
height: 400px;
margin: 20px auto;
padding: 10px;
overflow-y: scroll;
background: white;
border-radius: 10px;
border: 1px solid #ccc;
}
.msg {
margin: 10px;
padding: 10px;
border-radius: 5px;
max-width: 80%;
}
.user { background: #dcf8c6; text-align: right; margin-left: auto; }
.bot { background: #eee; text-align: left; margin-right: auto; }
button {
padding: 10px 20px;
font-size: 18px;
margin-top: 10px;
background: #28a745;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
</style>
</head>
<body>
<h2>๐ค Rasa Voice Assistant</h2>
<div id="chat"></div>
<button id="mic" title="Press 'S' to start voice chat">๐๏ธ Tap to Start Voice Chat</button>
<script>
const chat = document.getElementById("chat");
const micBtn = document.getElementById("mic");
let listening = false;
let inactivityTimer;
function appendMessage(text, className) {
const div = document.createElement("div");
div.className = `msg ${className}`;
div.textContent = text;
chat.appendChild(div);
chat.scrollTop = chat.scrollHeight;
}
function sendToRasa(text) {
appendMessage(text, "user");
resetInactivityTimer();
fetch("http://localhost:5005/webhooks/rest/webhook", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ sender: "user", message: text })
})
.then(res => res.json())
.then(data => {
if (data.length) {
data.forEach(msg => {
if (msg.text) {
appendMessage(msg.text, "bot");
speak(msg.text);
}
});
} else {
appendMessage("๐ค I didnโt get a reply from the assistant.", "bot");
}
})
.catch(err => {
console.error("Rasa server error:", err);
appendMessage("Oops! I couldn't reach the server.", "bot");
speak("Oops! I couldn't reach the server.");
});
}
function speak(text) {
const utterance = new SpeechSynthesisUtterance(text);
// Stop listening to prevent echo
if (listening) {
recognition.stop();
}
micBtn.textContent = "๐ฃ๏ธ Speaking...";
speechSynthesis.speak(utterance);
utterance.onend = () => {
if (listening) {
recognition.start();
micBtn.textContent = "๐ง Listening...";
} else {
micBtn.textContent = "๐๏ธ Tap to Start Voice Chat";
}
};
}
// ๐ค Voice input setup
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
const recognition = new SpeechRecognition();
recognition.lang = "en-US";
recognition.interimResults = false;
recognition.continuous = true;
micBtn.onclick = () => {
if (!listening) {
listening = true;
recognition.start();
micBtn.textContent = "๐ง Listening...";
resetInactivityTimer();
}
};
recognition.onresult = (event) => {
const transcript = event.results[event.results.length - 1][0].transcript.toLowerCase();
sendToRasa(transcript);
if (transcript.includes("bye") || transcript.includes("goodbye")) {
speak("Goodbye! Take care.");
appendMessage("Goodbye! Take care.", "bot");
recognition.stop();
listening = false;
micBtn.textContent = "๐๏ธ Tap to Start Voice Chat";
clearTimeout(inactivityTimer);
}
};
recognition.onend = () => {
if (listening) {
recognition.start(); // auto-restart if still active
}
};
recognition.onerror = (event) => {
console.log("Speech recognition error:", event.error);
micBtn.textContent = "๐๏ธ Tap to Start Voice Chat";
};
function resetInactivityTimer() {
clearTimeout(inactivityTimer);
inactivityTimer = setTimeout(() => {
speak("Goodbye! You've been silent for a while.");
appendMessage("Goodbye! You've been silent for a while.", "bot");
recognition.stop();
listening = false;
micBtn.textContent = "๐๏ธ Tap to Start Voice Chat";
}, 60000); // 1 minute
}
// ๐ข Keyboard shortcut: Press "S" to start voice
document.addEventListener("keydown", (e) => {
if (e.key.toLowerCase() === "s") {
micBtn.click();
}
});
</script>
</body>
</html>