freemt
commited on
Commit
·
a8fe3e5
1
Parent(s):
a35ce19
Update using blocks
Browse files- POST +0 -12
- app.py +28 -1
- app_blocks.py +102 -0
- app_interface.py +75 -0
POST
DELETED
|
@@ -1,12 +0,0 @@
|
|
| 1 |
-
HTTP/1.1 405 Method Not Allowed
|
| 2 |
-
Date: Sat, 06 Aug 2022 01:37:43 GMT
|
| 3 |
-
Content-Type: application/json
|
| 4 |
-
Content-Length: 31
|
| 5 |
-
Connection: keep-alive
|
| 6 |
-
Server: nginx
|
| 7 |
-
X-Powered-By: huggingface-moon
|
| 8 |
-
x-request-id: 23fbb637-9bd3-4eeb-9743-a4f6e73c1802
|
| 9 |
-
allow: POST
|
| 10 |
-
Link: <https://huggingface.co/spaces/mikeee/opus-mt>; rel="canonical"
|
| 11 |
-
Strict-Transport-Security: max-age=31536000; includeSubDomains
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py
CHANGED
|
@@ -6,7 +6,6 @@ import gradio as gr
|
|
| 6 |
from easynmt import EasyNMT
|
| 7 |
from logzero import logger
|
| 8 |
|
| 9 |
-
# model = EasyNMT("opus-mt")
|
| 10 |
translate = EasyNMT("opus-mt").translate
|
| 11 |
|
| 12 |
|
|
@@ -59,6 +58,7 @@ outputs = [
|
|
| 59 |
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.
|
| 60 |
"""
|
| 61 |
|
|
|
|
| 62 |
iface = gr.Interface(
|
| 63 |
fn=opusmt,
|
| 64 |
inputs=inputs,
|
|
@@ -72,4 +72,31 @@ iface = gr.Interface(
|
|
| 72 |
allow_flagging="never",
|
| 73 |
cache_examples=False,
|
| 74 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
iface.launch(enable_queue=True)
|
|
|
|
| 6 |
from easynmt import EasyNMT
|
| 7 |
from logzero import logger
|
| 8 |
|
|
|
|
| 9 |
translate = EasyNMT("opus-mt").translate
|
| 10 |
|
| 11 |
|
|
|
|
| 58 |
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.
|
| 59 |
"""
|
| 60 |
|
| 61 |
+
_ = """
|
| 62 |
iface = gr.Interface(
|
| 63 |
fn=opusmt,
|
| 64 |
inputs=inputs,
|
|
|
|
| 72 |
allow_flagging="never",
|
| 73 |
cache_examples=False,
|
| 74 |
)
|
| 75 |
+
# """
|
| 76 |
+
|
| 77 |
+
with gr.Blocks() as iface:
|
| 78 |
+
with gr.Row():
|
| 79 |
+
text = gr.Textbox(
|
| 80 |
+
label="text to translate",
|
| 81 |
+
value="",
|
| 82 |
+
lines=7,
|
| 83 |
+
max_lines=200,
|
| 84 |
+
)
|
| 85 |
+
out = gr.Textbox(
|
| 86 |
+
label="translated",
|
| 87 |
+
lines=7,
|
| 88 |
+
max_lines=200,
|
| 89 |
+
)
|
| 90 |
+
with gr.Row():
|
| 91 |
+
from_lang = gr.Textbox(label="from-lang", value="auto")
|
| 92 |
+
to_lang = gr.Textbox(label="to-lang", value="zh")
|
| 93 |
+
btn = gr.Button("Translate")
|
| 94 |
+
btn.click(
|
| 95 |
+
fn=opusmt,
|
| 96 |
+
inputs=[text, from_lang, to_lang],
|
| 97 |
+
outputs=out,
|
| 98 |
+
api_name="opusmt",
|
| 99 |
+
)
|
| 100 |
+
gr.Markdown(f"{description}")
|
| 101 |
+
|
| 102 |
iface.launch(enable_queue=True)
|
app_blocks.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Prep gradio API."""
|
| 2 |
+
# pylint: diable=invalid-name
|
| 3 |
+
from typing import List, Optional, Union
|
| 4 |
+
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from easynmt import EasyNMT
|
| 7 |
+
from logzero import logger
|
| 8 |
+
|
| 9 |
+
translate = EasyNMT("opus-mt").translate
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def opusmt(
|
| 13 |
+
text: str,
|
| 14 |
+
from_lang: Optional[str] = None,
|
| 15 |
+
to_lang: str = "zh",
|
| 16 |
+
) -> Union[str, List[str]]:
|
| 17 |
+
"""Translate via easyntm-opus-mt."""
|
| 18 |
+
logger.debug("%s, %s, %s", text[:100], from_lang, to_lang)
|
| 19 |
+
if to_lang in ["auto"]:
|
| 20 |
+
if from_lang in ["zh"]:
|
| 21 |
+
to_lang = "en"
|
| 22 |
+
else:
|
| 23 |
+
to_lang = "zh"
|
| 24 |
+
|
| 25 |
+
if from_lang in ["auto"]:
|
| 26 |
+
from_lang = None
|
| 27 |
+
|
| 28 |
+
try:
|
| 29 |
+
res = translate(
|
| 30 |
+
text,
|
| 31 |
+
target_lang=to_lang,
|
| 32 |
+
source_lang=from_lang,
|
| 33 |
+
)
|
| 34 |
+
except Exception as e:
|
| 35 |
+
logger.error(e)
|
| 36 |
+
res = f"errors: {e}"
|
| 37 |
+
return res
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
inputs = [
|
| 41 |
+
gr.Textbox(
|
| 42 |
+
label="text to translate",
|
| 43 |
+
value="",
|
| 44 |
+
lines=7,
|
| 45 |
+
max_lines=200,
|
| 46 |
+
),
|
| 47 |
+
gr.Textbox(label="from-lang", value="auto"),
|
| 48 |
+
gr.Textbox(label="to-lang", value="zh"),
|
| 49 |
+
]
|
| 50 |
+
outputs = [
|
| 51 |
+
gr.Textbox(
|
| 52 |
+
label="translated",
|
| 53 |
+
lines=7,
|
| 54 |
+
max_lines=200,
|
| 55 |
+
),
|
| 56 |
+
]
|
| 57 |
+
|
| 58 |
+
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.
|
| 59 |
+
"""
|
| 60 |
+
|
| 61 |
+
_ = """
|
| 62 |
+
iface = gr.Interface(
|
| 63 |
+
fn=opusmt,
|
| 64 |
+
inputs=inputs,
|
| 65 |
+
outputs=outputs,
|
| 66 |
+
title="opus mt",
|
| 67 |
+
description=description,
|
| 68 |
+
examples=[
|
| 69 |
+
["This is a test.", "en", "zh"],
|
| 70 |
+
["This is a test.", "en", "de"],
|
| 71 |
+
],
|
| 72 |
+
allow_flagging="never",
|
| 73 |
+
cache_examples=False,
|
| 74 |
+
)
|
| 75 |
+
# """
|
| 76 |
+
|
| 77 |
+
with gr.Blocks() as iface:
|
| 78 |
+
with gr.Row():
|
| 79 |
+
text = gr.Textbox(
|
| 80 |
+
label="text to translate",
|
| 81 |
+
value="",
|
| 82 |
+
lines=7,
|
| 83 |
+
max_lines=200,
|
| 84 |
+
)
|
| 85 |
+
out = gr.Textbox(
|
| 86 |
+
label="translated",
|
| 87 |
+
lines=7,
|
| 88 |
+
max_lines=200,
|
| 89 |
+
)
|
| 90 |
+
with gr.Row():
|
| 91 |
+
from_lang = gr.Textbox(label="from-lang", value="auto")
|
| 92 |
+
to_lang = gr.Textbox(label="to-lang", value="zh")
|
| 93 |
+
btn = gr.Button("Translate")
|
| 94 |
+
btn.click(
|
| 95 |
+
fn=opusmt,
|
| 96 |
+
inputs=[text, from_lang, to_lang],
|
| 97 |
+
outputs=out,
|
| 98 |
+
api_name="opusmt",
|
| 99 |
+
)
|
| 100 |
+
gr.Markdown(f"{description}")
|
| 101 |
+
|
| 102 |
+
iface.launch(enable_queue=True)
|
app_interface.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Prep gradio API."""
|
| 2 |
+
# pylint: diable=invalid-name
|
| 3 |
+
from typing import List, Optional, Union
|
| 4 |
+
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from easynmt import EasyNMT
|
| 7 |
+
from logzero import logger
|
| 8 |
+
|
| 9 |
+
# model = EasyNMT("opus-mt")
|
| 10 |
+
translate = EasyNMT("opus-mt").translate
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def opusmt(
|
| 14 |
+
text: str,
|
| 15 |
+
from_lang: Optional[str] = None,
|
| 16 |
+
to_lang: str = "zh",
|
| 17 |
+
) -> Union[str, List[str]]:
|
| 18 |
+
"""Translate via easyntm-opus-mt."""
|
| 19 |
+
logger.debug("%s, %s, %s", text[:100], from_lang, to_lang)
|
| 20 |
+
if to_lang in ["auto"]:
|
| 21 |
+
if from_lang in ["zh"]:
|
| 22 |
+
to_lang = "en"
|
| 23 |
+
else:
|
| 24 |
+
to_lang = "zh"
|
| 25 |
+
|
| 26 |
+
if from_lang in ["auto"]:
|
| 27 |
+
from_lang = None
|
| 28 |
+
|
| 29 |
+
try:
|
| 30 |
+
res = translate(
|
| 31 |
+
text,
|
| 32 |
+
target_lang=to_lang,
|
| 33 |
+
source_lang=from_lang,
|
| 34 |
+
)
|
| 35 |
+
except Exception as e:
|
| 36 |
+
logger.error(e)
|
| 37 |
+
res = f"errors: {e}"
|
| 38 |
+
return res
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
inputs = [
|
| 42 |
+
gr.Textbox(
|
| 43 |
+
label="text to translate",
|
| 44 |
+
value="",
|
| 45 |
+
lines=7,
|
| 46 |
+
max_lines=200,
|
| 47 |
+
),
|
| 48 |
+
gr.Textbox(label="from-lang", value="auto"),
|
| 49 |
+
gr.Textbox(label="to-lang", value="zh"),
|
| 50 |
+
]
|
| 51 |
+
outputs = [
|
| 52 |
+
gr.Textbox(
|
| 53 |
+
label="translated",
|
| 54 |
+
lines=7,
|
| 55 |
+
max_lines=200,
|
| 56 |
+
),
|
| 57 |
+
]
|
| 58 |
+
|
| 59 |
+
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.
|
| 60 |
+
"""
|
| 61 |
+
|
| 62 |
+
iface = gr.Interface(
|
| 63 |
+
fn=opusmt,
|
| 64 |
+
inputs=inputs,
|
| 65 |
+
outputs=outputs,
|
| 66 |
+
title="opus mt",
|
| 67 |
+
description=description,
|
| 68 |
+
examples=[
|
| 69 |
+
["This is a test.", "en", "zh"],
|
| 70 |
+
["This is a test.", "en", "de"],
|
| 71 |
+
],
|
| 72 |
+
allow_flagging="never",
|
| 73 |
+
cache_examples=False,
|
| 74 |
+
)
|
| 75 |
+
iface.launch(enable_queue=True)
|