-
Notifications
You must be signed in to change notification settings - Fork 55
feat: add event stream support #692
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
149991a
test: add e2e test for s3 SelectObjectContent event stream
aajtodd e77ee4b
fix s3 e2e tests
aajtodd 95da1b2
add e2e test for transcribe
aajtodd 11946ba
fix test
aajtodd 037fc10
refactor e2e test configuration to include resources
aajtodd 87d4c6b
feat: enable event stream support
aajtodd 1612b0a
rm comment
aajtodd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "id": "34fc4fc2-ad2d-4264-976c-5013324c10b7", | ||
| "type": "feature", | ||
| "description": "Add support for event streams", | ||
| "issues": [ | ||
| "awslabs/aws-sdk-kotlin#543" | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
93 changes: 93 additions & 0 deletions
93
services/transcribestreaming/e2eTest/src/TranscribeStreamingIntegrationTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package aws.sdk.kotlin.e2etest | ||
|
|
||
| import aws.sdk.kotlin.services.transcribestreaming.TranscribeStreamingClient | ||
| import aws.sdk.kotlin.services.transcribestreaming.model.* | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.flow.Flow | ||
| import kotlinx.coroutines.flow.flow | ||
| import kotlinx.coroutines.flow.flowOn | ||
| import kotlinx.coroutines.runBlocking | ||
| import org.junit.jupiter.api.Test | ||
| import org.junit.jupiter.api.TestInstance | ||
| import java.io.File | ||
| import java.nio.file.Paths | ||
| import javax.sound.sampled.AudioSystem | ||
| import kotlin.test.assertTrue | ||
|
|
||
| @TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
| class TranscribeStreamingIntegrationTest { | ||
|
|
||
| @Test | ||
| fun testTranscribeEventStream(): Unit = runBlocking { | ||
| val url = this::class.java.classLoader.getResource("hello-kotlin-8000.wav") ?: error("failed to load test resource") | ||
| val audioFile = Paths.get(url.toURI()).toFile() | ||
|
|
||
| TranscribeStreamingClient { region = "us-east-2" }.use { client -> | ||
| val transcript = getTranscript(client, audioFile) | ||
| assertTrue(transcript.startsWith("Hello from", true), "full transcript: $transcript") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private const val FRAMES_PER_CHUNK = 4096 | ||
|
|
||
| private fun audioStreamFromFile(file: File): Flow<AudioStream> { | ||
| val format = AudioSystem.getAudioFileFormat(file) | ||
| val ais = AudioSystem.getAudioInputStream(file) | ||
| val bytesPerFrame = ais.format.frameSize | ||
| println("audio stream format of $file: $format; bytesPerFrame=$bytesPerFrame") | ||
|
|
||
| return flow { | ||
| while (true) { | ||
| val frameBuffer = ByteArray(FRAMES_PER_CHUNK * bytesPerFrame) | ||
| val rc = ais.read(frameBuffer) | ||
| if (rc <= 0) { | ||
| break | ||
| } | ||
|
|
||
| val chunk = if (rc < frameBuffer.size) frameBuffer.sliceArray(0 until rc) else frameBuffer | ||
| val event = AudioStream.AudioEvent( | ||
| AudioEvent { | ||
| audioChunk = chunk | ||
| }, | ||
| ) | ||
|
|
||
| println("emitting event") | ||
| emit(event) | ||
| } | ||
| }.flowOn(Dispatchers.IO) | ||
| } | ||
|
|
||
| private suspend fun getTranscript(client: TranscribeStreamingClient, audioFile: File): String { | ||
| val req = StartStreamTranscriptionRequest { | ||
| languageCode = LanguageCode.EnUs | ||
| mediaSampleRateHertz = 8000 | ||
| mediaEncoding = MediaEncoding.Pcm | ||
| audioStream = audioStreamFromFile(audioFile) | ||
| } | ||
|
|
||
| val transcript = client.startStreamTranscription(req) { resp -> | ||
| val fullMessage = StringBuilder() | ||
| resp.transcriptResultStream?.collect { event -> | ||
| when (event) { | ||
| is TranscriptResultStream.TranscriptEvent -> { | ||
| event.value.transcript?.results?.forEach { result -> | ||
| val transcript = result.alternatives?.firstOrNull()?.transcript | ||
| println("received TranscriptEvent: isPartial=${result.isPartial}; transcript=$transcript") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Remove |
||
| if (!result.isPartial) { | ||
| transcript?.let { fullMessage.append(it) } | ||
| } | ||
| } | ||
| } | ||
| else -> error("unknown event $event") | ||
| } | ||
| } | ||
| fullMessage.toString() | ||
| } | ||
|
|
||
| return transcript | ||
| } | ||
Binary file added
BIN
+66.5 KB
services/transcribestreaming/e2eTest/test-resources/hello-kotlin-8000.wav
Binary file not shown.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Remove
println.