直接在客户端使用watson-developer-cloud节点SDK?

Using the watson-developer-cloud node SDK directly on the client?

我有一个基于 React 的客户端,我将它与 webpack 2 捆绑在一起。但是 import/require const SpeechToTextV1 = require('watson-developer-cloud/speech-to-text/v1'); 我遇到了一些麻烦。在我修复它不会破坏构建之后,它仍然会发出一些警告,例如:

Module not found: Error: Can't resolve '../build/Release/validation' in '/Users/denbox/Desktop/schedulebot/web-interface/node_modules/websocket/lib'
 @ ./~/websocket/lib/Validation.js 9:21-59
 @ ./~/websocket/lib/WebSocketConnection.js
 @ ./~/websocket/lib/websocket.js
 @ ./~/websocket/index.js
 @ ./~/watson-developer-cloud/speech-to-text/recognize_stream.js
 @ ./~/watson-developer-cloud/speech-to-text/v1.js
 @ ./src/components/chat.jsx
 @ ./src/components/chat-page.js
 @ ./src/index.js
 @ multi (webpack)-dev-server/client?http://localhost:8080 ./src/index.js

是否可以在客户端或仅直接在 nodejs 服务器上使用 watson-developer-cloud node sdk 进行语音转文本服务?谢谢你。

仅在 Node.js 中。从浏览器使用 Speech To Text 的机制是使用 websockets,但要做到这一点,您需要一个令牌,这需要服务器端请求。获得令牌后,您可以使用 websockets 接口。

Watson Node.js SDK 对 client-side 使用的兼容性越来越强,但还没有完全实现。但是,对于语音服务,有一个单独的 SDK 针对 client-side 用法:https://www.npmjs.com/package/watson-speech

我刚刚添加了一个 Webpack 示例并确认它可以工作:https://github.com/watson-developer-cloud/speech-javascript-sdk/blob/master/examples/webpack.config.js

更新:我还向 Node.js SDK 添加了一个 Webpack 示例 - 有了那里的配置,它可以为整个库构建,并且实际上适用于记录的模块子集: https://github.com/watson-developer-cloud/node-sdk/tree/master/examples/webpack

根据上面的答案找到了解决我的问题的方法,它可能会帮助其他想要开始使用 API 的人:

import axios from 'axios';
import recognizeMicrophone from 'watson-speech/speech-to-text/recognize-microphone';

axios.get(`${BACKEND_ROOT_URL}/watsoncloud/stt/token`)
        .then((res) => {
          console.log('res:', res.data);
          const stream = recognizeMicrophone({
            token: res.data.token,
            continuous: false, // false = automatically stop transcription the first time a pause is detected
          });
          stream.setEncoding('utf8');
          stream.on('error', (err) => {
            console.log(err);
          });
          stream.on('data', (msg) => {
            console.log('message:', msg);
          });
        })
        .catch((err) => {
          console.log(`The following gUM error occured: ${err}`);
        });

在后端,我创建了一个代理服务,它获取了 watson 语音到文本服务的令牌,因此我不必在客户端上保存我的凭据:

const watson = require('watson-developer-cloud');
const express = require('express');
const cors = require('cors');

app.use(cors());

const stt = new watson.SpeechToTextV1({
      // if left undefined, username and password to fall back to the SPEECH_TO_TEXT_USERNAME and
      // SPEECH_TO_TEXT_PASSWORD environment properties, and then to VCAP_SERVICES (on Bluemix)
      username: process.env.STT_SERVICE_USER,
      password: process.env.STT_SERVICE_PW,
    });
    const authService = new watson.AuthorizationV1(stt.getCredentials());
    // Endpoint to retrieve an watson speech to text api token
    // Get token using your credentials
    app.get('/watsoncloud/stt/token', (req, res, next) => {
      // TODO check jwt at the auth service
      authService.getToken((err, token) => {
        if (err) {
          next(err);
        } else {
          res.send({ token });
        }
      });
    });

app.listen(port, (err) => {
  if (err) {
    console.log(`Error: ${err}`);
  }
});