Spaces:
Sleeping
Sleeping
Add application file
Browse files- .gitattributes +1 -0
- app.py +77 -0
- example_corpus.txt +0 -0
- requirements.txt +5 -0
- source.spm +3 -0
- target.spm +3 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
*.spm filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import io, os, tempfile
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from huggingface_hub import snapshot_download
|
| 4 |
+
import ctranslate2
|
| 5 |
+
import sentencepiece as spm
|
| 6 |
+
import hanlp
|
| 7 |
+
import re
|
| 8 |
+
MODEL_REPO = "ogaith/zhen-ctranslate2" # your CT2 model repo
|
| 9 |
+
SRC_SPM = "source.spm"
|
| 10 |
+
TGT_SPM = "target.spm"
|
| 11 |
+
|
| 12 |
+
# Download model once into the Space cache
|
| 13 |
+
MODEL_DIR = snapshot_download(MODEL_REPO)
|
| 14 |
+
|
| 15 |
+
# Load translators / tokenizers
|
| 16 |
+
translator = ctranslate2.Translator(MODEL_DIR, device="auto")
|
| 17 |
+
sp_src = spm.SentencePieceProcessor(os.path.join(MODEL_DIR, SRC_SPM))
|
| 18 |
+
sp_tgt = spm.SentencePieceProcessor(os.path.join(MODEL_DIR, TGT_SPM))
|
| 19 |
+
hanlp_tok = hanlp.load(hanlp.pretrained.tok.COARSE_ELECTRA_SMALL_ZH)
|
| 20 |
+
|
| 21 |
+
def preprocess_source(hanlp_tok, text: str) -> str:
|
| 22 |
+
text = text.strip()
|
| 23 |
+
if not text:
|
| 24 |
+
return ""
|
| 25 |
+
tokens = hanlp_tok(text)
|
| 26 |
+
text = "".join(tokens)
|
| 27 |
+
|
| 28 |
+
text = re.sub(r'([\u4e00-\u9fff])(\d)', r'\1 \2', text)
|
| 29 |
+
text = re.sub(r'(\d)([\u4e00-\u9fff])', r'\1 \2', text)
|
| 30 |
+
|
| 31 |
+
text = re.sub(r'\s+', ' ', text).strip()
|
| 32 |
+
return text
|
| 33 |
+
|
| 34 |
+
def translate_file(file_obj, beam_size, max_len, batch_size):
|
| 35 |
+
if file_obj is None:
|
| 36 |
+
return None
|
| 37 |
+
data = file_obj.decode("utf-8").splitlines()
|
| 38 |
+
|
| 39 |
+
out_lines = []
|
| 40 |
+
for i in range(0, len(data), batch_size):
|
| 41 |
+
chunk = data[i:i+batch_size]
|
| 42 |
+
pre = [preprocess_source(s) for s in chunk]
|
| 43 |
+
src_tok = [sp_src.encode(s, out_type=str) for s in pre]
|
| 44 |
+
results = translator.translate_batch(
|
| 45 |
+
src_tok,
|
| 46 |
+
beam_size=int(beam_size),
|
| 47 |
+
max_decoding_length=int(max_len)
|
| 48 |
+
)
|
| 49 |
+
for r in results:
|
| 50 |
+
out_lines.append(sp_tgt.decode(r.hypotheses[0]))
|
| 51 |
+
|
| 52 |
+
# Return a downloadable txt
|
| 53 |
+
return "\n".join(out_lines) + ("\n" if out_lines else "")
|
| 54 |
+
|
| 55 |
+
with gr.Blocks() as demo:
|
| 56 |
+
gr.Markdown("# Chinese ➜ English (CTranslate2, HanLP+SentencePiece)")
|
| 57 |
+
gr.Markdown("Upload a UTF-8 `.txt` with one sentence per line (Chinese).")
|
| 58 |
+
with gr.Row():
|
| 59 |
+
inp = gr.File(label="Upload .txt", file_count="single", type="bytes")
|
| 60 |
+
with gr.Row():
|
| 61 |
+
beam = gr.Slider(1, 8, value=4, step=1, label="Beam size")
|
| 62 |
+
max_len = gr.Slider(16, 512, value=256, step=1, label="Max decoding length")
|
| 63 |
+
bs = gr.Slider(1, 128, value=32, step=1, label="Batch size")
|
| 64 |
+
btn = gr.Button("Translate")
|
| 65 |
+
out = gr.File(label="Download translations (.txt)")
|
| 66 |
+
|
| 67 |
+
def _run(file_bytes, beam_size, max_len, batch_size):
|
| 68 |
+
text = translate_file(file_bytes, beam_size, max_len, batch_size)
|
| 69 |
+
if text is None:
|
| 70 |
+
return None
|
| 71 |
+
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".txt", mode="w", encoding="utf-8")
|
| 72 |
+
tmp.write(text); tmp.close()
|
| 73 |
+
return tmp.name
|
| 74 |
+
|
| 75 |
+
btn.click(_run, [inp, beam, max_len, bs], out)
|
| 76 |
+
|
| 77 |
+
demo.launch()
|
example_corpus.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
ctranslate2
|
| 2 |
+
sentencepiece
|
| 3 |
+
hanlp
|
| 4 |
+
gradio
|
| 5 |
+
huggingface_hub
|
source.spm
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:09889d7c2bb953f6ad7e99abbf4c6c332f833db32663e2501cc90daf82e6746d
|
| 3 |
+
size 419918
|
target.spm
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c66a7dc5c34aba632c5ba256d99bb34f4086f61f73038e4978660ae773b2a5cc
|
| 3 |
+
size 799721
|