freemt
commited on
Commit
·
a5b0d33
1
Parent(s):
0984515
first commit
Browse files- app.py +55 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Prep gradio API."""
|
| 2 |
+
# pylint: diable=invalid-name
|
| 3 |
+
from typing import Optional
|
| 4 |
+
from easynmt import EasyNMT
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from logzero import logger
|
| 7 |
+
|
| 8 |
+
model = EasyNMT('opus-mt')
|
| 9 |
+
translate = model.translate
|
| 10 |
+
|
| 11 |
+
def opusmt(
|
| 12 |
+
text: str,
|
| 13 |
+
# from_lang: Optional[str] = None,
|
| 14 |
+
to_lang: str = "zh",
|
| 15 |
+
) -> Union[str, List[str]]:
|
| 16 |
+
try:
|
| 17 |
+
res = translate(text, target_lang=to_lang)
|
| 18 |
+
except Exception as e:
|
| 19 |
+
logger.error(e)
|
| 20 |
+
res = f"errors: {e}"
|
| 21 |
+
return res
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
inputs = [
|
| 25 |
+
gr.Textbox(
|
| 26 |
+
label="text to translate",
|
| 27 |
+
value="zh",
|
| 28 |
+
lines=7,
|
| 29 |
+
max_lines=200,
|
| 30 |
+
),
|
| 31 |
+
# "textarea",
|
| 32 |
+
gr.Textbox(label="to_lang", value="en"),
|
| 33 |
+
]
|
| 34 |
+
outputs = [
|
| 35 |
+
gr.Textbox(
|
| 36 |
+
label=f"translated",
|
| 37 |
+
lines=7,
|
| 38 |
+
max_lines=200,
|
| 39 |
+
),
|
| 40 |
+
|
| 41 |
+
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.
|
| 42 |
+
"""
|
| 43 |
+
|
| 44 |
+
iface = gr.Interface(
|
| 45 |
+
fn=opusmt,
|
| 46 |
+
inputs=inputs,
|
| 47 |
+
outputs=putputs,
|
| 48 |
+
title="opus mt",
|
| 49 |
+
description=description,
|
| 50 |
+
examples=[
|
| 51 |
+
["This is a test.", "zh"],
|
| 52 |
+
["This is a test.", "de"],
|
| 53 |
+
]
|
| 54 |
+
)
|
| 55 |
+
iface.launch(enable_queue=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
easynmt
|
| 2 |
+
logzero
|