Search in sources :

Example 1 with TranscriptionJobControl

use of org.opencastproject.transcription.ibmwatson.persistence.TranscriptionJobControl in project opencast by opencast.

the class IBMWatsonTranscriptionServiceTest method testWorkflowDispatcherJobInProgressTooLong.

@Test
public void testWorkflowDispatcherJobInProgressTooLong() throws Exception {
    InputStream stream = IBMWatsonTranscriptionServiceTest.class.getResourceAsStream("/" + IN_PROGRESS_JOB);
    HttpEntity httpEntity = EasyMock.createNiceMock(HttpEntity.class);
    EasyMock.expect(httpEntity.getContent()).andReturn(stream);
    CloseableHttpResponse response = EasyMock.createNiceMock(CloseableHttpResponse.class);
    StatusLine status = EasyMock.createNiceMock(StatusLine.class);
    EasyMock.expect(response.getStatusLine()).andReturn(status).anyTimes();
    EasyMock.expect(response.getEntity()).andReturn(httpEntity).anyTimes();
    EasyMock.expect(status.getStatusCode()).andReturn(HttpStatus.SC_OK).anyTimes();
    EasyMock.replay(httpEntity, response, status);
    Capture<HttpGet> capturedGet = Capture.newInstance();
    EasyMock.expect(httpClient.execute(EasyMock.capture(capturedGet))).andReturn(response).anyTimes();
    EasyMock.replay(httpClient);
    database.storeJobControl(MP_ID, TRACK_ID, JOB_ID, TranscriptionJobControl.Status.Progress.name(), 0);
    EasyMock.replay(workspace);
    WorkflowDispatcher dispatcher = service.new WorkflowDispatcher();
    dispatcher.run();
    // Check if it called the external service to get the results
    Assert.assertEquals("https://stream.watsonplatform.net/speech-to-text/api/v1/recognitions/" + JOB_ID, capturedGet.getValue().getURI().toString());
    // Check if the job status was updated and email was sent
    TranscriptionJobControl j = database.findByJob(JOB_ID);
    Assert.assertNotNull(j);
    Assert.assertEquals(TranscriptionJobControl.Status.Canceled.toString(), j.getStatus());
    EasyMock.verify(smtpService);
}
Also used : StatusLine(org.apache.http.StatusLine) TranscriptionJobControl(org.opencastproject.transcription.ibmwatson.persistence.TranscriptionJobControl) HttpEntity(org.apache.http.HttpEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) WorkflowDispatcher(org.opencastproject.transcription.ibmwatson.IBMWatsonTranscriptionService.WorkflowDispatcher) Test(org.junit.Test)

Example 2 with TranscriptionJobControl

use of org.opencastproject.transcription.ibmwatson.persistence.TranscriptionJobControl in project opencast by opencast.

the class IBMWatsonTranscriptionServiceTest method testCreateRecognitionsJob.

