# py-transgpt
> python 长文本/多平台翻译器(目前支持 baidu、tencent、chatgpt)
------
## 简介
封装了 baidu、tencent、chatgpt 翻译平台的接口的差异性,只需要简单几步配置即可使用。
同时支持长文本翻译:原生的平台接口有字数限制,但是 [transgpt](https://github.com/EXP-Codes/py-transgpt) 会自动切割长文本为多段再调用接口翻译、翻译结果自动拼接为长文本。
## 运行环境
![](https://img.shields.io/badge/Python-3.8%2B-brightgreen.svg)
## 安装说明
执行脚本:
```
python -m pip install --upgrade pip
python -m pip install py-transgpt
```
## 使用说明
### 百度翻译
> 使用前需要先到 [百度翻译开放平台](https://fanyi-api.baidu.com/manage/developer) 生成 API Key
```python
# 使用方法 1
from transgpt.translate import trans, BAIDU
result = trans(
${CONTENT},
from_lang='jp',
to_lang='zh',
platform=BAIDU,
api_id=${BD_APP_ID},
api_key=${BD_APP_KEY}
)
# 使用方法 2
from transgpt.trans_baidu import BaiduTranslation
client = BaiduTranslation(api_id=${BD_APP_ID}, api_key=${BD_APP_KEY})
result = client.translate(${CONTENT}, from_lang='jp', to_lang='zh')
```
更多使用方法详见单元测试:
- [使用方法 1](https://github.com/EXP-Codes/py-transgpt/blob/ae843092be17c53cfd40686129fa9e2976418042/test/test.py#L70)
- [使用方法 2](https://github.com/EXP-Codes/py-transgpt/blob/ae843092be17c53cfd40686129fa9e2976418042/test/test.py#L84)
![](./imgs/baidu.jpg)
### 腾讯翻译
> 使用前需要先到 [腾讯云机器翻译](https://console.cloud.tencent.com/cam/capi) 生成 API Key
```python
# 使用方法 1
from transgpt.translate import trans, TENCENT
result = trans(
${CONTENT},
from_lang='ja',
to_lang='zh',
platform=TENCENT,
api_id=${TX_SECRET_ID},
api_key=${TX_SECRET_KEY}
)
# 使用方法 2
from transgpt.trans_tencent import TencentTranslation
client = TencentTranslation(api_id=${BD_APP_ID}, api_key=${BD_APP_KEY})
result = client.translate(${CONTENT}, from_lang='ja', to_lang='zh')
```
更多使用方法详见单元测试:
- [使用方法 1](https://github.com/EXP-Codes/py-transgpt/blob/ae843092be17c53cfd40686129fa9e2976418042/test/test.py#L109)
- [使用方法 2](https://github.com/EXP-Codes/py-transgpt/blob/ae843092be17c53cfd40686129fa9e2976418042/test/test.py#L127)
![](./imgs/tencent.jpg)
### ChatGPT AI 翻译
> 使用前需要先到 [OpenAI](https://platform.openai.com/account/api-keys) 生成 API Key
```python
# 使用方法 1
from transgpt.translate import trans, CHATGPT, CHATGPT_35_TURBO
result = trans(
${CONTENT},
platform=CHATGPT,
api_id='',
api_key=${GPT_API_KEY},
args={
ARG_ROLE: ${GPT_AI_ROLE}, # Option: 定义人设
ARG_OPENAI_MODEL: CHATGPT_35_TURBO,
ARG_PROXY_IP: '127.0.0.1',
ARG_PROXY_PORT: 8888
}
)
# 使用方法 2
from transgpt.trans_chatgpt import ChatgptTranslation, CHATGPT_35_TURBO, ARG_ROLE
client = ChatgptTranslation(${GPT_API_KEY}, CHATGPT_35_TURBO, '127.0.0.1', 8888)
result = client.translate(${CONTENT}, from_lang='日文', to_lang='中文') # 使用内置 AI 人设
result = client.translate(${CONTENT}, args={ ARG_ROLE: ${GPT_AI_ROLE} }) # 使用自定义 AI 人设
```
更多使用方法详见单元测试:
- [使用方法 1](https://github.com/EXP-Codes/py-transgpt/blob/ae843092be17c53cfd40686129fa9e2976418042/test/test.py#L156)
- [使用方法 2](https://github.com/EXP-Codes/py-transgpt/blob/ae843092be17c53cfd40686129fa9e2976418042/test/test.py#L176)
![](./imgs/chatgpt.jpg)
Raw data
{
"_id": null,
"home_page": "https://github.com/EXP-Codes/py-transgpt",
"name": "py-transgpt",
"maintainer": null,
"docs_url": null,
"requires_python": "<4,>=3.5",
"maintainer_email": null,
"keywords": "translation, ChatGPT, Baidu, Tencent",
"author": "EXP",
"author_email": "exp.lqb@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/eb/24/c8ae3871dafe335ac74cedada5cbfba6738a2dd94067dbe381c62a80a723/py_transgpt-1.9.tar.gz",
"platform": null,
"description": "# py-transgpt\n\n> python \u957f\u6587\u672c/\u591a\u5e73\u53f0\u7ffb\u8bd1\u5668\uff08\u76ee\u524d\u652f\u6301 baidu\u3001tencent\u3001chatgpt\uff09\n\n------\n\n## \u7b80\u4ecb\n\n\u5c01\u88c5\u4e86 baidu\u3001tencent\u3001chatgpt \u7ffb\u8bd1\u5e73\u53f0\u7684\u63a5\u53e3\u7684\u5dee\u5f02\u6027\uff0c\u53ea\u9700\u8981\u7b80\u5355\u51e0\u6b65\u914d\u7f6e\u5373\u53ef\u4f7f\u7528\u3002\n\n\u540c\u65f6\u652f\u6301\u957f\u6587\u672c\u7ffb\u8bd1\uff1a\u539f\u751f\u7684\u5e73\u53f0\u63a5\u53e3\u6709\u5b57\u6570\u9650\u5236\uff0c\u4f46\u662f [transgpt](https://github.com/EXP-Codes/py-transgpt) \u4f1a\u81ea\u52a8\u5207\u5272\u957f\u6587\u672c\u4e3a\u591a\u6bb5\u518d\u8c03\u7528\u63a5\u53e3\u7ffb\u8bd1\u3001\u7ffb\u8bd1\u7ed3\u679c\u81ea\u52a8\u62fc\u63a5\u4e3a\u957f\u6587\u672c\u3002\n\n\n## \u8fd0\u884c\u73af\u5883\n\n![](https://img.shields.io/badge/Python-3.8%2B-brightgreen.svg)\n\n\n## \u5b89\u88c5\u8bf4\u660e\n\n\u6267\u884c\u811a\u672c\uff1a\n\n```\npython -m pip install --upgrade pip\npython -m pip install py-transgpt\n```\n\n## \u4f7f\u7528\u8bf4\u660e\n\n### \u767e\u5ea6\u7ffb\u8bd1\n\n> \u4f7f\u7528\u524d\u9700\u8981\u5148\u5230 [\u767e\u5ea6\u7ffb\u8bd1\u5f00\u653e\u5e73\u53f0](https://fanyi-api.baidu.com/manage/developer) \u751f\u6210 API Key\n\n```python\n# \u4f7f\u7528\u65b9\u6cd5 1\nfrom transgpt.translate import trans, BAIDU\nresult = trans(\n ${CONTENT}, \n from_lang='jp', \n to_lang='zh', \n platform=BAIDU, \n api_id=${BD_APP_ID}, \n api_key=${BD_APP_KEY}\n)\n\n# \u4f7f\u7528\u65b9\u6cd5 2\nfrom transgpt.trans_baidu import BaiduTranslation\nclient = BaiduTranslation(api_id=${BD_APP_ID}, api_key=${BD_APP_KEY})\nresult = client.translate(${CONTENT}, from_lang='jp', to_lang='zh')\n```\n\n\u66f4\u591a\u4f7f\u7528\u65b9\u6cd5\u8be6\u89c1\u5355\u5143\u6d4b\u8bd5\uff1a\n\n- [\u4f7f\u7528\u65b9\u6cd5 1](https://github.com/EXP-Codes/py-transgpt/blob/ae843092be17c53cfd40686129fa9e2976418042/test/test.py#L70)\n- [\u4f7f\u7528\u65b9\u6cd5 2](https://github.com/EXP-Codes/py-transgpt/blob/ae843092be17c53cfd40686129fa9e2976418042/test/test.py#L84)\n\n![](./imgs/baidu.jpg)\n\n\n### \u817e\u8baf\u7ffb\u8bd1\n\n> \u4f7f\u7528\u524d\u9700\u8981\u5148\u5230 [\u817e\u8baf\u4e91\u673a\u5668\u7ffb\u8bd1](https://console.cloud.tencent.com/cam/capi) \u751f\u6210 API Key\n\n```python\n# \u4f7f\u7528\u65b9\u6cd5 1\nfrom transgpt.translate import trans, TENCENT\nresult = trans(\n ${CONTENT}, \n from_lang='ja', \n to_lang='zh', \n platform=TENCENT, \n api_id=${TX_SECRET_ID}, \n api_key=${TX_SECRET_KEY}\n)\n\n# \u4f7f\u7528\u65b9\u6cd5 2\nfrom transgpt.trans_tencent import TencentTranslation\nclient = TencentTranslation(api_id=${BD_APP_ID}, api_key=${BD_APP_KEY})\nresult = client.translate(${CONTENT}, from_lang='ja', to_lang='zh')\n```\n\n\u66f4\u591a\u4f7f\u7528\u65b9\u6cd5\u8be6\u89c1\u5355\u5143\u6d4b\u8bd5\uff1a\n\n- [\u4f7f\u7528\u65b9\u6cd5 1](https://github.com/EXP-Codes/py-transgpt/blob/ae843092be17c53cfd40686129fa9e2976418042/test/test.py#L109)\n- [\u4f7f\u7528\u65b9\u6cd5 2](https://github.com/EXP-Codes/py-transgpt/blob/ae843092be17c53cfd40686129fa9e2976418042/test/test.py#L127)\n\n![](./imgs/tencent.jpg)\n\n\n### ChatGPT AI \u7ffb\u8bd1\n\n> \u4f7f\u7528\u524d\u9700\u8981\u5148\u5230 [OpenAI](https://platform.openai.com/account/api-keys) \u751f\u6210 API Key\n\n```python\n# \u4f7f\u7528\u65b9\u6cd5 1\nfrom transgpt.translate import trans, CHATGPT, CHATGPT_35_TURBO\nresult = trans(\n ${CONTENT}, \n platform=CHATGPT, \n api_id='', \n api_key=${GPT_API_KEY}, \n args={\n ARG_ROLE: ${GPT_AI_ROLE}, # Option: \u5b9a\u4e49\u4eba\u8bbe\n ARG_OPENAI_MODEL: CHATGPT_35_TURBO, \n ARG_PROXY_IP: '127.0.0.1', \n ARG_PROXY_PORT: 8888\n }\n)\n\n# \u4f7f\u7528\u65b9\u6cd5 2\nfrom transgpt.trans_chatgpt import ChatgptTranslation, CHATGPT_35_TURBO, ARG_ROLE\nclient = ChatgptTranslation(${GPT_API_KEY}, CHATGPT_35_TURBO, '127.0.0.1', 8888)\nresult = client.translate(${CONTENT}, from_lang='\u65e5\u6587', to_lang='\u4e2d\u6587') # \u4f7f\u7528\u5185\u7f6e AI \u4eba\u8bbe\nresult = client.translate(${CONTENT}, args={ ARG_ROLE: ${GPT_AI_ROLE} }) # \u4f7f\u7528\u81ea\u5b9a\u4e49 AI \u4eba\u8bbe\n```\n\n\u66f4\u591a\u4f7f\u7528\u65b9\u6cd5\u8be6\u89c1\u5355\u5143\u6d4b\u8bd5\uff1a\n\n- [\u4f7f\u7528\u65b9\u6cd5 1](https://github.com/EXP-Codes/py-transgpt/blob/ae843092be17c53cfd40686129fa9e2976418042/test/test.py#L156)\n- [\u4f7f\u7528\u65b9\u6cd5 2](https://github.com/EXP-Codes/py-transgpt/blob/ae843092be17c53cfd40686129fa9e2976418042/test/test.py#L176)\n\n![](./imgs/chatgpt.jpg)\n\n",
"bugtrack_url": null,
"license": null,
"summary": "Platform translation interface encapsulation: ChatGPT, Baidu, Tencent",
"version": "1.9",
"project_urls": {
"Bug Reports": "https://github.com/EXP-Codes/py-transgpt/issues",
"Funding": "https://github.com/EXP-Codes/py-transgpt/tree/master/imgs/donate-alipay.png",
"Homepage": "https://github.com/EXP-Codes/py-transgpt",
"Say Thanks!": "https://github.com/EXP-Codes/py-transgpt/tree/master/imgs/donate-wechat.png",
"Source": "https://github.com/EXP-Codes/py-transgpt"
},
"split_keywords": [
"translation",
" chatgpt",
" baidu",
" tencent"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "9ee9b5ff6b686f1bcfa47de8927d6c0e9336a9b900a9eb861ae04e9680854aaa",
"md5": "880ae24150636c583ec435f244cad905",
"sha256": "681a97dfd47cc50a3e80eff8da9137b9ad728f026a7ec0cacb57ab5810863dea"
},
"downloads": -1,
"filename": "py_transgpt-1.9-py3-none-any.whl",
"has_sig": false,
"md5_digest": "880ae24150636c583ec435f244cad905",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4,>=3.5",
"size": 11804,
"upload_time": "2024-08-28T14:46:39",
"upload_time_iso_8601": "2024-08-28T14:46:39.517950Z",
"url": "https://files.pythonhosted.org/packages/9e/e9/b5ff6b686f1bcfa47de8927d6c0e9336a9b900a9eb861ae04e9680854aaa/py_transgpt-1.9-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "eb24c8ae3871dafe335ac74cedada5cbfba6738a2dd94067dbe381c62a80a723",
"md5": "56becb433a11517fd2d124f59edbf115",
"sha256": "ef5ea062fab52579d7567a19df5c29c7508856b387fd99df3447cb1876627e64"
},
"downloads": -1,
"filename": "py_transgpt-1.9.tar.gz",
"has_sig": false,
"md5_digest": "56becb433a11517fd2d124f59edbf115",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4,>=3.5",
"size": 14020,
"upload_time": "2024-08-28T14:46:40",
"upload_time_iso_8601": "2024-08-28T14:46:40.998450Z",
"url": "https://files.pythonhosted.org/packages/eb/24/c8ae3871dafe335ac74cedada5cbfba6738a2dd94067dbe381c62a80a723/py_transgpt-1.9.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-08-28 14:46:40",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "EXP-Codes",
"github_project": "py-transgpt",
"travis_ci": true,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "py-transgpt"
}