-
Notifications
You must be signed in to change notification settings - Fork 10
/
caiyun.py
55 lines (50 loc) · 2.16 KB
/
caiyun.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# if your api can not input untranslated list such as ['Hello','World']
# but can only accept single text input like 'Hello World'
# use this api , the example is baidu.py
def tranlate_single(app_key,app_secret,source, target,proxies,text):
# return translated_text
pass
# otherwise,use this api
# notice that, any other function call should not be out of this function
# you can directly include other function in this function ,like tranlate_caiyun , otherwise will cause NameError
def tranlate_queue(app_key,app_secret,source, target,proxies,q):
# write import inside the function , otherwise will cause NameError
import json
import requests
import traceback
def tranlate_caiyun(token, from_to, proxies, q):
url = "http://api.interpreter.caiyunai.com/v1/translator"
payload = {
"source": q,
"trans_type": from_to,
"request_id": "demo",
"detect": True,
}
headers = {
"content-type": "application/json",
"x-authorization": "token " + token,
}
response = requests.request("POST", url, data=json.dumps(payload), headers=headers, proxies=proxies)
try:
result = json.loads(response.text)["target"]
l = []
for i, e in enumerate(result):
dic = dict()
dic['untranslatedText'] = q[i]
dic['translatedText'] = e
l.append(dic)
# you are supposed to return a list
# the list item should be a dict which contains two keys
# 'untranslatedText' : the original text
# 'translatedText : the translated text
return l
except Exception:
print(response.status_code)
print(response.text)
msg = traceback.format_exc()
print(msg)
from_to = source + '2' + target
return tranlate_caiyun(app_key,from_to,proxies,q)
# source = ["Lingocloud is the best translation service.", "彩云小译は最高の翻訳サービスです"]
# result = tranlate_queue('3975l6lr5pcbvidl6jl2',None,'auto','zh',{'http':'http://localhost:10809'},source)
# print(result)