mirror of
https://github.com/Physton/sd-webui-prompt-all-in-one.git
synced 2026-04-30 03:01:31 +00:00
Added translation API: Caiyun
增加翻译API:彩云小译
This commit is contained in:
@@ -15,6 +15,7 @@ from scripts.physton_prompt.translator.yandex_translator import YandexTranslator
|
||||
from scripts.physton_prompt.translator.youdao_translator import YoudaoTranslator
|
||||
from scripts.physton_prompt.translator.mymemory_translator import MyMemoryTranslator
|
||||
from scripts.physton_prompt.translator.niutrans_translator import NiutransTranslator
|
||||
from scripts.physton_prompt.translator.caiyun_translator import CaiyunTranslator
|
||||
|
||||
caches = {}
|
||||
|
||||
@@ -101,6 +102,8 @@ def translate(text, from_lang, to_lang, api, api_config=None):
|
||||
translator = MyMemoryTranslator()
|
||||
elif api == 'niutrans':
|
||||
translator = NiutransTranslator()
|
||||
elif api == 'caiyun':
|
||||
translator = CaiyunTranslator()
|
||||
elif 'type' in find and find['type'] == 'translators':
|
||||
translator = TranslatorsTranslator(api)
|
||||
translator.set_translator(find['translator'])
|
||||
|
||||
40
scripts/physton_prompt/translator/caiyun_translator.py
Normal file
40
scripts/physton_prompt/translator/caiyun_translator.py
Normal file
@@ -0,0 +1,40 @@
|
||||
from scripts.physton_prompt.translator.base_tanslator import BaseTranslator
|
||||
import uuid
|
||||
import requests
|
||||
import json
|
||||
from scripts.physton_prompt.get_lang import get_lang
|
||||
|
||||
|
||||
class CaiyunTranslator(BaseTranslator):
|
||||
def __init__(self):
|
||||
super().__init__('caiyun')
|
||||
|
||||
def translate(self, text):
|
||||
if not text:
|
||||
return ''
|
||||
url = 'http://api.interpreter.caiyunai.com/v1/translator'
|
||||
token = self.api_config.get('token', '')
|
||||
if not token:
|
||||
raise Exception(get_lang('is_required', {'0': 'Token'}))
|
||||
|
||||
payload = {
|
||||
"source": text,
|
||||
"trans_type": f'{self.from_lang}2{self.to_lang}',
|
||||
"request_id": str(uuid.uuid4()),
|
||||
"detect": True,
|
||||
}
|
||||
|
||||
headers = {
|
||||
"content-type": "application/json",
|
||||
"x-authorization": "token " + token,
|
||||
}
|
||||
|
||||
response = requests.post(url, data=json.dumps(payload), headers=headers)
|
||||
if not response.text:
|
||||
raise Exception(get_lang('response_is_empty', {'0': 'caiyun'}))
|
||||
result = response.json()
|
||||
if 'message' in result:
|
||||
raise Exception(result['message'])
|
||||
if 'target' not in result:
|
||||
raise Exception(get_lang('no_response_from', {'0': 'caiyun'}))
|
||||
return result['target']
|
||||
@@ -28,6 +28,8 @@ class NiutransTranslator(BaseTranslator):
|
||||
if not response.text:
|
||||
raise Exception(get_lang('response_is_empty', {'0': 'niutrans'}))
|
||||
result = response.json()
|
||||
if 'error_msg' in result:
|
||||
raise Exception(result['error_msg'])
|
||||
if 'tgt_text' not in result:
|
||||
raise Exception(get_lang('no_response_from', {'0': 'niutrans'}))
|
||||
return result['tgt_text']
|
||||
|
||||
Reference in New Issue
Block a user