constfunctions=require("firebase-functions");const{Deepgram}=require("@deepgram/sdk");exports.getTranscription=functions.https.onCall(async (data,context)=>{try{constdeepgram=newDeepgram(process.env.DEEPGRAM_API_KEY);constaudioSource={url:data.url,};constresponse=awaitdeepgram.transcription.preRecorded(audioSource,{punctuate:true,utterances:true,});console.log(response.results.utterances.length);constconfidenceList=[];for (leti=0;i<response.results.utterances.length;i++){confidenceList.push(response.results.utterances[i].confidence);}constwebvttTranscript=response.toWebVTT();constfinalTranscript={transcript:webvttTranscript,confidences:confidenceList,};constfinalTranscriptJSON=JSON.stringify(finalTranscript);console.log(finalTranscriptJSON);returnfinalTranscriptJSON;}catch (error){console.error(`Unable to transcribe. Error ${error}`);thrownewfunctions.https.HttpsError("aborted","Could not transcribe");}});
该getTranscription函数接受一个音频 URL,使用 Deepgram API 生成转录文本以及相应的置信度,并以特定的 JSON 格式(可在应用程序中解析)返回数据。