Spaces:
Running
Running
Update index.html
Browse files- index.html +83 -48
index.html
CHANGED
|
@@ -1,104 +1,139 @@
|
|
| 1 |
<!DOCTYPE html>
|
| 2 |
-
<html lang="
|
| 3 |
<head>
|
| 4 |
<meta charset="UTF-8">
|
| 5 |
-
<title>
|
| 6 |
<style>
|
| 7 |
-
|
| 8 |
-
text-align: center;
|
| 9 |
font-family: Arial, sans-serif;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
margin: 10px;
|
|
|
|
|
|
|
| 11 |
}
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
margin: 20px auto;
|
| 15 |
-
padding: 10px 20px;
|
| 16 |
-
font-size: 18px;
|
| 17 |
font-weight: bold;
|
| 18 |
-
|
| 19 |
-
color: white;
|
| 20 |
-
border: none;
|
| 21 |
-
border-radius: 5px;
|
| 22 |
-
cursor: pointer;
|
| 23 |
-
}
|
| 24 |
-
#speech:hover {
|
| 25 |
-
background: #0056b3;
|
| 26 |
}
|
| 27 |
</style>
|
| 28 |
</head>
|
| 29 |
<body>
|
| 30 |
|
| 31 |
-
<
|
| 32 |
-
<
|
| 33 |
-
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
<script>
|
| 36 |
-
const
|
| 37 |
-
const
|
| 38 |
-
const
|
| 39 |
|
| 40 |
-
// Load Japanese voice
|
| 41 |
-
let voices = [];
|
| 42 |
const synth = window.speechSynthesis;
|
|
|
|
|
|
|
|
|
|
| 43 |
function loadVoices() {
|
| 44 |
-
return new Promise(resolve => {
|
| 45 |
voices = synth.getVoices();
|
| 46 |
-
if (voices.length
|
| 47 |
-
|
| 48 |
voices = synth.getVoices();
|
| 49 |
-
resolve();
|
| 50 |
-
}
|
| 51 |
});
|
| 52 |
}
|
| 53 |
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
await loadVoices();
|
| 56 |
const utter = new SpeechSynthesisUtterance(text);
|
| 57 |
-
const
|
| 58 |
-
if (
|
| 59 |
-
utter.voice =
|
| 60 |
-
utter.lang =
|
|
|
|
|
|
|
| 61 |
}
|
| 62 |
-
synth.cancel(); // Cancel any ongoing speech
|
| 63 |
synth.speak(utter);
|
| 64 |
}
|
| 65 |
|
| 66 |
-
function
|
| 67 |
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
|
| 68 |
if (!SpeechRecognition) {
|
| 69 |
-
|
| 70 |
return;
|
| 71 |
}
|
| 72 |
|
| 73 |
const recognition = new SpeechRecognition();
|
| 74 |
-
|
|
|
|
| 75 |
recognition.interimResults = false;
|
| 76 |
|
| 77 |
recognition.onstart = () => {
|
| 78 |
-
|
| 79 |
-
status.textContent = '';
|
| 80 |
};
|
| 81 |
|
| 82 |
recognition.onresult = (event) => {
|
| 83 |
const transcript = event.results[0][0].transcript;
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
speakText(transcript); // Speak back the text
|
| 87 |
};
|
| 88 |
|
| 89 |
recognition.onerror = (event) => {
|
| 90 |
-
|
| 91 |
-
status.textContent = `認識エラー: ${event.error}`;
|
| 92 |
};
|
| 93 |
|
| 94 |
recognition.onend = () => {
|
| 95 |
-
|
|
|
|
|
|
|
| 96 |
};
|
| 97 |
|
| 98 |
recognition.start();
|
| 99 |
}
|
| 100 |
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
</script>
|
| 103 |
</body>
|
| 104 |
</html>
|
|
|
|
| 1 |
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
<head>
|
| 4 |
<meta charset="UTF-8">
|
| 5 |
+
<title>Multi-language Voice Agent</title>
|
| 6 |
<style>
|
| 7 |
+
body {
|
|
|
|
| 8 |
font-family: Arial, sans-serif;
|
| 9 |
+
text-align: center;
|
| 10 |
+
margin: 30px;
|
| 11 |
+
}
|
| 12 |
+
select, button {
|
| 13 |
margin: 10px;
|
| 14 |
+
padding: 10px;
|
| 15 |
+
font-size: 16px;
|
| 16 |
}
|
| 17 |
+
#status {
|
| 18 |
+
margin-top: 20px;
|
|
|
|
|
|
|
|
|
|
| 19 |
font-weight: bold;
|
| 20 |
+
color: #444;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
}
|
| 22 |
</style>
|
| 23 |
</head>
|
| 24 |
<body>
|
| 25 |
|
| 26 |
+
<h2>Voice Agent</h2>
|
| 27 |
+
<p>Select Language:</p>
|
| 28 |
+
<select id="languageSelector">
|
| 29 |
+
<option value="ja-JP">Japanese</option>
|
| 30 |
+
<option value="en-US" selected>English (US)</option>
|
| 31 |
+
<option value="hi-IN">Hindi</option>
|
| 32 |
+
<option value="es-ES">Spanish</option>
|
| 33 |
+
<option value="fr-FR">French</option>
|
| 34 |
+
</select>
|
| 35 |
+
<br>
|
| 36 |
+
<button id="speakButton">Tap to Speak</button>
|
| 37 |
+
<div id="status">Initializing...</div>
|
| 38 |
|
| 39 |
<script>
|
| 40 |
+
const speakButton = document.getElementById("speakButton");
|
| 41 |
+
const languageSelector = document.getElementById("languageSelector");
|
| 42 |
+
const statusDiv = document.getElementById("status");
|
| 43 |
|
|
|
|
|
|
|
| 44 |
const synth = window.speechSynthesis;
|
| 45 |
+
let voices = [];
|
| 46 |
+
|
| 47 |
+
// Load voices (Safari requires special handling)
|
| 48 |
function loadVoices() {
|
| 49 |
+
return new Promise((resolve) => {
|
| 50 |
voices = synth.getVoices();
|
| 51 |
+
if (voices.length) return resolve(voices);
|
| 52 |
+
synth.onvoiceschanged = () => {
|
| 53 |
voices = synth.getVoices();
|
| 54 |
+
resolve(voices);
|
| 55 |
+
};
|
| 56 |
});
|
| 57 |
}
|
| 58 |
|
| 59 |
+
// Unlock TTS for Safari by speaking an empty utterance
|
| 60 |
+
function unlockSafariAudio() {
|
| 61 |
+
const utterance = new SpeechSynthesisUtterance("");
|
| 62 |
+
utterance.volume = 0;
|
| 63 |
+
synth.speak(utterance);
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
async function askPermissions() {
|
| 67 |
+
try {
|
| 68 |
+
// Request microphone access
|
| 69 |
+
await navigator.mediaDevices.getUserMedia({ audio: true });
|
| 70 |
+
|
| 71 |
+
// Safari requires audio to be unlocked with user gesture
|
| 72 |
+
if (navigator.userAgent.includes("Safari") && !navigator.userAgent.includes("Chrome")) {
|
| 73 |
+
unlockSafariAudio();
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
statusDiv.textContent = "Ready. Tap the button and speak.";
|
| 77 |
+
} catch (error) {
|
| 78 |
+
statusDiv.textContent = "Microphone permission denied.";
|
| 79 |
+
}
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
async function speakText(text, langCode) {
|
| 83 |
await loadVoices();
|
| 84 |
const utter = new SpeechSynthesisUtterance(text);
|
| 85 |
+
const matchingVoice = voices.find(v => v.lang === langCode);
|
| 86 |
+
if (matchingVoice) {
|
| 87 |
+
utter.voice = matchingVoice;
|
| 88 |
+
utter.lang = matchingVoice.lang;
|
| 89 |
+
} else {
|
| 90 |
+
utter.lang = langCode;
|
| 91 |
}
|
|
|
|
| 92 |
synth.speak(utter);
|
| 93 |
}
|
| 94 |
|
| 95 |
+
function startRecognition() {
|
| 96 |
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
|
| 97 |
if (!SpeechRecognition) {
|
| 98 |
+
statusDiv.textContent = "Speech recognition not supported in this browser.";
|
| 99 |
return;
|
| 100 |
}
|
| 101 |
|
| 102 |
const recognition = new SpeechRecognition();
|
| 103 |
+
const lang = languageSelector.value;
|
| 104 |
+
recognition.lang = lang;
|
| 105 |
recognition.interimResults = false;
|
| 106 |
|
| 107 |
recognition.onstart = () => {
|
| 108 |
+
statusDiv.textContent = "Listening...";
|
|
|
|
| 109 |
};
|
| 110 |
|
| 111 |
recognition.onresult = (event) => {
|
| 112 |
const transcript = event.results[0][0].transcript;
|
| 113 |
+
statusDiv.textContent = `You said: "${transcript}"`;
|
| 114 |
+
speakText(transcript, lang);
|
|
|
|
| 115 |
};
|
| 116 |
|
| 117 |
recognition.onerror = (event) => {
|
| 118 |
+
statusDiv.textContent = `Error: ${event.error}`;
|
|
|
|
| 119 |
};
|
| 120 |
|
| 121 |
recognition.onend = () => {
|
| 122 |
+
if (!synth.speaking) {
|
| 123 |
+
statusDiv.textContent += " | Tap to speak again.";
|
| 124 |
+
}
|
| 125 |
};
|
| 126 |
|
| 127 |
recognition.start();
|
| 128 |
}
|
| 129 |
|
| 130 |
+
// On page load
|
| 131 |
+
window.onload = async () => {
|
| 132 |
+
await loadVoices();
|
| 133 |
+
await askPermissions();
|
| 134 |
+
};
|
| 135 |
+
|
| 136 |
+
speakButton.addEventListener("click", startRecognition);
|
| 137 |
</script>
|
| 138 |
</body>
|
| 139 |
</html>
|