Created
February 3, 2022 13:23
-
-
Save csiebler/637251db38acc8fa0395492533297650 to your computer and use it in GitHub Desktop.
Short example on how to secure use Speech API on a frontend client
This file contains 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
import azure.cognitiveservices.speech as speechsdk | |
import requests | |
# Access key to Speech API from Azure (this needs to be stored in the backend and show not get to the client) | |
access_key = 'xxxxxxxxxxxx' | |
region = "westeurope" | |
# This method should run in the backend, so that access_key is not needed in the client | |
def get_auth_token(access_key): | |
fetch_token_url = f"https://{region}.api.cognitive.microsoft.com/sts/v1.0/issueToken" | |
headers = { | |
'Ocp-Apim-Subscription-Key': access_key | |
} | |
response = requests.post(fetch_token_url, headers=headers) | |
auth_token = str(response.text) | |
return auth_token | |
# This call happens on the client side (calling the backend), as auth_token expires after 10 minutes | |
auth_token = get_auth_token(access_key) | |
speech_config = speechsdk.SpeechConfig(auth_token=auth_token, region=region) | |
speech_config.speech_recognition_language="de-DE" | |
def from_file(): | |
audio_input = speechsdk.AudioConfig(filename="test.wav") | |
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_input) | |
result = speech_recognizer.recognize_once_async().get() | |
print(result.text) | |
from_file() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment