Skip to content

Instantly share code, notes, and snippets.

@alexander-fenster
Last active March 6, 2021 23:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alexander-fenster/5ee5f04423ede10f008ae59e4b10d9c4 to your computer and use it in GitHub Desktop.
Save alexander-fenster/5ee5f04423ede10f008ae59e4b10d9c4 to your computer and use it in GitHub Desktop.
Google Cloud Speech-To-Text with API key
<!--
Copyright 2020 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<html>
<head>
<script src="./dist/speech.js"></script>
<title>Google Cloud Speech-To-Text in browser with API Key</title>
</head>
<body>
<h1>Google Cloud Speech-To-Text in browser with API Key</h1>
<script>
async function process(file) {
const div = document.getElementById('result');
try {
const arrayBuffer = await file.arrayBuffer();
const uint8Array = new Uint8Array(arrayBuffer);
const recognitionResult = await Speech.recognize(uint8Array);
div.innerHTML = recognitionResult.results[0].alternatives[0].transcript;
}
catch (err) {
div.innerHTML = 'Error: ' + err.toString();
}
}
</script>
<!-- Speech input example from https://developers.google.com/web/fundamentals/media/recording-audio#start_simple_and_progressively -->
<input type="file" accept="audio/*" capture id="recorder">
<audio id="player" controls></audio>
<script>
const recorder = document.getElementById('recorder');
const player = document.getElementById('player');
recorder.addEventListener('change', function(e) {
const file = e.target.files[0];
const url = URL.createObjectURL(file);
player.src = url;
process(file);
});
</script>
<div id="result"><i>Recognition result will appear here</i></div>
</body>
</html>
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Never use API keys in applications where users can access the source code
// (e.g. in websites). Use OAuth2Client instead. This example is for specific
// use case (kiosk where users have no source access).
// Details here: https://github.com/googleapis/nodejs-speech/issues/547
// For more information about API keys, please visit
// https://cloud.google.com/docs/authentication/api-keys
const apiKey = 'REDACTED';
const speech = require('@google-cloud/speech');
const {GoogleAuth} = require('google-auth-library');
const googleAuth = new GoogleAuth();
const auth = googleAuth.fromAPIKey(apiKey);
const client = new speech.v1p1beta1.SpeechClient({auth});
async function recognize(content) {
const audio = {
content
};
const config = {
encoding: 'LINEAR16',
languageCode: 'en-US',
};
const request = {
audio,
config,
};
console.log(request);
const [response] = await client.recognize(request);
return response;
}
module.exports = {recognize};
{
"dependencies": {
"@google-cloud/speech": "^3.6.0",
"google-auth-library": "^6.0.0"
},
"devDependencies": {
"null-loader": "^4.0.0",
"webpack": "^4.42.1",
"webpack-cli": "^3.3.11"
}
}
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
const path = require('path');
module.exports = {
entry: './index.js',
output: {
library: 'Speech',
filename: './speech.js',
},
node: {
child_process: 'empty',
fs: 'empty',
crypto: 'empty',
},
resolve: {
extensions: ['.js', '.json'],
},
module: {
rules: [
{
test: /node_modules[\\/]@grpc[\\/]grpc-js/,
use: 'null-loader',
},
{
test: /node_modules[\\/]grpc/,
use: 'null-loader',
},
{
test: /node_modules[\\/]retry-request/,
use: 'null-loader',
},
{
test: /node_modules[\\/]https?-proxy-agent/,
use: 'null-loader',
},
{
test: /node_modules[\\/]gtoken/,
use: 'null-loader',
},
],
},
mode: 'production',
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment