source: gs3-extensions/atea-nlp-tools/trunk/src/koreromaori-proxy/src/main/java/org/atea/nlptools/koreromaoriinterface/services/ReoTuhituhiApiService.java@ 35241

Last change on this file since 35241 was 35241, checked in by davidb, 3 years ago

Implement support for returning proper audio file name

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