Spaces:
Sleeping
Sleeping
| import requests, uuid, json, os | |
| def azure_translate_text(text_list, from_lang="en", to_lang="hi"): | |
| TRANSLATE_API_ENDPOINT = os.environ.get("TRANSLATE_API_ENDPOINT") | |
| url = f"{TRANSLATE_API_ENDPOINT}/translate" | |
| params = { | |
| 'api-version': '3.0', | |
| 'from': from_lang, | |
| 'to': [to_lang] | |
| } | |
| TRANSLATE_KEY = os.environ.get("TRANSLATE_KEY") | |
| LOCATION = os.environ.get("SPEECH_REGION") | |
| headers = { | |
| 'Ocp-Apim-Subscription-Key': TRANSLATE_KEY, | |
| 'Ocp-Apim-Subscription-Region': LOCATION, | |
| 'Content-type': 'application/json', | |
| 'X-ClientTraceId': str(uuid.uuid4()) | |
| } | |
| body = [{"text": text} for text in text_list] | |
| request = requests.post(url, params=params, headers=headers, json=body) | |
| response = request.json() | |
| response = [{"text": text["translations"][0]["text"]} for text in response] | |
| return response |