-
Notifications
You must be signed in to change notification settings - Fork 1
Add speaker ID function #48
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
Open
TudorCRL
wants to merge
4
commits into
main
Choose a base branch
from
feature/speaker-id-function
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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,15 @@ | ||
| # Live Real-Time Speaker ID Example | ||
|
|
||
| This example demonstrates how to use the Speechmatics Python SDK to perform speaker ID in real-time. | ||
|
|
||
| The SDK requires an API key to be set as an environment variable before it can be used. You can obtain an API key by signing up for a Speechmatics account at https://portal.speechmatics.com/dashboard | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - Install Speechmatics RT SDK: `pip install speechmatics-rt` | ||
| - Export Speechmatics API key: `export SPEECHMATICS_API_KEY=YOUR-API-KEY` | ||
|
|
||
| ## Usage | ||
|
|
||
| - Generate speaker IDs: `python generate.py` - this will generate a `speakers.json` file | ||
| - Transcribe audio: `python transcribe.py` - this will use the `speakers.json` file to perform speaker ID on a conversation |
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,56 @@ | ||
| import asyncio | ||
| import logging | ||
| import os | ||
| import json | ||
|
|
||
| from speechmatics.rt import ( | ||
| AsyncClient, | ||
| OperatingPoint, | ||
| TranscriptionConfig, | ||
| ServerMessageType, | ||
| ) | ||
|
|
||
|
|
||
| logging.basicConfig(level=logging.INFO) | ||
|
|
||
|
|
||
| speakers: list[dict] = [] | ||
|
|
||
|
|
||
| async def generate_ids(voice_file: str) -> None: | ||
| """Run async transcription example.""" | ||
|
|
||
| transcription_config = TranscriptionConfig( | ||
| operating_point=OperatingPoint.ENHANCED, | ||
| diarization="speaker", | ||
| ) | ||
|
|
||
| # Initialize client with API key from environment | ||
| async with AsyncClient() as client: | ||
| try: | ||
| @client.on(ServerMessageType.SPEAKERS_RESULT) | ||
| def handle_speakers_result(msg): | ||
| new_speakers = msg.get('speakers', []) | ||
| new_speakers[0]["label"] = voice_file | ||
| speakers.append(new_speakers[0]) | ||
|
|
||
| # Transcribe audio file | ||
| with open(os.path.join(voices_folder, voice_file), "rb") as audio_file: | ||
| await client.transcribe( | ||
| audio_file, | ||
| transcription_config=transcription_config, | ||
| get_speakers=True, | ||
| ) | ||
| except Exception as e: | ||
| print(f"Transcription error: {e}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| voices_folder = "./examples/rt/async/speaker_id/voices" | ||
| voice_files = [f for f in os.listdir(voices_folder) if os.path.isfile(os.path.join(voices_folder, f))] | ||
|
|
||
| for voice_file in voice_files: | ||
| asyncio.run(generate_ids(voice_file)) | ||
|
|
||
| with open('./speakers.json', 'w') as f: | ||
| json.dump(speakers, f) |
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,51 @@ | ||
| import asyncio | ||
| import logging | ||
| import json | ||
|
|
||
| from speechmatics.rt import SpeakerIdentifier | ||
| from speechmatics.rt import SpeakerDiarizationConfig | ||
| from speechmatics.rt import ( | ||
| AsyncClient, | ||
| OperatingPoint, | ||
| TranscriptionConfig, | ||
| ServerMessageType | ||
| ) | ||
|
|
||
|
|
||
| logging.basicConfig(level=logging.INFO) | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| """Run async transcription example.""" | ||
|
|
||
| with open('./speakers.json') as f: | ||
| speaker_identifiers = [SpeakerIdentifier(**s) for s in json.load(f)] | ||
|
|
||
| transcription_config = TranscriptionConfig( | ||
| operating_point=OperatingPoint.ENHANCED, | ||
| diarization="speaker", | ||
| max_delay=4, | ||
| speaker_diarization_config=SpeakerDiarizationConfig( | ||
| speakers=speaker_identifiers, | ||
| ) | ||
| ) | ||
|
|
||
| # Initialize client with API key from environment | ||
| async with AsyncClient() as client: | ||
| try: | ||
| @client.on(ServerMessageType.ADD_TRANSCRIPT) | ||
| def handle_finals(msg): | ||
| print(f"Final: {msg['metadata']['speaker']} {msg['metadata']['transcript']}") | ||
|
|
||
| # Transcribe audio file | ||
| with open("./examples/conversation.wav", "rb") as audio_file: | ||
| await client.transcribe( | ||
| audio_file, | ||
| transcription_config=transcription_config, | ||
| ) | ||
| except Exception as e: | ||
| print(f"Transcription error: {e}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) |
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
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
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.
I think get speakers should be enabled by default when speaker diarization is requested rather than having an additional parameter to enable it. The user who does't need it can easily ignore it and the user who needs it can capture it just like any other message using an event handler.
This makes
get_speakersfunction redundant as you don't have to teach the user how to enable it and how to capture it, only how to capture it. And how to capture it is already demonstrated in the example.Uh oh!
There was an error while loading. Please reload this page.
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.
it depends what type of diarization is requested as it must be set as "speaker" or "channel_and_speaker" only for the speaker id to work. I am in the process of adding the
get_speakersflag to our rt and batch transcribers, It will be part of thespeaker_diarization_config. Note that enabling speaker id by default, whenever diarization is enabled has its pros and cons - e.g. customers who don't want them would still receive the speaker id messages, just saying. I have tried to turn all the speaker id related errors into warnings as not to disrupt normal transcription job, so I think it will be still safe to auto-enable it. However, the moment we miss something, there will be no way for customers to disable speaker id if diarization is enabled . Hmm, I’m leaning slightly against auto-enabling it, explicit configuration feels safer and more transparent (just my thoughts atm)? So there are indeed pros and cons to that.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.
It's possible that I'm missing some context here but when requested
SpeakersResultmessage is only sent once at the end of the session? So it shouldn't cause any issues..But since you're updating the transcriber to be enabled via
speaker_diarization_configwe should hold off on merging this changeThere 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.
I would say we don't want to auto-enable, as speaker IDs are a form of biometics and storing and handling them has legal consequences beyond our usual transcripts. There's a risk if it's auto-enabled, that customers would receive and even store biometrics without being aware of it, and that could possibly place us in an awkward position? My instinct would be in general that we should err on the side of forcing people to request biometrics rather than assume they want them.
Separately, @dln22 when will the get_speakers flag be added to speaker_diarization_config? Just wondering if we want to merge this in the meantime or whether it's better to hold off on the additional flag until it's in the transcriber?
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.
@TudorCRL My changes have been merged already, but not yet officially released. Will let you know once that happens.