-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudio_listener.py
More file actions
36 lines (29 loc) · 1.43 KB
/
Copy pathaudio_listener.py
File metadata and controls
36 lines (29 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import os
import azure.cognitiveservices.speech as speechsdk
from dotenv import load_dotenv
# 1. Load the secret variables
load_dotenv()
SPEECH_KEY = os.getenv("AZURE_SPEECH_KEY")
SPEECH_REGION = os.getenv("AZURE_SPEECH_REGION")
def transcribe_audio(audio_file_path):
print(f"Sending '{audio_file_path}' to Azure for transcription...")
# 2. Configure Azure with your secret keys
speech_config = speechsdk.SpeechConfig(subscription=SPEECH_KEY, region=SPEECH_REGION)
speech_config.speech_recognition_language = "en-US" # You can change this later!
# 3. Tell Azure to listen to a specific file, not the microphone
audio_config = speechsdk.audio.AudioConfig(filename=audio_file_path)
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)
# 4. Do the transcription
result = speech_recognizer.recognize_once_async().get()
# 5. Check if it worked
if result.reason == speechsdk.ResultReason.RecognizedSpeech:
print("\n--- TRANSCRIPT SUCCESS ---")
print(result.text)
return result.text
elif result.reason == speechsdk.ResultReason.NoMatch:
print("\nNo speech could be recognized.")
elif result.reason == speechsdk.ResultReason.Canceled:
print(f"\nError: {result.cancellation_details.reason}")
# Let's test it!
# We will call the function with a dummy file name for now.
transcribe_audio("test_audio.wav")