Skip to content

Commit ef8dcc0

Browse files
marcusolssonAssemblyAI
andauthored
chore: sync sdk code with DeepLearning repo (#123)
Co-authored-by: AssemblyAI <[email protected]>
1 parent f8d0c76 commit ef8dcc0

File tree

1 file changed

+41
-112
lines changed

1 file changed

+41
-112
lines changed

README.md

Lines changed: 41 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ With a single API call, get access to AI models built on the latest AI breakthro
2929
- [**Core Examples**](#core-examples)
3030
- [**LeMUR Examples**](#lemur-examples)
3131
- [**Audio Intelligence Examples**](#audio-intelligence-examples)
32-
- [**Real-Time Examples**](#real-time-examples)
32+
- [**Streaming Examples**](#streaming-examples)
3333
- [Playgrounds](#playgrounds)
3434
- [Advanced](#advanced)
3535
- [How the SDK handles Default Configurations](#how-the-sdk-handles-default-configurations)
@@ -692,62 +692,77 @@ for result in transcript.auto_highlights.results:
692692

693693
---
694694

695-
### **Real-Time Examples**
695+
### **Streaming Examples**
696696

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)
698698

699699
<details>
700700
<summary>Stream your microphone in real-time</summary>
701701

702702
```python
703703
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+
)
704715

705-
def on_open(session_opened: aai.RealtimeSessionOpened):
716+
def on_begin(self: Type[StreamingClient], event: BeginEvent):
706717
"This function is called when the connection has been established."
707718

708-
print("Session ID:", session_opened.session_id)
719+
print("Session ID:", event.id)
709720

710-
def on_data(transcript: aai.RealtimeTranscript):
721+
def on_turn(self: Type[StreamingClient], event: TurnEvent):
711722
"This function is called when a new transcript has been received."
712723

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")
720725

721-
def on_error(error: aai.RealtimeError):
726+
def on_terminated(self: Type[StreamingClient], event: TerminationEvent):
722727
"This function is called when an error occurs."
723728

724-
print("An error occured:", error)
729+
print(
730+
f"Session terminated: {event.audio_duration_seconds} seconds of audio processed"
731+
)
725732

726-
def on_close():
733+
def on_error(self: Type[StreamingClient], error: StreamingError):
727734
"This function is called when the connection has been closed."
728735

729-
print("Closing Session")
736+
print(f"Error occurred: {error}")
730737

731738

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+
)
739744
)
740745

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+
741751
# Start the connection
742-
transcriber.connect()
752+
client.connect(
753+
StreamingParameters(
754+
sample_rate=16_000,
755+
formatted_finals=True,
756+
)
757+
)
743758

744759
# Open a microphone stream
745760
microphone_stream = aai.extras.MicrophoneStream()
746761

747762
# Press CTRL+C to abort
748763
transcriber.stream(microphone_stream)
749764

750-
transcriber.close()
765+
transcriber.disconnect()
751766
```
752767

753768
</details>
@@ -756,99 +771,13 @@ transcriber.close()
756771
<summary>Transcribe a local audio file in real-time</summary>
757772

758773
```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-
789774
# Only WAV/PCM16 single channel supported for now
790775
file_stream = aai.extras.stream_file(
791776
filepath="audio.wav",
792777
sample_rate=44_100,
793778
)
794779

795780
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-
)
852781
```
853782

854783
</details>

0 commit comments

Comments
 (0)