liuyizhang commited on
Commit
b561999
·
1 Parent(s): c4aa825

add translate

Browse files
Files changed (1) hide show
  1. baidu_translate/module.py +104 -0
baidu_translate/module.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+ import random
3
+ from hashlib import md5
4
+ from typing import Optional
5
+
6
+ import requests
7
+
8
+ import paddlehub as hub
9
+ from paddlehub.module.module import moduleinfo
10
+ from paddlehub.module.module import runnable
11
+ from paddlehub.module.module import serving
12
+
13
+
14
+ def make_md5(s, encoding='utf-8'):
15
+ return md5(s.encode(encoding)).hexdigest()
16
+
17
+
18
+ @moduleinfo(name="baidu_translate",
19
+ version="1.0.0",
20
+ type="text/machine_translation",
21
+ summary="",
22
+ author="baidu-nlp",
23
+ author_email="paddle-dev@baidu.com")
24
+ class BaiduTranslate:
25
+
26
+ def __init__(self, appid=None, appkey=None):
27
+ """
28
+ :param appid: appid for requesting Baidu translation service.
29
+ :param appkey: appkey for requesting Baidu translation service.
30
+ """
31
+ # Set your own appid/appkey.
32
+ if appid == None:
33
+ self.appid = '20201015000580007'
34
+ else:
35
+ self.appid = appid
36
+ if appkey is None:
37
+ self.appkey = 'IFJB6jBORFuMmVGDRud1'
38
+ else:
39
+ self.appkey = appkey
40
+ self.url = 'http://api.fanyi.baidu.com/api/trans/vip/translate'
41
+
42
+ def translate(self, query: str, from_lang: Optional[str] = "en", to_lang: Optional[int] = "zh"):
43
+ """
44
+ Create image by text prompts using ErnieVilG model.
45
+
46
+ :param query: Text to be translated.
47
+ :param from_lang: Source language.
48
+ :param to_lang: Dst language.
49
+
50
+ Return translated string.
51
+ """
52
+ # Generate salt and sign
53
+ salt = random.randint(32768, 65536)
54
+ sign = make_md5(self.appid + query + str(salt) + self.appkey)
55
+
56
+ # Build request
57
+ headers = {'Content-Type': 'application/x-www-form-urlencoded'}
58
+ payload = {'appid': self.appid, 'q': query, 'from': from_lang, 'to': to_lang, 'salt': salt, 'sign': sign}
59
+
60
+ # Send request
61
+ try:
62
+ r = requests.post(self.url, params=payload, headers=headers)
63
+ result = r.json()
64
+ except Exception as e:
65
+ error_msg = str(e)
66
+ raise RuntimeError(error_msg)
67
+ if 'error_code' in result:
68
+ raise RuntimeError(result['error_msg'])
69
+ return result['trans_result'][0]['dst']
70
+
71
+ @runnable
72
+ def run_cmd(self, argvs):
73
+ """
74
+ Run as a command.
75
+ """
76
+ self.parser = argparse.ArgumentParser(description="Run the {} module.".format(self.name),
77
+ prog='hub run {}'.format(self.name),
78
+ usage='%(prog)s',
79
+ add_help=True)
80
+ self.arg_input_group = self.parser.add_argument_group(title="Input options", description="Input data. Required")
81
+ self.add_module_input_arg()
82
+ args = self.parser.parse_args(argvs)
83
+ if args.appid is not None and args.appkey is not None:
84
+ self.appid = args.appid
85
+ self.appkey = args.appkey
86
+ result = self.translate(args.query, args.from_lang, args.to_lang)
87
+ return result
88
+
89
+ @serving
90
+ def serving_method(self, query, from_lang, to_lang):
91
+ """
92
+ Run as a service.
93
+ """
94
+ return self.translate(query, from_lang, to_lang)
95
+
96
+ def add_module_input_arg(self):
97
+ """
98
+ Add the command input options.
99
+ """
100
+ self.arg_input_group.add_argument('--query', type=str)
101
+ self.arg_input_group.add_argument('--from_lang', type=str, default='en', help="源语言")
102
+ self.arg_input_group.add_argument('--to_lang', type=str, default='zh', help="目标语言")
103
+ self.arg_input_group.add_argument('--appid', type=str, default=None, help="注册得到的个人appid")
104
+ self.arg_input_group.add_argument('--appkey', type=str, default=None, help="注册得到的个人appkey")