|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Asian Food QnA Chatbot</title>
|
|
|
|
<style>
|
|
#user-input {
|
|
width: 50%;
|
|
max-width: 600px;
|
|
}
|
|
</style>
|
|
|
|
<script>
|
|
let recognition;
|
|
|
|
function startSpeechRecognition() {
|
|
if (!('webkitSpeechRecognition' in window)) {
|
|
alert('Speech Recognition not supported in this browser.');
|
|
return;
|
|
}
|
|
|
|
recognition = new webkitSpeechRecognition();
|
|
recognition.continuous = false;
|
|
recognition.interimResults = false;
|
|
recognition.lang = 'en-US';
|
|
|
|
recognition.onstart = function () {
|
|
document.getElementById("speech-status").textContent = "Listening...";
|
|
};
|
|
|
|
recognition.onresult = function (event) {
|
|
const speechResult = event.results[0][0].transcript;
|
|
document.getElementById("user-input").value = speechResult;
|
|
askBot(speechResult);
|
|
};
|
|
|
|
recognition.onerror = function (event) {
|
|
alert("Speech Recognition Error: " + event.error);
|
|
};
|
|
|
|
recognition.onend = function () {
|
|
document.getElementById("speech-status").textContent = "";
|
|
};
|
|
|
|
recognition.start();
|
|
}
|
|
|
|
async function askBot(question) {
|
|
const responseBox = document.getElementById("response-box");
|
|
responseBox.textContent = "Waiting for response...";
|
|
|
|
try {
|
|
const response = await fetch('/ask', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({ question })
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
if (data.error) {
|
|
responseBox.textContent = "Error: " + data.error;
|
|
} else {
|
|
responseBox.innerHTML = "You: " + question + "<br>" + "Bot: " + data.answer;
|
|
|
|
const audioUrl = data.audio + "?t=" + new Date().getTime();
|
|
const audio = new Audio(audioUrl);
|
|
audio.play();
|
|
document.getElementById("user-input").value = "";
|
|
}
|
|
} catch (error) {
|
|
responseBox.textContent = "Error: Unable to process the request.";
|
|
}
|
|
}
|
|
|
|
function handleFormSubmit(event) {
|
|
event.preventDefault();
|
|
const question = document.getElementById("user-input").value;
|
|
if (question.trim()) {
|
|
askBot(question);
|
|
}
|
|
}
|
|
</script>
|
|
</head>
|
|
<body>
|
|
<h1>Asian Food QnA Chatbot - English</h1>
|
|
<form onsubmit="handleFormSubmit(event)">
|
|
<input type="text" id="user-input" placeholder="Type your question here..." required>
|
|
<button type="submit">Ask</button>
|
|
</form>
|
|
<button onclick="startSpeechRecognition()">π€ Speak</button>
|
|
<span id="speech-status" style="color: green; margin-left: 10px;"></span>
|
|
<div id="response-box" style="margin-top: 20px; font-weight: bold;">Response:</div>
|
|
</body>
|
|
</html>
|
|
|