Skip to content

Commit

Permalink
add scenarios for file and audio transcriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
meyertst-aws committed Dec 20, 2024
1 parent 46c2187 commit 48762e9
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 31 deletions.
38 changes: 36 additions & 2 deletions .doc_gen/metadata/transcribe-streaming_metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ transcribe-streaming_StartMedicalStreamTranscription:
- transcribe.java-medical-streaming-demo
services:
transcribe-streaming: {StartMedicalStreamTranscription}
transcribe-streaming_Scenario_StreamEvents:
title: Generate a transcription of an audio stream using an &AWS; SDK
transcribe-streaming_Scenario_StreamEvents_File:
title: Generate a transcription of a file using an &AWS; SDK
title_abbrev: Transcribe an audio file
synopsis: generate a transcription of a source audio file using &TSC; streaming.
category: Scenarios
Expand All @@ -58,5 +58,39 @@ transcribe-streaming_Scenario_StreamEvents:
snippet_tags:
- swift.transcribe-streaming.all
- swift.transcribe-streaming.transcribeerror
Java:
versions:
- sdk_version: 2
github: javav2/example_code/transcribe-streaming
sdkguide:
excerpts:
- description:
snippet_tags:
- transcribe.java-streaming-demo-file.main
C++:
versions:
- sdk_version: 1
github: cpp/example_code/transcribe-streaming
excerpts:
- description:
snippet_tags:
- transcribe.cpp.stream_transcription_async.code
services:
transcribe-streaming: {StartStreamTranscription}
transcribe-streaming_Scenario_StreamEvents_Microphone:
title: Generate a transcription from a microphone using an &AWS; SDK
title_abbrev: Transcribe audio from a microphone
synopsis: generate a transcription from a microphone using &TSC; streaming.
category: Scenarios
languages:
Java:
versions:
- sdk_version: 2
github: javav2/example_code/transcribe-streaming
sdkguide:
excerpts:
- description:
snippet_tags:
- transcribe.java-streaming-demo
services:
transcribe-streaming: {StartStreamTranscription}
21 changes: 20 additions & 1 deletion cpp/example_code/transcribe-streaming/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ Code excerpts that show you how to call individual service functions.

