Scripting Audio Conversations (part 1) — Live transcription with Google SpeechToText and Python Dash

Опубликовано: 04 Октябрь 2024
на канале: Andrew Fung
3,576
71

Hello Everyone! My name is Andrew Fung, in this video, I will be showing you how you can upload an audio conversation file and get the transcription of using Google’s Speech-To-Text API and display the script on the web using Python Dash. In this first part of the video, we will be focusing on getting the audio transcription program working and recognising multiple speakers in the conversation file.

#speechtotext #googlecloud #speakerdiarization #python #dash
#speakerdetection

Installation and Setup!
Google Speech to Text: https://cloud.google.com/docs/authent...
https://cloud.google.com/speech-to-te...
Dash documentation: https://dash.plotly.com/dash-core-com...

Source code for this project:
https://github.com/Andrew-FungKinHo/Y...

Check out my Github!
https://github.com/Andrew-FungKinHo

How I make my YouTube videos:
⌨️ Keyboard - Nuphy Air75 Mechanical Keyboard - https://amzn.to/3Xu4PD3
🎙 Microphone - MAONO A04 Professional Podcaster USB Microphone - https://amzn.to/3k8ocD5
🖱 Mouse - Microsoft Bluetooth Ergonomic Mouse - https://amzn.to/3CHhdHJ
🔌 Accessories - Laptop Docking Station for MacBook Pro - https://amzn.to/3CHi5Mv

Timestamps
0:00​ | Intro
6:05 | Google cloud Authentication
11:13 | First transcription
13:19 | Constructing full script
23:48 | Out tro

Full code:
———————————————————————————————
pip install --upgrade google-cloud-speech

imports
import io
import os
from google.cloud import speech_v1p1beta1 as speech


google authentication
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'YOUR PATH HERE'

wget -nc https://realenglishconversations.com/...

instantiate a speech client and declare an audio file
client = speech.SpeechClient()
speech_file = 'Driving-English-Conversation-Sample.mp3'

with io.open(speech_file, "rb") as audio_file:
content = audio_file.read()

audio = speech.RecognitionAudio(content=content)

config = speech.RecognitionConfig(
encoding=speech.RecognitionConfig.AudioEncoding.MP3,
sample_rate_hertz=16000,
language_code="en-US",
enable_speaker_diarization=True,
diarization_speaker_count=2,
)

print("Waiting for operation to complete...")
response = client.recognize(config=config, audio=audio)

result = response.results[-1]
words_info = result.alternatives[0].words

words_list = []
Printing out the output:
for word_info in words_info:
words_list.append(
{
'word': word_info.word,
'speaker_tag': word_info.speaker_tag,
'start_time': word_info.start_time,
'end_time': word_info.end_time,
}
)
print(words_list)

create a script based on the words_list
current_speaker = words_list[0]['speaker_tag']
current_line = []
script = []

for item in words_list:
if item['speaker_tag'] != current_speaker:
speaker changed, end of line
script.append(
{
'speaker': current_speaker,
'line': current_line
}
)
current_line = []
current_speaker = item['speaker_tag']
else:
same speaker, add to the current line
current_line.append(item['word'])

script.append(
{
'speaker': current_speaker,
'line': current_line
}
)

script = [print(f"Speaker {line['speaker']}: " + " ".join(line['line'])) for line in script]

———————————————————————————————

Feel free to drop a like and comment if you enjoy and video and let me know if you want me to do other types of programming videos ;) !!!