forked from TheDeathDragon/LiveTranslate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranslator.py
More file actions
432 lines (397 loc) · 16.4 KB
/
Copy pathtranslator.py
File metadata and controls
432 lines (397 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
import json
import logging
import time
import httpx
from openai import OpenAI
log = logging.getLogger("LiveTranslate.TL")
LANGUAGE_DISPLAY = {
"en": "English",
"ja": "Japanese",
"zh": "Chinese",
"ko": "Korean",
"fr": "French",
"de": "German",
"es": "Spanish",
"ru": "Russian",
"pt": "Portuguese",
"it": "Italian",
"nl": "Dutch",
"pl": "Polish",
"tr": "Turkish",
"ar": "Arabic",
"th": "Thai",
"vi": "Vietnamese",
"id": "Indonesian",
"ms": "Malay",
"hi": "Hindi",
"uk": "Ukrainian",
"cs": "Czech",
"ro": "Romanian",
"el": "Greek",
"hu": "Hungarian",
"sv": "Swedish",
"da": "Danish",
"fi": "Finnish",
"no": "Norwegian",
"he": "Hebrew",
}
DEFAULT_PROMPT = (
"You are a real-time subtitle translator. Translate {source_lang} into {target_lang}.\n"
"Rules:\n"
"- Output ONLY one single best translation, nothing else.\n"
"- Never include alternatives, parenthetical options, annotations, or explanations.\n"
"- Keep proper nouns, names, and brand names untranslated.\n"
"- Translate repeated expressions concisely, not mechanically word-for-word.\n"
"- Keep subtitles fluent and natural; avoid overly literal or stiff phrasing.\n"
"- Auto-correct likely ASR errors based on context and common sense."
)
PROMPT_PRESETS = {
"daily": (
"You are a real-time subtitle translator for casual conversation. "
"Translate {source_lang} into {target_lang}.\n"
"Rules:\n"
"- Output ONLY one single best translation, nothing else.\n"
"- Never include alternatives, parenthetical options, annotations, or explanations.\n"
"- Keep proper nouns, names, and brand names untranslated.\n"
"- Use natural, casual, everyday language. Keep it conversational and concise.\n"
"- Auto-correct likely ASR errors based on context and common sense."
),
"esports": (
"You are a real-time subtitle translator for esports/gaming live streams. "
"Translate {source_lang} into {target_lang}.\n"
"Rules:\n"
"- Output ONLY one single best translation, nothing else.\n"
"- Never include alternatives, parenthetical options, annotations, or explanations.\n"
"- Keep player names (IGN), team names, game terms, and brand names untranslated.\n"
"- Use energetic, concise language appropriate for competitive gaming commentary.\n"
"- Auto-correct likely ASR errors based on context and common sense."
),
"anime": (
"You are a real-time subtitle translator for anime, movies, and TV shows. "
"Translate {source_lang} into {target_lang}.\n"
"Rules:\n"
"- Output ONLY one single best translation, nothing else.\n"
"- Never include alternatives, parenthetical options, annotations, or explanations.\n"
"- Keep character names, place names, and cultural terms untranslated.\n"
"- Use natural, expressive language that matches the tone and emotion of the dialogue.\n"
"- Auto-correct likely ASR errors based on context and common sense."
),
"webid": (
"You are a real-time subtitle translator for an online identity-verification "
"(WebID / video KYC) call. Translate {source_lang} into {target_lang}.\n"
"Rules:\n"
"- Output ONLY one single best translation, nothing else.\n"
"- Never include alternatives, parenthetical options, annotations, or explanations.\n"
"- Context: a verification agent and a customer on a video call inspect ID documents "
"(passport, ID card). Words about reading or seeing refer to the document or the camera "
"image, NOT literacy — e.g. 'I can't read it' means the text/photo is unclear, not that "
"the person is illiterate.\n"
"- Render camera/document instructions naturally (hold it up, tilt it, move closer, "
"lighting, focus, read the number aloud, turn it over).\n"
"- Keep names, document numbers, and verification codes exactly as spoken.\n"
"- Auto-correct likely ASR errors based on this verification context."
),
}
def make_openai_client(
api_base: str, api_key: str, proxy: str = "none", timeout=None
) -> OpenAI:
kwargs = {"base_url": api_base, "api_key": api_key}
if timeout is not None:
kwargs["timeout"] = httpx.Timeout(timeout, connect=5.0)
if proxy == "system":
pass
elif proxy in ("none", "", None):
kwargs["http_client"] = httpx.Client(trust_env=False)
else:
kwargs["http_client"] = httpx.Client(proxy=proxy)
return OpenAI(**kwargs)
class RepetitionError(Exception):
"""Raised when model output contains repetition loops."""
pass
_OVERRIDE_KEYS = (
"temperature",
"top_p",
"max_tokens",
"frequency_penalty",
"presence_penalty",
"seed",
)
class Translator:
"""LLM-based translation using OpenAI-compatible API."""
def __init__(
self,
api_base,
api_key,
model,
target_language="zh",
max_tokens=256,
temperature=0.3,
streaming=True,
system_prompt=None,
proxy="none",
no_system_role=False,
no_think=False,
json_response=False,
timeout=10,
overrides=None,
extra_body=None,
):
self._client = make_openai_client(api_base, api_key, proxy, timeout=timeout)
self._no_system_role = no_system_role
self._no_think = no_think
self._json_response = json_response
if no_think:
log.info(f"Translator: no_think enabled for {model}")
if json_response:
log.info(f"Translator: json_response enabled for {model}")
self._model = model
self._target_language = target_language
self._max_tokens = max_tokens
self._temperature = temperature
self._streaming = streaming
self._timeout = timeout
self._overrides = {k: v for k, v in (overrides or {}).items() if v is not None}
self._extra_body = dict(extra_body) if extra_body else {}
if self._overrides:
log.info(f"Translator overrides: {self._overrides}")
if self._extra_body:
log.info(f"Translator extra_body: {self._extra_body}")
self._system_prompt_template = system_prompt or DEFAULT_PROMPT
self._context_turns = 0
self._history = [] # list of (source_text, translated_text)
self._last_prompt_tokens = 0
self._last_completion_tokens = 0
@property
def last_usage(self):
"""(prompt_tokens, completion_tokens) from last translate call."""
return self._last_prompt_tokens, self._last_completion_tokens
def set_target_language(self, target_language: str):
self._target_language = target_language
def set_timeout(self, timeout: int):
self._timeout = timeout
self._client = self._client.copy(timeout=timeout)
def set_context_turns(self, n: int):
self._context_turns = n
if n == 0:
self._history.clear()
def clear_history(self):
self._history.clear()
def _format_context(self) -> str:
if self._context_turns <= 0 or not self._history:
return ""
lines = []
for src, tgt in self._history[-self._context_turns:]:
lines.append(f"Source: {src}")
lines.append(f"Translation: {tgt}")
lines.append("")
return "\n".join(lines).rstrip()
def with_target_language(self, target_language: str) -> "Translator":
"""Create a new Translator with a different target language, sharing the same client."""
t = Translator.__new__(Translator)
t._client = self._client
t._no_system_role = self._no_system_role
t._no_think = self._no_think
t._json_response = self._json_response
t._model = self._model
t._target_language = target_language
t._max_tokens = self._max_tokens
t._temperature = self._temperature
t._streaming = self._streaming
t._timeout = self._timeout
t._overrides = dict(self._overrides)
t._extra_body = dict(self._extra_body)
t._system_prompt_template = self._system_prompt_template
t._context_turns = 0
t._history = []
t._last_prompt_tokens = 0
t._last_completion_tokens = 0
return t
def _build_system_prompt(self, source_lang):
src = LANGUAGE_DISPLAY.get(source_lang, source_lang)
tgt = LANGUAGE_DISPLAY.get(self._target_language, self._target_language)
try:
prompt = self._system_prompt_template.format(
source_lang=src,
target_lang=tgt,
context=self._format_context(),
)
except (KeyError, IndexError, ValueError) as e:
log.warning(f"Bad prompt template, falling back to default: {e}")
prompt = DEFAULT_PROMPT.format(source_lang=src, target_lang=tgt)
if self._json_response:
prompt += '\nRespond in JSON format: {"t": "translated text"}'
return prompt
def _build_messages(self, system_prompt, text):
if self._no_system_role:
msgs = [{"role": "user", "content": f"{system_prompt}\n{text}"}]
else:
msgs = [{"role": "system", "content": system_prompt}]
# Append recent history as context
if (
self._context_turns > 0
and self._history
and "{context}" not in self._system_prompt_template
):
for src, tgt in self._history[-self._context_turns:]:
msgs.append({"role": "user", "content": src})
msgs.append({"role": "assistant", "content": tgt})
msgs.append({"role": "user", "content": text})
return msgs
def _append_history(self, text, result):
if self._context_turns > 0 and result:
self._history.append((text, result))
max_keep = self._context_turns + 2
if len(self._history) > max_keep:
self._history = self._history[-self._context_turns:]
def _build_request_kwargs(self, system_prompt, text, stream=False):
kwargs = dict(
model=self._model,
messages=self._build_messages(system_prompt, text),
max_tokens=self._max_tokens,
temperature=self._temperature,
)
for k in _OVERRIDE_KEYS:
if k in self._overrides:
kwargs[k] = self._overrides[k]
extra_body = {}
if self._no_think:
extra_body["enable_thinking"] = False
if self._extra_body:
extra_body.update(self._extra_body)
if extra_body:
kwargs["extra_body"] = extra_body
if self._json_response:
kwargs["response_format"] = {
"type": "json_schema",
"json_schema": {
"name": "translation",
"strict": True,
"schema": {
"type": "object",
"properties": {"t": {"type": "string"}},
"required": ["t"],
"additionalProperties": False,
},
},
}
if stream:
kwargs["stream"] = True
return kwargs
def translate(self, text: str, source_language: str = "en"):
system_prompt = self._build_system_prompt(source_language)
if self._streaming:
result = self._translate_streaming(system_prompt, text)
else:
result = self._translate_sync(system_prompt, text)
if self._check_repetition(result):
raise RepetitionError(result)
self._append_history(text, result)
return result
def translate_iter(self, text: str, source_language: str = "en"):
"""Generator that yields accumulated partial text, then final result.
Non-streaming or json_response mode: yields once with the final result.
Streaming mode: yields partial accumulated text as chunks arrive.
The final yielded value is always the complete translation.
Caller should use the last yielded value as the final result.
"""
system_prompt = self._build_system_prompt(source_language)
if not self._streaming:
result = self._translate_sync(system_prompt, text)
self._append_history(text, result)
yield result
return
# Streaming path
self._last_prompt_tokens = 0
self._last_completion_tokens = 0
base_kwargs = self._build_request_kwargs(system_prompt, text, stream=True)
try:
stream = self._client.chat.completions.create(
**base_kwargs,
stream_options={"include_usage": True},
)
except Exception:
stream = self._client.chat.completions.create(**base_kwargs)
deadline = time.monotonic() + self._timeout
chunks = []
for chunk in stream:
if time.monotonic() > deadline:
stream.close()
raise TimeoutError(
f"Translation exceeded {self._timeout}s total timeout"
)
if hasattr(chunk, "usage") and chunk.usage:
self._last_prompt_tokens = chunk.usage.prompt_tokens or 0
self._last_completion_tokens = chunk.usage.completion_tokens or 0
if chunk.choices:
delta = chunk.choices[0].delta
if delta.content:
chunks.append(delta.content)
if not self._json_response:
yield "".join(chunks)
result = "".join(chunks).strip()
if self._json_response:
result = self._extract_json_translation(result)
if self._check_repetition(result):
raise RepetitionError(result)
self._append_history(text, result)
yield result
def _extract_json_translation(self, raw: str) -> str:
"""Extract translation from JSON response, fallback to raw text."""
try:
data = json.loads(raw)
if isinstance(data, dict) and "t" in data:
return data["t"]
except (json.JSONDecodeError, TypeError):
pass
return raw
@staticmethod
def _check_repetition(text: str) -> bool:
"""Detect repetition loops in model output."""
if not text or len(text) < 40:
return False
for plen in range(8, len(text) // 2 + 1):
if text[plen:plen * 2] == text[:plen]:
return True
return False
def _translate_sync(self, system_prompt, text):
kwargs = self._build_request_kwargs(system_prompt, text, stream=False)
resp = self._client.chat.completions.create(**kwargs)
self._last_prompt_tokens = 0
self._last_completion_tokens = 0
if resp.usage:
self._last_prompt_tokens = resp.usage.prompt_tokens or 0
self._last_completion_tokens = resp.usage.completion_tokens or 0
result = resp.choices[0].message.content.strip()
if self._json_response:
result = self._extract_json_translation(result)
return result
def _translate_streaming(self, system_prompt, text):
self._last_prompt_tokens = 0
self._last_completion_tokens = 0
base_kwargs = self._build_request_kwargs(system_prompt, text, stream=True)
try:
stream = self._client.chat.completions.create(
**base_kwargs,
stream_options={"include_usage": True},
)
except Exception:
stream = self._client.chat.completions.create(**base_kwargs)
deadline = time.monotonic() + self._timeout
chunks = []
for chunk in stream:
if time.monotonic() > deadline:
stream.close()
raise TimeoutError(
f"Translation exceeded {self._timeout}s total timeout"
)
if hasattr(chunk, "usage") and chunk.usage:
self._last_prompt_tokens = chunk.usage.prompt_tokens or 0
self._last_completion_tokens = chunk.usage.completion_tokens or 0
if chunk.choices:
delta = chunk.choices[0].delta
if delta.content:
chunks.append(delta.content)
result = "".join(chunks).strip()
if self._json_response:
result = self._extract_json_translation(result)
return result