1 | package org.atea.nlptools.koreromaoriinterface.services;
|
---|
2 |
|
---|
3 | import java.io.InputStream;
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.LinkedList;
|
---|
6 | import java.util.List;
|
---|
7 | import java.util.Queue;
|
---|
8 | import java.util.concurrent.Callable;
|
---|
9 | import java.util.concurrent.ExecutorService;
|
---|
10 | import java.util.concurrent.Future;
|
---|
11 |
|
---|
12 | import com.google.gson.Gson;
|
---|
13 |
|
---|
14 | import org.apache.logging.log4j.LogManager;
|
---|
15 | import org.apache.logging.log4j.Logger;
|
---|
16 | import org.atea.nlptools.koreromaoriinterface.models.TranscriptionResult;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Functions to interact with the Reo Tuhituhi API.
|
---|
20 | */
|
---|
21 | public class ReoTuhituhiApiService
|
---|
22 | {
|
---|
23 | private static final Logger logger = LogManager.getLogger(ReoTuhituhiApiService.class);
|
---|
24 |
|
---|
25 | private final ExecutorService threadPool;
|
---|
26 | private final Gson jsonSerialiser;
|
---|
27 | private final String apiEndpoint;
|
---|
28 | private final String apiKey;
|
---|
29 |
|
---|
30 | public ReoTuhituhiApiService(Gson jsonSerialiser, String apiEndpoint, String apiKey)
|
---|
31 | {
|
---|
32 | this.jsonSerialiser = jsonSerialiser;
|
---|
33 | this.apiEndpoint = apiEndpoint;
|
---|
34 | this.apiKey = apiKey;
|
---|
35 |
|
---|
36 | threadPool = java.util.concurrent.Executors.newFixedThreadPool(3);
|
---|
37 | }
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Queries the Reo Tuhituhi API to transcribe the given audio files.
|
---|
41 | *
|
---|
42 | * @param audioFileStreams The audio files to retrieve a transcription for.
|
---|
43 | * @return A list of {@link TranscriptionResult} objects.
|
---|
44 | * @throws Exception
|
---|
45 | */
|
---|
46 | public List<TranscriptionResult> GetTranscriptions(Iterable<InputStream> audioFileStreams)
|
---|
47 | throws Exception
|
---|
48 | {
|
---|
49 | Queue<Future<TranscriptionResult>> apiCalls = new LinkedList<Future<TranscriptionResult>>();
|
---|
50 |
|
---|
51 | // Queue each transcription request up asynchronously
|
---|
52 | for (InputStream audioStream : audioFileStreams)
|
---|
53 | {
|
---|
54 | //Callable<TranscriptionResult> transcriptionTask = getTranscriptionCallable(audioStream);
|
---|
55 | //apiCalls.add(threadPool.submit(transcriptionTask));
|
---|
56 | //logger.debug("Adding transcription task to thread pool.");
|
---|
57 | }
|
---|
58 |
|
---|
59 | List<TranscriptionResult> apiResults = new ArrayList<TranscriptionResult>(apiCalls.size());
|
---|
60 |
|
---|
61 | for (InputStream audioStream : audioFileStreams)
|
---|
62 | {
|
---|
63 | apiResults.add(getTranscription(audioStream));
|
---|
64 | logger.debug("API call has completed.");
|
---|
65 | }
|
---|
66 |
|
---|
67 | // Wait on the result of each call
|
---|
68 | // TODO: Implement proper timeout here
|
---|
69 | // while (!apiCalls.isEmpty())
|
---|
70 | // {
|
---|
71 | // apiResults.add(apiCalls.remove().get());
|
---|
72 | // logger.debug("API call has completed.");
|
---|
73 | // }
|
---|
74 |
|
---|
75 | return apiResults;
|
---|
76 | }
|
---|
77 |
|
---|
78 | private Callable<TranscriptionResult> getTranscriptionCallable(final InputStream audioStream)
|
---|
79 | {
|
---|
80 | return new Callable<TranscriptionResult>()
|
---|
81 | {
|
---|
82 | @Override
|
---|
83 | public TranscriptionResult call() throws Exception
|
---|
84 | {
|
---|
85 | return getTranscription(audioStream);
|
---|
86 | }
|
---|
87 | };
|
---|
88 | }
|
---|
89 |
|
---|
90 | private TranscriptionResult getTranscription(InputStream audioStream)
|
---|
91 | throws Exception
|
---|
92 | {
|
---|
93 | String jsonResponse = HttpRequestService
|
---|
94 | .post(apiEndpoint)
|
---|
95 | .authorization("Basic " + apiKey)
|
---|
96 | .send(audioStream)
|
---|
97 | .body();
|
---|
98 |
|
---|
99 | logger.debug("Reo Tuhituhi API responded with a success status.");
|
---|
100 | TranscriptionResult res = jsonSerialiser.fromJson(jsonResponse, TranscriptionResult.class);
|
---|
101 | logger.debug("Reo Tuhituhi API response was succesfully deserialised.");
|
---|
102 | return res;
|
---|
103 | }
|
---|
104 | }
|
---|