-
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat: Enhance OpenAI speech-to-text functionality and modify API en…
…dpoints This commit introduces new features to enhance the OpenAI speech-to-text functionality. It includes adding a new function for recording audio, modifying the existing OpenAI STT function, and updating API endpoints and proxy URLs for Microsoft Speech, Azure Speech, and OpenAI. Additionally, the commit involves deleting the "mimeType.ts" file, importing different functions and modules, updating import statements and function names, and modifying error handling and stopping behavior. Changes: - Enhancements to OpenAI speech-to-text functionality - Addition of a new function for recording audio - Modification of the existing OpenAI STT function - Updates to API endpoints and proxy URLs for Microsoft Speech, Azure Speech, and OpenAI - Deletion of "mimeType.ts" file - Importing different functions and modules - Updating import statements and function names - Modifying error handling and stopping behavior
- Loading branch information
1 parent
ef43fda
commit 61126aa
Showing
13 changed files
with
213 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
export { type OpenaiSpeechRecognitionOptions, useOpenaiSTT } from './useOpenaiSTT'; | ||
export { type OpenaiSTTFetcher, useOpenaiSTT } from './useOpenaiSTT'; | ||
export { useOpenaiSTTWithPSR } from './useOpenaiSTTWithPSR'; | ||
export { | ||
type OpenaiSpeechRecognitionOptions, | ||
useOpenaiSTTWithRecord, | ||
} from './useOpenaiSTTWithRecord'; | ||
export { useOpenaiSTTWithSR } from './useOpenaiSTTWithSR'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,25 @@ | ||
import { useCallback, useState } from 'react'; | ||
import useSWR from 'swr'; | ||
import useSWR, { type SWRConfiguration } from 'swr'; | ||
|
||
import { OpenaiSttOptions, fetchOpenaiSTT } from '@/services/fetchOpenaiSTT'; | ||
import { useAudioRecorder } from '@/useAudioRecorder'; | ||
import { SpeechRecognitionOptions } from '@/useSpeechRecognition/useSpeechRecognition'; | ||
import { getRecordMineType } from '@/utils/getRecordMineType'; | ||
|
||
export type OpenaiSpeechRecognitionOptions = SpeechRecognitionOptions & OpenaiSttOptions; | ||
export type OpenaiSTTFetcher = (blob: Blob, sttOptions: OpenaiSttOptions) => Promise<string>; | ||
export const useOpenaiSTT = ( | ||
shouldFetch?: boolean, | ||
blob?: Blob, | ||
options?: OpenaiSttOptions, | ||
config?: SWRConfiguration, | ||
fetcher?: OpenaiSTTFetcher, | ||
) => { | ||
const key = new Date().getDate().toString(); | ||
|
||
export const useOpenaiSTT = ({ | ||
onBolbAvailable, | ||
onTextChange, | ||
...options | ||
}: OpenaiSpeechRecognitionOptions) => { | ||
const [isGlobalLoading, setIsGlobalLoading] = useState<boolean>(false); | ||
const [shouldFetch, setShouldFetch] = useState<boolean>(false); | ||
const [text, setText] = useState<string>(); | ||
const { start, stop, blob, url, isRecording, time, formattedTime } = useAudioRecorder( | ||
(blobData) => { | ||
setShouldFetch(true); | ||
onBolbAvailable?.(blobData); | ||
}, | ||
); | ||
const optionsWithMineType: OpenaiSttOptions = { ...options, mineType: getRecordMineType() }; | ||
|
||
const key = new Date().getDate().toString(); | ||
const openaiSTTFetcher = fetcher ?? fetchOpenaiSTT; | ||
|
||
const { isLoading } = useSWR( | ||
return useSWR( | ||
shouldFetch && blob ? key : null, | ||
async () => await fetchOpenaiSTT(blob as any, options), | ||
{ | ||
onSuccess: (value) => { | ||
onTextChange?.(value); | ||
setIsGlobalLoading(false); | ||
}, | ||
}, | ||
async () => await openaiSTTFetcher(blob as Blob, optionsWithMineType), | ||
config, | ||
); | ||
|
||
const handleStart = useCallback(() => { | ||
setIsGlobalLoading(true); | ||
start(); | ||
setText(''); | ||
}, [start]); | ||
|
||
return { | ||
blob, | ||
formattedTime, | ||
isLoading: isGlobalLoading || isLoading || isRecording, | ||
isRecording, | ||
start: handleStart, | ||
stop, | ||
text, | ||
time, | ||
url, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { useCallback, useState } from 'react'; | ||
|
||
import { OpenaiSttOptions } from '@/services/fetchOpenaiSTT'; | ||
import { useAudioRecorder } from '@/useAudioRecorder'; | ||
import { OpenaiSTTFetcher, useOpenaiSTT } from '@/useOpenaiSTT/useOpenaiSTT'; | ||
import { SpeechRecognitionOptions } from '@/useSpeechRecognition/useSpeechRecognition'; | ||
|
||
export type OpenaiSpeechRecognitionOptions = SpeechRecognitionOptions & OpenaiSttOptions; | ||
|
||
export const useOpenaiSTTWithRecord = ( | ||
{ onBolbAvailable, onTextChange, ...options }: OpenaiSpeechRecognitionOptions, | ||
fetcher?: OpenaiSTTFetcher, | ||
) => { | ||
const [isGlobalLoading, setIsGlobalLoading] = useState<boolean>(false); | ||
const [shouldFetch, setShouldFetch] = useState<boolean>(false); | ||
const [text, setText] = useState<string>(); | ||
const { start, stop, blob, url, isRecording, time, formattedTime } = useAudioRecorder( | ||
(blobData) => { | ||
setShouldFetch(true); | ||
onBolbAvailable?.(blobData); | ||
}, | ||
); | ||
|
||
const handleStart = useCallback(() => { | ||
setIsGlobalLoading(true); | ||
start(); | ||
setText(''); | ||
}, [start]); | ||
|
||
const handleStop = useCallback(() => { | ||
stop(); | ||
setShouldFetch(false); | ||
setIsGlobalLoading(false); | ||
}, [stop]); | ||
|
||
const { isLoading } = useOpenaiSTT( | ||
shouldFetch, | ||
blob, | ||
options, | ||
{ | ||
onError: (err) => { | ||
console.error(err); | ||
handleStop(); | ||
}, | ||
onSuccess: (data, value) => { | ||
setText(data); | ||
onTextChange?.(value); | ||
handleStop(); | ||
}, | ||
}, | ||
fetcher, | ||
); | ||
|
||
return { | ||
blob, | ||
formattedTime, | ||
isLoading: isGlobalLoading || isLoading || isRecording, | ||
isRecording, | ||
start: handleStart, | ||
stop: handleStop, | ||
text, | ||
time, | ||
url, | ||
}; | ||
}; |
Oops, something went wrong.