wookimchye's picture
Update templates/index.html
caf8c11 verified
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Asian Food Chatbot</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f9;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.chat-container {
width: 500px;
background-color: #fff;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
border-radius: 8px;
overflow: hidden;
display: flex;
flex-direction: column;
}
.chat-header {
background-color: #007bff;
color: #fff;
padding: 10px;
text-align: center;
font-size: 1.2em;
}
.chat-messages {
flex: 1;
padding: 10px;
overflow-y: auto;
}
.chat-input {
display: flex;
border-top: 1px solid #ddd;
}
.chat-input input {
flex: 1;
padding: 10px;
border: none;
border-radius: 0;
outline: none;
}
.chat-input button {
padding: 10px;
background-color: #007bff;
color: #fff;
border: none;
cursor: pointer;
}
.chat-input button:hover {
background-color: #0056b3;
}
.message {
margin-bottom: 10px;
}
.message.user {
text-align: right;
}
.message.bot {
text-align: left;
}
.message .text {
display: inline-block;
padding: 10px;
border-radius: 8px;
max-width: 70%;
}
.message.user .text {
background-color: #007bff;
color: #fff;
}
.message.bot .text {
background-color: #f1f1f1;
color: #333;
}
</style>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script src="https://aka.ms/csspeech/jsbrowserpackageraw"></script>
</head>
<body>
<div class="chat-container">
<div class="chat-header">Asian Food Chatbot - English only</div>
<div class="chat-messages" id="chat-messages"></div>
<div class="chat-input">
<input type="text" id="question" placeholder="Type a message...">
<button onclick="askQuestion()">Ask</button>
<button onclick="startListening()" style="background-color:green">🎤 Speak</button>
</div>
</div>
<audio id="audio" controls style="display:none;"></audio>
<script>
const speechKey = 'BHMIZNZ8xH7JQHXaGAoaOlwdx3bjxvhyuLxpHumSiRPXxpo1Rpb5JQQJ99BAACqBBLyXJ3w3AAAYACOGvOBV';
const speechRegion = 'southeastasia';
function askQuestion() {
const question = document.getElementById('question').value;
if (!question) return;
axios.post('/ask', { question }).then(response => {
console.log("Response:", response.data); // Debugging log
const chatMessages = document.getElementById('chat-messages');
const userMessage = document.createElement('div');
userMessage.textContent = `You: ${question}`;
chatMessages.appendChild(userMessage);
const botMessage = document.createElement('div');
botMessage.textContent = `Bot: ${response.data.answer}`;
botMessage.style.marginBottom = "10px"; // Add margin-bottom for spacing
chatMessages.appendChild(botMessage);
const audioElement = document.getElementById('audio');
audioElement.src = response.data.audio + '?t=' + new Date().getTime();
audioElement.style.display = 'block';
audioElement.play();
document.getElementById('question').value = '';
}).catch(error => {
console.error("Error:", error); // Debugging log
});
}
function startListening() {
const speechConfig = SpeechSDK.SpeechConfig.fromSubscription(speechKey, speechRegion);
const audioConfig = SpeechSDK.AudioConfig.fromDefaultMicrophoneInput();
const recognizer = new SpeechSDK.SpeechRecognizer(speechConfig, audioConfig);
recognizer.recognizeOnceAsync(result => {
if (result.reason === SpeechSDK.ResultReason.RecognizedSpeech) {
console.log("Recognized Text:", result.text); // Debugging log
document.getElementById('question').value = result.text;
askQuestion();
} else {
console.error("Speech Recognition Error:", result.errorDetails); // Debugging log
}
}, err => {
console.error("Error recognizing speech:", err); // Debugging log
});
}
</script>
</body>
</html>