tf-bao commited on
Commit
80c37fe
·
verified ·
1 Parent(s): 666c557

Add translate.py: interactive zh<->en demo

Browse files
Files changed (1) hide show
  1. translate.py +47 -0
translate.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Interactive zh<->en translation demo for Interpreter-Qwen3-1.7B.
2
+
3
+ pip install vllm
4
+ python translate.py # loads Ismantic/Interpreter-Qwen3-1.7B
5
+ python translate.py --model_path ./ # or a local snapshot dir
6
+
7
+ Type a sentence + Enter; direction (zh->en / en->zh) is auto-detected by whether the
8
+ line contains Chinese characters. Ctrl-C / Ctrl-D to quit. Uses the model's ChatML
9
+ prompt format with <|im_end|> as the stop token and greedy decoding.
10
+ """
11
+ import sys
12
+ import argparse
13
+ from vllm import LLM, SamplingParams
14
+
15
+ PROMPT_ZH2EN = "Translate the following text from Chinese to English.\nChinese: {src}\nEnglish:"
16
+ PROMPT_EN2ZH = "Translate the following text from English to Chinese.\nEnglish: {src}\nChinese:"
17
+
18
+
19
+ def is_zh(s):
20
+ return any("一" <= c <= "鿿" for c in s)
21
+
22
+
23
+ def main():
24
+ ap = argparse.ArgumentParser()
25
+ ap.add_argument("--model_path", default="Ismantic/Interpreter-Qwen3-1.7B")
26
+ ap.add_argument("--gpu_mem", type=float, default=0.5)
27
+ args = ap.parse_args()
28
+
29
+ llm = LLM(model=args.model_path, gpu_memory_utilization=args.gpu_mem, max_model_len=1024)
30
+ sp = SamplingParams(max_tokens=256, temperature=0, stop=["<|im_end|>"])
31
+
32
+ print("\n>>> Type a sentence and press Enter (zh<->en auto-detected). Ctrl-C / Ctrl-D to quit.\n")
33
+ try:
34
+ for line in sys.stdin:
35
+ src = line.strip()
36
+ if not src:
37
+ continue
38
+ tmpl = PROMPT_ZH2EN if is_zh(src) else PROMPT_EN2ZH
39
+ prompt = f"<|im_start|>user\n{tmpl.format(src=src)}<|im_end|>\n<|im_start|>assistant\n"
40
+ out = llm.generate([prompt], sp, use_tqdm=False)[0].outputs[0].text.strip()
41
+ print("->", out, "\n")
42
+ except (KeyboardInterrupt, EOFError):
43
+ print("\nbye")
44
+
45
+
46
+ if __name__ == "__main__":
47
+ main()