1
votes

I have a working nodejs program in GCP that using GCP Speech to Text API

async function main() {
  // Imports the Google Cloud client library
  const speech = require('@google-cloud/speech');
  const fs = require('fs');

  // Creates a client
  const client = new speech.SpeechClient();

  const fileName = './resource/amazing30s_mono.flac';  // mono clip
  // Reads a local audio file and converts it to base64 
  const file = fs.readFileSync(fileName);
  const audioBytes = file.toString('base64');

  // The audio file's encoding, sample rate in hertz, and BCP-47 language code
  const audio = {
    content: audioBytes,
  };
  const config = {
    languageCode: 'en-US',  // Malay try 'ms-MY' (https://cloud.google.com/speech-to-text/docs/languages)
    enableAutomaticPunctuation: true, // https://cloud.google.com/speech-to-text/docs/automatic-punctuation
  };
  const request = {
    audio: audio,
    config: config,
  };

  // Detects speech in the audio file
  const [response] = await client.recognize(request);
  const transcription = response.results
    .map(result => result.alternatives[0].transcript)
    .join('\n');
  console.log(`Transcription: ${transcription}`);
}
main().catch(console.error);

And I have a working TypeScript Firebase Cloud Func

import * as functions from 'firebase-functions';

export const reqWithParam = functions.https.onRequest((request, response) => {
  let name = request.query.name
  response.send("Request with name= " + name);
});

But how could I integrate both programs up? Or how could I bring the nodejs code into my Firebase Cloud function to make my Firebase cloud function trigger Google API to convert speech to text?

Both programs above are under the same GCP project, same server (Google cloud shell)

I have read through the doc, but there are still missing gaps

  • How to 'Import' a node program
  • How to get the GS path of the audio file
  • How to validate token (same GCP project)
  • Any YouTube tutorial showing detail steps in 'calling GCP API via Firebase'
1

1 Answers

1
votes

i was surprised when i noticed nodejs code can just paste into Typescript without any error! i managed to run google API flawlessly in Firebase

export const GCPSpeech2Text = functions.https.onRequest(async (req, res) => {

    const speech = require('@google-cloud/speech');
    // Creates a client
    const client = new speech.SpeechClient();
    const gcsUri = 'gs://<project-name>.appspot.com/<file path...>/myaudio.flac'

    const encoding = 'FLAC';
    const languageCode = 'en-US';

    const config = {
        encoding: encoding,
        languageCode: languageCode,
        audioChannelCount: 2, // hit 'Invalid audio channel count' if not specify
        enableAutomaticPunctuation: true, // https://cloud.google.com/speech-to-text/docs/automatic-punctuation
    };

    const audio = {
        uri: gcsUri,
    };

    const request = {
        audio: audio,
        config: config,
    };

    // Detects speech in the audio file
    const [response] = await client.recognize(request);
    const transcription = response.results
        .map((result: any) => result.alternatives[0].transcript)
        .join('\n');
    res.send(`Transcription ${transcription} created.`);
})

Yeah!