<div class="chat-container">
<div class="chat-header">
Pharmacy Chat
</div>
<div class="chat-messages" id="chatMessages">
<!-- Messages will be displayed here -->
</div>
<div class="chat-input-container">
<input type="text" class="chat-input" id="chatInput" placeholder="Type a message..." />
<button class="send-button" onclick="sendMessage()">Send</button>
<button class="voice-button" id="voiceButton" onclick="toggleRecording()">🎤</button>
</div>
</div>
<script>
let mediaRecorder;
let audioChunks = [];
let isRecording = false;
const chatHistory = ["Show me all available medicines.", "What medicines are out of stock?"]; // Initial history
function sendMessage() {
const input = document.getElementById('chatInput');
const query = input.value.trim();
if (query) {
const sessionId = "user-1234"; // Hardcoded session ID
const payload = {
session_id: sessionId,
query: query,
history: chatHistory // Add previous chat history
};
// Send POST request to the Flask API
fetch('http://localhost:5000/chat/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(payload),
})
.then(response => response.json())
.then(data => {
displayMessage(query, true); // Display the sent message (bold for user)
displayMessage(data.result, false); // Display the response from server
chatHistory.push(query); // Add the query to the history
})
.catch((error) => {
console.error('Error:', error);
});
input.value = ''; // Clear the input field
}
}
function displayMessage(message, isUser) {
const chatMessages = document.getElementById('chatMessages');
const messageElement = document.createElement('div');
messageElement.classList.add(isUser ? 'user-message' : 'server-message'); // Add different styles for user and server
messageElement.textContent = message;
chatMessages.appendChild(messageElement);
chatMessages.scrollTop = chatMessages.scrollHeight;
}
function toggleRecording() {
if (!isRecording) {
startRecording();
} else {
stopRecording();
}
}
function startRecording() {
navigator.mediaDevices.getUserMedia({ audio: true })
.then(stream => {
mediaRecorder = new MediaRecorder(stream);
mediaRecorder.start();
isRecording = true;
document.getElementById('voiceButton').textContent = "⏹️"; // Change to stop icon
mediaRecorder.ondataavailable = event => {
audioChunks.push(event.data);
};
mediaRecorder.onstop = () => {
const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
audioChunks = [];
const audioUrl = URL.createObjectURL(audioBlob);
const audio = document.createElement('audio');
audio.src = audioUrl;
audio.controls = true;
const chatMessages = document.getElementById('chatMessages');
chatMessages.appendChild(audio);
chatMessages.scrollTop = chatMessages.scrollHeight;
};
})
.catch(error => {
console.error("Error accessing microphone:", error);
});
}
function stopRecording() {
mediaRecorder.stop();
isRecording = false;
document.getElementById('voiceButton').textContent = "🎤"; // Change back to mic icon
}
</script>
-
Notifications
You must be signed in to change notification settings - Fork 0
arjuncreativo/webusb
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
About
No description, website, or topics provided.
Resources
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published