Skip to content

Commit cb2dbb3

Browse files
authored
Add style parameter in text and doc translation
1 parent bb61e7e commit cb2dbb3

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

src/lara_sdk/_translator.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
from ._errors import LaraApiError
1212
from ._s3client import S3Client, S3UploadFields
1313

14+
TranslationStyle = Literal["faithful", "fluid", "creative"]
15+
1416
# Objects --------------------------------------------------------------------------------------------------------------
1517

1618

@@ -63,6 +65,7 @@ class DocumentOptions:
6365
adapt_to: Optional[List[str]] = None
6466
glossaries: Optional[List[str]] = None
6567
no_trace: Optional[bool] = None
68+
style: Optional[TranslationStyle] = None
6669

6770
class Document(LaraObject):
6871

@@ -295,7 +298,8 @@ def __init__(self, client: LaraClient):
295298
self._polling_interval: int = 2
296299

297300
def upload(self, file_path: str, filename: str, target: str, source: Optional[str] = None,
298-
adapt_to: Optional[List[str]] = None, glossaries: Optional[List[str]] = None, no_trace: bool = False) -> Document:
301+
adapt_to: Optional[List[str]] = None, glossaries: Optional[List[str]] = None, no_trace: bool = False,
302+
style: Optional[TranslationStyle] = None) -> Document:
299303
with open(file_path, 'rb') as file_payload:
300304
response_data = self._client.get('/documents/upload-url', {'filename': filename})
301305

@@ -317,6 +321,9 @@ def upload(self, file_path: str, filename: str, target: str, source: Optional[st
317321
if glossaries is not None:
318322
body['glossaries'] = glossaries
319323

324+
if style is not None:
325+
body['style'] = style
326+
320327
headers = None
321328
if no_trace is True:
322329
headers = {'X-No-Trace': 'true'}
@@ -335,10 +342,10 @@ def download(self, id: str, output_format: Optional[str] = None) -> bytes:
335342

336343
def translate(self, file_path: str, filename: str, target: str, source: Optional[str] = None,
337344
adapt_to: Optional[List[str]] = None, glossaries: Optional[List[str]] = None, output_format: Optional[str] = None,
338-
no_trace: bool = False) -> bytes:
345+
no_trace: bool = False, style: Optional[TranslationStyle] = None) -> bytes:
339346

340347
document = self.upload(file_path=file_path, filename=filename, target=target, source=source, adapt_to=adapt_to,
341-
glossaries=glossaries, no_trace=no_trace)
348+
glossaries=glossaries, no_trace=no_trace, style=style)
342349

343350
max_wait_time = 60 * 15 # 15 minutes
344351
start = time.time()
@@ -387,7 +394,7 @@ def translate(self, text: Union[str, Iterable[str], Iterable[TextBlock]], *,
387394
glossaries: List[str] = None, instructions: List[str] = None, content_type: str = None,
388395
multiline: bool = True, timeout_ms: int = None, priority: TranslatePriority = None,
389396
use_cache: Union[bool, UseCache] = None, cache_ttl_s: int = None,
390-
no_trace: bool = False, verbose: bool = False,
397+
no_trace: bool = False, verbose: bool = False, style: Optional[TranslationStyle] = None,
391398
headers: Optional[Dict[str, str]] = None) -> TextResult:
392399
if isinstance(text, str):
393400
q = text
@@ -411,7 +418,7 @@ def translate(self, text: Union[str, Iterable[str], Iterable[TextBlock]], *,
411418
'multiline': multiline, 'adapt_to': adapt_to, 'instructions': instructions, 'timeout': timeout_ms, 'q': q,
412419
'priority': priority.value if priority is not None else None,
413420
'use_cache': use_cache.value if use_cache is not None else None, 'cache_ttl': cache_ttl_s,
414-
'glossaries': glossaries, 'verbose': verbose
421+
'glossaries': glossaries, 'verbose': verbose, 'style': style
415422
}
416423

417424
request_headers = {}

0 commit comments

Comments
 (0)