awacke1 commited on
Commit
37aa882
·
verified ·
1 Parent(s): ea53bab

Create index.html

Browse files
Files changed (1) hide show
  1. mycomponent/index.html +215 -0
mycomponent/index.html ADDED
@@ -0,0 +1,215 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Speech Recognition Component</title>
5
+ <style>
6
+ body {
7
+ font-family: system-ui, -apple-system, sans-serif;
8
+ padding: 1rem;
9
+ max-width: 800px;
10
+ margin: 0 auto;
11
+ background: #f8f9fa;
12
+ }
13
+ .container {
14
+ background: white;
15
+ padding: 1.5rem;
16
+ border-radius: 8px;
17
+ box-shadow: 0 2px 4px rgba(0,0,0,0.1);
18
+ }
19
+ button {
20
+ padding: 0.5rem 1rem;
21
+ margin: 0.5rem;
22
+ font-size: 1rem;
23
+ border: none;
24
+ border-radius: 4px;
25
+ cursor: pointer;
26
+ transition: all 0.2s;
27
+ }
28
+ button:disabled {
29
+ opacity: 0.5;
30
+ cursor: not-allowed;
31
+ }
32
+ #start { background: #28a745; color: white; }
33
+ #stop { background: #dc3545; color: white; }
34
+ #clear { background: #6c757d; color: white; }
35
+ #status {
36
+ margin: 1rem 0;
37
+ padding: 0.75rem;
38
+ border-radius: 4px;
39
+ background: #e9ecef;
40
+ }
41
+ #output {
42
+ white-space: pre-wrap;
43
+ padding: 1rem;
44
+ background: #f8f9fa;
45
+ border-radius: 4px;
46
+ border: 1px solid #dee2e6;
47
+ margin: 1rem 0;
48
+ min-height: 100px;
49
+ max-height: 400px;
50
+ overflow-y: auto;
51
+ }
52
+ </style>
53
+ </head>
54
+ <body>
55
+ <div class="container">
56
+ <input id="myinput" type="hidden" value="" />
57
+ <div class="controls">
58
+ <button id="start">Start Listening</button>
59
+ <button id="stop" disabled>Stop Listening</button>
60
+ <button id="clear">Clear Text</button>
61
+ </div>
62
+ <div id="status">Ready to start speech recognition...</div>
63
+ <div id="output"></div>
64
+ </div>
65
+
66
+ <script>
67
+ // Check for speech recognition support
68
+ if (!('webkitSpeechRecognition' in window)) {
69
+ document.getElementById('status').textContent = 'Speech recognition not supported in this browser';
70
+ } else {
71
+ const recognition = new webkitSpeechRecognition();
72
+ const startButton = document.getElementById('start');
73
+ const stopButton = document.getElementById('stop');
74
+ const clearButton = document.getElementById('clear');
75
+ const status = document.getElementById('status');
76
+ const output = document.getElementById('output');
77
+ let fullTranscript = '';
78
+ let lastUpdateTime = Date.now();
79
+
80
+ // Configure recognition
81
+ recognition.continuous = true;
82
+ recognition.interimResults = true;
83
+
84
+ // Start recognition handler
85
+ const startRecognition = () => {
86
+ try {
87
+ recognition.start();
88
+ status.textContent = 'Listening...';
89
+ startButton.disabled = true;
90
+ stopButton.disabled = false;
91
+ } catch (e) {
92
+ console.error(e);
93
+ status.textContent = 'Error starting recognition: ' + e.message;
94
+ }
95
+ };
96
+
97
+ // Auto-start on load
98
+ window.addEventListener('load', () => {
99
+ setTimeout(startRecognition, 1000);
100
+ });
101
+
102
+ // Button handlers
103
+ startButton.onclick = startRecognition;
104
+
105
+ stopButton.onclick = () => {
106
+ recognition.stop();
107
+ status.textContent = 'Stopped listening';
108
+ startButton.disabled = false;
109
+ stopButton.disabled = true;
110
+ };
111
+
112
+ clearButton.onclick = () => {
113
+ fullTranscript = '';
114
+ output.textContent = '';
115
+ sendDataToPython({
116
+ value: '',
117
+ dataType: 'json'
118
+ });
119
+ };
120
+
121
+ // Recognition result handler
122
+ recognition.onresult = (event) => {
123
+ let interimTranscript = '';
124
+ let finalTranscript = '';
125
+
126
+ for (let i = event.resultIndex; i < event.results.length; i++) {
127
+ const transcript = event.results[i][0].transcript;
128
+ if (event.results[i].isFinal) {
129
+ finalTranscript += transcript + '\\n';
130
+ } else {
131
+ interimTranscript += transcript;
132
+ }
133
+ }
134
+
135
+ if (finalTranscript || (Date.now() - lastUpdateTime > 5000)) {
136
+ if (finalTranscript) {
137
+ fullTranscript += finalTranscript;
138
+ }
139
+ lastUpdateTime = Date.now();
140
+ }
141
+
142
+ output.textContent = fullTranscript + (interimTranscript ? '... ' + interimTranscript : '');
143
+ output.scrollTop = output.scrollHeight;
144
+
145
+ // Send to Streamlit
146
+ sendDataToPython({
147
+ value: fullTranscript,
148
+ dataType: 'json'
149
+ });
150
+ };
151
+
152
+ // Auto-restart recognition
153
+ recognition.onend = () => {
154
+ if (!stopButton.disabled) {
155
+ try {
156
+ recognition.start();
157
+ console.log('Restarted recognition');
158
+ } catch (e) {
159
+ console.error('Failed to restart:', e);
160
+ status.textContent = 'Error restarting: ' + e.message;
161
+ startButton.disabled = false;
162
+ stopButton.disabled = true;
163
+ }
164
+ }
165
+ };
166
+
167
+ recognition.onerror = (event) => {
168
+ console.error('Recognition error:', event.error);
169
+ status.textContent = 'Error: ' + event.error;
170
+ if (event.error === 'not-allowed') {
171
+ startButton.disabled = false;
172
+ stopButton.disabled = true;
173
+ }
174
+ };
175
+ }
176
+
177
+ // Streamlit Component Functions
178
+ function sendMessageToStreamlitClient(type, data) {
179
+ var outData = Object.assign({
180
+ isStreamlitMessage: true,
181
+ type: type,
182
+ }, data);
183
+ window.parent.postMessage(outData, "*");
184
+ }
185
+
186
+ function init() {
187
+ sendMessageToStreamlitClient("streamlit:componentReady", {apiVersion: 1});
188
+ }
189
+
190
+ function setFrameHeight(height) {
191
+ sendMessageToStreamlitClient("streamlit:setFrameHeight", {height: height});
192
+ }
193
+
194
+ function sendDataToPython(data) {
195
+ sendMessageToStreamlitClient("streamlit:setComponentValue", data);
196
+ }
197
+
198
+ // Setup event listeners
199
+ window.addEventListener("message", (event) => {
200
+ if (event.data.type !== "streamlit:render") return;
201
+ // Handle any incoming data from Python if needed
202
+ });
203
+
204
+ // Initialize
205
+ init();
206
+
207
+ // Set frame height
208
+ window.addEventListener("load", () => {
209
+ setTimeout(() => {
210
+ setFrameHeight(document.documentElement.clientHeight);
211
+ }, 0);
212
+ });
213
+ </script>
214
+ </body>
215
+ </html>