|
1 | 1 | import { useEffect, useCallback, useState } from 'react'; |
2 | 2 | import { ETError, getError } from '../../Error'; |
3 | 3 | import { SpeechToTextModule } from '../../modules/natural_language_processing/SpeechToTextModule'; |
4 | | -import { SpeechToTextModelConfig } from '../../types/stt'; |
| 4 | +import { DecodingOptions, SpeechToTextModelConfig } from '../../types/stt'; |
5 | 5 |
|
6 | 6 | export const useSpeechToText = ({ |
7 | 7 | model, |
@@ -65,24 +65,29 @@ export const useSpeechToText = ({ |
65 | 65 | [isReady, isGenerating, modelInstance] |
66 | 66 | ); |
67 | 67 |
|
68 | | - const stream = useCallback(async () => { |
69 | | - if (!isReady) throw new Error(getError(ETError.ModuleNotLoaded)); |
70 | | - if (isGenerating) throw new Error(getError(ETError.ModelGenerating)); |
71 | | - setIsGenerating(true); |
72 | | - setCommittedTranscription(''); |
73 | | - setNonCommittedTranscription(''); |
74 | | - let transcription = ''; |
75 | | - try { |
76 | | - for await (const { committed, nonCommitted } of modelInstance.stream()) { |
77 | | - setCommittedTranscription((prev) => prev + committed); |
78 | | - setNonCommittedTranscription(nonCommitted); |
79 | | - transcription += committed; |
| 68 | + const stream = useCallback( |
| 69 | + async (options?: DecodingOptions) => { |
| 70 | + if (!isReady) throw new Error(getError(ETError.ModuleNotLoaded)); |
| 71 | + if (isGenerating) throw new Error(getError(ETError.ModelGenerating)); |
| 72 | + setIsGenerating(true); |
| 73 | + setCommittedTranscription(''); |
| 74 | + setNonCommittedTranscription(''); |
| 75 | + let transcription = ''; |
| 76 | + try { |
| 77 | + for await (const { committed, nonCommitted } of modelInstance.stream( |
| 78 | + options |
| 79 | + )) { |
| 80 | + setCommittedTranscription((prev) => prev + committed); |
| 81 | + setNonCommittedTranscription(nonCommitted); |
| 82 | + transcription += committed; |
| 83 | + } |
| 84 | + } finally { |
| 85 | + setIsGenerating(false); |
80 | 86 | } |
81 | | - } finally { |
82 | | - setIsGenerating(false); |
83 | | - } |
84 | | - return transcription; |
85 | | - }, [isReady, isGenerating, modelInstance]); |
| 87 | + return transcription; |
| 88 | + }, |
| 89 | + [isReady, isGenerating, modelInstance] |
| 90 | + ); |
86 | 91 |
|
87 | 92 | const wrapper = useCallback( |
88 | 93 | <T extends (...args: any[]) => any>(fn: T) => { |
|
0 commit comments