如何在 Angular 上使用 Google Speech-To-Text 作为流数据

问题描述 投票:0回答:1

我在我的node.js firebase函数上通过@google-cloud/speech库使用谷歌语音到文本服务,但是我想将它用作流数据,据我所知,在firebase上不可能实现这一点功能。

所以我决定在客户端这样做,这对我的案例来说是有角度的。尽管如此,我找不到任何有用的文档。

我想知道是否有可能以安全的方式在 Angular 上使用谷歌语音转文本服务。

这是我在node.js上的函数;

import {SpeechClient} from "@google-cloud/speech";

    export const transcriptAudio = onCall(async (data) => {
      let userId = data.auth?.token?.uid;
      if(!userId)
        throw new HttpsError('unauthenticated', 'The user is not authenticated.');
  
      let audioByte = data.data.audioByte as string;
    
      // Detects speech in the audio file
      const client = new SpeechClient();
      const response = await client.recognize(
        {
          audio: {content: audioByte},
          config: {
            encoding: "LINEAR16",
            audioChannelCount: 2,
            languageCode: "en-US"}});

      const transcription = response[0].results?.map(result => result.alternatives![0].transcript).join('\n');
      return transcription;
});

更新1: 我已要求它 chatgpt ,它建议我使用 Firebase Functions 来验证用户身份并获取 accessToken ,并在角度方面使用该 accessToken 来验证 api 请求。我觉得这听起来很合理。

更新2: 原来我需要使用grpc来使用语音转文本作为流数据。所以我需要设置并使用类似 ngx-grpc 的库来实现这一点,对吗?看起来工作量很大。我不想错过更简单的解决方案。

node.js angular google-cloud-functions google-speech-api
1个回答
0
投票

我在我的node.js firebase函数上通过@google-cloud/speech库使用谷歌语音到文本服务,但是我想将它用作流数据,据我所知,在firebase上不可能实现这一点功能。

您无法在 Cloud Functions 中真正有效地使用流数据。云函数意味着

stateless
。他们一次只处理一个请求,然后进行清理。如果您尝试将其用作语音转文本用例的数据流,则它不会按您期望的方式工作。因为 Cloud Functions 不会向请求者保持打开的套接字。一旦发送响应,连接就会关闭,并且无法保持连接打开状态。意味着不会有数据流。

相反,根据您的假设,您必须在角度客户端应用程序中实现该功能。该实现的一个示例是 Medium 上的 Donishka Tharindu

Creating a Speech Recognition App in Angular

© www.soinside.com 2019 - 2024. All rights reserved.