Skip to content

Commit 51fce49

Browse files
authored
Add support for image and audio translation
1 parent da64cd0 commit 51fce49

32 files changed

Lines changed: 1830 additions & 253 deletions

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import com.translated.lara.translator.TextResult;
4848
import com.translated.lara.errors.LaraException;
4949
import java.util.Arrays;
5050
import java.util.List;
51+
import java.util.Map;
5152

5253
public class Example {
5354
public static void main(String[] args) {
@@ -287,6 +288,17 @@ Glossary glossary = lara.glossaries.create("MyGlossary");
287288
File csvFile = new File("/path/to/your/glossary.csv"); // Replace with actual CSV file path
288289
GlossaryImport glossaryImport = lara.glossaries.importCsv("gls_1A2b3C4d5E6f7G8h9I0jKl", csvFile);
289290

291+
// Add (or replace) individual terms to glossary
292+
List<Map<String, String>> terms = Arrays.asList(
293+
Map.of("language", "fr-FR", "value", "Bonjour"),
294+
Map.of("language", "es-ES", "value", "Hola")
295+
);
296+
Object addResult = lara.glossaries.addOrReplaceEntry("gls_1A2b3C4d5E6f7G8h9I0jKl", terms, null);
297+
298+
// Remove a specific term from glossary
299+
Map<String, String> termToRemove = Map.of("language", "fr-FR", "value", "Bonjour");
300+
Object removeResult = lara.glossaries.deleteEntry("gls_1A2b3C4d5E6f7G8h9I0jKl", termToRemove, null);
301+
290302
// Check import status
291303
GlossaryImport importStatus = lara.glossaries.getImportStatus("gls_1A2b3C4d5E6f7G8h9I0jKl");
292304

examples/DocumentTranslation.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ public static void main(String[] args) {
104104

105105
// Download translated document
106106
System.out.println("\nStep 3: Downloading would happen after translation completes...");
107+
108+
while(status.getStatus() != Document.Status.TRANSLATED) {
109+
Thread.sleep(1000); // Wait for 1 second before checking status again
110+
status = lara.documents.status(document.getId());
111+
System.out.println("Current status: " + status.getStatus());
112+
}
107113

108114
InputStream fileStream = lara.documents.download(document.getId());
109115

examples/GlossariesManagement.java

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.io.File;
66
import java.io.FileOutputStream;
77
import java.io.IOException;
8+
import java.util.Arrays;
89
import java.util.List;
910
import java.util.Map;
1011

@@ -13,6 +14,7 @@
1314
*
1415
* This example demonstrates:
1516
* - Create, list, update, delete glossaries
17+
* - Individual term management (add/remove terms)
1618
* - CSV import with status monitoring
1719
* - Glossary export
1820
* - Glossary terms count
@@ -52,18 +54,38 @@ public static void main(String[] args) {
5254
if (retrievedGlossary != null) {
5355
System.out.println("📖 Glossary: " + retrievedGlossary.getName() + " (Owner: " + retrievedGlossary.getOwnerId() + ")");
5456
}
55-
56-
// Get glossary terms count
57-
GlossaryCounts counts = lara.glossaries.counts(glossaryId);
58-
for (Map.Entry<String, Integer> entry : counts.getUnidirectional().entrySet()) {
59-
System.out.println(" " + entry.getKey() + ": " + entry.getValue() + " entries");
60-
}
6157

6258
// Update glossary
6359
Glossary updatedGlossary = lara.glossaries.update(glossaryId, "UpdatedDemoGlossary");
6460
System.out.println("📝 Updated name: '" + glossary.getName() + "' -> '" + updatedGlossary.getName() + "'");
6561

66-
// Example 3: CSV import functionality
62+
// Example 3: Term management
63+
System.out.println("=== Term Management ===");
64+
65+
// Add (or replace) individual terms to glossary
66+
try {
67+
List<Map<String, String>> terms = Arrays.asList(
68+
Map.of("language", "fr-FR", "value", "Bonjour"),
69+
Map.of("language", "es-ES", "value", "Hola")
70+
);
71+
Object addResult = lara.glossaries.addOrReplaceEntry(glossaryId, terms, null);
72+
System.out.println("✅ Terms added successfully to glossary");
73+
System.out.println();
74+
} catch (LaraException e) {
75+
System.out.println("⚠️ Could not add terms: " + e.getMessage() + "\n");
76+
}
77+
78+
// Remove a specific term from glossary
79+
try {
80+
Map<String, String> termToRemove = Map.of("language", "fr-FR", "value", "Bonjour");
81+
Object removeResult = lara.glossaries.deleteEntry(glossaryId, termToRemove, null);
82+
System.out.println("✅ Term removed successfully from glossary");
83+
System.out.println();
84+
} catch (LaraException e) {
85+
System.out.println("⚠️ Could not remove term: " + e.getMessage() + "\n");
86+
}
87+
88+
// Example 4: CSV import functionality
6789
System.out.println("=== CSV Import Functionality ===");
6890

6991
// Replace with your actual CSV file path
@@ -97,12 +119,12 @@ public static void main(String[] args) {
97119
System.out.println("CSV file not found: " + csvFilePath);
98120
}
99121

100-
// Example 4: Export functionality
122+
// Example 5: Export functionality
101123
System.out.println("=== Export Functionality ===");
102124
try {
103125
// Export as CSV table unidirectional format
104126
System.out.println("📤 Exporting as CSV table unidirectional...");
105-
String csvUniData = lara.glossaries.export(glossaryId, "csv/table-uni", "en-US");
127+
String csvUniData = lara.glossaries.export(glossaryId, Glossary.Type.CSV_TABLE_UNI, "en-US");
106128
System.out.println("✅ CSV unidirectional export successful (" + csvUniData.length() + " bytes)");
107129

108130
// Save sample exports to files - replace with your desired output paths
@@ -114,7 +136,7 @@ public static void main(String[] args) {
114136
System.out.println("Error with export: " + e.getMessage() + "\n");
115137
}
116138

117-
// Example 5: Glossary Terms Count
139+
// Example 6: Glossary Terms Count
118140
System.out.println("=== Glossary Terms Count ===");
119141
try {
120142
// Get detailed counts

examples/ImageTranslation.java

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
import com.translated.lara.Credentials;
2+
import com.translated.lara.errors.LaraException;
3+
import com.translated.lara.translator.*;
4+
5+
import java.io.*;
6+
import java.nio.file.Files;
7+
import java.nio.file.Paths;
8+
import java.util.List;
9+
10+
/**
11+
* Complete image translation examples for the Lara Java SDK
12+
*
13+
* This example demonstrates:
14+
* - Basic image translation (returns translated image)
15+
* - Image translation with advanced options (memories, glossaries, style)
16+
* - Text extraction from images with translation
17+
* - Text extraction with verbose output for paragraph details
18+
*/
19+
public class ImageTranslation {
20+
21+
public static void main(String[] args) {
22+
// All examples can use environment variables for credentials:
23+
// export LARA_ACCESS_KEY_ID="your-access-key-id"
24+
// export LARA_ACCESS_KEY_SECRET="your-access-key-secret"
25+
26+
// Set your credentials here
27+
String accessKeyId = System.getenv("LARA_ACCESS_KEY_ID");
28+
String accessKeySecret = System.getenv("LARA_ACCESS_KEY_SECRET");
29+
30+
Credentials credentials = new Credentials(accessKeyId, accessKeySecret);
31+
Translator lara = new Translator(credentials);
32+
33+
// Replace with your actual image file path
34+
String sampleImagePath = "sample_image.png"; // Create this file with your content
35+
File sampleImage = new File(sampleImagePath);
36+
37+
if (!sampleImage.exists()) {
38+
System.out.println("Please create a sample image file at: " + sampleImagePath);
39+
System.out.println("Use an image containing text to translate.\n");
40+
return;
41+
}
42+
43+
// Example 1: Basic image translation
44+
System.out.println("=== Basic Image Translation ===");
45+
String targetLang = "de-DE";
46+
47+
System.out.println("Translating image: " + sampleImage.getName() + " to " + targetLang);
48+
49+
try {
50+
InputStream imageStream = lara.images.translate(sampleImage, targetLang);
51+
52+
// Save translated image - replace with your desired output path
53+
String outputPath = "sample_image_translated.png";
54+
Files.copy(imageStream, Paths.get(outputPath));
55+
imageStream.close();
56+
57+
System.out.println("✅ Image translation completed");
58+
System.out.println("📄 Translated image saved to: " + outputPath + "\n");
59+
} catch (LaraException | IOException e) {
60+
System.out.println("Error translating image: " + e.getMessage() + "\n");
61+
return;
62+
}
63+
64+
// Example 2: Image translation with source language and advanced options
65+
System.out.println("=== Image Translation with Advanced Options ===");
66+
String sourceLang = "en-US";
67+
68+
try {
69+
ImageTranslateOptions options = new ImageTranslateOptions()
70+
.setAdaptTo("mem_1A2b3C4d5E6f7G8h9I0jKl") // Replace with actual memory IDs
71+
.setGlossaries("gls_1A2b3C4d5E6f7G8h9I0jKl") // Replace with actual glossary IDs
72+
.setStyle(TranslationStyle.FLUID)
73+
.setNoTrace(false);
74+
75+
InputStream imageStream = lara.images.translate(sampleImage, sourceLang, "fr-FR", options);
76+
77+
String outputPath = "advanced_image_translated.png";
78+
Files.copy(imageStream, Paths.get(outputPath));
79+
imageStream.close();
80+
81+
System.out.println("✅ Advanced image translation completed");
82+
System.out.println("📄 Translated image saved to: " + outputPath + "\n");
83+
} catch (LaraException | IOException e) {
84+
System.out.println("Error in advanced translation: " + e.getMessage() + "\n");
85+
return;
86+
}
87+
88+
// Example 3: Extract and translate text from image (basic)
89+
System.out.println("=== Text Extraction and Translation from Image ===");
90+
91+
try {
92+
ImageTextResult result = lara.images.translateText(sampleImage, "es-ES");
93+
94+
System.out.println("✅ Text extraction and translation completed");
95+
System.out.println("🌍 Detected source language: " + result.getSourceLanguage());
96+
System.out.println("📝 Extracted paragraphs:");
97+
98+
List<ImageParagraph> paragraphs = result.getParagraphs();
99+
for (int i = 0; i < paragraphs.size(); i++) {
100+
ImageParagraph paragraph = paragraphs.get(i);
101+
System.out.println("\n Paragraph " + (i + 1) + ":");
102+
System.out.println(" Original: " + paragraph.getText());
103+
System.out.println(" Translated: " + paragraph.getTranslation());
104+
}
105+
System.out.println();
106+
} catch (LaraException e) {
107+
System.out.println("Error extracting text: " + e.getMessage() + "\n");
108+
return;
109+
}
110+
111+
// Example 4: Extract and translate text with advanced options
112+
System.out.println("=== Text Extraction with Advanced Options ===");
113+
114+
try {
115+
ImageTextTranslateOptions options = new ImageTextTranslateOptions()
116+
.setAdaptTo("mem_1A2b3C4d5E6f7G8h9I0jKl") // Replace with actual memory IDs
117+
.setGlossaries("gls_1A2b3C4d5E6f7G8h9I0jKl") // Replace with actual glossary IDs
118+
.setStyle(TranslationStyle.CREATIVE)
119+
.setVerbose(true) // Include detailed matching information
120+
.setNoTrace(false);
121+
122+
ImageTextResult result = lara.images.translateText(sampleImage, sourceLang, "it-IT", options);
123+
124+
System.out.println("✅ Advanced text extraction completed");
125+
System.out.println("🌍 Source language: " + result.getSourceLanguage());
126+
127+
// Show adapted-to resources used
128+
List<String> adaptedTo = result.getAdaptedTo();
129+
if (adaptedTo != null && !adaptedTo.isEmpty()) {
130+
System.out.println("📚 Adapted to memories: " + String.join(", ", adaptedTo));
131+
}
132+
133+
// Show glossaries used
134+
List<String> glossaries = result.getGlossaries();
135+
if (glossaries != null && !glossaries.isEmpty()) {
136+
System.out.println("📖 Glossaries applied: " + String.join(", ", glossaries));
137+
}
138+
139+
System.out.println("\n📝 Paragraphs with detailed matches:");
140+
List<ImageParagraph> paragraphs = result.getParagraphs();
141+
for (int i = 0; i < paragraphs.size(); i++) {
142+
ImageParagraph paragraph = paragraphs.get(i);
143+
System.out.println("\n Paragraph " + (i + 1) + ":");
144+
System.out.println(" Original: " + paragraph.getText());
145+
System.out.println(" Translated: " + paragraph.getTranslation());
146+
147+
// Show memory matches if available
148+
if (paragraph.getAdaptedToMatches() != null && !paragraph.getAdaptedToMatches().isEmpty()) {
149+
System.out.println(" Memory matches found: " + paragraph.getAdaptedToMatches().size());
150+
}
151+
152+
// Show glossary matches if available
153+
if (paragraph.getGlossariesMatches() != null && !paragraph.getGlossariesMatches().isEmpty()) {
154+
System.out.println(" Glossary matches found: " + paragraph.getGlossariesMatches().size());
155+
}
156+
}
157+
System.out.println();
158+
} catch (LaraException e) {
159+
System.out.println("Error in advanced text extraction: " + e.getMessage() + "\n");
160+
}
161+
162+
System.out.println("=== All examples completed ===");
163+
}
164+
}
165+

examples/TextTranslation.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
import java.util.Arrays;
66
import java.util.List;
7+
import java.util.function.Consumer;
78

89
/**
910
* Complete text translation examples for the Lara Java SDK
10-
*
11+
*
1112
* This example demonstrates:
1213
* - Single string translation
1314
* - Multiple strings translation
@@ -120,7 +121,28 @@ public static void main(String[] args) {
120121
return;
121122
}
122123

123-
// Example 7: Get available languages
124+
// Example 7: Translation with reasoning
125+
System.out.println("=== Translation with Reasoning ===");
126+
try {
127+
TranslateOptions reasoningOptions = new TranslateOptions()
128+
.setReasoning(true);
129+
130+
System.out.println("Original: Wonderful cavernous interior in a central but quiet and private area!");
131+
System.out.println("Streaming partial results:");
132+
133+
Consumer<TextResult> streamingCallback = partialResult -> {
134+
System.out.println("Partial result: " + partialResult.getTranslation());
135+
};
136+
137+
TextResult finalResult = lara.translate("Wonderful cavernous interior in a central but quiet and private area!", "en-US", "it-IT", reasoningOptions, streamingCallback);
138+
139+
System.out.println("Final result: " + finalResult.getTranslation() + "\n");
140+
} catch (LaraException e) {
141+
System.out.println("Error with reasoning translation: " + e.getMessage() + "\n");
142+
return;
143+
}
144+
145+
// Example 8: Get available languages
124146
System.out.println("=== Available Languages ===");
125147
try {
126148
List<String> languages = lara.getLanguages();
@@ -130,7 +152,7 @@ public static void main(String[] args) {
130152
return;
131153
}
132154

133-
// Example 8: Detect language of a given text
155+
// Example 9: Detect language of a given text
134156
System.out.println("=== Language Detection ===");
135157
try {
136158
DetectResult detectResult = lara.detect("Hola, ¿cómo estás?");
@@ -141,7 +163,7 @@ public static void main(String[] args) {
141163
return;
142164
}
143165

144-
// Example 9: Detect languages with hint and passlist
166+
// Example 10: Detect languages with hint and passlist
145167
System.out.println("=== Language Detection with Hint and Passlist ===");
146168
try {
147169
DetectResult detectResult = lara.detect("Hola, ¿cómo estás?", "es", new String[]{"es", "pt", "it"});

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.translated.lara</groupId>
66
<artifactId>lara-sdk</artifactId>
7-
<version>1.6.3</version>
7+
<version>1.6.0-SNAPSHOT</version>
88
<packaging>jar</packaging>
99

1010
<name>Lara SDK</name>

0 commit comments

Comments
 (0)