-
Notifications
You must be signed in to change notification settings - Fork 1
/
translate.py
36 lines (28 loc) · 994 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
34
35
36
import requests, uuid, json
# Add your subscription key and endpoint
subscription_key = "206a88b788d14952ae98ee2d7a391bae"
endpoint = "https://manitranslateapi.cognitiveservices.azure.com"
# Add your location, also known as region. The default is global.
# This is required if using a Cognitive Services resource.
location = "westus"
path = '/translate'
constructed_url = endpoint + path
params = {
'api-version': '3.0',
'from': 'en',
'to': ['de', 'it']
}
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': 'Hello World!'
}]
request = requests.post(constructed_url, params=params, headers=headers, json=body)
response = request.json()
print(json.dumps(response, sort_keys=True, ensure_ascii=False, indent=4, separators=(',', ': ')))