Spaces:
Running
Running
Commit
·
b7b11ce
1
Parent(s):
499dfda
Update baidu_translate/module.py
Browse files
baidu_translate/module.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import argparse
|
| 2 |
-
import random
|
| 3 |
from hashlib import md5
|
| 4 |
from typing import Optional
|
| 5 |
|
|
@@ -10,11 +10,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",
|
|
@@ -28,13 +26,15 @@ class BaiduTranslate:
|
|
| 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
|
| 33 |
-
self.appid = '
|
| 34 |
else:
|
| 35 |
self.appid = appid
|
| 36 |
if appkey is None:
|
| 37 |
-
self.appkey = '
|
| 38 |
else:
|
| 39 |
self.appkey = appkey
|
| 40 |
self.url = 'http://api.fanyi.baidu.com/api/trans/vip/translate'
|
|
@@ -42,11 +42,9 @@ class BaiduTranslate:
|
|
| 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
|
|
@@ -102,3 +100,4 @@ class BaiduTranslate:
|
|
| 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")
|
|
|
|
|
|
| 1 |
import argparse
|
| 2 |
+
import random, os
|
| 3 |
from hashlib import md5
|
| 4 |
from typing import Optional
|
| 5 |
|
|
|
|
| 10 |
from paddlehub.module.module import runnable
|
| 11 |
from paddlehub.module.module import serving
|
| 12 |
|
|
|
|
| 13 |
def make_md5(s, encoding='utf-8'):
|
| 14 |
return md5(s.encode(encoding)).hexdigest()
|
| 15 |
|
|
|
|
| 16 |
@moduleinfo(name="baidu_translate",
|
| 17 |
version="1.0.0",
|
| 18 |
type="text/machine_translation",
|
|
|
|
| 26 |
:param appid: appid for requesting Baidu translation service.
|
| 27 |
:param appkey: appkey for requesting Baidu translation service.
|
| 28 |
"""
|
| 29 |
+
appid = os.environ.get('baidu_translate_appid')
|
| 30 |
+
appkey = os.environ.get('baidu_translate_appkey')
|
| 31 |
# Set your own appid/appkey.
|
| 32 |
+
if appid is None:
|
| 33 |
+
self.appid = ''
|
| 34 |
else:
|
| 35 |
self.appid = appid
|
| 36 |
if appkey is None:
|
| 37 |
+
self.appkey = ''
|
| 38 |
else:
|
| 39 |
self.appkey = appkey
|
| 40 |
self.url = 'http://api.fanyi.baidu.com/api/trans/vip/translate'
|
|
|
|
| 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 |
:param query: Text to be translated.
|
| 46 |
:param from_lang: Source language.
|
| 47 |
:param to_lang: Dst language.
|
|
|
|
| 48 |
Return translated string.
|
| 49 |
"""
|
| 50 |
# Generate salt and sign
|
|
|
|
| 100 |
self.arg_input_group.add_argument('--to_lang', type=str, default='zh', help="目标语言")
|
| 101 |
self.arg_input_group.add_argument('--appid', type=str, default=None, help="注册得到的个人appid")
|
| 102 |
self.arg_input_group.add_argument('--appkey', type=str, default=None, help="注册得到的个人appkey")
|
| 103 |
+
|