Add AWS Transcribe to Spring boot App
Tags: aws, transcribe, springboot, java
Published: 2020-04-22 14:37:57

Amazon Transcribe uses a deep learning process called automatic speech recognition (ASR) to convert speech to text quickly and accurately. Amazon Transcribe can be used to transcribe customer service calls, to automate closed captioning and subtitling, and to generate metadata for media assets to create a fully searchable archive. Check <a href="https://aws.amazon.com/transcribe/">here.</a>

<blockquote class="te tf tg">| Also Read | <a href="https://tekraze.com/2020/04/add-amazon-comprehend-to-spring-boot-project/">Add Amazon Comprehend To Spring Boot</a> </blockquote>
<h2 id="4871" class="hr hs av ce cd ey lh oi lj oj ok ol om on oo op hv">Steps for Integration</h2>
1.Create A transcribe Account and setup credentials

2.Setup SDK for Transcribe and S3(Required for upload)
<pre class="prettyprint lang-xml" data-start-line="1" data-visibility="visible" data-highlight="" data-caption="">&lt;!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-s3 --&gt;
&lt;dependency&gt;
    &lt;groupId&gt;com.amazonaws&lt;/groupId&gt;
    &lt;artifactId&gt;aws-java-sdk-s3&lt;/artifactId&gt;
    &lt;version&gt;1.11.759&lt;/version&gt;
&lt;/dependency&gt;
&lt;!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-transcribe --&gt;
&lt;dependency&gt;
    &lt;groupId&gt;com.amazonaws&lt;/groupId&gt;
    &lt;artifactId&gt;aws-java-sdk-transcribe&lt;/artifactId&gt;
    &lt;version&gt;1.11.759&lt;/version&gt;
&lt;/dependency&gt;</pre>
<p id="a95e" class="hw hx av ce hy b hz mv ib mw mx my mz na nb nc ij gz" data-selectable-paragraph="">3. Create A java Service file to add code</p>
<p id="7837" class="hw hx av ce hy b hz mv ib mw mx my mz na nb nc ij gz" data-selectable-paragraph="">4. Initialize Clients for Transcribe and S3</p>

<pre class="prettyprint lang-java" data-start-line="1" data-visibility="visible" data-highlight="" data-caption="">AmazonTranscribe transcribeClient() {
log.debug("Intialize Transcribe Client");
BasicAWSCredentials awsCreds = new BasicAWSCredentials(awsAccessKey, awsSecretKey);
AWSStaticCredentialsProvider awsStaticCredentialsProvider = new AWSStaticCredentialsProvider(awsCreds);
return AmazonTranscribeClientBuilder.standard().withCredentials(awsStaticCredentialsProvider)
.withRegion(awsRegion).build();
}
AmazonS3 s3Client() {
log.debug("Intialize AWS S3 Client");
BasicAWSCredentials awsCreds = new BasicAWSCredentials(awsAccessKey, awsSecretKey);
AWSStaticCredentialsProvider awsStaticCredentialsProvider = new AWSStaticCredentialsProvider(awsCreds);
return AmazonS3ClientBuilder.standard().withCredentials(awsStaticCredentialsProvider).withRegion(awsRegion)
.build();
}</pre>
5. File upload/delete methods for S3. Skip if you want to use the file present on S3
<pre class="prettyprint lang-java" data-start-line="1" data-visibility="visible" data-highlight="" data-caption="">public void uploadFileToAwsBucket(MultipartFile file) {
log.debug("Upload file to AWS Bucket {}", file);
String key = file.getOriginalFilename().replaceAll(" ", "_").toLowerCase();
try {
s3Client().putObject(bucketName, key, file.getInputStream(), null);
} catch (SdkClientException | IOException e) {
e.printStackTrace();
}
}
public void deleteFileFromAwsBucket(String fileName) {
log.debug("Delete File from AWS Bucket {}", fileName);
String key = fileName.replaceAll(" ", "_").toLowerCase();
s3Client().deleteObject(bucketName, key);
}</pre>
<blockquote class="te tf tg">| Also Read | <a href="https://tekraze.com/2018/01/things-need-know-developer/">Things you need to know to be a Developer</a> </blockquote>
6. Start Transcription Process method
<pre class="prettyprint lang-java" data-start-line="1" data-visibility="visible" data-highlight="" data-caption="">StartTranscriptionJobResult startTranscriptionJob(String key) {
log.debug("Start Transcription Job By Key {}",key);
Media media = new Media().withMediaFileUri(s3Client().getUrl(bucketName, key).toExternalForm());
String jobName = key.concat(RandomString.make());
StartTranscriptionJobRequest startTranscriptionJobRequest = new StartTranscriptionJobRequest()
.withLanguageCode(LanguageCode.EnUS).withTranscriptionJobName(jobName).withMedia(media);
StartTranscriptionJobResult startTranscriptionJobResult = transcribeClient()
.startTranscriptionJob(startTranscriptionJobRequest);
return startTranscriptionJobResult;
}</pre>
<p id="079d" class="hw hx av ce hy b hz mv ib mw mx my mz na nb nc ij gz" data-selectable-paragraph="">7. Get Transcription Job Results Method</p>

