File size: 2,014 Bytes
a5b0d33 |
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 |
"""Prep gradio API."""
# pylint: diable=invalid-name
from typing import Optional
from easynmt import EasyNMT
import gradio as gr
from logzero import logger
model = EasyNMT('opus-mt')
translate = model.translate
def opusmt(
text: str,
# from_lang: Optional[str] = None,
to_lang: str = "zh",
) -> Union[str, List[str]]:
try:
res = translate(text, target_lang=to_lang)
except Exception as e:
logger.error(e)
res = f"errors: {e}"
return res
inputs = [
gr.Textbox(
label="text to translate",
value="zh",
lines=7,
max_lines=200,
),
# "textarea",
gr.Textbox(label="to_lang", value="en"),
]
outputs = [
gr.Textbox(
label=f"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.
"""
iface = gr.Interface(
fn=opusmt,
inputs=inputs,
outputs=putputs,
title="opus mt",
description=description,
examples=[
["This is a test.", "zh"],
["This is a test.", "de"],
]
)
iface.launch(enable_queue=True)
|