Java SDK¶
QuickStart¶
The quickest way to get started with Taskmonk is use the upload file and download of output features as shown below. The projectId will be provided by the annotation partner
1 import io.taskmonk.auth.OAuthClientCredentials;
2 import io.taskmonk.client.TaskMonkClient;
3
4 import java.io.File;
5
6 public class Test {
7 public static void main(String[] args) throws InterruptedException {
8 String projectId = "projectId";
9 /**
10 * Initialize the taskmonk client witht he oauth credentials and projectId
11 */
12 TaskMonkClient client = new TaskMonkClient(projectId, "demo.taskmonk.io",
13 new OAuthClientCredentials("uIUSPlDMnH8gLEIrnlkdIPRE6bZYhHpw", "zsYgKGLUnftFgkASD8pndMwn3viA0IPoGKAiw6S7aVukgMWI8hGJflFs0P2QYxTg"));
14
15 /*
16 * Upload the tasks csv to a new batch that will be created with name batchName
17 */
18 String batchId = client.uploadTasks("batchName", new File("/Users/taskmonk/tmp.csv")).batchId;
19
20 /*
21 * check the returned batch id
22 */
23 System.out.println("task batch id = " + batchId);
24
25 /*
26 * Wait while the tasks are being uploaded to the database
27 */
28 while (!client.isUploadComplete(batchId)) {
29 System.out.println("Upload Not Completed");
30 Thread.sleep(1000);
31 }
32 System.out.println("Upload Completed");
33
34 /*
35 * Wait while the tasks are being processed by the analysts
36 */
37 while (!client.isProcessComplete(batchId)) {
38 System.out.println("Processing not complete");
39 Thread.sleep(1000);
40 }
41
42 /*
43 * Get the output in a local csv file
44 */
45 String outputPath = client.getBatchOutput(batchId);
46 System.out.println("outputPath = " + outputPath);
47 }
48 }
The sample code can be cloned from Github <https://github.com/taskmonk/taskmonkSDK/tree/master/java/samples>
Integration¶
A more detailed version for further integration is shown below:
1 import io.taskmonk.auth.OAuthClientCredentials;
2 import io.taskmonk.client.TaskMonkClient;
3 import io.taskmonk.entities.*;
4
5 import java.io.File;
6 import java.time.LocalDate;
7 import java.time.ZoneId;
8 import java.util.*;
9
10 public class Test {
11 public static Date getDate(LocalDate localDate) {
12 return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
13 }
14
15 public static void main(String[] args) throws Exception {
16
17 TaskMonkClient client = new TaskMonkClient("https://preprod.taskmonk.io",
18 new OAuthClientCredentials("client_id", "client_secret"));
19 String projectId = "1";
20 /*
21 * Create a new batch
22 */
23 String comments = "Special Directions to Annotation Partner";
24 Map<String, String> metaData = new HashMap<String, String>();
25 metaData.put("email_address", "info@taskmonk.ai");
26 Notification notification = new Notification("Email", metaData);
27
28 List<Notification> notifications = new ArrayList<Notification>();
29 notifications.add(notification);
30
31 LocalDate tomorrow = LocalDate.now().plusDays(1);
32 NewBatchData newBatchData = new NewBatchData()
33 .setPriority(5)
34 .setNotifications(notifications)
35 .setComments(comments)
36 .setStartTime(getDate(tomorrow));
37 String batchId = client.createBatch("newBatch", newBatchData);
38 System.out.println("batchId = " + batchId);
39
40 // Update the priority
41 System.out.println(client.updateBatchPriority(batchId, 20));
42
43
44 /*
45 * Upload files from a file
46 */
47 TaskImportResponse taskImportResponse = client.uploadTasks(projectId, batchId, new File("/Users/taskmonk/tmp.xlsx"));
48 System.out.println("task import job id = " + taskImportResponse.job_id);
49
50 JobProgressResponse jobProgressResponse = client.getJobProgress(taskImportResponse.job_id);
51 while (!jobProgressResponse.isCompleted()) {
52 System.out.println("waiting for job to complete = " + jobProgressResponse.percentage);
53 Thread.sleep(1000);
54 jobProgressResponse = client.getJobProgress(taskImportResponse.job_id);
55 }
56 System.out.println("Completed task import for batch " + batchId);
57
58 Map<String, String> input = new HashMap<String, String>();
59 input.put("Serial", "1");
60 Task task = new Task(batchId, "1", input);
61 List<Task> tasks = new ArrayList<Task>();
62 tasks.add(task);
63 String uploadJobId = client.uploadTasks(batchId, tasks);
64
65 jobProgressResponse = client.getJobProgress(uploadJobId);
66 while (!jobProgressResponse.isCompleted()) {
67 System.out.println("waiting for job to complete = " + jobProgressResponse.percentage);
68 Thread.sleep(1000);
69 jobProgressResponse = client.getJobProgress(uploadJobId);
70 }
71
72 /*
73 * Get status of batch completion by analysts
74 */
75
76 BatchStatus batchStatus = client.getBatchStatus(batchId);
77 System.out.println("Completed = " + batchStatus.getCompleted());
78 if (batchStatus.getCompleted() == batchStatus.getTotal()) {
79 System.out.println("Batch is complete");
80 // Get the output of the batch
81 String outputPath = "/tmp/output.xlsx";
82 client.getBatchOutput(batchId, "Excel", outputPath);
83 System.out.println("Batch output saved in " + outputPath);
84 }
85
86 }
87 }
Streaming¶
Taskmonk supports a streaming interface built over Azure queues. A send and receive queue pair is created for each project and real-time information can be shared accross this. The queue identifiers and the accessKey for the queues will be provided by Taskmonk for each project.
The following events are supported from Taskmonk to the client:
Task Update
This is sent when a task is completed by the Annotation Partner. The format is shown below:
{
"message_type": "task_update",
"task": {
"project_id": "projectid1",
"batch_id": "batchid1",
"fields": {
"in_fielda": "valuea",
"in_fieldb": "valueb",
"out_fieldc": "valuec",
"out_fieldd": "valued"
}
}
}
Batch Status
This is sent whenever a batch event occurs. Some of the events include
Change in ETA
Change in Status - Completed, Cancelled, Active
The format is shown below:
{
"message_type": "batch_status",
"batch": {
{
"completed": 0,
"rejected": 0,
"batch_id": "string",
"state": "string",
"eta": "string",
"created_date": "2019-10-09",
"batch_name": "string",
"total": 0,
"pending": 0,
"eta_type": "string"
}
}
The following events can be sent from the client to Taskmonk:
New Task
This is used to upload a set of tasks to Taskmonk. The format is shown below:
{
"message_type": "new_tasks",
"task": [{
"batch_id": "batchid1",
"external_id": "item1",
"data": {
"in_fielda": "valuea1",
"in_fieldb": "valueb1",
},
{
"batch_id": "batchid1",
"external_id": "item1",
"data": {
"in_fielda": "valuea2",
"in_fieldb": "valueb2",
}
]
}
}
Sample Code¶
1 /*
2 * Setup the task streamer
3 */
4
5 String queueName = "testqueue_fomclient";
6 String accessKey = "access_key";
7 StreamWriter sender = new StreamWriter(queueName, accessKey);
8
9 /*
10 * Send a task on the stream
11 */
12 Map<String, String> input = new HashMap<String, String>();
13 input.put("input1", "value1");
14 Map<String, String> output = new HashMap<String, String>();
15 Task task = new Task(UUID.randomUUID().toString(),
16 "batch_id",
17 input);
18 List<Task> tasks = new ArrayList<Task>();
19 tasks.add(task);
20 sender.send("project_id", "batch_id", tasks);
21
22 /*
23 * Consume results on the stream
24 */
25 queueName = "testqueue_toclient";
26 accessKey = "access_key";
27 StreamListener listener = new StreamListener(queueName, accessKey);
28 listener.addListener(new MessageListener() {
29 @Override
30 public MessageAction onTaskUpdate(Task task) {
31 System.out.println("Got updated task {}" + task.externalId);
32 return MessageAction.COMPLETE;
33 }
34
35 @Override
36 public MessageAction onBatchStatus(BatchStatus batchStatus) {
37 System.out.println("Got batch status {}" + batchStatus);
38 return MessageAction.COMPLETE;
39 }
40
41 @Override
42 public MessageAction onGenericMessage(String message) {
43 return null;
44 }
45 });
** Contact TaskMonk for the queue and access keys to use for the project. **
Documentation¶
SDK documentation is available at REST API.
Quickstart with maven¶
Add the following dependency
<dependency>
<groupId>ai.taskmonk</groupId>
<artifactId>taskmonk-sdk</artifactId>
<version>1.16.2</version>
</dependency>