Skip to content

Commit 5933363

Browse files
Add support for image and audio translation
1 parent 161ff02 commit 5933363

14 files changed

Lines changed: 1000 additions & 130 deletions

README.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ All major translation features are accessible, making it easy to integrate and c
1010
## 🌍 **Features:**
1111
- **Text Translation**: Single strings, multiple strings, and complex text blocks
1212
- **Document Translation**: Word, PDF, and other document formats with status monitoring
13+
- **Image Translation**: Translate whole images or extract and translate text blocks
14+
- **Audio Translation**: Audio file translation with status monitoring
1315
- **Translation Memory**: Store and reuse translations for consistency
1416
- **Glossaries**: Enforce terminology standards across translations
1517
- **Language Detection**: Automatic source language identification
@@ -87,6 +89,28 @@ cd examples
8789
python document_translation.py
8890
```
8991

92+
### Image Translation
93+
- **[image_translation.py](examples/image_translation.py)** - Image translation examples
94+
- Full image translation with overlay or inpainting
95+
- Text-only extraction and translation
96+
- Style, memories, and glossaries options
97+
98+
```bash
99+
cd examples
100+
python image_translation.py
101+
```
102+
103+
### Audio Translation
104+
- **[audio_translation.py](examples/audio_translation.py)** - Audio translation examples
105+
- Basic audio translation
106+
- Advanced options with memories and glossaries
107+
- Step-by-step translation with status monitoring
108+
109+
```bash
110+
cd examples
111+
python audio_translation.py
112+
```
113+
90114
### Translation Memory Management
91115
- **[memories_management.py](examples/memories_management.py)** - Memory management examples
92116
- Create, list, update, delete memories
@@ -251,6 +275,68 @@ status = lara.documents.status(document.id)
251275
translated_content = lara.documents.download(document.id)
252276
```
253277

278+
### 🖼️ Image Translation
279+
#### Simple image translation
280+
```python
281+
translated_image = lara.images.translate(
282+
source="en-US",
283+
target="fr-FR",
284+
image_path="/path/to/your/image.png", # Replace with actual file path
285+
text_removal="overlay"
286+
)
287+
```
288+
289+
#### Text-only image translation
290+
```python
291+
text_results = lara.images.translate_text(
292+
source="en-US",
293+
target="es-ES",
294+
image_path="/path/to/your/image.png" # Replace with actual file path
295+
)
296+
```
297+
298+
### 🎵 Audio Translation
299+
#### Simple audio translation
300+
```python
301+
audio_content = lara.audio.translate(
302+
file_path="/path/to/your/audio.mp3", # Replace with actual file path
303+
filename="audio.mp3",
304+
source="en-US",
305+
target="fr-FR"
306+
)
307+
308+
# With options
309+
audio_content = lara.audio.translate(
310+
file_path="/path/to/your/audio.mp3", # Replace with actual file path
311+
filename="audio.mp3",
312+
source="en-US",
313+
target="fr-FR",
314+
adapt_to=["mem_1A2b3C4d5E6f7G8h9I0jKl"], # Replace with actual memory IDs
315+
glossaries=["gls_1A2b3C4d5E6f7G8h9I0jKl"] # Replace with actual glossary IDs
316+
)
317+
```
318+
### Audio translation with status monitoring
319+
#### Audio upload
320+
```python
321+
# Optional: upload options
322+
audio = lara.audio.upload(
323+
file_path="/path/to/your/audio.mp3", # Replace with actual file path
324+
filename="audio.mp3",
325+
source="en-US",
326+
target="fr-FR",
327+
adapt_to=["mem_1A2b3C4d5E6f7G8h9I0jKl"], # Replace with actual memory IDs
328+
glossaries=["gls_1A2b3C4d5E6f7G8h9I0jKl"] # Replace with actual glossary IDs
329+
)
330+
```
331+
#### Audio translation status monitoring
332+
```python
333+
status = lara.audio.status(audio.id)
334+
```
335+
#### Download translated audio
336+
```python
337+
audio_content = lara.audio.download(audio.id)
338+
```
339+
254340
### 🧠 Memory Management
255341

