File size: 1,606 Bytes
547d420
 
 
 
 
 
 
 
48cb7fe
547d420
1d14376
547d420
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d14376
48cb7fe
 
1d14376
 
48cb7fe
547d420
48cb7fe
547d420
 
48cb7fe
547d420
 
53365eb
547d420
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
import os
import json
import requests
import gradio as gr


def translate(source, direction):
    if not source or not direction:
        return "Please enter valid text and select the mode!"

    # WARNING, this token is a test token for new developers, and it should be replaced by your token
    payload = {
        "source": source,
        "trans_type": direction,
        "request_id": "demo",
        "detect": True,
    }
    headers = {
        "content-type": "application/json",
        "x-authorization": f"token {os.getenv('token')}",
    }
    try:
        response = requests.request(
            "POST",
            "http://api.interpreter.caiyunai.com/v1/translator",
            data=json.dumps(payload),
            headers=headers,
        )

        return json.loads(response.text)["target"]

    except Exception as e:
        return f"{e}"


if __name__ == "__main__":
    gr.Interface(
        fn=translate,
        inputs=[
            gr.TextArea(
                label="Input text area",
                placeholder="Type the text here...",
                show_copy_button=True,
            ),
            gr.Dropdown(choices=["auto2en", "auto2zh", "auto2ja"], label="Mode"),
        ],
        outputs=gr.TextArea(label="Translation results", show_copy_button=True),
        flagging_mode="never",
        examples=[
            ["彩云小译は最高の翻訳サービスです", "auto2en"],
            ["Lingocloud is the best translation service.", "auto2zh"],
        ],
        cache_examples=False,
    ).launch()