- [StartStreamTranscription](get_transcript.cpp#L28)

### Scenarios

Code examples that show you how to accomplish a specific task by calling multiple
functions within the same service.

- [Transcribe an audio file](get_transcript.cpp)


<!--custom.examples.start-->
<!--custom.examples.end-->
Expand All @@ -69,6 +76,18 @@ folder.



#### Transcribe an audio file

This example shows you how to generate a transcription of a source audio file using Amazon Transcribe streaming.


<!--custom.scenario_prereqs.transcribe-streaming_Scenario_StreamEvents_File.start-->
<!--custom.scenario_prereqs.transcribe-streaming_Scenario_StreamEvents_File.end-->


<!--custom.scenarios.transcribe-streaming_Scenario_StreamEvents_File.start-->
<!--custom.scenarios.transcribe-streaming_Scenario_StreamEvents_File.end-->

### Tests

⚠ Running tests might result in charges to your AWS account.
Expand Down Expand Up @@ -99,4 +118,4 @@ folder.

Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
SPDX-License-Identifier: Apache-2.0
36 changes: 34 additions & 2 deletions javav2/example_code/transcribe-streaming/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,15 @@ For prerequisites, see the [README](../../README.md#Prerequisites) in the `javav
Code excerpts that show you how to call individual service functions.

- [StartMedicalStreamTranscription](src/main/java/com/amazonaws/transcribestreaming/TranscribeMedicalStreamingDemoApp.java#L25)
- [StartStreamTranscription](src/main/java/com/amazonaws/transcribestreaming/TranscribeStreamingDemoApp.java#L26)
- [StartStreamTranscription](src/main/java/com/amazonaws/transcribestreaming/TranscribeStreamingDemoApp.java#L36)

### Scenarios

Code examples that show you how to accomplish a specific task by calling multiple
functions within the same service.

- [Transcribe an audio file](src/main/java/com/amazonaws/transcribestreaming/TranscribeStreamingDemoFile.java)
- [Transcribe audio from a microphone](src/main/java/com/amazonaws/transcribestreaming/TranscribeStreamingDemoApp.java)


<!--custom.examples.start-->
Expand All @@ -50,6 +58,30 @@ Code excerpts that show you how to call individual service functions.



#### Transcribe an audio file

This example shows you how to generate a transcription of a source audio file using Amazon Transcribe streaming.


<!--custom.scenario_prereqs.transcribe-streaming_Scenario_StreamEvents_File.start-->
<!--custom.scenario_prereqs.transcribe-streaming_Scenario_StreamEvents_File.end-->


<!--custom.scenarios.transcribe-streaming_Scenario_StreamEvents_File.start-->
<!--custom.scenarios.transcribe-streaming_Scenario_StreamEvents_File.end-->

#### Transcribe audio from a microphone

This example shows you how to generate a transcription from a microphone using Amazon Transcribe streaming.


<!--custom.scenario_prereqs.transcribe-streaming_Scenario_StreamEvents_Microphone.start-->
<!--custom.scenario_prereqs.transcribe-streaming_Scenario_StreamEvents_Microphone.end-->


<!--custom.scenarios.transcribe-streaming_Scenario_StreamEvents_Microphone.start-->
<!--custom.scenarios.transcribe-streaming_Scenario_StreamEvents_Microphone.end-->

### Tests

⚠ Running tests might result in charges to your AWS account.
Expand All @@ -76,4 +108,4 @@ in the `javav2` folder.

Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
SPDX-License-Identifier: Apache-2.0
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,18 @@
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.transcribestreaming.TranscribeStreamingAsyncClient;
import software.amazon.awssdk.services.transcribestreaming.model.*;
import javax.sound.sampled.*;
import java.io.*;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.TargetDataLine;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UncheckedIOException;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.util.List;
Expand All @@ -28,7 +38,7 @@ public class TranscribeStreamingDemoApp {
private static final Region REGION = Region.US_EAST_1;
private static TranscribeStreamingAsyncClient client;

public static void main(String args[])
public static void main(String[] args)
throws URISyntaxException, ExecutionException, InterruptedException, LineUnavailableException {

client = TranscribeStreamingAsyncClient.builder()
Expand Down Expand Up @@ -85,7 +95,7 @@ private static StartStreamTranscriptionResponseHandler getResponseHandler() {
System.out.println(e.getMessage());
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
System.out.println("Error Occurred: " + sw.toString());
System.out.println("Error Occurred: " + sw);
})
.onComplete(() -> {
System.out.println("=== All records stream successfully ===");
Expand All @@ -101,19 +111,10 @@ private static StartStreamTranscriptionResponseHandler getResponseHandler() {
.build();
}

private InputStream getStreamFromFile(String audioFileName) {
try {
File inputFile = new File(getClass().getClassLoader().getResource(audioFileName).getFile());
InputStream audioStream = new FileInputStream(inputFile);
return audioStream;
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}


private static class AudioStreamPublisher implements Publisher<AudioStream> {
private final InputStream inputStream;
private static Subscription currentSubscription;
private final InputStream inputStream;

private AudioStreamPublisher(InputStream inputStream) {
this.inputStream = inputStream;
Expand All @@ -122,22 +123,22 @@ private AudioStreamPublisher(InputStream inputStream) {
@Override
public void subscribe(Subscriber<? super AudioStream> s) {

if (this.currentSubscription == null) {
this.currentSubscription = new SubscriptionImpl(s, inputStream);
if (currentSubscription == null) {
currentSubscription = new SubscriptionImpl(s, inputStream);
} else {
this.currentSubscription.cancel();
this.currentSubscription = new SubscriptionImpl(s, inputStream);
currentSubscription.cancel();
currentSubscription = new SubscriptionImpl(s, inputStream);
}
s.onSubscribe(currentSubscription);
}
}

public static class SubscriptionImpl implements Subscription {
private static final int CHUNK_SIZE_IN_BYTES = 1024 * 1;
private static final int CHUNK_SIZE_IN_BYTES = 1024;
private final Subscriber<? super AudioStream> subscriber;
private final InputStream inputStream;
private ExecutorService executor = Executors.newFixedThreadPool(1);
private AtomicLong demand = new AtomicLong(0);
private final ExecutorService executor = Executors.newFixedThreadPool(1);
private final AtomicLong demand = new AtomicLong(0);

SubscriptionImpl(Subscriber<? super AudioStream> s, InputStream inputStream) {
this.subscriber = s;
Expand Down
8 changes: 4 additions & 4 deletions swift/example_code/transcribe-streaming/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ and run commands.
This example shows you how to generate a transcription of a source audio file using Amazon Transcribe streaming.


<!--custom.scenario_prereqs.transcribe-streaming_Scenario_StreamEvents.start-->
<!--custom.scenario_prereqs.transcribe-streaming_Scenario_StreamEvents.end-->
<!--custom.scenario_prereqs.transcribe-streaming_Scenario_StreamEvents_File.start-->
<!--custom.scenario_prereqs.transcribe-streaming_Scenario_StreamEvents_File.end-->


<!--custom.scenarios.transcribe-streaming_Scenario_StreamEvents.start-->
<!--custom.scenarios.transcribe-streaming_Scenario_StreamEvents.end-->
<!--custom.scenarios.transcribe-streaming_Scenario_StreamEvents_File.start-->
<!--custom.scenarios.transcribe-streaming_Scenario_StreamEvents_File.end-->

### Tests

Expand Down

0 comments on commit 48762e9

Please sign in to comment.