Spaces:
Sleeping
Sleeping
Update static/interview.js
Browse files- static/interview.js +65 -12
static/interview.js
CHANGED
|
@@ -1,16 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
async function askBot(message) {
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
}
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
});
|
|
|
|
| 1 |
+
const video = document.getElementById("localVideo");
|
| 2 |
+
const messages = document.getElementById("messages");
|
| 3 |
+
const startBtn = document.getElementById("startInterview");
|
| 4 |
+
|
| 5 |
+
async function startCamera() {
|
| 6 |
+
try {
|
| 7 |
+
const stream = await navigator.mediaDevices.getUserMedia({ video: true, audio: true });
|
| 8 |
+
video.srcObject = stream;
|
| 9 |
+
} catch (err) {
|
| 10 |
+
alert("Could not access camera/mic: " + err.message);
|
| 11 |
+
}
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
function appendMessage(sender, text) {
|
| 15 |
+
const msg = document.createElement("div");
|
| 16 |
+
msg.textContent = `${sender}: ${text}`;
|
| 17 |
+
messages.appendChild(msg);
|
| 18 |
+
messages.scrollTop = messages.scrollHeight;
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
async function askBot(message) {
|
| 22 |
+
const res = await fetch("/ask", {
|
| 23 |
+
method: "POST",
|
| 24 |
+
headers: { "Content-Type": "application/json" },
|
| 25 |
+
body: JSON.stringify({ message })
|
| 26 |
+
});
|
| 27 |
+
const data = await res.json();
|
| 28 |
+
return data.response;
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
function speak(text) {
|
| 32 |
+
const utterance = new SpeechSynthesisUtterance(text);
|
| 33 |
+
utterance.lang = "en-US";
|
| 34 |
+
utterance.pitch = 1.1;
|
| 35 |
+
utterance.rate = 1;
|
| 36 |
+
speechSynthesis.speak(utterance);
|
| 37 |
}
|
| 38 |
|
| 39 |
+
function startSpeechRecognition() {
|
| 40 |
+
const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
|
| 41 |
+
recognition.lang = "en-US";
|
| 42 |
+
recognition.interimResults = false;
|
| 43 |
+
recognition.maxAlternatives = 1;
|
| 44 |
+
|
| 45 |
+
recognition.start();
|
| 46 |
+
|
| 47 |
+
recognition.onresult = async (event) => {
|
| 48 |
+
const transcript = event.results[0][0].transcript;
|
| 49 |
+
appendMessage("You", transcript);
|
| 50 |
+
|
| 51 |
+
const reply = await askBot(transcript);
|
| 52 |
+
appendMessage("Interviewer", reply);
|
| 53 |
+
speak(reply);
|
| 54 |
+
};
|
| 55 |
+
|
| 56 |
+
recognition.onerror = (event) => {
|
| 57 |
+
console.error("Speech recognition error:", event.error);
|
| 58 |
+
};
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
startBtn.addEventListener("click", async () => {
|
| 62 |
+
await startCamera();
|
| 63 |
+
|
| 64 |
+
const opening = await askBot("Start the interview.");
|
| 65 |
+
appendMessage("Interviewer", opening);
|
| 66 |
+
speak(opening);
|
| 67 |
+
|
| 68 |
+
startSpeechRecognition();
|
| 69 |
});
|