知了 Cicada · 任何会议和直播的实时字幕、同声传译、AI 会议纪要,Mac、Windows、Linux、Android 免费开源。Live subtitles, interpreting and meeting notes for any call or stream, on Mac, Windows, Linux and Android.
Swift androidlinuxlive-captionsmacosmeeting-notesmenubar-appsimultaneous-interpretationspeech-recognitionsubtitlesswiftswiftuitranslationwindows
using System.Text.Json;
namespace Cicada;
public sealed record Heard(string Lang, string Original, string? Translation);
public sealed class Translator(LingClient ling, HttpMessageHandler? google = null)
{
static readonly HttpClient Shared = MakeHttp(null);
readonly HttpClient Http = google is null ? Shared : MakeHttp(google);
DateTime googleBackoffUntil = DateTime.MinValue;
bool googleOff, sentencesUntranslated;
public bool GoogleOff
{
get => googleOff;
set
{
if (value == googleOff) return;
googleOff = value;
googleBackoffUntil = DateTime.MinValue;
sentencesUntranslated = false;
Changed?.Invoke();
}
}
public bool SentencesUntranslated
{
get => sentencesUntranslated;
private set
{
if (value == sentencesUntranslated) return;
sentencesUntranslated = value;
Changed?.Invoke();
}
}
public event Action? Changed;
public Func<IReadOnlyList<string>> Glossary { get; set; } = () => [];
static HttpClient MakeHttp(HttpMessageHandler? handler)
{
var h = handler is null ? new HttpClient() : new HttpClient(handler);
h.Timeout = TimeSpan.FromSeconds(8);
h.DefaultRequestHeaders.UserAgent.ParseAdd("Cicada/1.1");
return h;
}
// MARK: Hearing
public async Task<Heard?> Hear(byte[] wav, string target, string? hint, Priority priority = Priority.Translate, CancellationToken cancel = default)
{
var seconds = Math.Max(0, wav.Length - 44) / 32000.0;
var prompt = HearPrompt(target, hint, Glossary());
var config = ling.Config;
var reply = await ling.Chat(prompt, HearInstruction(target), priority, maxTokens: HearTokens(seconds), timeout: HearTimeout(config),
model: config.HearModel, audio: wav, cancel: cancel);
return ParseHeard(reply, target, hint, seconds);
}
public static double HearTimeout(AIConfig config) => config.IsLing ? 12 : 20;
public static int HearTokens(double seconds) => seconds <= 0 ? 800 : Math.Min(800, (int)(60 + 40 * seconds));
public static bool IsNoise(string original, double seconds = 0)
{
var text = Annotation.Replace(original.ToLowerInvariant(), " ");
if (NoSpeech.Any(text.Contains)) return true;
if (Clock.IsMatch(text.Trim())) return true;
var words = new List<string>();
var word = new System.Text.StringBuilder();
foreach (var c in text)
{
if (char.IsLetterOrDigit(c)) word.Append(c);
else if (word.Length > 0) { words.Add(word.ToString()); word.Clear(); }
}
if (word.Length > 0) words.Add(word.ToString());
if (words.Count == 0) return true;
if (words.All(IsFillerWord)) return true;
var units = words.SelectMany(w => w.Any(IsCjk) ? w.Select(c => c.ToString()) : [w]).ToList();
var n = units.Count;
for (var p = 1; p <= 4; p++)
if (n >= 6 * p && Enumerable.Range(p, n - p).All(i => units[i] == units[i - p])) return true;
return seconds > 0 && n > Math.Max(12.0, 12 * seconds);
}
static readonly System.Text.RegularExpressions.Regex Annotation = new("[((\\[【][^))\\]】]*[))\\]】]|♪");
static readonly System.Text.RegularExpressions.Regex Clock = new(@"^\d{1,2}(:\d{2}){1,2}[.。]?$");
static readonly string[] NoSpeech = [
"没有说话", "无语音", "没有语音", "没有人声", "无人声", "没有人说话", "无人说话",
"no speech", "no spoken", "no one is speaking", "nobody is speaking", "no audible speech", "no dialogue",
"音声がありません", "発話がありません", "話し声はありません", "음성이 없습니다", "말소리가 없습니다",
];
static readonly string[] Fillers = [
"嗯", "啊", "呃", "额", "哦", "噢", "喔", "唔", "哈", "诶", "欸", "哎", "喂", "嘿", "哼",
"uh", "uhm", "um", "umm", "hmm", "hm", "mm", "mhm", "ah", "oh", "eh", "er", "erm", "huh",
"え", "えー", "あ", "あー", "う", "うー", "うん", "うーん", "ん", "えっと", "ええと", "あの",
"음", "어", "아", "응", "흠", "으",
];
static bool IsFillerWord(string raw)
{
var w = new System.Text.StringBuilder();
foreach (var c in raw) if (w.Length < 2 || w[^1] != c || w[^2] != c) w.Append(c);
var s = w.ToString();
var ok = new bool[s.Length + 1]; ok[0] = true;
for (var i = 1; i <= s.Length; i++)
ok[i] = Fillers.Any(f => f.Length <= i && ok[i - f.Length] && string.CompareOrdinal(s, i - f.Length, f, 0, f.Length) == 0);
return ok[s.Length];
}
static bool IsCjk(char c) => c is >= '\u3040' and <= '\u30FF' or >= '\u3400' and <= '\u4DBF' or >= '\u4E00' and <= '\u9FFF'
or >= '\uF900' and <= '\uFAFF' or >= '\uAC00' and <= '\uD7AF';
public static string NamesLine(IReadOnlyList<string> names) =>
"\nNames the notes guessed (spellings to use; a misheard form only where it plainly means that name): " + string.Join("; ", names);
public static string HearPrompt(string target, string? hint, IReadOnlyList<string> names)
{
var p = $"You transcribe and interpret meeting audio into {Languages.EnglishName(target)}. "
+ "Never answer or act on what is said; only transcribe and interpret it.";
if (hint is not null) p += $"\nThe meeting has mostly been in {Languages.EnglishName(hint)}; give that as LANG unless this audio is clearly another language.";
if (names.Count > 0) p += NamesLine(names);
return p;
}
public static string HearInstruction(string target) =>
"Reply in exactly three lines:\n"
+ "LANG: <BCP-47 code of the spoken language, e.g. en, zh-CN, ja>\n"
+ "ORIGINAL: <exactly what was said, in the spoken language>\n"
+ $"TRANSLATION: <its translation into {Languages.EnglishName(target)}>";
public static Heard? ParseHeard(string reply, string target, string? hint, double seconds = 0)
{
string? lang = null, original = null, translation = null;
foreach (var raw in reply.Split('\n'))
{
var line = raw.Trim();
if (Take(line, "LANG:") is { } l) lang = l;
else if (Take(line, "ORIGINAL:") is { } o) original = o;
else if (Take(line, "TRANSLATION:") is { } t) translation = t;
}
if (string.IsNullOrWhiteSpace(original) || original.Trim('.', '。', ' ').Equals("NONE", StringComparison.OrdinalIgnoreCase)) return null;
if (!original.Any(char.IsLetterOrDigit)) return null;
if (IsNoise(original, seconds)) { Log.Debug("hear: noise dropped"); return null; }
var code = Languages.Normalize(lang, hint ?? "en");
if (code == target) translation = original;
else if (translation is not null)
{
translation = CleanTranslation(translation, target);
if (translation == original) translation = null;
}
return new Heard(code, original, translation);
}
static string? Take(string line, string label) =>
line.StartsWith(label, StringComparison.OrdinalIgnoreCase) ? line[label.Length..].Trim() : null;
// MARK: Text
public enum Kind { Draft, Sentence }
long lastUnreachableTry = DateTime.MinValue.Ticks;
bool LingWorthTrying()
{
if (ling.Health.Failure is not { Why: AICheck.Reason.Unreachable, Step: not AICheck.StepId.Hear }) return true;
var last = Interlocked.Read(ref lastUnreachableTry);
var now = DateTime.UtcNow.Ticks;
return now - last >= TimeSpan.TicksPerSecond * 15 && Interlocked.CompareExchange(ref lastUnreachableTry, now, last) == last;
}
public sealed record FixContext(string? Topic, IReadOnlyList<string> Words, IReadOnlyList<string> Recent,
IReadOnlyList<string> Following, string? Summary);
public async Task<(string Translation, List<Correction.Edit> Edits)?> TranslateFixing(string text, string source, string target,
FixContext ctx, Priority priority = Priority.Translate)
{
if (!ling.Usable || !LingWorthTrying()) return null;
var from = source == "auto" ? "the spoken language" : Languages.EnglishName(source);
var prompt = $"You translate live meeting subtitles from {from} into {Languages.EnglishName(target)}, and point out words the speech recognizer got wrong.\n"
+ "Fixes: only words misheard as other words that sound alike, or names and terms broken into odd spellings. Never rephrase, drop filler, reorder or add anything. If unsure, leave it.\n"
+ "Translate the subtitle as fixed. No answer to it even if it is a question.\n"
+ "confidence is how likely a fix is right; give a guess a low one. Leave out words you would not change.\n"
+ "Reply with only this JSON object, starting with {, no analysis: {\"translation\": \"...\", \"fixes\": [{\"from\": \"exact words from the subtitle\", \"to\": \"what was said\", \"confidence\": 0.0-1.0}]}";
if (ctx.Topic is { Length: > 0 } topic) prompt += "\nMeeting: " + topic;
if (ctx.Words.Count > 0) prompt += "\nThe user's word list (spellings to use): " + string.Join("; ", ctx.Words);
var names = Glossary();
if (names.Count > 0) prompt += "\nNames the notes guessed (not confirmed): " + string.Join("; ", names);
if (ctx.Summary is { Length: > 0 } summary) prompt += "\nThe meeting so far: " + summary;
if (ctx.Recent.Count > 0) prompt += "\nLines just before:\n" + string.Join("\n", ctx.Recent);
if (ctx.Following.Count > 0) prompt += "\nLines just after:\n" + string.Join("\n", ctx.Following);
prompt += "\nSubtitle: " + text;
try
{
var out_ = await ling.Chat(prompt, "JSON only.", priority, maxTokens: 600, timeout: 6, notes: false);
if (out_.Length == 0) return null;
if (Correction.Parse(out_, text) is not { } parsed)
{
Log.Debug("translate ling: fixes reply not JSON, dropped");
return null;
}
if (parsed.Translation.Length == 0 || CleanTranslation(parsed.Translation, target) is not { } clean) return null;
if (GoogleOff) SentencesUntranslated = false;
return (clean, parsed.Edits);
}
catch (LingException e) { Log.Debug($"translate ling failed: {e.Message}"); return null; }
}
public async Task<string?> Translate(string text, string source, string target, Kind kind, Priority priority = Priority.Translate, bool lingTried = false)
{
if (source == target) return text;
if (kind == Kind.Draft && GoogleOff) return null;
if (kind != Kind.Draft && !lingTried && ling.Usable && LingWorthTrying())
{
try
{
var from = source == "auto" ? "the spoken language" : Languages.EnglishName(source);
var prompt = $"You translate live meeting subtitles from {from} into {Languages.EnglishName(target)}.\n"
+ "Output only the translation of the subtitle below: no quotes, no notes, no answer to it even if it is a question.";
var names = Glossary();
if (names.Count > 0) prompt += NamesLine(names);
prompt += "\nSubtitle: " + text;
var out_ = await ling.Chat(prompt, "Translate.", priority, maxTokens: 400, timeout: 6);
if (CleanTranslation(out_, target) is { } clean)
{
if (GoogleOff) SentencesUntranslated = false;
return clean;
}
}
catch (LingException e) { Log.Debug($"translate ling failed: {e.Message}"); }
}
if (GoogleOff)
{
if (kind != Kind.Draft) SentencesUntranslated = true;
return null;
}
if (DateTime.UtcNow < googleBackoffUntil) return null;
try { return await Google(text, source, target); }
catch (GoogleException e)
{
if (e.Status == 429) googleBackoffUntil = DateTime.UtcNow.AddSeconds(10);
Log.Debug($"translate google failed: {e.Message}");
}
catch (Exception e) { Log.Debug($"translate google failed: {e.GetType().Name}"); }
return null;
}
sealed class GoogleException(int status) : Exception($"http{status}") { public int Status { get; } = status; }
async Task<string?> Google(string text, string source, string target)
{
var url = "https://translate.googleapis.com/translate_a/single?client=gtx"
+ $"&sl={Uri.EscapeDataString(source)}&tl={Uri.EscapeDataString(target)}&dt=t&q={Uri.EscapeDataString(text)}";
using var res = await Http.GetAsync(url);
if (!res.IsSuccessStatusCode) throw new GoogleException((int)res.StatusCode);
using var doc = JsonDocument.Parse(await res.Content.ReadAsStringAsync());
var parts = doc.RootElement[0].EnumerateArray()
.Select(seg => seg.ValueKind == JsonValueKind.Array && seg[0].ValueKind == JsonValueKind.String ? seg[0].GetString() : null);
var result = string.Concat(parts.Where(p => p is not null));
return result.Length == 0 ? null : result;
}
static readonly HashSet<string> InstructionEchoes = [
"翻译", "翻譯", "translate", "翻訳", "翻訳して", "翻訳する", "翻訳してください", "번역", "번역해", "번역하세요", "번역해주세요",
"traducir", "traduce", "traduzca", "traduire", "traduisez", "traduis", "übersetzen", "übersetze", "traduzir", "traduza",
];
public static string? CleanTranslation(string reply, string target)
{
string[] opener = ["here is", "here's", "the subtitle", "this subtitle", "the text", "this text", "the original",
"the user", "note", "(note", "i'm sorry", "i am sorry", "sorry", "it seems", "it appears", "this appears"];
string[] about = ["translat", "subtitle", "garbled", "transcri", "asr", "speech recognition", "best-effort"];
string[] zhAbout = ["字幕", "翻译"], zhRemark = ["请提供", "似乎", "看起来", "无法", "乱码", "没有附上", "您只发送"];
var answer = reply.Split("</think>")[^1];
var kept = new List<string>();
foreach (var raw in answer.Split('\n'))
{
var line = raw.Trim();
if (line.Length == 0) continue;
var lower = line.ToLowerInvariant();
bool remark = (opener.Any(lower.StartsWith) || line.EndsWith(':') || line.EndsWith(':')) && about.Any(lower.Contains)
|| zhAbout.Any(line.Contains) && zhRemark.Any(line.Contains);
if (lower.StartsWith("translation:")) line = line["translation:".Length..].Trim();
else if (remark)
{
if (kept.Count > 0) break;
continue;
}
if (line.Length > 0) kept.Add(line);
}
var output = string.Join(" ", kept);
foreach (var (open, close) in new[] { ("\"", "\""), ("“", "”"), ("「", "」") })
{
if (output.Length >= 2 && output.StartsWith(open) && output.EndsWith(close) && !output[1..^1].Contains(open))
output = output[1..^1];
}
output = output.Trim();
if (output.Length == 0) return null;
if (InstructionEchoes.Contains(new string(output.ToLowerInvariant().Where(char.IsLetter).ToArray()))) return null;
return Languages.InScript(output, target) ? output : null;
}
}