Beispiel #1
0
async def basic_transcribe():
    # Setup up our client with our chosen AWS region
    client = TranscribeStreamingClient(region="eu-central-1")

    # Start transcription to generate our async stream
    stream = await client.start_stream_transcription(
        language_code="de-DE",
        media_sample_rate_hz=16000,
        media_encoding="pcm",
    )

    async def write_chunks():
        # An example file can be found at tests/integration/assets/test.wav
        # NOTE: For pre-recorded files longer than 5 minutes, the sent audio
        # chunks should be rate limited to match the realtime bitrate of the
        # audio stream to avoid signing issues.
        async with aiofile.AIOFile("tests/record.wav", "rb") as afp:
            reader = aiofile.Reader(afp, chunk_size=1024 * 16)
            async for chunk in reader:
                await stream.input_stream.send_audio_event(audio_chunk=chunk)
        await stream.input_stream.end_stream()

    # Instantiate our handler and start processing events
    handler = MyEventHandler(stream.output_stream)
    await asyncio.gather(write_chunks(), handler.handle_events())
Beispiel #2
0
async def basic_transcribe():
    # Setup up our client with our chosen AWS region
    client = TranscribeStreamingClient(region="us-east-1")

    # Start transcription to generate our async stream
    stream = await client.start_stream_transcription(
        language_code="en-US",
        media_sample_rate_hz=16000,
        media_encoding="pcm",
    )

    print("STREAM: ", stream)

    async def write_chunks():
        # An example file can be found at tests/integration/assets/test.wav
        # NOTE: For pre-recorded files longer than 5 minutes, the sent audio
        # chunks should be rate limited to match the realtime bitrate of the
        # audio stream to avoid signing issues.
        async with aiofile.AIOFile(
                "/Users/thunt/Downloads/5ab118cd-d252-4e5a-b5d0-b158f7c399c4-45.wav",
                "rb+") as afp:
            print("Reading file: %s", afp)
            reader = aiofile.Reader(afp, chunk_size=1024 * 16)
            async for chunk in reader:
                print("writing chunk...", len(chunk))
                await stream.input_stream.send_audio_event(audio_chunk=chunk)
                print("sent chunk...")
        print("ending stream....", stream)
        await stream.input_stream.end_stream()

    # Instantiate our handler and start processing events
    handler = MyEventHandler(stream.output_stream)
    await asyncio.gather(write_chunks(), handler.handle_events())
async def basic_transcribe(file, transcript, media_sample_rate_hz=8000, language_code="en-US", region="us-east-1"):
    client = TranscribeStreamingClient(region=region)

    stream = await client.start_stream_transcription(
        language_code=language_code,
        media_sample_rate_hz=media_sample_rate_hz,
        media_encoding="pcm",
    )

    async def write_chunks():

        async with aiofile.AIOFile(file, 'rb') as afp:
            reader = aiofile.Reader(afp, chunk_size=media_sample_rate_hz * 1.024)
            async for chunk in reader:
                await stream.input_stream.send_audio_event(audio_chunk=chunk)
     
        await stream.input_stream.end_stream()

    handler = MyEventHandler(stream.output_stream)

    await asyncio.gather(write_chunks(), handler.handle_events())

    if handler.result_holder == [] :
        transcript.put('')
    else :
        transcript.put(handler.result_holder[-1])

    return()  