256342
```python

examples/audio_translation.py

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
from lara_sdk import AccessKey, Translator, AudioStatus
2+
import os
3+
import time
4+
5+
"""
6+
Complete audio translation examples for the Lara Python SDK
7+
8+
Supported audio formats: .wav, .mp3, .opus, .ogg, .webm
9+
10+
This example demonstrates:
11+
- Basic audio translation
12+
- Advanced options with memories and glossaries
13+
- Step-by-step audio translation with status monitoring
14+
"""
15+
16+
def main():
17+
# All examples use environment variables for credentials, so set them first:
18+
# export LARA_ACCESS_KEY_ID="your-access-key-id"
19+
# export LARA_ACCESS_KEY_SECRET="your-access-key-secret"
20+
21+
# Set your credentials here
22+
access_key_id = os.getenv("LARA_ACCESS_KEY_ID", "your-access-key-id")
23+
access_key_secret = os.getenv("LARA_ACCESS_KEY_SECRET", "your-access-key-secret")
24+
25+
credentials = AccessKey(access_key_id, access_key_secret)
26+
lara = Translator(credentials)
27+
28+
# Replace with your actual audio file path
29+
sample_audio_file = "sample_audio.mp3" # Supported: .wav, .mp3, .opus, .ogg, .webm
30+
31+
if not os.path.exists(sample_audio_file):
32+
print(f"Please create a sample audio file at: {sample_audio_file}")
33+
return
34+
35+
try:
36+
# Example 1: Basic audio translation
37+
print("=== Basic Audio Translation ===")
38+
source_lang = "en-US"
39+
target_lang = "de-DE"
40+
41+
print(f"Translating audio: {os.path.basename(sample_audio_file)} from {source_lang} to {target_lang}")
42+
43+
translated_audio = lara.audio.translate(
44+
file_path=sample_audio_file,
45+
filename=os.path.basename(sample_audio_file),
46+
source=source_lang,
47+
target=target_lang
48+
)
49+
50+
output_path = "sample_audio_translated.mp3"
51+
with open(output_path, 'wb') as f:
52+
f.write(translated_audio)
53+
54+
print("✅ Audio translation completed")
55+
print(f"📄 Translated file saved to: {output_path}\n")
56+
57+
except Exception as error:
58+
print(f"Error translating audio: {error}\n")
59+
return
60+
61+
# Example 2: Audio translation with advanced options
62+
print("=== Audio Translation with Advanced Options ===")
63+
try:
64+
translated_audio2 = lara.audio.translate(
65+
file_path=sample_audio_file,
66+
filename=os.path.basename(sample_audio_file),
67+
source=source_lang,
68+
target=target_lang,
69+
adapt_to=["mem_1A2b3C4d5E6f7G8h9I0jKl"], # Replace with actual memory IDs
70+
glossaries=["gls_1A2b3C4d5E6f7G8h9I0jKl"] # Replace with actual glossary IDs
71+
)
72+
73+
output_path2 = "advanced_audio_translated.mp3"
74+
with open(output_path2, 'wb') as f:
75+
f.write(translated_audio2)
76+
77+
print("✅ Advanced Audio translation completed")
78+
print(f"📄 Translated file saved to: {output_path2}\n")
79+
80+
except Exception as error:
81+
print(f"Error in advanced translation: {error}")
82+
83+
print()
84+
85+
# Example 3: Step-by-step audio translation
86+
print("=== Step-by-Step Audio Translation ===")
87+
88+
try:
89+
# Upload audio
90+
print("Step 1: Uploading audio...")
91+
audio = lara.audio.upload(
92+
file_path=sample_audio_file,
93+
filename=os.path.basename(sample_audio_file),
94+
source=source_lang,
95+
target=target_lang,
96+
adapt_to=["mem_1A2b3C4d5E6f7G8h9I0jKl"], # Replace with actual memory IDs
97+
glossaries=["gls_1A2b3C4d5E6f7G8h9I0jKl"] # Replace with actual glossary IDs
98+
)
99+
print(f"Audio uploaded with ID: {audio.id}")
100+
print(f"Initial status: {audio.status.value}")
101+
102+
# Check status
103+
print("\nStep 2: Checking status...")
104+
updated_audio = lara.audio.status(audio.id)
105+
print(f"Current status: {updated_audio.status.value}")
106+
107+
# Download translated audio
108+
print("\nStep 3: Downloading would happen after translation completes...")
109+
translated_audio3 = lara.audio.download(audio.id)
110+
111+
output_path3 = "step_audio_translated.mp3"
112+
with open(output_path3, 'wb') as f:
113+
f.write(translated_audio3)
114+
115+
print("✅ Step-by-step translation completed")
116+
117+
except Exception as error:
118+
print(f"Error in step-by-step process: {error}")
119+
120+
if __name__ == "__main__":
121+
main()

examples/document_translation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from lara_sdk import Credentials, Translator
1+
from lara_sdk import AccessKey, Translator
22
import os
33

44
"""
@@ -19,7 +19,7 @@ def main():
1919
access_key_id = os.getenv("LARA_ACCESS_KEY_ID", "your-access-key-id")
2020
access_key_secret = os.getenv("LARA_ACCESS_KEY_SECRET", "your-access-key-secret")
2121

22-
credentials = Credentials(access_key_id, access_key_secret)
22+
credentials = AccessKey(access_key_id, access_key_secret)
2323
lara = Translator(credentials)
2424

2525
# Replace with your actual document file path

examples/glossaries_management.py

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from lara_sdk import Credentials, Translator
1+
from lara_sdk import AccessKey, Translator, GlossaryTerm
22
import os
33

44
"""
@@ -10,6 +10,7 @@
1010
- Glossary export
1111
- Glossary terms count
1212
- Import status checking
13+
- Add/replace and delete glossary term entries
1314
"""
1415

1516
def main():
@@ -21,7 +22,7 @@ def main():
2122
access_key_id = os.getenv("LARA_ACCESS_KEY_ID", "your-access-key-id")
2223
access_key_secret = os.getenv("LARA_ACCESS_KEY_SECRET", "your-access-key-secret")
2324

24-
credentials = Credentials(access_key_id, access_key_secret)
25+
credentials = AccessKey(access_key_id, access_key_secret)
2526
lara = Translator(credentials)
2627

2728
print("🗒️ Glossaries require a specific subscription plan.")
@@ -126,6 +127,45 @@ def main():
126127
except Exception as e:
127128
print(f"Error getting glossary terms count: {e}\n")
128129

130+
# Example 6: Add/Replace and Delete glossary term entries
131+
print("=== Glossary Term Entries ===")
132+
try:
133+
# Add a new entry with multiple language terms
134+
terms = [
135+
GlossaryTerm(language="en-US", value="computer"),
136+
GlossaryTerm(language="it-IT", value="computer")
137+
]
138+
add_result = lara.glossaries.add_or_replace_entry(glossary_id, terms)
139+
print(f"Added entry, import ID: {add_result.id}")
140+
141+
# Add another entry with a custom GUID
142+
terms_with_guid = [
143+
GlossaryTerm(language="en-US", value="keyboard"),
144+
GlossaryTerm(language="it-IT", value="tastiera")
145+
]
146+
lara.glossaries.add_or_replace_entry(glossary_id, terms_with_guid, guid="custom-guid-123")
147+
print("Added entry with custom GUID")
148+
149+
# Replace an existing entry by using the same GUID
150+
updated_terms = [
151+
GlossaryTerm(language="en-US", value="keyboard"),
152+
GlossaryTerm(language="it-IT", value="tastiera"),
153+
GlossaryTerm(language="fr-FR", value="clavier")
154+
]
155+
lara.glossaries.add_or_replace_entry(glossary_id, updated_terms, guid="custom-guid-123")
156+
print("Replaced entry using existing GUID")
157+
158+
# Delete an entry by GUID
159+
lara.glossaries.delete_entry(glossary_id, guid="custom-guid-123")
160+
print("Deleted entry by GUID")
161+
162+
# Delete an entry by term
163+
lara.glossaries.delete_entry(glossary_id, term=GlossaryTerm(language="en-US", value="computer"))
164+
print("Deleted entry by term")
165+
print()
166+
except Exception as e:
167+
print(f"Error with term entries: {e}\n")
168+
129169
except Exception as e:
130170
print(f"Error creating glossary: {e}\n")
131171
finally:

0 commit comments

Comments
 (0)