UNPKG

@cloudraker/use-whisper

Version:

React Hook for OpenAI Whisper API with speech recorder and silence removal built-in.

11 lines (7 loc) 5.38 kB
'use strict'; var chunkZ2HWN2Y5_cjs = require('./chunk-Z2HWN2Y5.cjs'); var chunkCACFNBF4_cjs = require('./chunk-CACFNBF4.cjs'); var chunkZ7KQX4E2_cjs = require('./chunk-Z7KQX4E2.cjs'); var react = require('react'); var ae={apiKey:"",autoStart:!1,autoTranscribe:!0,mode:"transcriptions",nonStop:!1,removeSilence:!1,stopTimeout:5e3,streaming:!1,timeSlice:1e3,onDataAvailable:void 0,onTranscribe:void 0,whisperApiEndpoints:{transcriptions:`${chunkZ2HWN2Y5_cjs.d}/transcriptions`,translations:`${chunkZ2HWN2Y5_cjs.d}/translations`}},ie={stop:void 0},se={blob:void 0,text:void 0},de=K=>{let{apiKey:h,autoStart:U,autoTranscribe:W,mode:T,nonStop:B,removeSilence:j,stopTimeout:q,streaming:k,timeSlice:z,whisperConfig:c,onDataAvailable:L,onTranscribe:R,whisperApiEndpoints:p}={...ae,...K};if(!p)throw new Error("whisperApiEndpoints is required and should not have been nullified");p.transcriptions=p?.transcriptions||`${chunkZ2HWN2Y5_cjs.d}/transcriptions`,p.translations=p?.translations||`${chunkZ2HWN2Y5_cjs.d}/translations`;let G=p?.transcriptions||p?.translations;if(!h&&!R&&!G)throw new Error("Either apiKey, whisperApiEndpoints, or onTranscribeCallback is required");let d=react.useRef([]),i=react.useRef(),s=react.useRef(),t=react.useRef(),a=react.useRef(),l=react.useRef(ie),[J,A]=react.useState(!1),[N,C]=react.useState(!1),[Q,b]=react.useState(!1),[V,m]=react.useState(se);react.useEffect(()=>()=>{d.current&&(d.current=[]),i.current&&(i.current.flush(),i.current=void 0),t.current&&(t.current.destroy(),t.current=void 0),y("stop"),s.current&&(s.current.off("speaking",E),s.current.off("stopped_speaking",v)),a.current&&(a.current.getTracks().forEach(e=>e.stop()),a.current=void 0);},[]),chunkCACFNBF4_cjs.a(async()=>{U&&await F();},[U]);let X=async()=>{await F();},Y=async()=>{await re();},Z=async()=>{await D();},F=async()=>{try{if(a.current||await ee(),a.current){if(!t.current){let{default:{RecordRTCPromisesHandler:r,StereoAudioRecorder:o}}=await import('recordrtc'),n={mimeType:"audio/wav",numberOfAudioChannels:1,recorderType:o,sampleRate:44100,timeSlice:k?z:void 0,type:"audio",ondataavailable:W&&k?ne:void 0};t.current=new r(a.current,n);}if(!i.current){let{Mp3Encoder:r}=await import('lamejs');i.current=new r(1,44100,96);}let e=await t.current.getState();(e==="inactive"||e==="stopped")&&await t.current.startRecording(),e==="paused"&&await t.current.resumeRecording(),B&&H("stop"),A(!0);}}catch{}},ee=async()=>{try{if(a.current&&a.current.getTracks().forEach(e=>e.stop()),a.current=await navigator.mediaDevices.getUserMedia({audio:!0}),!s.current){let{default:e}=await import('hark');s.current=e(a.current,{interval:100,play:!1}),s.current.on("speaking",E),s.current.on("stopped_speaking",v);}}catch{}},H=e=>{l.current[e]||(l.current[e]=setTimeout(D,q));},E=()=>{C(!0),y("stop");},v=()=>{C(!1),B&&H("stop");},re=async()=>{try{t.current&&(await t.current.getState()==="recording"&&await t.current.pauseRecording(),y("stop"),A(!1));}catch{}},D=async()=>{try{if(t.current){let e=await t.current.getState();if((e==="recording"||e==="paused")&&await t.current.stopRecording(),te(),y("stop"),A(!1),W)await _();else {let r=await t.current.getBlob();m({blob:r});}await t.current.destroy(),d.current=[],i.current&&(i.current.flush(),i.current=void 0),t.current=void 0;}}catch{}},te=()=>{s.current&&(s.current.off("speaking",E),s.current.off("stopped_speaking",v),s.current=void 0),a.current&&(a.current.getTracks().forEach(e=>e.stop()),a.current=void 0);},y=e=>{l.current[e]&&(clearTimeout(l.current[e]),l.current[e]=void 0);},_=async()=>{try{if(i.current&&t.current&&await t.current.getState()==="stopped"){b(!0);let r=await t.current.getBlob();if(j){let{FFmpeg:o}=await import('@ffmpeg/ffmpeg'),n=new o;await n.load({coreURL:chunkZ2HWN2Y5_cjs.b});let u=await r.arrayBuffer();await n.writeFile("in.wav",new Uint8Array(u)),await n.exec(["-i","in.wav","-acodec","libmp3lame","-b:a","96k","-ar","44100","-af",chunkZ2HWN2Y5_cjs.c,"out.mp3"]);let g=await n.readFile("out.mp3","binary");if(g.length<=225){n.terminate(),m({blob:r}),b(!1);return}r=new Blob([g],{type:"audio/mpeg"}),n.terminate();}else {let o=await r.arrayBuffer(),n=i.current.encodeBuffer(new Int16Array(o));r=new Blob([n],{type:"audio/mpeg"});}if(typeof R=="function"){let o=await R(r);m(o);}else {let o=new File([r],"speech.mp3",{type:"audio/mpeg"}),n=await x(o);m({blob:r,text:n});}b(!1);}}catch{b(!1);}},ne=async e=>{try{if(k&&t.current){if(L?.(e),i.current){let o=await e.arrayBuffer(),n=i.current.encodeBuffer(new Int16Array(o)),u=new Blob([n],{type:"audio/mpeg"});d.current.push(u);}if(await t.current.getState()==="recording"){let o=new Blob(d.current,{type:"audio/mpeg"}),n=new File([o],"speech.mp3",{type:"audio/mpeg"}),u=await x(n);u&&m(g=>({...g,text:u}));}}}catch{}},x=chunkZ7KQX4E2_cjs.a(async e=>{let r=new FormData;r.append("file",e),r.append("model","whisper-1"),T==="transcriptions"&&r.append("language",c?.language??"en"),c?.prompt&&r.append("prompt",c.prompt),c?.response_format&&r.append("response_format",c.response_format),c?.temperature&&r.append("temperature",`${c.temperature}`);let o={};h&&(o.Authorization=`Bearer ${h}`);let n=p[T??"transcriptions"];return (await(await fetch(n,{method:"POST",body:r,headers:o})).json()).text},[h,T,c]);return {recording:J,speaking:N,transcribing:Q,transcript:V,pauseRecording:Y,startRecording:X,stopRecording:Z,onTranscribing:_}}; exports.a = de;