Gathering detailed insights and metrics for @dhansoo/use-audio-stream
Gathering detailed insights and metrics for @dhansoo/use-audio-stream
Gathering detailed insights and metrics for @dhansoo/use-audio-stream
Gathering detailed insights and metrics for @dhansoo/use-audio-stream
npm install @dhansoo/use-audio-stream
Typescript
Module System
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
React Hook for OpenAI Whisper API with speech recorder, real-time transcription and silence removal built-in
https://user-images.githubusercontent.com/2707253/224465747-0b1ee159-21dd-4cd0-af9d-6fc9b882d716.mp4
useAudioStream for React Native is being developed.
Repository: https://github.com/ElectricSpeech/use-audio-stream-native
Progress: https://github.com/ElectricSpeech/use-audio-stream-native/issues/1
npm i @dhansoo/use-audio-stream
yarn add @dhansoo/use-audio-stream
1import { useAudioStream } from '@dhansoo/use-audio-stream' 2 3const App = () => { 4 const { 5 recording, 6 speaking, 7 transcribing, 8 transcript, 9 pauseRecording, 10 startRecording, 11 stopRecording, 12 } = useAudioStream({ 13 apiKey: process.env.OPENAI_API_TOKEN, // YOUR_OPEN_AI_TOKEN 14 }) 15 16 return ( 17 <div> 18 <p>Recording: {recording}</p> 19 <p>Speaking: {speaking}</p> 20 <p>Transcribing: {transcribing}</p> 21 <p>Transcribed Text: {transcript.text}</p> 22 <button onClick={() => startRecording()}>Start</button> 23 <button onClick={() => pauseRecording()}>Pause</button> 24 <button onClick={() => stopRecording()}>Stop</button> 25 </div> 26 ) 27}
1import { useAudioStream } from '@dhansoo/use-audio-stream' 2 3const App = () => { 4 /** 5 * you have more control like this 6 * do whatever you want with the recorded speech 7 * send it to your own custom server 8 * and return the response back to useAudioStream 9 */ 10 const onTranscribe = (blob: Blob) => { 11 const base64 = await new Promise<string | ArrayBuffer | null>( 12 (resolve) => { 13 const reader = new FileReader() 14 reader.onloadend = () => resolve(reader.result) 15 reader.readAsDataURL(blob) 16 } 17 ) 18 const body = JSON.stringify({ file: base64, model: 'whisper-1' }) 19 const headers = { 'Content-Type': 'application/json' } 20 const { default: axios } = await import('axios') 21 const response = await axios.post('/api/whisper', body, { 22 headers, 23 }) 24 const { text } = await response.data 25 // you must return result from your server in Transcript format 26 return { 27 blob, 28 text, 29 } 30 } 31 32 const { transcript } = useAudioStream({ 33 // callback to handle transcription with custom server 34 onTranscribe, 35 }) 36 37 return ( 38 <div> 39 <p>{transcript.text}</p> 40 </div> 41 ) 42}
1import { useAudioStream } from '@dhansoo/use-audio-stream' 2 3const App = () => { 4 const { transcript } = useAudioStream({ 5 apiKey: process.env.OPENAI_API_TOKEN, // YOUR_OPEN_AI_TOKEN 6 streaming: true, 7 timeSlice: 1_000, // 1 second 8 whisperConfig: { 9 language: 'en', 10 }, 11 }) 12 13 return ( 14 <div> 15 <p>{transcript.text}</p> 16 </div> 17 ) 18}
1import { useAudioStream } from '@dhansoo/use-audio-stream' 2 3const App = () => { 4 const { transcript } = useAudioStream({ 5 apiKey: process.env.OPENAI_API_TOKEN, // YOUR_OPEN_AI_TOKEN 6 // use ffmpeg-wasp to remove silence from recorded speech 7 removeSilence: true, 8 }) 9 10 return ( 11 <div> 12 <p>{transcript.text}</p> 13 </div> 14 ) 15}
1import { useAudioStream } from '@dhansoo/use-audio-stream' 2 3const App = () => { 4 const { transcript } = useAudioStream({ 5 apiKey: process.env.OPENAI_API_TOKEN, // YOUR_OPEN_AI_TOKEN 6 // will auto start recording speech upon component mounted 7 autoStart: true, 8 }) 9 10 return ( 11 <div> 12 <p>{transcript.text}</p> 13 </div> 14 ) 15}
1import { useAudioStream } from '@dhansoo/use-audio-stream'
2
3const App = () => {
4 const { transcript } = useAudioStream({
5 apiKey: process.env.OPENAI_API_TOKEN, // YOUR_OPEN_AI_TOKEN
6 nonStop: true, // keep recording as long as the user is speaking
7 stopTimeout: 5000, // auto stop after 5 seconds
8 })
9
10 return (
11 <div>
12 <p>{transcript.text}</p>
13 </div>
14 )
15}
1import { useAudioStream } from '@dhansoo/use-audio-stream'
2
3const App = () => {
4 const { transcript } = useAudioStream({
5 apiKey: process.env.OPENAI_API_TOKEN, // YOUR_OPEN_AI_TOKEN
6 autoTranscribe: true,
7 whisperConfig: {
8 prompt: 'previous conversation', // you can pass previous conversation for context
9 response_format: 'text', // output text instead of json
10 temperature: 0.8, // random output
11 language: 'es', // Spanish
12 },
13 })
14
15 return (
16 <div>
17 <p>{transcript.text}</p>
18 </div>
19 )
20}
most of these dependecies are lazy loaded, so it is only imported when it is needed
Name | Type | Default Value | Description |
---|---|---|---|
apiKey | string | '' | your OpenAI API token |
autoStart | boolean | false | auto start speech recording on component mount |
autoTranscribe | boolean | true | should auto transcribe after stop recording |
mode | string | transcriptions | control Whisper mode either transcriptions or translations, currently only support translation to English |
nonStop | boolean | false | if true, record will auto stop after stopTimeout. However if user keep on speaking, the recorder will keep recording |
removeSilence | boolean | false | remove silence before sending file to OpenAI API |
stopTimeout | number | 5,000 ms | if nonStop is true, this become required. This control when the recorder auto stop |
streaming | boolean | false | transcribe speech in real-time based on timeSlice |
timeSlice | number | 1000 ms | interval between each onDataAvailable event |
whisperConfig | WhisperApiConfig | undefined | Whisper API transcription config |
onDataAvailable | (blob: Blob) => void | undefined | callback function for getting recorded blob in interval between timeSlice |
onTranscribe | (blob: Blob) => Promise<Transcript> | undefined | callback function to handle transcription on your own custom server |
Name | Type | Default Value | Description |
---|---|---|---|
prompt | string | undefined | An optional text to guide the model's style or continue a previous audio segment. The prompt should match the audio language. |
response_format | string | json | The format of the transcript output, in one of these options: json, text, srt, verbose_json, or vtt. |
temperature | number | 0 | The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use log probability to automatically increase the temperature until certain thresholds are hit. |
language | string | en | The language of the input audio. Supplying the input language in ISO-639-1 format will improve accuracy and latency. |
Name | Type | Description |
---|---|---|
recording | boolean | speech recording state |
speaking | boolean | detect when user is speaking |
transcribing | boolean | while removing silence from speech and send request to OpenAI Whisper API |
transcript | Transcript | object return after Whisper transcription complete |
pauseRecording | Promise | pause speech recording |
startRecording | Promise | start speech recording |
stopRecording | Promise | stop speech recording |
Name | Type | Description |
---|---|---|
blob | Blob | recorded speech in JavaScript Blob |
text | string | transcribed text returned from Whisper API |
Contact me for web or mobile app development using React or React Native
https://ElectricSpeech.github.io
No vulnerabilities found.
No security vulnerabilities found.