File size: 2,526 Bytes
a8fe3e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
73
74
75
76
"""Prep gradio API."""
# pylint: diable=invalid-name
from typing import List, Optional, Union

import gradio as gr
from easynmt import EasyNMT
from logzero import logger

# model = EasyNMT("opus-mt")
translate = EasyNMT("opus-mt").translate


def opusmt(
    text: str,
    from_lang: Optional[str] = None,
    to_lang: str = "zh",
) -> Union[str, List[str]]:
    """Translate via easyntm-opus-mt."""
    logger.debug("%s, %s, %s", text[:100], from_lang, to_lang)
    if to_lang in ["auto"]:
        if from_lang in ["zh"]:
            to_lang = "en"
        else:
            to_lang = "zh"

    if from_lang in ["auto"]:
        from_lang = None

    try:
        res = translate(
            text,
            target_lang=to_lang,
            source_lang=from_lang,
        )
    except Exception as e:
        logger.error(e)
        res = f"errors: {e}"
    return res


inputs = [
    gr.Textbox(
        label="text to translate",
        value="",
        lines=7,
        max_lines=200,
    ),
    gr.Textbox(label="from-lang", value="auto"),
    gr.Textbox(label="to-lang", value="zh"),
]
outputs = [
    gr.Textbox(
        label="translated",
        lines=7,
        max_lines=200,
    ),
]

description = """Supported languages: aav, aed, af, alv, am, ar, art, ase, az, bat, bcl, be, bem, ber, bg, bi, bn, bnt, bzs, ca, cau, ccs, ceb, cel, chk, cpf, crs, cs, csg, csn, cus, cy, da, de, dra, ee, efi, el, en, eo, es, et, eu, euq, fi, fj, fr, fse, ga, gaa, gil, gl, grk, guw, gv, ha, he, hi, hil, ho, hr, ht, hu, hy, id, ig, ilo, is, iso, it, ja, jap, ka, kab, kg, kj, kl, ko, kqn, kwn, kwy, lg, ln, loz, lt, lu, lua, lue, lun, luo, lus, lv, map, mfe, mfs, mg, mh, mk, mkh, ml, mos, mr, ms, mt, mul, ng, nic, niu, nl, no, nso, ny, nyk, om, pa, pag, pap, phi, pis, pl, pon, poz, pqe, pqw, prl, pt, rn, rnd, ro, roa, ru, run, rw, sal, sg, sh, sit, sk, sl, sm, sn, sq, srn, ss, ssp, st, sv, sw, swc, taw, tdt, th, ti, tiv, tl, tll, tn, to, toi, tpi, tr, trk, ts, tum, tut, tvl, tw, ty, tzo, uk, umb, ur, ve, vi, vsl, wa, wal, war, wls, xh, yap, yo, yua, zai, zh, zne. Refer to [https://github.com/UKPLab/EasyNMT](https://github.com/UKPLab/EasyNMT) for details. Estimated speed 6-60 sents/sec.
"""

iface = gr.Interface(
    fn=opusmt,
    inputs=inputs,
    outputs=outputs,
    title="opus mt",
    description=description,
    examples=[
        ["This is a test.", "en", "zh"],
        ["This is a test.", "en", "de"],
    ],
    allow_flagging="never",
    cache_examples=False,
)
iface.launch(enable_queue=True)