feat(stt): pass last assistant response as Whisper context to reduce mishearings
This commit is contained in:
@@ -40,16 +40,20 @@ class VoiceApi {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// POST WebM/Opus audio bytes and return the transcript string.
|
/// POST WebM/Opus audio bytes and return the transcript string.
|
||||||
|
/// [context] is optional recent conversation text passed as initial_prompt
|
||||||
|
/// to Whisper, reducing mishearings of domain-specific words.
|
||||||
/// Returns empty string on empty or error response.
|
/// Returns empty string on empty or error response.
|
||||||
Future<String> transcribe(Uint8List audioBytes) async {
|
Future<String> transcribe(Uint8List audioBytes, {String? context}) async {
|
||||||
try {
|
try {
|
||||||
final formData = FormData.fromMap({
|
final fields = <String, dynamic>{
|
||||||
'audio': MultipartFile.fromBytes(
|
'audio': MultipartFile.fromBytes(
|
||||||
audioBytes,
|
audioBytes,
|
||||||
filename: 'audio.m4a',
|
filename: 'audio.m4a',
|
||||||
contentType: DioMediaType('audio', 'mp4'),
|
contentType: DioMediaType('audio', 'mp4'),
|
||||||
),
|
),
|
||||||
});
|
if (context != null && context.isNotEmpty) 'context': context,
|
||||||
|
};
|
||||||
|
final formData = FormData.fromMap(fields);
|
||||||
final response = await _dio.post(
|
final response = await _dio.post(
|
||||||
'/api/voice/transcribe',
|
'/api/voice/transcribe',
|
||||||
data: formData,
|
data: formData,
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ class VoiceRepository {
|
|||||||
const VoiceRepository(this._api);
|
const VoiceRepository(this._api);
|
||||||
|
|
||||||
Future<VoiceStatus> checkStatus() => _api.checkStatus();
|
Future<VoiceStatus> checkStatus() => _api.checkStatus();
|
||||||
Future<String> transcribe(Uint8List audioBytes) => _api.transcribe(audioBytes);
|
Future<String> transcribe(Uint8List audioBytes, {String? context}) =>
|
||||||
|
_api.transcribe(audioBytes, context: context);
|
||||||
Future<Uint8List> synthesise(String text) => _api.synthesise(text);
|
Future<Uint8List> synthesise(String text) => _api.synthesise(text);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -108,6 +108,10 @@ class VoiceNotifier extends Notifier<VoiceState> {
|
|||||||
int _lastSeenLength = 0;
|
int _lastSeenLength = 0;
|
||||||
bool _streamComplete = false;
|
bool _streamComplete = false;
|
||||||
|
|
||||||
|
// Last complete assistant response — passed to Whisper as initial_prompt
|
||||||
|
// to reduce STT mishearings of domain-specific words.
|
||||||
|
String _lastAssistantContent = '';
|
||||||
|
|
||||||
// TTS playback queue
|
// TTS playback queue
|
||||||
final _ttsQueue = Queue<Uint8List>();
|
final _ttsQueue = Queue<Uint8List>();
|
||||||
bool _ttsPlaying = false;
|
bool _ttsPlaying = false;
|
||||||
@@ -205,6 +209,7 @@ class VoiceNotifier extends Notifier<VoiceState> {
|
|||||||
_dispatchSentences(flush: isComplete);
|
_dispatchSentences(flush: isComplete);
|
||||||
|
|
||||||
if (isComplete) {
|
if (isComplete) {
|
||||||
|
_lastAssistantContent = fullContent;
|
||||||
_streamComplete = true;
|
_streamComplete = true;
|
||||||
_checkRestartListening();
|
_checkRestartListening();
|
||||||
}
|
}
|
||||||
@@ -278,8 +283,10 @@ class VoiceNotifier extends Notifier<VoiceState> {
|
|||||||
|
|
||||||
if (!state.voiceModeActive) return;
|
if (!state.voiceModeActive) return;
|
||||||
|
|
||||||
final transcript =
|
final transcript = await ref.read(voiceRepositoryProvider).transcribe(
|
||||||
await ref.read(voiceRepositoryProvider).transcribe(bytes);
|
bytes,
|
||||||
|
context: _lastAssistantContent.isNotEmpty ? _lastAssistantContent : null,
|
||||||
|
);
|
||||||
|
|
||||||
if (!state.voiceModeActive) return;
|
if (!state.voiceModeActive) return;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user