-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCustomVocabularies.java
More file actions
83 lines (70 loc) · 3.52 KB
/
Copy pathCustomVocabularies.java
File metadata and controls
83 lines (70 loc) · 3.52 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
import ai.rev.speechtotext.CustomVocabulariesClient;
import ai.rev.speechtotext.models.vocabulary.CustomVocabulary;
import ai.rev.speechtotext.models.vocabulary.CustomVocabularyInformation;
import ai.rev.speechtotext.models.vocabulary.CustomVocabularyStatus;
import ai.rev.speechtotext.models.vocabulary.CustomVocabularySubmission;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class CustomVocabularies {
public static void main(String[] args) {
// Assign your access token to a String
String accessToken = "<YOUR_ACCESS_TOKEN>";
// Initialize the CustomVocabulariesClient with your access token
CustomVocabulariesClient customVocabulariesClient = new CustomVocabulariesClient(accessToken);
// Create a custom vocabulary for your submission
CustomVocabulary customVocabulary =
new CustomVocabulary(Arrays.asList("Robert Berwick", "Noam Chomsky", "Evelina Fedorenko"));
// Set up the notification url if desired
String notificationUrl = "https://example.com";
// Authorization header is optional; use it if needed to access the callback notification url
Map<String, String> notificationAuth = new HashMap<>();
notificationAuth.put("Authorization", "Bearer <callback_token>");
CustomVocabularySubmission customVocabularySubmission = new CustomVocabularySubmission();
customVocabularySubmission.setCustomVocabularies(Arrays.asList(customVocabulary));
customVocabularySubmission.setNotificationConfig(notificationUrl, notificationAuth);
customVocabularySubmission.setMetadata("My first submission");
CustomVocabularyInformation submittedCustomVocabularyInfo;
try {
submittedCustomVocabularyInfo = customVocabulariesClient.submitCustomVocabularies(customVocabularySubmission);
} catch (IOException e) {
throw new RuntimeException("Failed to submit custom vocabulary " + e.getMessage());
}
String customVocabularyId = submittedCustomVocabularyInfo.getId();
System.out.println("Vocabulary Id: " + customVocabularyId);
System.out.println("Vocabulary Status: " + submittedCustomVocabularyInfo.getStatus());
System.out.println("Created On: " + submittedCustomVocabularyInfo.getCreatedOn());
// Waits 5 seconds between each status check to see if job is complete
boolean isProcessingComplete = false;
while (!isProcessingComplete) {
CustomVocabularyInformation retrievedVocabularyInfo;
try {
retrievedVocabularyInfo =
customVocabulariesClient.getCustomVocabularyInformation(customVocabularyId);
} catch (IOException e) {
String message = String.format("Failed to retrieve custom vocabulary info [{0}] {1}", customVocabularyId,
e.getMessage());
throw new RuntimeException(message);
}
CustomVocabularyStatus retrievedVocabularyInfoStatus = retrievedVocabularyInfo.getStatus();
if (retrievedVocabularyInfoStatus == CustomVocabularyStatus.COMPLETE
|| retrievedVocabularyInfoStatus == CustomVocabularyStatus.FAILED) {
System.out.println(
"Custom vocabulary processing for "
+ customVocabularyId
+ ": "
+ retrievedVocabularyInfoStatus.getStatus());
isProcessingComplete = true;
} else {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
// Once the custom vocabulary processing status is "complete" it can now be used in any
// streaming job
}
}