Spaces:
Running
Running
Update index.html
Browse files- index.html +108 -23
index.html
CHANGED
|
@@ -14,28 +14,7 @@
|
|
| 14 |
|
| 15 |
<!-- Bootstrap CSS -->
|
| 16 |
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
<body style="font-family: Arial, sans-serif;">
|
| 20 |
-
<div class="container mt-5" style="max-width: 800px; margin: 0 auto;">
|
| 21 |
-
<h1 class="text-center mb-4" style="font-weight: bold; color: #333;">Conversational Voice Chatbot</h1>
|
| 22 |
-
|
| 23 |
-
<!-- Chatbot Interface -->
|
| 24 |
-
<div id="chat-container" class="p-3" style="height: 70vh; overflow-y: auto;">
|
| 25 |
-
<!-- Chat messages will appear here -->
|
| 26 |
-
</div>
|
| 27 |
-
|
| 28 |
-
<!-- Input and Button Section -->
|
| 29 |
-
<div class="input-group mt-3">
|
| 30 |
-
<input id="user-input" type="text" class="form-control" placeholder="Type your message..." />
|
| 31 |
-
<button id="send-button" class="btn btn-primary" onclick="handleUserInput()">Send</button>
|
| 32 |
-
<button id="record-button" class="btn btn-secondary" onclick="toggleRecording()">🎙️</button>
|
| 33 |
-
</div>
|
| 34 |
-
</div>
|
| 35 |
-
|
| 36 |
-
<!-- External JavaScript -->
|
| 37 |
-
<script src="script.js"></script>
|
| 38 |
-
|
| 39 |
<style>
|
| 40 |
/* Chat message styling */
|
| 41 |
.user-message {
|
|
@@ -69,6 +48,112 @@
|
|
| 69 |
border-radius: 10px;
|
| 70 |
}
|
| 71 |
</style>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
</body>
|
| 73 |
|
| 74 |
-
</html>
|
|
|
|
| 14 |
|
| 15 |
<!-- Bootstrap CSS -->
|
| 16 |
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
|
| 17 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
<style>
|
| 19 |
/* Chat message styling */
|
| 20 |
.user-message {
|
|
|
|
| 48 |
border-radius: 10px;
|
| 49 |
}
|
| 50 |
</style>
|
| 51 |
+
</head>
|
| 52 |
+
|
| 53 |
+
<body style="font-family: Arial, sans-serif;">
|
| 54 |
+
<div class="container mt-5" style="max-width: 800px; margin: 0 auto;">
|
| 55 |
+
<h1 class="text-center mb-4" style="font-weight: bold; color: #333;">Conversational Voice Chatbot</h1>
|
| 56 |
+
|
| 57 |
+
<div id="chat-container" class="p-3" style="height: 70vh; overflow-y: auto;"></div>
|
| 58 |
+
|
| 59 |
+
<div class="input-group mt-3">
|
| 60 |
+
<input id="user-input" type="text" class="form-control" placeholder="Type your message..." />
|
| 61 |
+
<button id="send-button" class="btn btn-primary" onclick="handleUserInput()">Send</button>
|
| 62 |
+
<button id="record-button" class="btn btn-secondary" onclick="toggleRecording()">🎙️</button>
|
| 63 |
+
</div>
|
| 64 |
+
</div>
|
| 65 |
+
|
| 66 |
+
<script>
|
| 67 |
+
let transcriber, generator;
|
| 68 |
+
let isRecording = false;
|
| 69 |
+
let mediaRecorder;
|
| 70 |
+
let audioChunks = [];
|
| 71 |
+
|
| 72 |
+
async function initializeModels() {
|
| 73 |
+
try {
|
| 74 |
+
console.log("Loading transcriber model...");
|
| 75 |
+
transcriber = await pipeline('automatic-speech-recognition', 'Xenova/whisper-tiny.en');
|
| 76 |
+
console.log("Loading generator model...");
|
| 77 |
+
generator = await pipeline('text2text-generation', 'Xenova/LaMini-Flan-T5-783M');
|
| 78 |
+
} catch (error) {
|
| 79 |
+
console.error("Error loading models:", error);
|
| 80 |
+
displayMessage("Error loading models.", 'bot-message');
|
| 81 |
+
}
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
function displayMessage(message, className) {
|
| 85 |
+
const messageElement = document.createElement('div');
|
| 86 |
+
messageElement.className = className;
|
| 87 |
+
messageElement.innerText = message;
|
| 88 |
+
document.getElementById('chat-container').appendChild(messageElement);
|
| 89 |
+
document.getElementById('chat-container').scrollTop = document.getElementById('chat-container').scrollHeight;
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
async function handleUserInput() {
|
| 93 |
+
const userInput = document.getElementById('user-input').value.trim();
|
| 94 |
+
if (!userInput) return;
|
| 95 |
+
displayMessage("You: " + userInput, 'user-message');
|
| 96 |
+
document.getElementById('user-input').value = "";
|
| 97 |
+
await generateResponse(userInput);
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
async function toggleRecording() {
|
| 101 |
+
isRecording = !isRecording;
|
| 102 |
+
document.getElementById('record-button').innerText = isRecording ? "⏹️" : "🎙️";
|
| 103 |
+
isRecording ? startRecording() : stopRecording();
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
async function startRecording() {
|
| 107 |
+
try {
|
| 108 |
+
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
| 109 |
+
mediaRecorder = new MediaRecorder(stream);
|
| 110 |
+
audioChunks = [];
|
| 111 |
+
mediaRecorder.ondataavailable = event => audioChunks.push(event.data);
|
| 112 |
+
mediaRecorder.start();
|
| 113 |
+
} catch (error) {
|
| 114 |
+
console.error("Error starting recording:", error);
|
| 115 |
+
displayMessage("Error starting recording.", 'bot-message');
|
| 116 |
+
}
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
async function stopRecording() {
|
| 120 |
+
mediaRecorder.stop();
|
| 121 |
+
mediaRecorder.onstop = async () => {
|
| 122 |
+
const audioBlob = new Blob(audioChunks);
|
| 123 |
+
const url = URL.createObjectURL(audioBlob);
|
| 124 |
+
try {
|
| 125 |
+
console.log("Transcribing audio...");
|
| 126 |
+
const result = await transcriber(url);
|
| 127 |
+
const transcribedText = result?.text || "Error in transcription.";
|
| 128 |
+
displayMessage("You (voice): " + transcribedText, 'user-message');
|
| 129 |
+
await generateResponse(transcribedText);
|
| 130 |
+
} catch (error) {
|
| 131 |
+
console.error("Error in transcription:", error);
|
| 132 |
+
displayMessage("Error in transcription.", 'bot-message');
|
| 133 |
+
}
|
| 134 |
+
};
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
async function generateResponse(inputText) {
|
| 138 |
+
try {
|
| 139 |
+
console.log("Generating response for input:", inputText);
|
| 140 |
+
const result = await generator(inputText, { max_new_tokens: 100 });
|
| 141 |
+
const generatedText = result;
|
| 142 |
+
displayMessage("Bot: " + generatedText, 'bot-message');
|
| 143 |
+
speakText(generatedText);
|
| 144 |
+
} catch (error) {
|
| 145 |
+
console.error("Error in response generation:", error);
|
| 146 |
+
displayMessage("Error in response generation: " + error.message, 'bot-message');
|
| 147 |
+
}
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
function speakText(text) {
|
| 151 |
+
const utterance = new SpeechSynthesisUtterance(text);
|
| 152 |
+
window.speechSynthesis.speak(utterance);
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
+
window.addEventListener("DOMContentLoaded", initializeModels);
|
| 156 |
+
</script>
|
| 157 |
</body>
|
| 158 |
|
| 159 |
+
</html>
|