File size: 1,952 Bytes
e29fae9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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",
    "翻译器": "Translator",
}


def _L(zh_txt: str):
    return ZH2EN[zh_txt] if EN_US else zh_txt


def infer(source, direction):
    status = "Success"
    result = None
    try:
        if not source or not direction:
            raise ValueError("请输入有效文本!")

        response = requests.request(
            "POST",
            API_TRANS,
            data=json.dumps(
                {
                    "source": source,
                    "trans_type": direction,
                    "request_id": "demo",
                    "detect": True,
                }
            ),
            headers={
                "content-type": "application/json",
                "x-authorization": f"token {KEY_TRANS}",
            },
        )

        result = json.loads(response.text)["target"]

    except Exception as e:
        status = f"{e}"

    return status, result


if __name__ == "__main__":
    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"],
            ["This is the best translation service.", "auto2zh"],
        ],
        cache_examples=False,
        title=_L("翻译器")
    ).launch()