Skip to content

知了 Cicada · 任何会议和直播的实时字幕、同声传译、AI 会议纪要,Mac、Windows、Linux、Android 免费开源。Live subtitles, interpreting and meeting notes for any call or stream, on Mac, Windows, Linux and Android.

fix(linux): when a custom AI service's text address can't be reached, translations no longer wait seconds on every line before Google translates them

wqugurube87d6da993e
diff --git a/linux/cicada/core/translator.py b/linux/cicada/core/translator.py
index 39df675..3c4e655 100644
--- a/linux/cicada/core/translator.py
+++ b/linux/cicada/core/translator.py
@@ -27,6 +27,7 @@ class Translator:
self.ling = ling
self.transport = transport
self._google_backoff_until = 0.0
+ self._last_unreachable_try = 0.0
# Names and terms the notes have settled on, e.g. "江充 (…; misheard as 江松)".
self.glossary = lambda: []
@@ -51,11 +52,26 @@ class Translator:
DRAFT, SENTENCE = "draft", "sentence"
+ def _ling_worth_trying(self) -> bool:
+ """A text model that answers nothing costs every line its timeout before Google gets it
+ (Translator.swift › lingWorthTrying). That happens here when a custom service hears at an
+ address of its own that still answers. While the check says the text side is unreachable,
+ one line in 15 s still goes to it, so a server that is back is noticed without a check.
+ Lines translate on a pool: two in the same instant may both try, which is harmless."""
+ f = self.ling.check_result.failure
+ if f is None or f[1] != "unreachable" or f[0] == "hear":
+ return True
+ now = time.time()
+ if now - self._last_unreachable_try < 15:
+ return False
+ self._last_unreachable_try = now
+ return True
+
def translate(self, text: str, source: str, target: str, kind: str) -> str | None:
"""Ling (finished sentences only) → Google. None when both fail."""
if source == target:
return text
- if kind != self.DRAFT and self.ling.usable:
+ if kind != self.DRAFT and self.ling.usable and self._ling_worth_trying():
try:
frm = "the spoken language" if source == "auto" else english_name(source)
prompt = ("You translate live meeting subtitles from %s into %s.\n" % (frm, english_name(target)) +
diff --git a/linux/tests/test_core.py b/linux/tests/test_core.py
index 086dd21..10d4668 100644
--- a/linux/tests/test_core.py
+++ b/linux/tests/test_core.py
@@ -419,6 +419,34 @@ class Hearing(Base):
self.assertEqual("好的,翻译。", clean_translation("好的,翻译。", "zh-CN"))
self.assertEqual("Done.", clean_translation("<think>x</think>Done.", "en"))
+ def test_an_unreachable_service_is_tried_once_in_15_seconds(self):
+ # Translator.swift › lingWorthTrying: while the check says the text model is out of reach,
+ # lines go straight to Google but for one in 15 s; a hear model at its own address doesn't count.
+ from cicada.core import aicheck
+ tried = []
+
+ class Net:
+ def send(self, url, method, body, headers, timeout):
+ if "googleapis" in url:
+ return Response(200, '[[["你好","hello",null,null,1]]]')
+ tried.append(url)
+ raise OSError("no route")
+ net = Net()
+ ling = client(net)
+ t = Translator(ling, transport=net)
+ ling.check_result.fail("reach", aicheck.UNREACHABLE, reached=False)
+ self.assertEqual("你好", t.translate("hello", "en", "zh-CN", Translator.SENTENCE))
+ self.assertEqual(1, len(tried), "the first line still tries the service")
+ self.assertEqual("你好", t.translate("hello", "en", "zh-CN", Translator.SENTENCE))
+ self.assertEqual(1, len(tried), "the next one goes straight to Google")
+ t._last_unreachable_try -= 15
+ t.translate("hello", "en", "zh-CN", Translator.SENTENCE)
+ self.assertEqual(2, len(tried), "15 s later it is tried again")
+ ling.check_result = aicheck.AICheck()
+ ling.check_result.fail("hear", aicheck.UNREACHABLE)
+ t.translate("hello", "en", "zh-CN", Translator.SENTENCE)
+ self.assertEqual(3, len(tried), "only the hear model is away: text is tried")
+
def test_joins_cut_sentences(self):
last = TranscriptLine(src="We finished the migration and", srcLang="en")
self.assertTrue(continues(last, 0.2, Heard("en", "the error rate dropped.", None)))