Skip to main content

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', 10090)
result = client.translate(${CONTENT}, from_lang='日文', to_lang='中文')     # 使用内置 AI 人设
result = client.translate(${CONTENT}, args={ ARG_ROLE: ${GPT_AI_ROLE} })    # 使用自定义 AI 人设

更多使用方法详见单元测试:

Metadata

Release files for py-transgpt 2.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for py-transgpt 2.4
File Size Uploaded
py_transgpt-2.4.tar.gz 14.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for py-transgpt 2.4
File Interpreter ABI Platform
py_transgpt-2.4-py3-none-any.whl Python 3 none any Details

Total release size: 26.4 kB

Release files / py_transgpt-2.4.tar.gz

Download URL py_transgpt-2.4.tar.gz
Size 14.3 kB
Tags Source
SHA-256 checksum
How to use checksums
c7117a14a28fb99923384410a5b53728b27e3537bf10fbaa5b36d38a0bb38114
BLAKE2b-256 checksum
How to use checksums
00d07ff80bae1e70f2d7ce3b1b410678564c81a7b37fababada76529e67497c9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release files / py_transgpt-2.4-py3-none-any.whl

Download URL py_transgpt-2.4-py3-none-any.whl
Size 12.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
80cd680104e120ca4bd3875febcc2da98a6c15191f33db7418e3abe6ca4a9a65
BLAKE2b-256 checksum
How to use checksums
6108eb2ff67ef519bad9c66180bba31b71c965bfba15699fea6a92a218ffd895
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release history Release notifications | RSS feed

This release

2.4 This release

2 release files

2.3

2 release files

2.2

2 release files

2.1

2 release files

2.0

2 release files

1.9

2 release files

1.8

2 release files

1.7

2 release files

1.6

2 release files

1.5

2 release files

1.4

2 release files

1.3

2 release files

1.2

2 release files

1.1

2 release files

1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page