async def basic_transcribe(file,
                           media_sample_rate_hz=16000,
                           language_code="en-US",
                           region="us-east-1"):

    # Setup up our client with our chosen AWS region
    client = TranscribeStreamingClient(region=region)

    # Start transcription to generate our async stream
    stream = await client.start_stream_transcription(
        language_code=language_code,
        media_sample_rate_hz=media_sample_rate_hz,
        media_encoding="pcm",
    )

    async def write_chunks():

        async with aiofile.AIOFile(file, 'rb') as afp:
            reader = aiofile.Reader(afp,
                                    chunk_size=media_sample_rate_hz * 1.024)
            async for chunk in reader:
                await stream.input_stream.send_audio_event(audio_chunk=chunk)

        await stream.input_stream.end_stream()

    # Instantiate our handler and start processing events
    handler = MyEventHandler(stream.output_stream)

    await asyncio.gather(write_chunks(), handler.handle_events())

    if handler.result_holder == []:
        return ('')
    else:
        return (handler.result_holder[-1])
        async def basic_transcribe():

            client = TranscribeStreamingClient(region="us-west-2")

            stream = await client.start_stream_transcription(
                language_code="en-US",
                media_sample_rate_hz=16000,
                media_encoding="pcm",
            )

            handler = MyEventHandler(stream.output_stream)
            await asyncio.gather(write_chunks(stream), handler.handle_events())
async def basic_transcribe():
    # Setup up our client with our chosen AWS region
    client = TranscribeStreamingClient(region="us-east-1")

    # Start transcription to generate our async stream
    stream = await client.start_stream_transcription(
        language_code="en-US",
        media_sample_rate_hz=16000,
        media_encoding="pcm")

    # Instantiate our handler and start processing events
    handler = MyEventHandler(stream.output_stream)
    await asyncio.gather(write_chunks(stream), handler.handle_events())
    async def test_extended_transcribe_handler(self, chunks):
        client = TranscribeStreamingClient(region="us-west-2")

        stream = await client.start_stream_transcription(
            language_code="en-US",
            media_sample_rate_hz=16000,
            media_encoding="pcm",
        )

        async def write_chunks():
            for chunk in chunks:
                await stream.input_stream.send_audio_event(audio_chunk=chunk)
            await stream.input_stream.end_stream()

        handler = self.ExampleStreamHandler(stream.output_stream)
        await asyncio.gather(write_chunks(), handler.handle_events())
        assert len(handler.result_holder) > 0
    async def test_base_transcribe_handler(self, chunks):
        client = TranscribeStreamingClient(region="us-west-2")

        stream = await client.start_stream_transcription(
            language_code="en-US",
            media_sample_rate_hz=16000,
            media_encoding="pcm",
        )

        async def write_chunks():
            for chunk in chunks:
                await stream.input_stream.send_audio_event(audio_chunk=chunk)
            await stream.input_stream.end_stream()

        handler = TranscriptResultStreamHandler(stream.output_stream)
        with pytest.raises(NotImplementedError):
            await asyncio.gather(write_chunks(), handler.handle_events())
Beispiel #9
0
async def basic_transcribe():
    client = TranscribeStreamingClient(region="us-east-1")

    stream = await client.start_stream_transcription(
        language_code="en-US",
        media_sample_rate_hz=16000,
        media_encoding="pcm",
    )

    async def write_chunks():
        async with aiofile.AIOFile('audio/A.wav', 'rb') as afp:
            reader = aiofile.Reader(afp, chunk_size=1024 * 16)
            async for chunk in reader:
                await stream.input_stream.send_audio_event(audio_chunk=chunk)
        await stream.input_stream.end_stream()

    handler = MyEventHandler(stream.output_stream)
    await asyncio.gather(write_chunks(), handler.handle_events())
async def basic_trascribe(wavfile: str):
    client = TranscribeStreamingClient(region="us-east-1")

    # wanted to set up the vocab_filter_name and vocab_filter_method param, but vocab_filter_name is missing
    # see https://github.com/awslabs/amazon-transcribe-streaming-sdk/issues/8
    stream = await client.start_stream_transcription(
        language_code='en-US',
        media_sample_rate_hz=16000,
        media_encoding='pcm')

    async def write_chunks():
        async with aiofile.AIOFile(wavfile, 'rb') as afp:
            reader = aiofile.Reader(afp, chunk_size=1024 * 16)
            async for chunk in reader:
                await stream.input_stream.send_audio_event(audio_chunk=chunk)
            await stream.input_stream.end_stream()

    handler = MyEventHandler(stream.output_stream)
    await asyncio.gather(write_chunks(), handler.handle_events())
