forked from lingodotdev/sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.py
More file actions
693 lines (576 loc) · 23.5 KB
/
engine.py
File metadata and controls
693 lines (576 loc) · 23.5 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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
"""
LingoDotDevEngine implementation for Python SDK - Async version with httpx
"""
# mypy: disable-error-code=unreachable
import asyncio
import json
from typing import Any, Callable, Dict, List, Optional
from urllib.parse import urljoin
import httpx
from nanoid import generate
from pydantic import BaseModel, Field, validator
class EngineConfig(BaseModel):
"""Configuration for the LingoDotDevEngine"""
api_key: str
api_url: str = "https://engine.lingo.dev"
batch_size: int = Field(default=25, ge=1, le=250)
ideal_batch_item_size: int = Field(default=250, ge=1, le=2500)
@validator("api_url")
@classmethod
def validate_api_url(cls, v: str) -> str:
if not v.startswith(("http://", "https://")):
raise ValueError("API URL must be a valid HTTP/HTTPS URL")
return v
class LocalizationParams(BaseModel):
"""Parameters for localization requests"""
source_locale: Optional[str] = None
target_locale: str
fast: Optional[bool] = None
reference: Optional[Dict[str, Dict[str, Any]]] = None
class LingoDotDevEngine:
"""
LingoDotDevEngine class for interacting with the LingoDotDev API
A powerful localization engine that supports various content types including
plain text, objects, chat sequences, and HTML documents.
"""
def __init__(self, config: Dict[str, Any]):
"""
Create a new LingoDotDevEngine instance
Args:
config: Configuration options for the Engine
"""
self.config = EngineConfig(**config)
self._client: Optional[httpx.AsyncClient] = None
async def __aenter__(self):
"""Async context manager entry"""
await self._ensure_client()
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
"""Async context manager exit"""
await self.close()
async def _ensure_client(self):
"""Ensure the httpx client is initialized"""
if self._client is None or self._client.is_closed:
self._client = httpx.AsyncClient(
headers={
"Content-Type": "application/json; charset=utf-8",
"Authorization": f"Bearer {self.config.api_key}",
},
timeout=60.0,
)
async def close(self):
"""Close the httpx client"""
if self._client and not self._client.is_closed:
await self._client.aclose()
@staticmethod
def _truncate_response(text: str, max_length: int = 200) -> str:
"""Truncate response text for error messages"""
if len(text) > max_length:
return text[:max_length] + "..."
return text
@staticmethod
def _safe_parse_json(response: httpx.Response) -> Dict[str, Any]:
"""
Safely parse JSON response, handling HTML error pages gracefully.
Args:
response: The httpx response object
Returns:
Parsed JSON as a dictionary
Raises:
RuntimeError: If the response cannot be parsed as JSON
"""
try:
return response.json()
except (json.JSONDecodeError, UnicodeDecodeError) as err:
try:
text = response.text
except UnicodeDecodeError:
text = response.content.decode("utf-8", errors="replace")
preview = LingoDotDevEngine._truncate_response(text)
raise RuntimeError(
f"Failed to parse API response as JSON (status {response.status_code}). "
f"This may indicate a gateway or proxy error. Response: {preview}"
) from err
async def _localize_raw(
self,
payload: Dict[str, Any],
params: LocalizationParams,
progress_callback: Optional[
Callable[[int, Dict[str, str], Dict[str, str]], None]
] = None,
concurrent: bool = False,
) -> Dict[str, str]:
"""
Localize content using the Lingo.dev API
Args:
payload: The content to be localized
params: Localization parameters
progress_callback: Optional callback function to report progress (0-100)
concurrent: Whether to process chunks concurrently (faster but no progress tracking)
Returns:
Localized content
"""
await self._ensure_client()
chunked_payload = self._extract_payload_chunks(payload)
workflow_id = generate()
if concurrent and not progress_callback:
# Process chunks concurrently for better performance
tasks = []
for chunk in chunked_payload:
task = self._localize_chunk(
params.source_locale,
params.target_locale,
{"data": chunk, "reference": params.reference},
workflow_id,
params.fast or False,
)
tasks.append(task)
processed_payload_chunks = await asyncio.gather(*tasks)
else:
# Process chunks sequentially (supports progress tracking)
processed_payload_chunks = []
for i, chunk in enumerate(chunked_payload):
percentage_completed = round(((i + 1) / len(chunked_payload)) * 100)
processed_payload_chunk = await self._localize_chunk(
params.source_locale,
params.target_locale,
{"data": chunk, "reference": params.reference},
workflow_id,
params.fast or False,
)
if progress_callback:
progress_callback(
percentage_completed, chunk, processed_payload_chunk
)
processed_payload_chunks.append(processed_payload_chunk)
result = {}
for chunk in processed_payload_chunks:
result.update(chunk)
return result
async def _localize_chunk(
self,
source_locale: Optional[str],
target_locale: str,
payload: Dict[str, Any],
workflow_id: str,
fast: bool,
) -> Dict[str, str]:
"""
Localize a single chunk of content
Args:
source_locale: Source locale
target_locale: Target locale
payload: Payload containing the chunk to be localized
workflow_id: Workflow ID for tracking
fast: Whether to use fast mode
Returns:
Localized chunk
"""
await self._ensure_client()
assert self._client is not None # Type guard for mypy
url = urljoin(self.config.api_url, "/i18n")
request_data = {
"params": {"workflowId": workflow_id, "fast": fast},
"locale": {"source": source_locale, "target": target_locale},
"data": payload["data"],
}
if payload.get("reference"):
request_data["reference"] = payload["reference"]
try:
response = await self._client.post(url, json=request_data)
if not response.is_success:
if 500 <= response.status_code < 600:
error_details = ""
try:
error_json = response.json()
if isinstance(error_json, dict) and "error" in error_json:
error_details = f" {error_json['error']}"
except Exception:
pass
raise RuntimeError(
f"Server error ({response.status_code}): {response.reason_phrase}.{error_details} "
"This may be due to temporary service issues."
)
try:
text = response.text
except UnicodeDecodeError:
text = response.content.decode("utf-8", errors="replace")
response_preview = self._truncate_response(text)
if response.status_code == 400:
raise ValueError(
f"Invalid request ({response.status_code}): {response.reason_phrase}. "
f"Response: {response_preview}"
)
else:
raise RuntimeError(
f"Request failed ({response.status_code}): {response_preview}"
)
json_response = self._safe_parse_json(response)
# Handle streaming errors
if not json_response.get("data") and json_response.get("error"):
raise RuntimeError(json_response["error"])
return json_response.get("data") or {}
except httpx.RequestError as e:
raise RuntimeError(f"Request failed: {str(e)}")
def _extract_payload_chunks(self, payload: Dict[str, Any]) -> List[Dict[str, Any]]:
"""
Extract payload chunks based on the ideal chunk size
Args:
payload: The payload to be chunked
Returns:
An array of payload chunks
"""
result = []
current_chunk = {}
current_chunk_item_count = 0
for key, value in payload.items():
current_chunk[key] = value
current_chunk_item_count += 1
current_chunk_size = self._count_words_in_record(current_chunk)
if (
current_chunk_size > self.config.ideal_batch_item_size
or current_chunk_item_count >= self.config.batch_size
or key == list(payload.keys())[-1]
):
result.append(current_chunk)
current_chunk = {}
current_chunk_item_count = 0
return result
def _count_words_in_record(self, payload: Any) -> int:
"""
Count words in a record or array
Args:
payload: The payload to count words in
Returns:
The total number of words
"""
if isinstance(payload, list):
return sum(self._count_words_in_record(item) for item in payload)
elif isinstance(payload, dict):
return sum(self._count_words_in_record(item) for item in payload.values())
elif isinstance(payload, str):
return len([word for word in payload.strip().split() if word])
else:
return 0
async def localize_object(
self,
obj: Dict[str, Any],
params: Dict[str, Any],
progress_callback: Optional[
Callable[[int, Dict[str, str], Dict[str, str]], None]
] = None,
concurrent: bool = False,
) -> Dict[str, Any]:
"""
Localize a typical Python dictionary
Args:
obj: The object to be localized (strings will be extracted and translated)
params: Localization parameters:
- source_locale: The source language code (e.g., 'en')
- target_locale: The target language code (e.g., 'es')
- fast: Optional boolean to enable fast mode
progress_callback: Optional callback function to report progress (0-100)
concurrent: Whether to process chunks concurrently (faster but no progress tracking)
Returns:
A new object with the same structure but localized string values
"""
localization_params = LocalizationParams(**params)
return await self._localize_raw(
obj, localization_params, progress_callback, concurrent
)
async def localize_text(
self,
text: str,
params: Dict[str, Any],
progress_callback: Optional[Callable[[int], None]] = None,
) -> str:
"""
Localize a single text string
Args:
text: The text string to be localized
params: Localization parameters:
- source_locale: The source language code (e.g., 'en')
- target_locale: The target language code (e.g., 'es')
- fast: Optional boolean to enable fast mode
progress_callback: Optional callback function to report progress (0-100)
Returns:
The localized text string
"""
localization_params = LocalizationParams(**params)
def wrapped_progress_callback(
progress: int, source_chunk: Dict[str, str], processed_chunk: Dict[str, str]
):
if progress_callback:
progress_callback(progress)
response = await self._localize_raw(
{"text": text}, localization_params, wrapped_progress_callback
)
return response.get("text", "")
async def batch_localize_text(self, text: str, params: Dict[str, Any]) -> List[str]:
"""
Localize a text string to multiple target locales
Args:
text: The text string to be localized
params: Localization parameters:
- source_locale: The source language code (e.g., 'en')
- target_locales: A list of target language codes (e.g., ['es', 'fr'])
- fast: Optional boolean to enable fast mode
Returns:
A list of localized text strings
"""
if "target_locales" not in params:
raise ValueError("target_locales is required")
target_locales = params["target_locales"]
source_locale = params.get("source_locale")
fast = params.get("fast", False)
# Create tasks for concurrent execution
tasks = []
for target_locale in target_locales:
task = self.localize_text(
text,
{
"source_locale": source_locale,
"target_locale": target_locale,
"fast": fast,
},
)
tasks.append(task)
# Execute all localization tasks concurrently
responses = await asyncio.gather(*tasks)
return responses
async def localize_chat(
self,
chat: List[Dict[str, str]],
params: Dict[str, Any],
progress_callback: Optional[Callable[[int], None]] = None,
) -> List[Dict[str, str]]:
"""
Localize a chat sequence while preserving speaker names
Args:
chat: Array of chat messages, each with 'name' and 'text' properties
params: Localization parameters:
- source_locale: The source language code (e.g., 'en')
- target_locale: The target language code (e.g., 'es')
- fast: Optional boolean to enable fast mode
progress_callback: Optional callback function to report progress (0-100)
Returns:
Array of localized chat messages with preserved structure
"""
# Validate chat format
for message in chat:
if "name" not in message or "text" not in message:
raise ValueError(
"Each chat message must have 'name' and 'text' properties"
)
localization_params = LocalizationParams(**params)
def wrapped_progress_callback(
progress: int, source_chunk: Dict[str, str], processed_chunk: Dict[str, str]
):
if progress_callback:
progress_callback(progress)
localized = await self._localize_raw(
{"chat": chat}, localization_params, wrapped_progress_callback
)
# The API returns the localized chat in the same structure
chat_result = localized.get("chat")
if chat_result and isinstance(chat_result, list):
return chat_result
return []
async def recognize_locale(self, text: str) -> str:
"""
Detect the language of a given text
Args:
text: The text to analyze
Returns:
A locale code (e.g., 'en', 'es', 'fr')
"""
if not text or not text.strip():
raise ValueError("Text cannot be empty")
await self._ensure_client()
assert self._client is not None # Type guard for mypy
url = urljoin(self.config.api_url, "/recognize")
try:
response = await self._client.post(url, json={"text": text})
if not response.is_success:
if 500 <= response.status_code < 600:
error_details = ""
try:
error_json = response.json()
if isinstance(error_json, dict) and "error" in error_json:
error_details = f" {error_json['error']}"
except Exception:
pass
raise RuntimeError(
f"Server error ({response.status_code}): {response.reason_phrase}.{error_details} "
"This may be due to temporary service issues."
)
try:
text = response.text
except UnicodeDecodeError:
text = response.content.decode("utf-8", errors="replace")
response_preview = self._truncate_response(text)
raise RuntimeError(
f"Error recognizing locale ({response.status_code}): {response.reason_phrase}. "
f"Response: {response_preview}"
)
json_response = self._safe_parse_json(response)
return json_response.get("locale") or ""
except httpx.RequestError as e:
raise RuntimeError(f"Request failed: {str(e)}")
async def whoami(self) -> Optional[Dict[str, str]]:
"""
Get information about the current API key
Returns:
Dictionary with 'email' and 'id' keys, or None if not authenticated
"""
await self._ensure_client()
assert self._client is not None # Type guard for mypy
url = urljoin(self.config.api_url, "/whoami")
try:
response = await self._client.post(url)
if response.is_success:
payload = self._safe_parse_json(response)
if payload.get("email"):
return {"email": payload["email"], "id": payload["id"]}
if 500 <= response.status_code < 600:
error_details = ""
try:
error_json = response.json()
if isinstance(error_json, dict) and "error" in error_json:
error_details = f" {error_json['error']}"
except Exception:
pass
raise RuntimeError(
f"Server error ({response.status_code}): {response.reason_phrase}.{error_details} "
"This may be due to temporary service issues."
)
return None
except httpx.RequestError as e:
# Return None for network errors, but re-raise server errors
if "Server error" in str(e):
raise
return None
async def batch_localize_objects(
self, objects: List[Dict[str, Any]], params: Dict[str, Any]
) -> List[Dict[str, Any]]:
"""
Localize multiple objects concurrently
Args:
objects: List of objects to localize
params: Localization parameters
Returns:
List of localized objects
"""
tasks = []
for obj in objects:
task = self.localize_object(obj, params, concurrent=True)
tasks.append(task)
return await asyncio.gather(*tasks)
@classmethod
async def quick_translate(
cls,
content: Any,
api_key: str,
target_locale: str,
source_locale: Optional[str] = None,
api_url: str = "https://engine.lingo.dev",
fast: bool = True,
) -> Any:
"""
Quick one-off translation without manual context management.
Automatically handles the async context manager.
Args:
content: Text string or dict to translate
api_key: Your Lingo.dev API key
target_locale: Target language code (e.g., 'es', 'fr')
source_locale: Source language code (optional, auto-detected if None)
api_url: API endpoint URL
fast: Enable fast mode for quicker translations
Returns:
Translated content (same type as input)
Example:
# Translate text
result = await LingoDotDevEngine.quick_translate(
"Hello world",
"your-api-key",
"es"
)
# Translate object
result = await LingoDotDevEngine.quick_translate(
{"greeting": "Hello", "farewell": "Goodbye"},
"your-api-key",
"es"
)
"""
config = {
"api_key": api_key,
"api_url": api_url,
}
async with cls(config) as engine:
params = {
"source_locale": source_locale,
"target_locale": target_locale,
"fast": fast,
}
if isinstance(content, str):
return await engine.localize_text(content, params)
elif isinstance(content, dict):
return await engine.localize_object(content, params, concurrent=True)
else:
raise ValueError("Content must be a string or dictionary")
@classmethod
async def quick_batch_translate(
cls,
content: Any,
api_key: str,
target_locales: List[str],
source_locale: Optional[str] = None,
api_url: str = "https://engine.lingo.dev",
fast: bool = True,
) -> List[Any]:
"""
Quick batch translation to multiple target locales.
Automatically handles the async context manager.
Args:
content: Text string or dict to translate
api_key: Your Lingo.dev API key
target_locales: List of target language codes (e.g., ['es', 'fr', 'de'])
source_locale: Source language code (optional, auto-detected if None)
api_url: API endpoint URL
fast: Enable fast mode for quicker translations
Returns:
List of translated content (one for each target locale)
Example:
results = await LingoDotDevEngine.quick_batch_translate(
"Hello world",
"your-api-key",
["es", "fr", "de"]
)
# Results: ["Hola mundo", "Bonjour le monde", "Hallo Welt"]
"""
config = {
"api_key": api_key,
"api_url": api_url,
}
async with cls(config) as engine:
if isinstance(content, str):
batch_params = {
"source_locale": source_locale,
"target_locales": target_locales,
"fast": fast,
}
return await engine.batch_localize_text(content, batch_params)
elif isinstance(content, dict):
# For objects, run concurrent translations to each target locale
tasks = []
for target_locale in target_locales:
task_params = {
"source_locale": source_locale,
"target_locale": target_locale,
"fast": fast,
}
task = engine.localize_object(content, task_params, concurrent=True)
tasks.append(task)
return await asyncio.gather(*tasks)
else:
raise ValueError("Content must be a string or dictionary")