forked from zhayujie/chatgpt-on-wechat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
81 additions
and
5 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import random | ||
from hashlib import md5 | ||
|
||
import requests | ||
|
||
from config import conf | ||
from translate.translator import Translator | ||
|
||
# from langid import classify | ||
|
||
|
||
class BaiduTranslator(Translator): | ||
def __init__(self) -> None: | ||
super().__init__() | ||
endpoint = "http://api.fanyi.baidu.com" | ||
path = "/api/trans/vip/translate" | ||
self.url = endpoint + path | ||
self.appid = conf().get("baidu_translate_app_id") | ||
self.appkey = conf().get("baidu_translate_app_key") | ||
|
||
# For list of language codes, please refer to `https://api.fanyi.baidu.com/doc/21`, need to convert to ISO 639-1 codes | ||
def translate(self, query: str, from_lang: str = "", to_lang: str = "en") -> str: | ||
if not from_lang: | ||
from_lang = "auto" # baidu suppport auto detect | ||
# from_lang = classify(query)[0] | ||
salt = random.randint(32768, 65536) | ||
sign = self.make_md5(self.appid + query + str(salt) + self.appkey) | ||
headers = {"Content-Type": "application/x-www-form-urlencoded"} | ||
payload = {"appid": self.appid, "q": query, "from": from_lang, "to": to_lang, "salt": salt, "sign": sign} | ||
|
||
retry_cnt = 3 | ||
while retry_cnt: | ||
r = requests.post(self.url, params=payload, headers=headers) | ||
result = r.json() | ||
if errcode := result.get("error_code", "52000") != "52000": | ||
if errcode == "52001" or errcode == "52002": | ||
retry_cnt -= 1 | ||
continue | ||
else: | ||
raise Exception(result["error_msg"]) | ||
else: | ||
break | ||
text = "\n".join([item["dst"] for item in result["trans_result"]]) | ||
return text | ||
|
||
def make_md5(self, s, encoding="utf-8"): | ||
return md5(s.encode(encoding)).hexdigest() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
def create_translator(voice_type): | ||
if voice_type == "baidu": | ||
from translate.baidu.baidu_translate import BaiduTranslator | ||
|
||
return BaiduTranslator() | ||
raise RuntimeError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
""" | ||
Voice service abstract class | ||
""" | ||
|
||
|
||
class Translator(object): | ||
# please use https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes to specify language | ||
def translate(self, query: str, from_lang: str = "", to_lang: str = "en") -> str: | ||
""" | ||
Translate text from one language to another | ||
""" | ||
raise NotImplementedError |
File renamed without changes.