@Test
public void testCreateRecognitionsJob() throws Exception {
    EasyMock.expect(workspace.get(EasyMock.anyObject(URI.class))).andReturn(audioFile);
    EasyMock.replay(workspace);
    HttpEntity httpEntity = EasyMock.createNiceMock(HttpEntity.class);
    EasyMock.expect(httpEntity.getContent()).andReturn(new ByteArrayInputStream(new String("{\"id\": \"" + JOB_ID + "\", \"status\": \"waiting\", \"url\": \"http://stream.watsonplatform.net/speech-to-text/api/v1/recognitions/" + JOB_ID + "\"}").getBytes(StandardCharsets.UTF_8)));
    CloseableHttpResponse response = EasyMock.createNiceMock(CloseableHttpResponse.class);
    StatusLine status = EasyMock.createNiceMock(StatusLine.class);
    EasyMock.expect(response.getStatusLine()).andReturn(status).anyTimes();
    EasyMock.expect(response.getEntity()).andReturn(httpEntity).anyTimes();
    EasyMock.expect(status.getStatusCode()).andReturn(HttpStatus.SC_CREATED).anyTimes();
    EasyMock.replay(httpEntity, response, status);
    Capture<HttpPost> capturedPost = Capture.newInstance();
    EasyMock.expect(httpClient.execute(EasyMock.capture(capturedPost))).andReturn(response).anyTimes();
    EasyMock.replay(httpClient);
    service.createRecognitionsJob(MP_ID, mediaPackage.getTrack("audioTrack1"));
    Assert.assertEquals("https://stream.watsonplatform.net/speech-to-text/api/v1/recognitions?user_token=" + MP_ID + "&inactivity_timeout=-1&timestamps=true&smart_formatting=true" + "&callback_url=http://ADMIN_SERVER/transcripts/watson/results&events=recognitions.completed_with_results,recognitions.failed", capturedPost.getValue().getURI().toString());
    TranscriptionJobControl j = database.findByJob(JOB_ID);
    Assert.assertNotNull(j);
    Assert.assertEquals(MP_ID, j.getMediaPackageId());
    Assert.assertEquals(TRACK_ID, j.getTrackId());
}
Also used : StatusLine(org.apache.http.StatusLine) HttpPost(org.apache.http.client.methods.HttpPost) TranscriptionJobControl(org.opencastproject.transcription.ibmwatson.persistence.TranscriptionJobControl) HttpEntity(org.apache.http.HttpEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) URI(java.net.URI) Test(org.junit.Test)

Example 3 with TranscriptionJobControl

use of org.opencastproject.transcription.ibmwatson.persistence.TranscriptionJobControl in project opencast by opencast.

the class IBMWatsonTranscriptionServiceTest method testGetAndSaveJobResults.

@Test
public void testGetAndSaveJobResults() throws Exception {
    InputStream stream = IBMWatsonTranscriptionServiceTest.class.getResourceAsStream("/" + PULLED_TRANSCRIPTION_FILE);
    database.storeJobControl(MP_ID, TRACK_ID, JOB_ID, TranscriptionJobControl.Status.Progress.name(), TRACK_DURATION);
    Capture<String> capturedCollection = Capture.newInstance();
    Capture<String> capturedFileName = Capture.newInstance();
    EasyMock.expect(workspace.putInCollection(EasyMock.capture(capturedCollection), EasyMock.capture(capturedFileName), EasyMock.anyObject(InputStream.class))).andReturn(new URI("http://anything"));
    EasyMock.replay(workspace);
    HttpEntity httpEntity = EasyMock.createNiceMock(HttpEntity.class);
    EasyMock.expect(httpEntity.getContent()).andReturn(stream);
    CloseableHttpResponse response = EasyMock.createNiceMock(CloseableHttpResponse.class);
    StatusLine status = EasyMock.createNiceMock(StatusLine.class);
    EasyMock.expect(response.getStatusLine()).andReturn(status).anyTimes();
    EasyMock.expect(response.getEntity()).andReturn(httpEntity).anyTimes();
    EasyMock.expect(status.getStatusCode()).andReturn(HttpStatus.SC_OK).anyTimes();
    EasyMock.replay(httpEntity, response, status);
    Capture<HttpGet> capturedGet = Capture.newInstance();
    EasyMock.expect(httpClient.execute(EasyMock.capture(capturedGet))).andReturn(response).anyTimes();
    EasyMock.replay(httpClient);
    long before = System.currentTimeMillis();
    service.getAndSaveJobResults(JOB_ID);
    long after = System.currentTimeMillis();
    // Check if correct url was invoked
    Assert.assertEquals("https://stream.watsonplatform.net/speech-to-text/api/v1/recognitions/" + JOB_ID, capturedGet.getValue().getURI().toString());
    // Check if status and date in db was updated
    TranscriptionJobControl job = database.findByJob(JOB_ID);
    Assert.assertNotNull(job);
    Assert.assertEquals(TranscriptionJobControl.Status.TranscriptionComplete.name(), job.getStatus());
    Assert.assertNotNull(job.getDateCompleted());
    Assert.assertTrue(before <= job.getDateCompleted().getTime() && job.getDateCompleted().getTime() <= after);
    // Check if results were saved into a collection
    Assert.assertEquals(IBMWatsonTranscriptionService.TRANSCRIPT_COLLECTION, capturedCollection.getValue());
    Assert.assertEquals(JOB_ID + ".json", capturedFileName.getValue());
}
Also used : StatusLine(org.apache.http.StatusLine) TranscriptionJobControl(org.opencastproject.transcription.ibmwatson.persistence.TranscriptionJobControl) HttpEntity(org.apache.http.HttpEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) URI(java.net.URI) Test(org.junit.Test)

