@@ -29,7 +29,7 @@ With a single API call, get access to AI models built on the latest AI breakthro
29
29
- [ ** Core Examples** ] ( #core-examples )
30
30
- [ ** LeMUR Examples** ] ( #lemur-examples )
31
31
- [ ** Audio Intelligence Examples** ] ( #audio-intelligence-examples )
32
- - [ ** Real-Time Examples** ] ( #real-time -examples )
32
+ - [ ** Streaming Examples** ] ( #streaming -examples )
33
33
- [ Playgrounds] ( #playgrounds )
34
34
- [ Advanced] ( #advanced )
35
35
- [ How the SDK handles Default Configurations] ( #how-the-sdk-handles-default-configurations )
@@ -692,62 +692,77 @@ for result in transcript.auto_highlights.results:
692
692
693
693
---
694
694
695
- ### ** Real-Time Examples**
695
+ ### ** Streaming Examples**
696
696
697
- [ Read more about our Real-Time service.] ( https://www.assemblyai.com/docs/Guides/real-time_streaming_transcription )
697
+ [ Read more about our streaming service.] ( https://www.assemblyai.com/docs/getting-started/transcribe-streaming-audio )
698
698
699
699
<details >
700
700
<summary >Stream your microphone in real-time</summary >
701
701
702
702
``` python
703
703
import assemblyai as aai
704
+ from assemblyai.streaming.v3 import (
705
+ BeginEvent,
706
+ StreamingClient,
707
+ StreamingClientOptions,
708
+ StreamingError,
709
+ StreamingEvents,
710
+ StreamingParameters,
711
+ StreamingSessionParameters,
712
+ TerminationEvent,
713
+ TurnEvent,
714
+ )
704
715
705
- def on_open ( session_opened : aai.RealtimeSessionOpened ):
716
+ def on_begin ( self : Type[StreamingClient], event : BeginEvent ):
706
717
" This function is called when the connection has been established."
707
718
708
- print (" Session ID:" , session_opened.session_id )
719
+ print (" Session ID:" , event.id )
709
720
710
- def on_data ( transcript : aai.RealtimeTranscript ):
721
+ def on_turn ( self : Type[StreamingClient], event : TurnEvent ):
711
722
" This function is called when a new transcript has been received."
712
723
713
- if not transcript.text:
714
- return
715
-
716
- if isinstance (transcript, aai.RealtimeFinalTranscript):
717
- print (transcript.text, end = " \r\n " )
718
- else :
719
- print (transcript.text, end = " \r " )
724
+ print (event.transcript, end = " \r\n " )
720
725
721
- def on_error ( error : aai.RealtimeError ):
726
+ def on_terminated ( self : Type[StreamingClient], event : TerminationEvent ):
722
727
" This function is called when an error occurs."
723
728
724
- print (" An error occured:" , error)
729
+ print (
730
+ f " Session terminated: { event.audio_duration_seconds} seconds of audio processed "
731
+ )
725
732
726
- def on_close ( ):
733
+ def on_error ( self : Type[StreamingClient], error : StreamingError ):
727
734
" This function is called when the connection has been closed."
728
735
729
- print (" Closing Session " )
736
+ print (f " Error occurred: { error } " )
730
737
731
738
732
- # Create the Real-Time transcriber
733
- transcriber = aai.RealtimeTranscriber(
734
- on_data = on_data,
735
- on_error = on_error,
736
- sample_rate = 44_100 ,
737
- on_open = on_open, # optional
738
- on_close = on_close, # optional
739
+ # Create the streaming client
740
+ transcriber = StreamingClient(
741
+ StreamingClientOptions(
742
+ api_key = " YOUR_API_KEY" ,
743
+ )
739
744
)
740
745
746
+ client.on(StreamingEvents.Begin, on_begin)
747
+ client.on(StreamingEvents.Turn, on_turn)
748
+ client.on(StreamingEvents.Termination, on_terminated)
749
+ client.on(StreamingEvents.Error, on_error)
750
+
741
751
# Start the connection
742
- transcriber.connect()
752
+ client.connect(
753
+ StreamingParameters(
754
+ sample_rate = 16_000 ,
755
+ formatted_finals = True ,
756
+ )
757
+ )
743
758
744
759
# Open a microphone stream
745
760
microphone_stream = aai.extras.MicrophoneStream()
746
761
747
762
# Press CTRL+C to abort
748
763
transcriber.stream(microphone_stream)
749
764
750
- transcriber.close ()
765
+ transcriber.disconnect ()
751
766
```
752
767
753
768
</details >
@@ -756,99 +771,13 @@ transcriber.close()
756
771
<summary >Transcribe a local audio file in real-time</summary >
757
772
758
773
``` python
759
- import assemblyai as aai
760
-
761
-
762
- def on_data (transcript : aai.RealtimeTranscript):
763
- " This function is called when a new transcript has been received."
764
-
765
- if not transcript.text:
766
- return
767
-
768
- if isinstance (transcript, aai.RealtimeFinalTranscript):
769
- print (transcript.text, end = " \r\n " )
770
- else :
771
- print (transcript.text, end = " \r " )
772
-
773
- def on_error (error : aai.RealtimeError):
774
- " This function is called when the connection has been closed."
775
-
776
- print (" An error occured:" , error)
777
-
778
-
779
- # Create the Real-Time transcriber
780
- transcriber = aai.RealtimeTranscriber(
781
- on_data = on_data,
782
- on_error = on_error,
783
- sample_rate = 44_100 ,
784
- )
785
-
786
- # Start the connection
787
- transcriber.connect()
788
-
789
774
# Only WAV/PCM16 single channel supported for now
790
775
file_stream = aai.extras.stream_file(
791
776
filepath = " audio.wav" ,
792
777
sample_rate = 44_100 ,
793
778
)
794
779
795
780
transcriber.stream(file_stream)
796
-
797
- transcriber.close()
798
- ```
799
-
800
- </details >
801
-
802
- <details >
803
- <summary >End-of-utterance controls</summary >
804
-
805
- ``` python
806
- transcriber = aai.RealtimeTranscriber(... )
807
-
808
- # Manually end an utterance and immediately produce a final transcript.
809
- transcriber.force_end_utterance()
810
-
811
- # Configure the threshold for automatic utterance detection.
812
- transcriber = aai.RealtimeTranscriber(
813
- ... ,
814
- end_utterance_silence_threshold = 500
815
- )
816
-
817
- # Can be changed any time during a session.
818
- # The valid range is between 0 and 20000.
819
- transcriber.configure_end_utterance_silence_threshold(300 )
820
- ```
821
-
822
- </details >
823
-
824
- <details >
825
- <summary >Disable partial transcripts</summary >
826
-
827
- ``` python
828
- # Set disable_partial_transcripts to `True`
829
- transcriber = aai.RealtimeTranscriber(
830
- ... ,
831
- disable_partial_transcripts = True
832
- )
833
- ```
834
-
835
- </details >
836
-
837
- <details >
838
- <summary >Enable extra session information</summary >
839
-
840
- ``` python
841
- # Define a callback to handle the extra session information message
842
- def on_extra_session_information (data : aai.RealtimeSessionInformation):
843
- " This function is called when a session information message has been received."
844
-
845
- print (data.audio_duration_seconds)
846
-
847
- # Configure the RealtimeTranscriber
848
- transcriber = aai.RealtimeTranscriber(
849
- ... ,
850
- on_extra_session_information = on_extra_session_information,
851
- )
852
781
```
853
782
854
783
</details >
0 commit comments