Spaces:
Running
Running
import json | |
import requests | |
import gradio as gr | |
from utils import API_TRANS, KEY_TRANS, EN_US | |
ZH2EN = { | |
"输入文本区域": "Input text area", | |
"在这里输入文本...": "Type the text here...", | |
"模式": "Mode", | |
"翻译结果": "Translation results", | |
"状态栏": "Status", | |
} | |
def _L(zh_txt: str): | |
return ZH2EN[zh_txt] if EN_US else zh_txt | |
def infer(source, direction): | |
status = "Success" | |
result = None | |
payload = { | |
"source": source, | |
"trans_type": direction, | |
"request_id": "demo", | |
"detect": True, | |
} | |
headers = { | |
"content-type": "application/json", | |
"x-authorization": f"token {KEY_TRANS}", | |
} | |
try: | |
if not source or not direction: | |
raise ValueError("请输入有效文本并选择模式!") | |
response = requests.request( | |
"POST", | |
API_TRANS, | |
data=json.dumps(payload), | |
headers=headers, | |
) | |
result = json.loads(response.text)["target"] | |
except Exception as e: | |
status = f"{e}" | |
return status, result | |
def translator(): | |
return gr.Interface( | |
fn=infer, | |
inputs=[ | |
gr.TextArea(label=_L("输入文本区域"), placeholder=_L("在这里输入文本...")), | |
gr.Textbox(label=_L("模式"), value="auto2en"), | |
], | |
outputs=[ | |
gr.Textbox(label=_L("状态栏"), show_copy_button=True), | |
gr.TextArea(label=_L("翻译结果"), show_copy_button=True), | |
], | |
flagging_mode="never", | |
examples=[ | |
["彩云小译是最好的翻译服务。", "auto2ja"], | |
["彩云小译は最高の翻訳サービスです", "auto2en"], | |
["Lingocloud is the best translation service.", "auto2zh"], | |
], | |
cache_examples=False, | |
) | |