Skip to content

Instantly share code, notes, and snippets.

@amankharwal
Created September 19, 2020 05:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amankharwal/df0b48b142b59bac894e6615dbd7a615 to your computer and use it in GitHub Desktop.
Save amankharwal/df0b48b142b59bac894e6615dbd7a615 to your computer and use it in GitHub Desktop.
# Let's process only mp3 files
if storage_uri[-4:] ==".mp3":
client = speech_v1p1beta1.SpeechClient()
# Sample rate in Hertz of the audio data sent
sample_rate_hertz = 8000
# The language of the supplied audio
language_code = "en-US"
model = "phone_call"
# Encoding of audio data sent. This sample sets this explicitly.
# This field is optional for FLAC and WAV audio formats.
encoding = enums.RecognitionConfig.AudioEncoding.MP3
config = {
"sample_rate_hertz": sample_rate_hertz,
"language_code": language_code,
"encoding": encoding,
"model": model,
"use_enhanced": True,
"enable_automatic_punctuation": True,
"enable_speaker_diarization": True,
"diarization_speaker_count": 2,
"speech_contexts": [{
"phrases": ["Thank you for calling ABC",
"Thank you for contacting ABC",
"Welcome to ABC",
"ABC customer service",
"Thank you for calling ABC customer support."]
}]
}
audio = {"uri": storage_uri}
operation = client.long_running_recognize(config, audio)
#print(u"Waiting for operation to complete...")
response = operation.result()
transcript = ""
transcriptw = ""
sendtrans = False
keyword = "Empty Audio"
speaker = ""
for result in response.results:
words_info = result.alternatives[0].words
for word_info in words_info:
if str(word_info.speaker_tag) != "0":
if str(word_info.speaker_tag) != str(speaker):
#print(str(word_info.speaker_tag) + " is not " + str(speaker))
speaker = str(word_info.speaker_tag)
transcriptw = transcriptw + "\n-------\n*Speaker " + speaker + ":* " + word_info.word
else:
#print(str(word_info.speaker_tag) + " is " + speaker)
transcriptw = transcriptw + " " + word_info.word
speaker = str(word_info.speaker_tag)
sendtrans = False
keyword = "Empty Audio"
print(transcriptw)
if transcriptw.strip() == "":
transcriptw = "*No Sound*"
sendtrans = True
else:
list = ["bitcoin","payment", "invoice", "bill", "utilities", "utility", "electricity", "credit card", "package", "testing","kits","financial", "supplies", "mask", "symptoms", "isolate","oxygen","ventilator","social security","government","internal revenue","covid", "world health", "national institute", "virus", "corona","quarantine","stimulus","relief","cdc","disease","pandemic","epidemic","sickness"]
# Using for loop
for i in list:
if i.lower() in transcriptw.lower():
keyword = i.lower()
sendtrans = True
break
if sendtrans == True:
print(f"Sending to Slack: {file['name']}.")
filename = file['name']
send_slack(transcript.strip(),filename,keyword)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment