Skip to content

Commit 3c3a4a2

Browse files
committed
fix: retry rest after blank live transcript
1 parent 32c0d20 commit 3c3a4a2

3 files changed

Lines changed: 50 additions & 2 deletions

File tree

app/src/main/java/com/whispertranscriber/network/WhisperApiClient.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,6 @@ data class TranscriptionResult(
109109
val text: String,
110110
val error: String? = null
111111
)
112+
113+
fun TranscriptionResult.shouldRetryRestAfterLive(): Boolean =
114+
success && text.isBlank()

app/src/main/java/com/whispertranscriber/service/FloatingOverlayService.kt

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import com.whispertranscriber.network.WhisperLiveKitClient
3434
import com.whispertranscriber.network.WhisperLiveKitSession
3535
import com.whispertranscriber.network.WhisperServerDiscovery
3636
import com.whispertranscriber.network.TranscriptionResult
37+
import com.whispertranscriber.network.shouldRetryRestAfterLive
3738
import kotlinx.coroutines.CoroutineScope
3839
import kotlinx.coroutines.Dispatchers
3940
import kotlinx.coroutines.Job
@@ -257,9 +258,11 @@ class FloatingOverlayService : Service() {
257258
val serverUrl = resolveServerUrl(settings.whisperServerUrl)
258259
val session = liveKitSession
259260
liveKitSession = null
260-
val result = liveResult ?: if (liveKitReady && session != null) {
261+
val result = liveResult?.let {
262+
retryRestIfLiveResultIsBlank(serverUrl, wavData, it)
263+
} ?: if (liveKitReady && session != null) {
261264
try {
262-
session.finish()
265+
retryRestIfLiveResultIsBlank(serverUrl, wavData, session.finish())
263266
} catch (e: Exception) {
264267
Log.w(TAG, "Live transcription finalization failed, retrying with REST", e)
265268
whisperClient.transcribe(
@@ -312,6 +315,19 @@ class FloatingOverlayService : Service() {
312315
}
313316
}
314317

318+
private suspend fun retryRestIfLiveResultIsBlank(
319+
serverUrl: String,
320+
wavData: ByteArray,
321+
liveResult: TranscriptionResult
322+
): TranscriptionResult {
323+
if (!liveResult.shouldRetryRestAfterLive()) return liveResult
324+
Log.w(TAG, "Live transcription returned empty text, retrying with REST")
325+
return whisperClient.transcribe(
326+
serverUrl = serverUrl,
327+
audioData = wavData
328+
)
329+
}
330+
315331
private fun handleLivePartial(partial: String) {
316332
transcriptionText = partial
317333
if (realtimeInsertionActive) {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.whispertranscriber.network
2+
3+
import org.junit.Assert.assertFalse
4+
import org.junit.Assert.assertTrue
5+
import org.junit.Test
6+
7+
class TranscriptionResultPolicyTest {
8+
9+
@Test
10+
fun blankSuccessfulLiveResultShouldRetryWithRest() {
11+
val result = TranscriptionResult(success = true, text = "")
12+
13+
assertTrue(result.shouldRetryRestAfterLive())
14+
}
15+
16+
@Test
17+
fun nonBlankSuccessfulLiveResultShouldNotRetryWithRest() {
18+
val result = TranscriptionResult(success = true, text = "hello")
19+
20+
assertFalse(result.shouldRetryRestAfterLive())
21+
}
22+
23+
@Test
24+
fun failedLiveResultShouldNotBeTreatedAsNoSpeech() {
25+
val result = TranscriptionResult(success = false, text = "", error = "socket failed")
26+
27+
assertFalse(result.shouldRetryRestAfterLive())
28+
}
29+
}

0 commit comments

Comments
 (0)