srivatsavdamaraju commited on
Commit
ea69ea7
·
verified ·
1 Parent(s): 4965c2d

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +80 -22
index.html CHANGED
@@ -55,6 +55,14 @@
55
  color: red;
56
  text-align: center;
57
  }
 
 
 
 
 
 
 
 
58
  </style>
59
  </head>
60
  <body>
@@ -70,8 +78,13 @@
70
  <h3>Recognition Result:</h3>
71
  <p id="result"></p>
72
  </div>
 
 
 
 
73
  <div>
74
  <button onclick="requestMicrophonePermission()">Start Listening</button>
 
75
  </div>
76
  </div>
77
  </div>
@@ -80,20 +93,29 @@
80
  <script>
81
  let recognition = null;
82
  let silenceTimeout = null;
 
83
 
84
  async function query(data) {
85
- const response = await fetch(
86
- "https://srivatsavdamaraju-flowise.hf.space/api/v1/prediction/2875301a-c26f-4bd5-ab10-71fa13393541",
87
- {
88
- method: "POST",
89
- headers: {
90
- "Content-Type": "application/json"
91
- },
92
- body: JSON.stringify(data)
93
- }
94
- );
95
- const result = await response.json();
96
- return result;
 
 
 
 
 
 
 
 
97
  }
98
 
99
  function displayStatus(message, isError = false) {
@@ -120,6 +142,13 @@
120
  return newRecognition;
121
  }
122
 
 
 
 
 
 
 
 
123
  function startCommandRecognition() {
124
  try {
125
  if (recognition) {
@@ -130,25 +159,53 @@
130
  if (!recognition) return;
131
 
132
  displayStatus('Listening for command...');
 
 
133
 
134
  recognition.onresult = async function(event) {
135
  clearTimeout(silenceTimeout);
136
- const result = event.results[event.results.length - 1][0].transcript.toLowerCase();
137
- document.getElementById('result').innerText = result;
138
-
139
- silenceTimeout = setTimeout(async () => {
140
- const apiResponse = await query({"question": result});
141
- console.log(apiResponse);
142
- }, 2000);
 
 
 
 
 
 
 
 
143
  };
144
 
145
  recognition.onerror = function(event) {
146
  displayStatus(`Recognition Error: ${event.error}`, true);
147
  };
148
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149
  recognition.start();
150
  } catch (error) {
151
- displayStatus('Failed to start command recognition', true);
152
  }
153
  }
154
 
@@ -164,8 +221,9 @@
164
  }
165
 
166
  window.onload = function() {
167
- requestMicrophonePermission();
 
168
  };
169
  </script>
170
  </body>
171
- </html>
 
55
  color: red;
56
  text-align: center;
57
  }
58
+ .response-container {
59
+ margin-top: 20px;
60
+ background-color: rgba(10, 116, 218, 0.1);
61
+ padding: 10px;
62
+ border-radius: 5px;
63
+ max-height: 200px;
64
+ overflow-y: auto;
65
+ }
66
  </style>
67
  </head>
68
  <body>
 
78
  <h3>Recognition Result:</h3>
79
  <p id="result"></p>
80
  </div>
81
+ <div class="response-container">
82
+ <h3>API Response:</h3>
83
+ <p id="api-response"></p>
84
+ </div>
85
  <div>
86
  <button onclick="requestMicrophonePermission()">Start Listening</button>
87
+ <button onclick="stopListening()">Stop Listening</button>
88
  </div>
89
  </div>
90
  </div>
 
93
  <script>
94
  let recognition = null;
95
  let silenceTimeout = null;
96
+ let isFinalResult = false;
97
 
98
  async function query(data) {
99
+ displayStatus("Sending to API...");
100
+ try {
101
+ const response = await fetch(
102
+ "https://srivatsavdamaraju-flowise.hf.space/api/v1/prediction/2875301a-c26f-4bd5-ab10-71fa13393541",
103
+ {
104
+ method: "POST",
105
+ headers: {
106
+ "Content-Type": "application/json"
107
+ },
108
+ body: JSON.stringify(data)
109
+ }
110
+ );
111
+ const result = await response.json();
112
+ document.getElementById('api-response').innerText = JSON.stringify(result, null, 2);
113
+ displayStatus("API response received");
114
+ return result;
115
+ } catch (error) {
116
+ displayStatus(`API Error: ${error.message}`, true);
117
+ return null;
118
+ }
119
  }
120
 
121
  function displayStatus(message, isError = false) {
 
142
  return newRecognition;
143
  }
144
 
145
+ function stopListening() {
146
+ if (recognition) {
147
+ recognition.stop();
148
+ displayStatus('Listening stopped');
149
+ }
150
+ }
151
+
152
  function startCommandRecognition() {
153
  try {
154
  if (recognition) {
 
159
  if (!recognition) return;
160
 
161
  displayStatus('Listening for command...');
162
+ document.getElementById('result').innerText = '';
163
+ document.getElementById('api-response').innerText = '';
164
 
165
  recognition.onresult = async function(event) {
166
  clearTimeout(silenceTimeout);
167
+ const lastResult = event.results[event.results.length - 1];
168
+ const transcript = lastResult[0].transcript.trim();
169
+ isFinalResult = lastResult.isFinal;
170
+
171
+ document.getElementById('result').innerText = transcript;
172
+
173
+ // If it's a final result or we've stopped getting input for a while, send to API
174
+ if (isFinalResult) {
175
+ await query({"question": transcript});
176
+ } else {
177
+ // Set a timeout for silence detection
178
+ silenceTimeout = setTimeout(async () => {
179
+ await query({"question": transcript});
180
+ }, 2000);
181
+ }
182
  };
183
 
184
  recognition.onerror = function(event) {
185
  displayStatus(`Recognition Error: ${event.error}`, true);
186
  };
187
 
188
+ recognition.onend = function() {
189
+ // If recognition ended but we have a transcript that wasn't sent, send it now
190
+ const transcript = document.getElementById('result').innerText;
191
+ if (transcript && !isFinalResult) {
192
+ query({"question": transcript});
193
+ }
194
+
195
+ displayStatus('Recognition ended');
196
+ };
197
+
198
+ recognition.onspeechend = function() {
199
+ // When speech ends, send the final transcript to the API
200
+ const transcript = document.getElementById('result').innerText;
201
+ if (transcript) {
202
+ query({"question": transcript});
203
+ }
204
+ };
205
+
206
  recognition.start();
207
  } catch (error) {
208
+ displayStatus(`Failed to start command recognition: ${error.message}`, true);
209
  }
210
  }
211
 
 
221
  }
222
 
223
  window.onload = function() {
224
+ // Don't request permission automatically - wait for user to click button
225
+ displayStatus('Ready - Click "Start Listening" to begin');
226
  };
227
  </script>
228
  </body>
229
+ </html>