Beispiel #11
0
async def nonstop_stream_transcribe(apiKey,
                                    sessionId,
                                    token,
                                    filterEnabled=False):
    myoutput = open("out.log", 'w')

    # Create FIFO
    path = "/tmp/" + ''.join(choice(ascii_uppercase) for i in range(12))
    try:
        os.mkfifo(path, 0o777)
    except OSError as e:
        print("Failed to create FIFO: %s" % e)
    else:
        print("FIFO Created")

    # Launch Native Application
    print("Launching native application process")
    process = Popen(
        ['build/vonage-audio-renderer', path, apiKey, sessionId, token],
        stdout=myoutput,
        stderr=myoutput)
    nativeProcesses[sessionId] = process
    print("Process started", sessionId)

    with open(path, 'rb') as fifo:
        print("FIFO Opened")

        # Create Client (can this be single client?)
        client = TranscribeStreamingClient(region="us-west-2")
        stream = await client.start_stream_transcription(
            language_code="en-US",
            media_sample_rate_hz=16000,
            media_encoding="pcm")
        handler = MyEventHandler(stream.output_stream)
        handler.setSessionId(sessionId)
        handler.setFilterEnabled(filterEnabled)

        print("Starting gather")
        await asyncio.gather(nonstop_write_chunks(stream, fifo),
                             handler.handle_events())
        print("Gather ended")

    print("Thread ended")
Beispiel #12
0
async def basic_transcribe():
    # Setup up our client with our chosen AWS region
    client = TranscribeStreamingClient(region="us-west-2")

    # Start transcription to generate our async stream
    stream = await client.start_stream_transcription(
        language_code="en-US",
        media_sample_rate_hz=RATE,
        media_encoding="pcm",
        show_speaker_label=True,
    )

    async def write_chunks():
        print("listening to audio")
        while ProgramIsRunning:
            chunk = audiostream.read(CHUNK)
            await stream.input_stream.send_audio_event(audio_chunk=chunk)
            #print("Sent 1k audio")
        await stream.input_stream.end_stream()
  
    # Instantiate our handler and start processing events
    handler = MyEventHandler(stream.output_stream)
    await asyncio.gather(write_chunks(), handler.handle_events())
Beispiel #13
0
import asyncio
from amazon_transcribe.client import TranscribeStreamingClient
from amazon_transcribe.handlers import TranscriptResultStreamHandler
from amazon_transcribe.model import TranscriptEvent

client = TranscribeStreamingClient(region="us-east-1")

class TranscriptResult:
  def __init__(self):
    self.result = None

  def set(self, result):
    self.result = result

  def get(self):
    return self.result

class MyEventHandler(TranscriptResultStreamHandler):
  def __init__(self, transcript_result_stream, transcript_result):
    self.transcript_result = transcript_result
    TranscriptResultStreamHandler.__init__(self, transcript_result_stream)

  async def handle_transcript_event(self, transcript_event: TranscriptEvent):
    results = transcript_event.transcript.results
    for result in results:
      for alt in result.alternatives:
        self.transcript_result.set(alt.transcript)

async def basic_transcribe(transcript_result):
  stream = await client.start_stream_transcription(
      language_code="en-US",
Beispiel #14
0
 def test_basic_client_setup(self):
     client = TranscribeStreamingClient(region="us-west-2")
     assert client.service_name == "transcribe"
     assert client.region == "us-west-2"
     assert client._endpoint_resolver is not None
 def test_client_setup_without_region(self):
     # The client must take a keyword `region`
     with pytest.raises(TypeError) as e:
         client = TranscribeStreamingClient()
Beispiel #16
0
 def client(self):
     return TranscribeStreamingClient(region="us-west-2")