做了一个本地离线翻译器

发布于:2025-07-20 ⋅ 阅读:(17) ⋅ 点赞:(0)

概述

项目上要求做一个简单的翻译器,支持中英语言互翻,不能联网,离线使用。

起初找了现有的开源方案,发现存在以下问题:

  • 功能简洁的方案用的模型太老,效果有限
  • 较新且热门的方案功能太多,不够简洁

于是打算重新做一个,力求美观简洁,交互简单,上手即用。

效果演示

软件起名为本地翻译器(LocalTranslator)

效果演示:

开源地址:https://github.com/zstar1003/LocalTranslator

下载地址(windows):

  • Github:https://github.com/zstar1003/LocalTranslator/releases/download/v1.0.0/LocalTranslator_Setup.exe

  • 百度网盘:https://pan.baidu.com/s/1kVaEJL-V24ZXaVWZ5rQWfw?pwd=8888 提取码: 8888

软件支持中文、英文、俄语三种语言的互相翻译,同时支持两种主题颜色的切换。

翻译算法简介

算法模型通过 hugging face 上搜索 Translation 关键词进行筛选。

下载量较大的模型往往支持的语言种类会更多,但模型体积会较大。

结合下载量和模型体积综合考虑,选取了t5_translate_en_ru_zh_small_1024这款模型,模型参数量仅 111 M, BF16 精度,体积约 211 M。

通过以下脚本,可以快速实现将英文翻译成中文。

from transformers import T5ForConditionalGeneration, T5Tokenizer

device = 'cuda' #or 'cpu' for translate on cpu

model_name = 'utrobinmv/t5_translate_en_ru_zh_small_1024'
model = T5ForConditionalGeneration.from_pretrained(model_name)
model.to(device)
tokenizer = T5Tokenizer.from_pretrained(model_name)

prefix = 'translate to zh: '
src_text = prefix + "Neural network is a computational model inspired by biological neural system, which is one of the core technologies of machine learning and artificial intelligence. It can automatically learn complex patterns and relationships from data by simulating the connection and information processing between neurons in the human brain."

# translate English to Chinese
input_ids = tokenizer(src_text, return_tensors="pt")

generated_tokens = model.generate(**input_ids.to(device))

result = tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)
print(result)

不过,由于模型本身较小,超出一定长度的输入无法被正确翻译,甚至会出现幻觉,因此设定了单次输入的翻译上限长度为500字符。


网站公告

今日签到

点亮在社区的每一天
去签到