-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCaiYun.py
More file actions
38 lines (30 loc) · 1.02 KB
/
CaiYun.py
File metadata and controls
38 lines (30 loc) · 1.02 KB
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
import json
from typing import List
import requests
from requests import Response
from Settings import CaiYun_Token
class CaiYun:
def __init__(self):
self.__url: str = "https://api.interpreter.caiyunai.com/v1/translator"
self.__token: str = CaiYun_Token
self.__headers = {
'content-type': "application/json",
'x-authorization': "token " + self.__token,
}
def translate(self, source: List[str], direction: str) -> str:
detect = False
if direction == "en":
direction += "2zh"
elif direction == "zh":
direction += "2en"
else:
direction = "auto2zh"
detect = True
payload = {
"source": source,
"trans_type": direction,
"request_id": "demo",
"detect": detect,
}
response: Response = requests.request("POST", self.__url, data=json.dumps(payload), headers=self.__headers)
return json.loads(response.text)['target'][0]