Я использую клиент Google Speech to Text Node.js.
При запуске длительной транскрипции речи мне нужно разделить операцию на два шага.
1. Запустите транскрипцию и верните «имя».
2. Спросите о статусе операции.
Т.е. репликация https://cloud.google.com/speech-to-text/docs/async-recognize#speech-async-recognize-gcs-protocol в nodejs.
Моя задача - выяснить, как создать OperationsClient, и заставить getOperation ({name}) фактически вернуть результат транскрипции.
Я запускаю этот код внутри двух облачных функций Firebase:
1. Тот, который запускает транскрипцию, а затем возвращает «имя». Этот действительно хорошо работает с SpeechClient.
2. Еще один, который должен вызывать operationsClient.getOperation, такой же, как curl для "https://speech.googleapis.com/v1/operations/"
import gax, {GrpcClient, GrpcClientOptions, lro} from "google-gax";
const gaxOpts = {
clientConfig: {}
}
const gaxGrpc = new GrpcClient(gaxOpts);
...
app.get('/operations/:googleSpeechRef', async (req, res) => {
const googleSpeechRef = req.params.googleSpeechRef;
const operationsClient = lro({
auth: gaxGrpc.auth,
grpc: gaxGrpc.grpc,
}).operationsClient(gaxOpts);
try {
const [responses] = await operationsClient.getOperation({name: googleSpeechRef},{});
if (responses) {
const operation = responses[0]
console.log("getOperation. responses: ", responses)
const initialApiResponse = responses[1]
operation
.on("complete", (longRunningRecognizeResponse /*, longRunningRecognizeMetadata, finalApiResponse*/) => {
// Adding a listener for the "complete" event starts polling for the
// completion of the operation.
const speechRecognitionResults = longRunningRecognizeResponse.results as ISpeechRecognitionResult[]
// resolve(speechRecognitionResults)
console.log("complete: ", speechRecognitionResults)
})
.on("progress", async (longRunningRecognizeMetadata /*, apiResponse*/) => {
// Adding a listener for the "progress" event causes the callback to be
// called on any change in metadata when the operation is polled.
const percent = longRunningRecognizeMetadata.progressPercent
console.log("progress. Percent", longRunningRecognizeMetadata.progressPercent /*, apiResponse*/)
})
.on("error", (error: Error) => {
// Adding a listener for the "error" event handles any errors found during polling.
// reject(error)
console.log("error: ", error)
})
res.contentType("application/json").status(200).send(JSON.stringify(initialApiResponse))
} else {
res.send(404)
}
} catch (error) {
console.error("Failed to fetch operation by googleSpeechRef: ", googleSpeechRef, ". Error: ", error);
res.status(500).send(serializeError(error))
}
}
Ошибка, которую я получаю:
{
"code": 13,
"details": "Not enough responses received",
"metadata":{
"internalRepr":{}
},
"note": "Exception occurred in retry method that was not classified as transient",
"name": "Error",
"message": "Not enough responses received",
"stack": "Error: Not enough responses received\n at Http2CallStream.call.on (/srv/node_modules/@grpc/grpc-js/build/src/client.js:102:45)\n at emitOne (events.js:121:20)\n at Http2CallStream.emit (events.js:211:7)\n at Http2CallStream.endCall (/srv/node_modules/@grpc/grpc-js/build/src/call-stream.js:74:18)\n at /srv/node_modules/@grpc/grpc-js/build/src/call-stream.js:355:18\n at <anonymous>\n at process._tickDomainCallback (internal/process/next_tick.js:229:7)"
}