Platform translation interface encapsulation: ChatGPT, Baidu, Tencent
Project description
py-transgpt
python 长文本/多平台翻译器(目前支持 baidu、tencent、chatgpt)
简介
封装了 baidu、tencent、chatgpt 翻译平台的接口的差异性,只需要简单几步配置即可使用。
同时支持长文本翻译:原生的平台接口有字数限制,但是 transgpt 会自动切割长文本为多段再调用接口翻译、翻译结果自动拼接为长文本。
运行环境
安装说明
执行脚本:
python -m pip install --upgrade pip
python -m pip install py-transgpt
使用说明
百度翻译
使用前需要先到 百度翻译开放平台 生成 API Key
# 使用方法 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')
更多使用方法详见单元测试:
腾讯翻译
使用前需要先到 腾讯云机器翻译 生成 API Key
# 使用方法 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')
更多使用方法详见单元测试:
ChatGPT AI 翻译
使用前需要先到 OpenAI 生成 API Key
# 使用方法 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 人设
更多使用方法详见单元测试:
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
py_transgpt-1.6.tar.gz
(13.5 kB
view details)
Built Distribution
py_transgpt-1.6-py3-none-any.whl
(11.2 kB
view details)
File details
Details for the file py_transgpt-1.6.tar.gz
.
File metadata
- Download URL: py_transgpt-1.6.tar.gz
- Upload date:
- Size: 13.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 28fee58dc644c3c91b839e92e7eb565e34ac1ee1fbcfb73d4bf76161b8c0f642 |
|
MD5 | 295d7753a2b5576f361ce934c743be23 |
|
BLAKE2b-256 | e742482bbeaeb26f031d3cbb42d747e09fc29881f93d1952a562a0a1edef166d |
File details
Details for the file py_transgpt-1.6-py3-none-any.whl
.
File metadata
- Download URL: py_transgpt-1.6-py3-none-any.whl
- Upload date:
- Size: 11.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a4e3867ba77f758a9b218cc849d0e94d59eaa25e579e2b62666d1265d4a91afc |
|
MD5 | fd209c557b433c08f592adf5c188bf61 |
|
BLAKE2b-256 | 582422f8a2fdaab13650fdc8961a50cdad8ca1f50c00c2115e5a7e5cbce900f6 |