This setup securely connects to Azure Speech-to-Text and Translator services using secrets stored in Azure Key Vault, accessed via `DefaultAzureCredential`.
β
1. Create a Key Vault in Azure Portal.
2. Add the following secrets:
| Secret Name | Example Value |
| βββββββββ | βββββββββ |
| `speech-key` | (Your Azure Speech API key) |
| `speech-region` | `eastus` |
| `translator-key` | (Your Translator API key) |
| `translator-region` | `global` |
3. Assign the executing identity (e.g. user or Managed Identity):
β
```bash brew install azure-cli pip install azure-identity azure-keyvault-secrets azure-cognitiveservices-speech requests ```
```bash az login ```
This enables `DefaultAzureCredential` to work locally.
β
```python
import azure.cognitiveservices.speech as speechsdk
import requests
from azure.identity import DefaultAzureCredential
from azure.keyvault.secrets import SecretClient
# π Load secrets from Azure Key Vault
VAULT_NAME = "your-keyvault-name" # Replace with your Key Vault name
KV_URI = f"https://{VAULT_NAME}.vault.azure.net"
credential = DefaultAzureCredential()
secret_client = SecretClient(vault_url=KV_URI, credential=credential)
# Fetch secrets
speech_key = secret_client.get_secret("speech-key").value
speech_region = secret_client.get_secret("speech-region").value
translator_key = secret_client.get_secret("translator-key").value
translator_region = secret_client.get_secret("translator-region").value
# π Language settings
SPEECH_LANGUAGE = "en-US"
TARGET_LANGUAGE = "ta" # Tamil
# π€ Speech Recognition
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=speech_region)
speech_config.speech_recognition_language = SPEECH_LANGUAGE
recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config)
print("π€ Say something...")
result = recognizer.recognize_once()
if result.reason == speechsdk.ResultReason.RecognizedSpeech:
text = result.text
print("β
Recognized:", text)
# π Translate
endpoint = f"https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&to={TARGET_LANGUAGE}"
headers = {
"Ocp-Apim-Subscription-Key": translator_key,
"Ocp-Apim-Subscription-Region": translator_region,
"Content-Type": "application/json"
}
body = [{"text": text}]
response = requests.post(endpoint, headers=headers, json=body)
if response.status_code == 200:
translated = response.json()[0]["translations"][0]["text"]
print(f"π Translated ({TARGET_LANGUAGE}): {translated}")
else:
print("β Translation failed:", response.status_code)
print("π", response.text)
elif result.reason == speechsdk.ResultReason.NoMatch:
print("β οΈ Speech not recognized.")
elif result.reason == speechsdk.ResultReason.Canceled:
print("β Error:", result.cancellation_details.reason)
```