一个开源可本地部署的英文翻译服务----EnToZhAPI

news/2024/10/9 7:20:27 标签: 开源, django

EnToZhAPI项目简介

项目背景

提供本地化的英文翻译服务API。支持单句翻译请求或者批量翻译请求。支持建立查询词汇表。

项目架构

前端:使用原生js,使用MDB作为CSS框架。django模板引擎渲染可视化界面。

后端:使用waitress作为后端服务器。django作为web应用框架。

翻译模型:Helsinki-NLP/opus-mt-en-zh
ps: 有效果更好的模型欢迎留言。

API:使用django-ninjaAPI实现RESTFUL API服务以及API文档。

数据库:默认使用sqlite。

缓存:使用redis进行缓存数据管理。

异步任务队列:使用celery进行异步任务处理。

反向代理:使用Nginx处理用户请求并完成分发。

项目构建:使用docker进行项目开发部署以及项目生产部署。

项目开源地址

https://gitee.com/chaofanat/EnToZhAPI

在线测试地址http://api.chaofan.online/

轻量服务器计算性能有限,翻译速度较慢。翻译效果取决于翻译的模型。

python调用API示例

import httpx, json, time

translate_api = "http://api.chaofan.online/api/translate"

def batchtranslate(texts : list[str]):
    
    payload = {"text": texts}
    payload = json.dumps(payload)
    r = httpx.post(url = "http://api.chaofan.online/api/translate/batch/", data = payload).json()
    task_ids = r["task_ids"]
    print("已经获取任务ID:")
    print(task_ids)
    print("可以通过url:"+translate_api+"/{task_id} 获取任务执行结果。")
    print("任务执行中,等待获取结果(默认等待1分钟后获取任务结果)。。。")
    time.sleep(1*60)
    
    translate_res = []
    for task_id in task_ids: 
        res = httpx.get(url = f"{translate_api}/{task_id}").json()
        translate_res = translate_res+res["result"]

    print("获取任务结果成功:")
    print("##################")
    for res in translate_res:
        print(res)
    return translate_res


sentences = [
    "The quick brown fox jumps over the lazy dog.",
    "Every moment is a fresh beginning.",
    "Life is what happens when you're busy making other plans.",
    "Stars can't shine without darkness.",
    "Believe in yourself and all that you are.",
    "Success is not final, failure is not fatal: it is the courage to continue that counts.",
    "The best way to predict your future is to create it.",
    "Smile, because it confuses people.",
    "Dream big and dare to fail.",
    "Happiness is a direction, not a destination.",
    "You miss 100% of the shots you don't take.",
    "The only way to do great work is to love what you do.",
    "The future belongs to those who believe in the beauty of their dreams.",
    "Be the change you wish to see in the world.",
    "If you want to go fast, go alone. If you want to go far, go together.",
    "The only limit to our realization of tomorrow will be our doubts of today.",
    "The greatest glory in living lies not in never falling, but in rising every time we fall.",
    "Opportunity is missed by most people because it is dressed in overalls and looks like work.",
    "Life is 10% what happens to us and 90% how we react to it.",
    "The best time to plant a tree was 20 years ago. The second best time is now."
]

translate_res = batchtranslate(sentences)


for i in translate_res:
    print(i)

http://www.niftyadmin.cn/n/5695463.html

相关文章

GPT-2 的 Transformer Block 设计与基础 Transformer 的比较

随着深度学习在自然语言处理领域的迅猛发展,Transformer 架构逐渐成为了语言模型的主流结构。自从 Vaswani 等人提出的基础 Transformer 在《Attention is All You Need》论文中首次亮相以来,各种改进版本相继问世。GPT-2 是其中一个重要的里程碑&#x…

OCP迎来新版本,让OceanBase的运维管理更高效

近期,OceanBase的OCP发布了新版本,全面支持 OceanBase 内核 4.3.2 及更低版本。新版本针对基础运维、性能监控、运维配置、外部集成等多个方面实现了 20余项的优化及强化措施,增强产品的易用性和稳定性,从而帮助用户更加高效地管理…

大厂面试真题-CAS的弊端和规避措施

CAS 操作的弊端和规避措施 1. CAS 操作的弊端 CAS 操作的弊端主要有以下 4 点。 (1)ABA问题 使用 CAS 操作内存数据时,当数据发生过变化也能更新成功,如操作序列 A>B>A 时,最 后一个 CAS 的预期数据 A 实际…

【数据结构】介绍

介绍数据结构 数据结构是计算机科学中重要的概念,是指组织和管理数据的方式。它涉及到数据的存储、操作和访问等操作。数据结构可以分为线性结构、树形结构和图形结构等。 线性结构是最简单的数据结构之一(本玄也是这样觉得(* ̄▽ ̄*))&#…

《系统架构设计师教程(第2版)》第17章-通信系统架构设计理论与实践-07-通信网络构建案例分析

文章目录 1. 高可用网络构建分析1.1 网络接入层高可用性设计1.1.1 高可用接入层特征1.1.2 接入汇聚层的方式1)倒U 形接法(组网模型一)2)U 形接法(组网模型二)3)矩形接法(组网模型三)4)三角形接法(组网模型四) 1.2 网络…

构建高效水果购物平台:SpringBoot飘香网站案例

1系统概述 1.1 研究背景 如今互联网高速发展,网络遍布全球,通过互联网发布的消息能快而方便的传播到世界每个角落,并且互联网上能传播的信息也很广,比如文字、图片、声音、视频等。从而,这种种好处使得互联网成了信息传…

C#/.NET/.NET Core技术前沿周刊 | 第 8 期(2024年10.01-10.06)

前言 C#/.NET/.NET Core技术前沿周刊,你的每周技术指南针!记录、追踪C#/.NET/.NET Core领域、生态的每周最新、最实用、最有价值的技术文章、社区动态、优质项目和学习资源等。让你时刻站在技术前沿,助力技术成长与视野拓宽。 欢迎投稿&…

CSS 图标和文本对齐

比如下面一段HTML代码&#xff0c;我们想在图标旁边显示文本或者数字 <body> <div><img src"smile.svg" alt"smile"><span>12</span></div> <div><img src"heartShape.svg" alt"…