use of org.opencastproject.transcription.api.TranscriptionServiceException in project opencast by opencast.
the class IBMWatsonTranscriptionService method createRecognitionsJob.
/**
* From: https://www.ibm.com/watson/developercloud/speech-to-text/api/v1/#register_callback:
*
* curl -X POST -u "{username}":"{password}" --header "Content-Type: audio/flac" --data-binary @audio-file.flac
* "https://stream.watsonplatform.net/speech-to-text/api/v1/recognitions?callback_url=http://{user_callback_path}/results&user_token=job25&continuous=true×tamps=true"
*
* Response: { "id": "4bd734c0-e575-21f3-de03-f932aa0468a0", "status": "waiting", "url":
* "http://stream.watsonplatform.net/speech-to-text/api/v1/recognitions/4bd734c0-e575-21f3-de03-f932aa0468a0" }
*/
void createRecognitionsJob(String mpId, Track track) throws TranscriptionServiceException {
if (!callbackAlreadyRegistered)
registerCallback();
// Get audio track file
File audioFile = null;
try {
audioFile = workspace.get(track.getURI());
} catch (Exception e) {
throw new TranscriptionServiceException("Error reading audio track", e);
}
CloseableHttpClient httpClient = makeHttpClient();
String additionalParms = "";
if (callbackAlreadyRegistered) {
additionalParms = String.format("&callback_url=%s&events=%s,%s", callbackUrl, JobEvent.COMPLETED_WITH_RESULTS, JobEvent.FAILED);
}
if (!StringUtils.isEmpty(model)) {
additionalParms += String.format("&model=%s", model);
}
CloseableHttpResponse response = null;
try {
HttpPost httpPost = new HttpPost(IBM_WATSON_SERVICE_URL + RECOGNITIONS + String.format("?user_token=%s&inactivity_timeout=-1×tamps=true&smart_formatting=true%s", mpId, additionalParms));
logger.debug("Url to invoke ibm watson service: {}", httpPost.getURI().toString());
httpPost.setHeader(HttpHeaders.CONTENT_TYPE, track.getMimeType().toString());
httpPost.setEntity(new FileEntity(audioFile));
response = httpClient.execute(httpPost);
int code = response.getStatusLine().getStatusCode();
switch(code) {
case // 201
HttpStatus.SC_CREATED:
logger.info("Recognitions job has been successfully created");
HttpEntity entity = response.getEntity();
// Response returned is a json object:
// {
// "id": "4bd734c0-e575-21f3-de03-f932aa0468a0",
// "status": "waiting",
// "url":
// "http://stream.watsonplatform.net/speech-to-text/api/v1/recognitions/4bd734c0-e575-21f3-de03-f932aa0468a0"
// }
String jsonString = EntityUtils.toString(response.getEntity());
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(jsonString);
String jobId = (String) jsonObject.get("id");
String jobStatus = (String) jsonObject.get("status");
String jobUrl = (String) jsonObject.get("url");
logger.info(String.format("Transcription for mp %s has been submitted. Job id: %s, job status: %s, job url: %s", mpId, jobId, jobStatus, jobUrl));
database.storeJobControl(mpId, track.getIdentifier(), jobId, TranscriptionJobControl.Status.Progress.name(), track.getDuration() == null ? 0 : track.getDuration().longValue());
EntityUtils.consume(entity);
return;
case // 400
HttpStatus.SC_BAD_REQUEST:
logger.info("Invalid argument returned, status: {}", code);
break;
case // 503
HttpStatus.SC_SERVICE_UNAVAILABLE:
logger.info("Service unavailable returned, status: {}", code);
break;
default:
logger.info("Unknown return status: {}.", code);
break;
}
throw new TranscriptionServiceException("Could not create recognition job. Status returned: " + code);
} catch (Exception e) {
logger.warn("Exception when calling the recognitions endpoint", e);
throw new TranscriptionServiceException("Exception when calling the recognitions endpoint", e);
} finally {
try {
httpClient.close();
if (response != null)
response.close();
} catch (IOException e) {
}
}
}
use of org.opencastproject.transcription.api.TranscriptionServiceException in project opencast by opencast.
the class IBMWatsonTranscriptionService method getAndSaveJobResults.
/**
* From: https://www.ibm.com/watson/developercloud/speech-to-text/api/v1 Check a job: GET /v1/recognitions/{id}
*
* curl -X GET -u "{username}":"{password}"
* "https://stream.watsonplatform.net/speech-to-text/api/v1/recognitions/{id}"
*
* Response: { "results": [ { "result_index": 0, "results": [ { "final": true, "alternatives": [ { "transcript":
* "several tornadoes touch down as a line of severe thunderstorms swept through Colorado on Sunday ", "timestamps": [
* [ "several", 1, 1.52 ], [ "tornadoes", 1.52, 2.15 ], . . . [ "Sunday", 5.74, 6.33 ] ], "confidence": 0.885 } ] } ]
* } ], "created": "2016-08-17T19:11:04.298Z", "updated": "2016-08-17T19:11:16.003Z", "status": "completed" }
*/
boolean getAndSaveJobResults(String jobId) throws TranscriptionServiceException {
CloseableHttpClient httpClient = makeHttpClient();
CloseableHttpResponse response = null;
String mpId = "unknown";
try {
HttpGet httpGet = new HttpGet(IBM_WATSON_SERVICE_URL + RECOGNITIONS + "/" + jobId);
response = httpClient.execute(httpGet);
int code = response.getStatusLine().getStatusCode();
switch(code) {
case // 200
HttpStatus.SC_OK:
HttpEntity entity = response.getEntity();
// Response returned is a json object described above
String jsonString = EntityUtils.toString(entity);
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(jsonString);
String jobStatus = (String) jsonObject.get("status");
mpId = (String) jsonObject.get("user_token");
// user_token doesn't come back if this is not in the context of a callback so get the mpId from the db
if (mpId == null) {
TranscriptionJobControl jc = database.findByJob(jobId);
if (jc != null)
mpId = jc.getMediaPackageId();
}
logger.info("Recognitions job {} has been found, status {}", jobId, jobStatus);
EntityUtils.consume(entity);
if (jobStatus.indexOf("completed") > -1 && jsonObject.get("results") != null) {
transcriptionDone(mpId, jsonObject);
return true;
}
return false;
case // 404
HttpStatus.SC_NOT_FOUND:
logger.info("Job not found: {}", jobId);
break;
case // 503
HttpStatus.SC_SERVICE_UNAVAILABLE:
logger.info("Service unavailable returned, status: {}", code);
break;
default:
logger.info("Unknown return status: {}.", code);
break;
}
throw new TranscriptionServiceException(String.format("Could not check recognition job for media package %s, job id %s. Status returned: %d", mpId, jobId, code), code);
} catch (TranscriptionServiceException e) {
throw e;
} catch (Exception e) {
String msg = String.format("Exception when calling the recognitions endpoint for media package %s, job id %s", mpId, jobId);
logger.warn(String.format(msg, mpId, jobId), e);
throw new TranscriptionServiceException(String.format("Exception when calling the recognitions endpoint for media package %s, job id %s", mpId, jobId), e);
} finally {
try {
httpClient.close();
if (response != null)
response.close();
} catch (IOException e) {
}
}
}
use of org.opencastproject.transcription.api.TranscriptionServiceException in project opencast by opencast.
the class IBMWatsonTranscriptionService method transcriptionDone.
@Override
public void transcriptionDone(String mpId, Object obj) throws TranscriptionServiceException {
JSONObject jsonObj = null;
String jobId = null;
try {
jsonObj = (JSONObject) obj;
jobId = (String) jsonObj.get("id");
logger.info("Transcription done for mpId {}, jobId {}", mpId, jobId);
// Update state in database
// If there's an optimistic lock exception here, it's ok because the workflow dispatcher
// may be doing the same thing
database.updateJobControl(jobId, TranscriptionJobControl.Status.TranscriptionComplete.name());
// Save results in file system if there
if (jsonObj.get("results") != null)
saveResults(jobId, jsonObj);
} catch (IOException e) {
logger.warn("Could not save transcription results file for mpId {}, jobId {}: {}", mpId, jobId, jsonObj == null ? "null" : jsonObj.toJSONString());
throw new TranscriptionServiceException("Could not save transcription results file", e);
} catch (TranscriptionDatabaseException e) {
logger.warn("Transcription results file were saved but state in db not updated for mpId {}, jobId {}", mpId, jobId);
throw new TranscriptionServiceException("Could not update transcription job control db", e);
}
}
use of org.opencastproject.transcription.api.TranscriptionServiceException in project opencast by opencast.
the class IBMWatsonTranscriptionService method getGeneratedTranscription.
@Override
public MediaPackageElement getGeneratedTranscription(String mpId, String jobId) throws TranscriptionServiceException {
try {
// If jobId is unknown, look for all jobs associated to that mpId
if (jobId == null || "null".equals(jobId)) {
jobId = null;
for (TranscriptionJobControl jc : database.findByMediaPackage(mpId)) {
if (TranscriptionJobControl.Status.Closed.name().equals(jc.getStatus()) || TranscriptionJobControl.Status.TranscriptionComplete.name().equals(jc.getStatus()))
jobId = jc.getTranscriptionJobId();
}
}
if (jobId == null)
throw new TranscriptionServiceException("No completed or closed transcription job found in database for media package " + mpId);
// Results already saved?
URI uri = workspace.getCollectionURI(TRANSCRIPT_COLLECTION, buildResultsFileName(jobId));
try {
workspace.get(uri);
} catch (Exception e) {
// Not saved yet so call the ibm watson service to get the results
getAndSaveJobResults(jobId);
}
MediaPackageElementBuilder builder = MediaPackageElementBuilderFactory.newInstance().newElementBuilder();
return builder.elementFromURI(uri, Attachment.TYPE, new MediaPackageElementFlavor("captions", "ibm-watson-json"));
} catch (TranscriptionDatabaseException e) {
throw new TranscriptionServiceException("Job id not informed and could not find transcription", e);
}
}
use of org.opencastproject.transcription.api.TranscriptionServiceException in project opencast by opencast.
the class IBMWatsonTranscriptionServiceTest method testGetAndSaveJobResultsError404.
@Test
public void testGetAndSaveJobResultsError404() throws Exception {
CloseableHttpResponse response = EasyMock.createNiceMock(CloseableHttpResponse.class);
StatusLine status = EasyMock.createNiceMock(StatusLine.class);
EasyMock.expect(response.getStatusLine()).andReturn(status).anyTimes();
EasyMock.expect(status.getStatusCode()).andReturn(HttpStatus.SC_NOT_FOUND).anyTimes();
EasyMock.replay(response, status);
Capture<HttpGet> capturedGet = Capture.newInstance();
EasyMock.expect(httpClient.execute(EasyMock.capture(capturedGet))).andReturn(response).anyTimes();
EasyMock.replay(httpClient);
try {
service.getAndSaveJobResults(JOB_ID);
} catch (TranscriptionServiceException e) {
Assert.assertEquals(404, e.getCode());
return;
}
Assert.fail("TranscriptionServiceException not thrown");
}
Aggregations