<pre class="prettyprint lang-java" data-start-line="1" data-visibility="visible" data-highlight="" data-caption="">GetTranscriptionJobResult getTranscriptionJobResult(String jobName) {
log.debug("Get Transcription Job Result By Job Name : {}",jobName);
GetTranscriptionJobRequest getTranscriptionJobRequest = new GetTranscriptionJobRequest()
.withTranscriptionJobName(jobName);
Boolean resultFound = false;
TranscriptionJob transcriptionJob = new TranscriptionJob();
GetTranscriptionJobResult getTranscriptionJobResult = new GetTranscriptionJobResult();
while (resultFound == false) {
getTranscriptionJobResult = transcribeClient().getTranscriptionJob(getTranscriptionJobRequest);
transcriptionJob = getTranscriptionJobResult.getTranscriptionJob();
if (transcriptionJob.getTranscriptionJobStatus()
.equalsIgnoreCase(TranscriptionJobStatus.COMPLETED.name())) {
return getTranscriptionJobResult;
} else if (transcriptionJob.getTranscriptionJobStatus()
.equalsIgnoreCase(TranscriptionJobStatus.FAILED.name())) {
return null;
} else if (transcriptionJob.getTranscriptionJobStatus()
.equalsIgnoreCase(TranscriptionJobStatus.IN_PROGRESS.name())) {
try {
Thread.sleep(15000);
} catch (InterruptedException e) {
log.debug("Interrupted Exception {}", e.getMessage());
}
}
}
return getTranscriptionJobResult;
}</pre>
<p id="2bd6" class="hw hx av ce hy b hz mv ib mw mx my mz na nb nc ij gz" data-selectable-paragraph="">8. Download Transcription Result method to fetch result from URI</p>

<pre class="prettyprint lang-java" data-start-line="1" data-visibility="visible" data-highlight="" data-caption="">TranscriptionResponseDTO downloadTranscriptionResponse(String uri){
log.debug("Download Transcription Result from Transcribe URi {}", uri);
OkHttpClient okHttpClient = new OkHttpClient()
.newBuilder()
.connectTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.build();
Request request = new Request.Builder().url(uri).build();
Response response;
try {
response = okHttpClient.newCall(request).execute();
String body = response.body().string();</pre>
<p id="a0ed" class="hw hx av ce hy b hz mv ib mw mx my mz na nb nc ij gz" data-selectable-paragraph="">9. Delete the Transcription Job Method. To delete after processing is done, or it will automatically get deleted after 90 days.</p>

<pre class="prettyprint lang-java" data-start-line="1" data-visibility="visible" data-highlight="" data-caption="">void deleteTranscriptionJob(String jobName) {
log.debug("Delete Transcription Job from amazon Transcribe {}",jobName);
DeleteTranscriptionJobRequest deleteTranscriptionJobRequest = new DeleteTranscriptionJobRequest()
.withTranscriptionJobName(jobName);
transcribeClient().deleteTranscriptionJob(deleteTranscriptionJobRequest);
}</pre>
<p id="8a7e" class="hw hx av ce hy b hz mv ib mw mx my mz na nb nc ij gz" data-selectable-paragraph="">10. Combined method ExtractSpeechToText to Get Result of Transcription As a DTO</p>

<pre class="prettyprint lang-java" data-start-line="1" data-visibility="visible" data-highlight="" data-caption="">public TranscriptionResponseDTO extractSpeechTextFromVideo(MultipartFile file) {
log.debug("Request to extract Speech Text from Video : {}",file);
uploadFileToAwsBucket(file);
String key = file.getOriginalFilename().replaceAll(" ", "_").toLowerCase();
StartTranscriptionJobResult startTranscriptionJobResult = startTranscriptionJob(key);
String transcriptionJobName = startTranscriptionJobResult.getTranscriptionJob().getTranscriptionJobName();
GetTranscriptionJobResult getTranscriptionJobResult = getTranscriptionJobResult(transcriptionJobName);
deleteFileFromAwsBucket(key);
String transcriptFileUriString = getTranscriptionJobResult.getTranscriptionJob().getTranscript().getTranscriptFileUri();
TranscriptionResponseDTO transcriptionResponseDTO = downloadTranscriptionResponse(transcriptFileUriString);
deleteTranscriptionJob(transcriptionJobName);
return transcriptionResponseDTO;
}</pre>11. Now, you can use the above methods to get your video/audio file processed and get the text from Speech. The complete code Link is Here &gt;&gt;&gt; <a href="https://gist.github.com/balvinder294/005ea8856f8d914078c28618cfa9093b#file-audio-transcriptions-aws-java">Link to Gist</a>

and for the response DTO check <a href="https://gist.github.com/balvinder294/fd16a7bd8cf5d9a889161d370cf8ee3f#file-transcriptionresponsedto-java">Link to Gist</a>
<br />
<blockquote class="te tf tg">
<p id="5f9d" class="hw hx av th hy b hz mv ib mw mx my mz na nb nc ij gz" data-selectable-paragraph="">Some references were taken from Edgardo Genini comment on StackOverflow <a class="da eb sv sw sx sy" href="https://stackoverflow.com/a/49986440/9147967" target="_blank" rel="noopener nofollow noreferrer">here</a></p>
</blockquote>
<br />
<blockquote class="te tf tg">
| Also Read | <a href="https://tekraze.com/2018/01/text-editors-code-2018/">Text Editors for Code</a>
</blockquote>

I hope the code helps you, if yes please do share your support by Writing in the comments below.Thanks for reading.

<blockquote>
Originally published At <a href="https://tekraze.com/2020/04/add-aws-transcribe-to-spring-boot-app/">Tekraze.com</a>
</blockquote>