Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Update mycomponent/index.html
Browse files- mycomponent/index.html +139 -98
mycomponent/index.html
CHANGED
@@ -1,95 +1,57 @@
|
|
1 |
-
<!DOCTYPE html>
|
2 |
<html>
|
3 |
<head>
|
|
|
4 |
<style>
|
5 |
-
|
6 |
-
font-family:
|
7 |
-
padding: 20px;
|
8 |
-
|
9 |
-
|
10 |
-
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
11 |
}
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
margin-bottom: 15px;
|
17 |
-
}
|
18 |
-
|
19 |
-
button {
|
20 |
-
padding: 8px 16px;
|
21 |
-
border-radius: 5px;
|
22 |
-
border: none;
|
23 |
-
background: #2196F3;
|
24 |
-
color: white;
|
25 |
-
cursor: pointer;
|
26 |
-
transition: background 0.3s;
|
27 |
-
}
|
28 |
-
|
29 |
-
button:disabled {
|
30 |
-
background: #ccc;
|
31 |
-
cursor: not-allowed;
|
32 |
}
|
33 |
-
|
34 |
-
button:hover:not(:disabled) {
|
35 |
-
background: #1976D2;
|
36 |
-
}
|
37 |
-
|
38 |
-
#status {
|
39 |
margin: 10px 0;
|
40 |
padding: 10px;
|
41 |
-
|
42 |
-
|
43 |
}
|
44 |
-
|
45 |
#output {
|
46 |
-
|
47 |
padding: 15px;
|
48 |
-
|
49 |
-
|
|
|
50 |
min-height: 100px;
|
51 |
-
max-height:
|
52 |
overflow-y: auto;
|
53 |
-
|
|
|
|
|
54 |
}
|
55 |
</style>
|
56 |
</head>
|
57 |
<body>
|
58 |
-
|
59 |
-
|
60 |
-
<button id="start">Start Listening</button>
|
61 |
-
<button id="stop" disabled>Stop</button>
|
62 |
-
<button id="clear">Clear</button>
|
63 |
-
</div>
|
64 |
-
<div id="status">Ready</div>
|
65 |
-
<div id="output"></div>
|
66 |
-
</div>
|
67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
<script>
|
69 |
-
// Streamlit Component Initialization
|
70 |
-
function sendMessageToStreamlit(type, data) {
|
71 |
-
const outData = Object.assign({
|
72 |
-
isStreamlitMessage: true,
|
73 |
-
type: type,
|
74 |
-
}, data);
|
75 |
-
window.parent.postMessage(outData, "*");
|
76 |
-
}
|
77 |
-
|
78 |
-
function init() {
|
79 |
-
sendMessageToStreamlit("streamlit:componentReady", {apiVersion: 1});
|
80 |
-
}
|
81 |
-
|
82 |
-
function setFrameHeight(height) {
|
83 |
-
sendMessageToStreamlit("streamlit:setFrameHeight", {height: height});
|
84 |
-
}
|
85 |
-
|
86 |
-
function sendDataToStreamlit(data) {
|
87 |
-
sendMessageToStreamlit("streamlit:setComponentValue", data);
|
88 |
-
}
|
89 |
-
|
90 |
-
// Speech Recognition Setup
|
91 |
if (!('webkitSpeechRecognition' in window)) {
|
92 |
-
|
93 |
} else {
|
94 |
const recognition = new webkitSpeechRecognition();
|
95 |
const startButton = document.getElementById('start');
|
@@ -97,25 +59,33 @@
|
|
97 |
const clearButton = document.getElementById('clear');
|
98 |
const status = document.getElementById('status');
|
99 |
const output = document.getElementById('output');
|
100 |
-
|
101 |
let fullTranscript = '';
|
102 |
-
let
|
103 |
|
|
|
104 |
recognition.continuous = true;
|
105 |
recognition.interimResults = true;
|
106 |
|
107 |
-
|
|
|
108 |
try {
|
109 |
recognition.start();
|
110 |
status.textContent = 'Listening...';
|
111 |
startButton.disabled = true;
|
112 |
stopButton.disabled = false;
|
113 |
} catch (e) {
|
114 |
-
console.error(
|
115 |
status.textContent = 'Error: ' + e.message;
|
116 |
}
|
117 |
};
|
118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
stopButton.onclick = () => {
|
120 |
recognition.stop();
|
121 |
status.textContent = 'Stopped';
|
@@ -126,10 +96,9 @@
|
|
126 |
clearButton.onclick = () => {
|
127 |
fullTranscript = '';
|
128 |
output.textContent = '';
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
});
|
133 |
};
|
134 |
|
135 |
recognition.onresult = (event) => {
|
@@ -139,31 +108,36 @@
|
|
139 |
for (let i = event.resultIndex; i < event.results.length; i++) {
|
140 |
const transcript = event.results[i][0].transcript;
|
141 |
if (event.results[i].isFinal) {
|
142 |
-
finalTranscript += transcript + '
|
143 |
} else {
|
144 |
interimTranscript += transcript;
|
145 |
}
|
146 |
}
|
147 |
|
148 |
-
if (finalTranscript || (Date.now() -
|
149 |
if (finalTranscript) {
|
150 |
fullTranscript += finalTranscript;
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
}
|
156 |
-
|
157 |
-
}
|
158 |
|
159 |
output.textContent = fullTranscript + (interimTranscript ? '... ' + interimTranscript : '');
|
160 |
output.scrollTop = output.scrollHeight;
|
|
|
|
|
|
|
|
|
161 |
};
|
162 |
|
163 |
recognition.onend = () => {
|
164 |
if (!stopButton.disabled) {
|
165 |
try {
|
166 |
recognition.start();
|
|
|
167 |
} catch (e) {
|
168 |
console.error('Failed to restart recognition:', e);
|
169 |
status.textContent = 'Error restarting: ' + e.message;
|
@@ -176,20 +150,87 @@
|
|
176 |
recognition.onerror = (event) => {
|
177 |
console.error('Recognition error:', event.error);
|
178 |
status.textContent = 'Error: ' + event.error;
|
179 |
-
|
|
|
180 |
startButton.disabled = false;
|
181 |
stopButton.disabled = true;
|
182 |
}
|
183 |
};
|
184 |
}
|
185 |
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
192 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
193 |
</script>
|
194 |
-
</body>
|
195 |
-
</html>
|
|
|
|
|
|
|
|
1 |
<html>
|
2 |
<head>
|
3 |
+
<title>Continuous Speech Demo</title>
|
4 |
<style>
|
5 |
+
body {
|
6 |
+
font-family: sans-serif;
|
7 |
+
padding: 20px;
|
8 |
+
max-width: 800px;
|
9 |
+
margin: 0 auto;
|
|
|
10 |
}
|
11 |
+
button {
|
12 |
+
padding: 10px 20px;
|
13 |
+
margin: 10px 5px;
|
14 |
+
font-size: 16px;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
}
|
16 |
+
#status {
|
|
|
|
|
|
|
|
|
|
|
17 |
margin: 10px 0;
|
18 |
padding: 10px;
|
19 |
+
background: #e8f5e9;
|
20 |
+
border-radius: 4px;
|
21 |
}
|
|
|
22 |
#output {
|
23 |
+
white-space: pre-wrap;
|
24 |
padding: 15px;
|
25 |
+
background: #f5f5f5;
|
26 |
+
border-radius: 4px;
|
27 |
+
margin: 10px 0;
|
28 |
min-height: 100px;
|
29 |
+
max-height: 400px;
|
30 |
overflow-y: auto;
|
31 |
+
}
|
32 |
+
.controls {
|
33 |
+
margin: 10px 0;
|
34 |
}
|
35 |
</style>
|
36 |
</head>
|
37 |
<body>
|
38 |
+
<!-- Set up your HTML here -->
|
39 |
+
<input id="myinput" value="" />
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
+
<div class="controls">
|
42 |
+
<button id="start">Start Listening</button>
|
43 |
+
<button id="stop" disabled>Stop Listening</button>
|
44 |
+
<button id="clear">Clear Text</button>
|
45 |
+
</div>
|
46 |
+
<div id="status">Ready</div>
|
47 |
+
<div id="output"></div>
|
48 |
+
|
49 |
+
<!-- Add the hidden input here -->
|
50 |
+
<input type="hidden" id="streamlit-data" value="">
|
51 |
+
|
52 |
<script>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
if (!('webkitSpeechRecognition' in window)) {
|
54 |
+
alert('Speech recognition not supported');
|
55 |
} else {
|
56 |
const recognition = new webkitSpeechRecognition();
|
57 |
const startButton = document.getElementById('start');
|
|
|
59 |
const clearButton = document.getElementById('clear');
|
60 |
const status = document.getElementById('status');
|
61 |
const output = document.getElementById('output');
|
|
|
62 |
let fullTranscript = '';
|
63 |
+
let lastUpdateTime = Date.now();
|
64 |
|
65 |
+
// Configure recognition
|
66 |
recognition.continuous = true;
|
67 |
recognition.interimResults = true;
|
68 |
|
69 |
+
// Function to start recognition
|
70 |
+
const startRecognition = () => {
|
71 |
try {
|
72 |
recognition.start();
|
73 |
status.textContent = 'Listening...';
|
74 |
startButton.disabled = true;
|
75 |
stopButton.disabled = false;
|
76 |
} catch (e) {
|
77 |
+
console.error(e);
|
78 |
status.textContent = 'Error: ' + e.message;
|
79 |
}
|
80 |
};
|
81 |
|
82 |
+
// Auto-start on load
|
83 |
+
window.addEventListener('load', () => {
|
84 |
+
setTimeout(startRecognition, 1000);
|
85 |
+
});
|
86 |
+
|
87 |
+
startButton.onclick = startRecognition;
|
88 |
+
|
89 |
stopButton.onclick = () => {
|
90 |
recognition.stop();
|
91 |
status.textContent = 'Stopped';
|
|
|
96 |
clearButton.onclick = () => {
|
97 |
fullTranscript = '';
|
98 |
output.textContent = '';
|
99 |
+
window.parent.postMessage({
|
100 |
+
type: 'clear_transcript',
|
101 |
+
}, '*');
|
|
|
102 |
};
|
103 |
|
104 |
recognition.onresult = (event) => {
|
|
|
108 |
for (let i = event.resultIndex; i < event.results.length; i++) {
|
109 |
const transcript = event.results[i][0].transcript;
|
110 |
if (event.results[i].isFinal) {
|
111 |
+
finalTranscript += transcript + '\\n';
|
112 |
} else {
|
113 |
interimTranscript += transcript;
|
114 |
}
|
115 |
}
|
116 |
|
117 |
+
if (finalTranscript || (Date.now() - lastUpdateTime > 5000)) {
|
118 |
if (finalTranscript) {
|
119 |
fullTranscript += finalTranscript;
|
120 |
+
|
121 |
+
// Update the hidden input value
|
122 |
+
document.getElementById('streamlit-data').value = fullTranscript;
|
123 |
+
|
124 |
}
|
125 |
+
lastUpdateTime = Date.now();
|
126 |
+
}
|
127 |
|
128 |
output.textContent = fullTranscript + (interimTranscript ? '... ' + interimTranscript : '');
|
129 |
output.scrollTop = output.scrollHeight;
|
130 |
+
|
131 |
+
document.getElementById('streamlit-data').value = fullTranscript;
|
132 |
+
sendDataToPython({value: fullTranscript,dataType: "json",});
|
133 |
+
|
134 |
};
|
135 |
|
136 |
recognition.onend = () => {
|
137 |
if (!stopButton.disabled) {
|
138 |
try {
|
139 |
recognition.start();
|
140 |
+
console.log('Restarted recognition');
|
141 |
} catch (e) {
|
142 |
console.error('Failed to restart recognition:', e);
|
143 |
status.textContent = 'Error restarting: ' + e.message;
|
|
|
150 |
recognition.onerror = (event) => {
|
151 |
console.error('Recognition error:', event.error);
|
152 |
status.textContent = 'Error: ' + event.error;
|
153 |
+
|
154 |
+
if (event.error === 'not-allowed' || event.error === 'service-not-allowed') {
|
155 |
startButton.disabled = false;
|
156 |
stopButton.disabled = true;
|
157 |
}
|
158 |
};
|
159 |
}
|
160 |
|
161 |
+
|
162 |
+
// ----------------------------------------------------
|
163 |
+
// Just copy/paste these functions as-is:
|
164 |
+
|
165 |
+
function sendMessageToStreamlitClient(type, data) {
|
166 |
+
var outData = Object.assign({
|
167 |
+
isStreamlitMessage: true,
|
168 |
+
type: type,
|
169 |
+
}, data);
|
170 |
+
window.parent.postMessage(outData, "*");
|
171 |
+
}
|
172 |
+
|
173 |
+
function init() {
|
174 |
+
sendMessageToStreamlitClient("streamlit:componentReady", {apiVersion: 1});
|
175 |
+
}
|
176 |
+
|
177 |
+
function setFrameHeight(height) {
|
178 |
+
sendMessageToStreamlitClient("streamlit:setFrameHeight", {height: height});
|
179 |
+
}
|
180 |
+
|
181 |
+
// The `data` argument can be any JSON-serializable value.
|
182 |
+
function sendDataToPython(data) {
|
183 |
+
sendMessageToStreamlitClient("streamlit:setComponentValue", data);
|
184 |
+
}
|
185 |
+
|
186 |
+
// ----------------------------------------------------
|
187 |
+
// Now modify this part of the code to fit your needs:
|
188 |
+
|
189 |
+
var myInput = document.getElementById("myinput");
|
190 |
+
var myOutput = document.getElementById("output");
|
191 |
+
|
192 |
+
// data is any JSON-serializable value you sent from Python,
|
193 |
+
// and it's already deserialized for you.
|
194 |
+
function onDataFromPython(event) {
|
195 |
+
if (event.data.type !== "streamlit:render") return;
|
196 |
+
myInput.value = event.data.args.my_input_value; // Access values sent from Python here!
|
197 |
+
}
|
198 |
+
|
199 |
+
myInput.addEventListener("change", function() {
|
200 |
+
sendDataToPython({
|
201 |
+
value: myInput.value,
|
202 |
+
dataType: "json",
|
203 |
+
});
|
204 |
+
})
|
205 |
+
|
206 |
+
myOutput.addEventListener("change", function() {
|
207 |
+
sendDataToPython({
|
208 |
+
value: myOutput.value,
|
209 |
+
dataType: "json",
|
210 |
});
|
211 |
+
})
|
212 |
+
|
213 |
+
// Hook things up!
|
214 |
+
window.addEventListener("message", onDataFromPython);
|
215 |
+
init();
|
216 |
+
|
217 |
+
// Hack to autoset the iframe height.
|
218 |
+
window.addEventListener("load", function() {
|
219 |
+
window.setTimeout(function() {
|
220 |
+
setFrameHeight(document.documentElement.clientHeight)
|
221 |
+
}, 0);
|
222 |
+
});
|
223 |
+
|
224 |
+
// Optionally, if the automatic height computation fails you, give this component a height manually
|
225 |
+
// by commenting out below:
|
226 |
+
//setFrameHeight(200);
|
227 |
+
|
228 |
+
|
229 |
+
|
230 |
+
|
231 |
+
|
232 |
</script>
|
233 |
+
</body>
|
234 |
+
</html>
|
235 |
+
|
236 |
+
|