Spaces:
Running
Running
Update index.html
Browse files- index.html +45 -246
index.html
CHANGED
@@ -1,258 +1,57 @@
|
|
1 |
<!DOCTYPE html>
|
2 |
-
<html
|
3 |
<head>
|
4 |
-
<
|
5 |
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
-
<title>Voice Assistant Demo</title>
|
7 |
<style>
|
8 |
-
body {
|
9 |
-
|
10 |
-
|
11 |
-
margin: 0 auto;
|
12 |
-
padding: 20px;
|
13 |
-
background: #f0f0f0;
|
14 |
-
}
|
15 |
-
.container {
|
16 |
-
background: white;
|
17 |
-
padding: 20px;
|
18 |
-
border-radius: 8px;
|
19 |
-
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
20 |
-
}
|
21 |
-
.status {
|
22 |
-
margin: 20px 0;
|
23 |
-
padding: 10px;
|
24 |
-
border-radius: 4px;
|
25 |
-
background: #e8f5e9;
|
26 |
-
}
|
27 |
-
.transcript {
|
28 |
-
margin: 20px 0;
|
29 |
-
padding: 15px;
|
30 |
-
background: #f5f5f5;
|
31 |
-
border-radius: 4px;
|
32 |
-
min-height: 50px;
|
33 |
-
}
|
34 |
-
button {
|
35 |
-
background: #2196F3;
|
36 |
-
color: white;
|
37 |
-
border: none;
|
38 |
-
padding: 10px 20px;
|
39 |
-
border-radius: 4px;
|
40 |
-
cursor: pointer;
|
41 |
-
font-size: 16px;
|
42 |
-
}
|
43 |
-
button:hover { background: #1976D2; }
|
44 |
-
button:disabled {
|
45 |
-
background: #ccc;
|
46 |
-
cursor: not-allowed;
|
47 |
-
}
|
48 |
-
.controls {
|
49 |
-
display: flex;
|
50 |
-
gap: 10px;
|
51 |
-
margin: 20px 0;
|
52 |
-
}
|
53 |
-
.voice-settings {
|
54 |
-
margin: 20px 0;
|
55 |
-
padding: 15px;
|
56 |
-
background: #f8f9fa;
|
57 |
-
border-radius: 4px;
|
58 |
-
}
|
59 |
-
.setting-group { margin: 10px 0; }
|
60 |
-
label {
|
61 |
-
display: inline-block;
|
62 |
-
width: 100px;
|
63 |
-
}
|
64 |
-
.error {
|
65 |
-
background: #ffebee;
|
66 |
-
color: #c62828;
|
67 |
-
padding: 10px;
|
68 |
-
border-radius: 4px;
|
69 |
-
margin: 10px 0;
|
70 |
-
display: none;
|
71 |
-
}
|
72 |
</style>
|
73 |
</head>
|
74 |
<body>
|
75 |
-
<
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
<li>"Hello" - The assistant will greet you back</li>
|
80 |
-
<li>"What time is it?" - Get the current time</li>
|
81 |
-
<li>"Tell me a fact" - Hear an interesting fact</li>
|
82 |
-
</ul>
|
83 |
-
|
84 |
-
<div class="controls">
|
85 |
-
<button id="startBtn">Start Listening</button>
|
86 |
-
<button id="stopBtn" disabled>Stop Listening</button>
|
87 |
-
</div>
|
88 |
-
|
89 |
-
<div class="error" id="error"></div>
|
90 |
-
<div class="status" id="status">Status: Ready</div>
|
91 |
-
<div class="transcript" id="transcript"></div>
|
92 |
-
|
93 |
-
<div class="voice-settings">
|
94 |
-
<h3>Voice Settings</h3>
|
95 |
-
<div class="setting-group">
|
96 |
-
<label for="voice">Voice:</label>
|
97 |
-
<select id="voice"></select>
|
98 |
-
</div>
|
99 |
-
<div class="setting-group">
|
100 |
-
<label for="rate">Rate:</label>
|
101 |
-
<input type="range" id="rate" min="0.5" max="2" value="1" step="0.1">
|
102 |
-
<span id="rateValue">1</span>
|
103 |
-
</div>
|
104 |
-
<div class="setting-group">
|
105 |
-
<label for="pitch">Pitch:</label>
|
106 |
-
<input type="range" id="pitch" min="0.5" max="2" value="1" step="0.1">
|
107 |
-
<span id="pitchValue">1</span>
|
108 |
-
</div>
|
109 |
-
</div>
|
110 |
-
</div>
|
111 |
|
112 |
<script>
|
113 |
-
if (!('webkitSpeechRecognition' in window)
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
}
|
149 |
-
}
|
150 |
-
|
151 |
-
populateVoices();
|
152 |
-
if (speechSynthesis.onvoiceschanged !== undefined) {
|
153 |
-
speechSynthesis.onvoiceschanged = populateVoices;
|
154 |
-
}
|
155 |
-
|
156 |
-
function speak(text) {
|
157 |
-
if (synth.speaking) {
|
158 |
-
console.error('speechSynthesis.speaking');
|
159 |
-
return;
|
160 |
-
}
|
161 |
-
const utterance = new SpeechSynthesisUtterance(text);
|
162 |
-
const selectedVoice = voices[voiceSelect.selectedIndex];
|
163 |
-
utterance.voice = selectedVoice;
|
164 |
-
utterance.rate = rate.value;
|
165 |
-
utterance.pitch = pitch.value;
|
166 |
-
synth.speak(utterance);
|
167 |
-
}
|
168 |
-
|
169 |
-
function handleCommand(command) {
|
170 |
-
command = command.toLowerCase();
|
171 |
-
let response = '';
|
172 |
-
|
173 |
-
if (command.includes('hello')) {
|
174 |
-
response = 'Hello! How can I help you today?';
|
175 |
-
} else if (command.includes('what time')) {
|
176 |
-
const now = new Date();
|
177 |
-
response = `The current time is ${now.toLocaleTimeString()}`;
|
178 |
-
} else if (command.includes('tell me a fact')) {
|
179 |
-
const facts = [
|
180 |
-
'The shortest war in history was between Britain and Zanzibar on August 27, 1896. Zanzibar surrendered after just 38 minutes.',
|
181 |
-
'Honey never spoils. Archaeologists have found pots of honey in ancient Egyptian tombs that are over 3,000 years old and still perfectly good to eat.',
|
182 |
-
'The first oranges weren't orange. The original oranges from Southeast Asia were actually green.'
|
183 |
-
];
|
184 |
-
response = facts[Math.floor(Math.random() * facts.length)];
|
185 |
-
} else {
|
186 |
-
response = "I'm sorry, I didn't understand that command. Please try again.";
|
187 |
-
}
|
188 |
-
|
189 |
-
transcript.textContent = `You said: ${command}\nAssistant: ${response}`;
|
190 |
-
speak(response);
|
191 |
}
|
192 |
-
|
193 |
-
startBtn.addEventListener('click', () => {
|
194 |
-
errorDiv.style.display = 'none';
|
195 |
-
try {
|
196 |
-
recognition.start();
|
197 |
-
startBtn.disabled = true;
|
198 |
-
stopBtn.disabled = false;
|
199 |
-
status.textContent = 'Status: Listening...';
|
200 |
-
console.log('Recognition started');
|
201 |
-
} catch (err) {
|
202 |
-
console.error('Recognition error:', err);
|
203 |
-
errorDiv.style.display = 'block';
|
204 |
-
errorDiv.textContent = `Error starting recognition: ${err.message}`;
|
205 |
-
}
|
206 |
-
});
|
207 |
-
|
208 |
-
stopBtn.addEventListener('click', () => {
|
209 |
-
try {
|
210 |
-
recognition.stop();
|
211 |
-
startBtn.disabled = false;
|
212 |
-
stopBtn.disabled = true;
|
213 |
-
status.textContent = 'Status: Stopped';
|
214 |
-
} catch (err) {
|
215 |
-
console.error('Stop error:', err);
|
216 |
-
errorDiv.style.display = 'block';
|
217 |
-
errorDiv.textContent = `Error stopping recognition: ${err.message}`;
|
218 |
-
}
|
219 |
-
});
|
220 |
-
|
221 |
-
recognition.onstart = () => {
|
222 |
-
console.log('Recognition started');
|
223 |
-
status.textContent = 'Status: Listening...';
|
224 |
-
};
|
225 |
-
|
226 |
-
recognition.onresult = (event) => {
|
227 |
-
console.log('Recognition result received');
|
228 |
-
const command = event.results[0][0].transcript;
|
229 |
-
handleCommand(command);
|
230 |
-
};
|
231 |
-
|
232 |
-
recognition.onend = () => {
|
233 |
-
console.log('Recognition ended');
|
234 |
-
startBtn.disabled = false;
|
235 |
-
stopBtn.disabled = true;
|
236 |
-
status.textContent = 'Status: Ready';
|
237 |
-
};
|
238 |
-
|
239 |
-
recognition.onerror = (event) => {
|
240 |
-
console.error('Recognition error:', event.error);
|
241 |
-
errorDiv.style.display = 'block';
|
242 |
-
errorDiv.textContent = `Error: ${event.error}. ${event.error === 'not-allowed' ? 'Please ensure microphone permissions are granted.' : ''}`;
|
243 |
-
status.textContent = 'Status: Error';
|
244 |
-
startBtn.disabled = false;
|
245 |
-
stopBtn.disabled = true;
|
246 |
-
};
|
247 |
-
|
248 |
-
rate.addEventListener('input', () => {
|
249 |
-
rateValue.textContent = rate.value;
|
250 |
-
});
|
251 |
-
|
252 |
-
pitch.addEventListener('input', () => {
|
253 |
-
pitchValue.textContent = pitch.value;
|
254 |
-
});
|
255 |
</script>
|
256 |
-
</div>
|
257 |
</body>
|
258 |
</html>
|
|
|
1 |
<!DOCTYPE html>
|
2 |
+
<html>
|
3 |
<head>
|
4 |
+
<title>Simple Speech Demo</title>
|
|
|
|
|
5 |
<style>
|
6 |
+
body { font-family: sans-serif; padding: 20px; }
|
7 |
+
button { padding: 10px 20px; margin: 10px 0; }
|
8 |
+
#status { margin: 10px 0; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
</style>
|
10 |
</head>
|
11 |
<body>
|
12 |
+
<h1>Simple Speech Demo</h1>
|
13 |
+
<button id="start">Start Listening</button>
|
14 |
+
<div id="status">Ready</div>
|
15 |
+
<div id="output"></div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
<script>
|
18 |
+
if (!('webkitSpeechRecognition' in window)) {
|
19 |
+
alert('Speech recognition not supported');
|
20 |
+
} else {
|
21 |
+
const recognition = new webkitSpeechRecognition();
|
22 |
+
const startButton = document.getElementById('start');
|
23 |
+
const status = document.getElementById('status');
|
24 |
+
const output = document.getElementById('output');
|
25 |
+
|
26 |
+
recognition.continuous = false;
|
27 |
+
recognition.interimResults = false;
|
28 |
+
|
29 |
+
startButton.onclick = () => {
|
30 |
+
try {
|
31 |
+
recognition.start();
|
32 |
+
status.textContent = 'Listening...';
|
33 |
+
startButton.disabled = true;
|
34 |
+
} catch (e) {
|
35 |
+
console.error(e);
|
36 |
+
status.textContent = 'Error: ' + e.message;
|
37 |
+
}
|
38 |
+
};
|
39 |
+
|
40 |
+
recognition.onresult = (event) => {
|
41 |
+
const text = event.results[0][0].transcript;
|
42 |
+
output.textContent = 'You said: ' + text;
|
43 |
+
};
|
44 |
+
|
45 |
+
recognition.onend = () => {
|
46 |
+
status.textContent = 'Ready';
|
47 |
+
startButton.disabled = false;
|
48 |
+
};
|
49 |
+
|
50 |
+
recognition.onerror = (event) => {
|
51 |
+
status.textContent = 'Error: ' + event.error;
|
52 |
+
startButton.disabled = false;
|
53 |
+
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
</script>
|
|
|
56 |
</body>
|
57 |
</html>
|