Example 4 with TranscriptionJobControl

use of org.opencastproject.transcription.ibmwatson.persistence.TranscriptionJobControl in project opencast by opencast.

the class IBMWatsonTranscriptionServiceTest method testWorkflowDispatcherJobNotFound.

@Test
public void testWorkflowDispatcherJobNotFound() 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);
    database.storeJobControl(MP_ID, TRACK_ID, JOB_ID, TranscriptionJobControl.Status.Progress.name(), 0);
    EasyMock.replay(workspace);
    WorkflowDispatcher dispatcher = service.new WorkflowDispatcher();
    dispatcher.run();
    // Check if it called the external service to get the results
    Assert.assertEquals("https://stream.watsonplatform.net/speech-to-text/api/v1/recognitions/" + JOB_ID, capturedGet.getValue().getURI().toString());
    // Check if the job status was updated and email was sent
    TranscriptionJobControl j = database.findByJob(JOB_ID);
    Assert.assertNotNull(j);
    Assert.assertEquals(TranscriptionJobControl.Status.Canceled.toString(), j.getStatus());
    EasyMock.verify(smtpService);
}
Also used : StatusLine(org.apache.http.StatusLine) TranscriptionJobControl(org.opencastproject.transcription.ibmwatson.persistence.TranscriptionJobControl) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) WorkflowDispatcher(org.opencastproject.transcription.ibmwatson.IBMWatsonTranscriptionService.WorkflowDispatcher) Test(org.junit.Test)

Example 5 with TranscriptionJobControl

use of org.opencastproject.transcription.ibmwatson.persistence.TranscriptionJobControl 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) {
        }
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) TranscriptionJobControl(org.opencastproject.transcription.ibmwatson.persistence.TranscriptionJobControl) HttpEntity(org.apache.http.HttpEntity) JSONObject(org.json.simple.JSONObject) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) JSONParser(org.json.simple.parser.JSONParser) IOException(java.io.IOException) TranscriptionServiceException(org.opencastproject.transcription.api.TranscriptionServiceException) TranscriptionServiceException(org.opencastproject.transcription.api.TranscriptionServiceException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) TranscriptionDatabaseException(org.opencastproject.transcription.ibmwatson.persistence.TranscriptionDatabaseException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) IOException(java.io.IOException)

Aggregations

TranscriptionJobControl (org.opencastproject.transcription.ibmwatson.persistence.TranscriptionJobControl)16 Test (org.junit.Test)13 ByteArrayInputStream (java.io.ByteArrayInputStream)6 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)6 InputStream (java.io.InputStream)5 URI (java.net.URI)5 HttpEntity (org.apache.http.HttpEntity)5 StatusLine (org.apache.http.StatusLine)5 HttpGet (org.apache.http.client.methods.HttpGet)5 JSONObject (org.json.simple.JSONObject)4 WorkflowDispatcher (org.opencastproject.transcription.ibmwatson.IBMWatsonTranscriptionService.WorkflowDispatcher)4 TranscriptionServiceException (org.opencastproject.transcription.api.TranscriptionServiceException)3 TranscriptionDatabaseException (org.opencastproject.transcription.ibmwatson.persistence.TranscriptionDatabaseException)3 IOException (java.io.IOException)2 InputStreamReader (java.io.InputStreamReader)2 ArrayList (java.util.ArrayList)2 Set (java.util.Set)2 Snapshot (org.opencastproject.assetmanager.api.Snapshot)2 AQueryBuilder (org.opencastproject.assetmanager.api.query.AQueryBuilder)2 ARecord (org.opencastproject.assetmanager.api.query.ARecord)2