-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate.py
33 lines (26 loc) · 887 Bytes
/
translate.py
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
import os, requests, uuid, json
def translate(text):
endpoint = os.getenv("TRANSLATOR_ENDPOINT")
location = os.getenv("TRANSLATOR_LOCATION")
subscription_key = os.getenv('TRANSLATOR_SUBSCRIPTION_KEY')
path = '/translate'
constructed_url = endpoint + path
params = {
'api-version': '3.0',
'from': 'en',
'to': 'pt'
}
constructed_url = endpoint + path
headers = {
'Ocp-Apim-Subscription-Key': subscription_key,
'Ocp-Apim-Subscription-Region': location,
'Content-type': 'application/json',
'X-ClientTraceId': str(uuid.uuid4())
}
# You can pass more than one object in body.
body = [{
'text': text
}]
request = requests.post(constructed_url, params=params, headers=headers, json=body)
response = request.json()
return response[0]['translations'][0]['text']