Skip to main content

PDFなどのドキュメントを翻訳するツール。

Project description

translatedoc

Code style: black Lint&Test PyPI version

PDFなどのドキュメントを翻訳するツール。

Unstructuredで読み込み、OpenAI APIに渡しているだけ。

インストール

1. Unstructured

インストール例:

sudo apt install poppler-utils poppler-data tesseract-ocr tesseract-ocr-jpn
pip install unstructured[all-docs] --extra-index-url https://download.pytorch.org/whl/cu118
python -m nltk.downloader --exit-on-error punkt_tab averaged_perceptron_tagger_eng

cu118のところは環境に合わせて変更する。

詳細はUnstructuredのドキュメントを参照。

2. translatedoc

pip install translatedoc

使い方

基本

export OPENAI_API_KEY=<your_api_key>
translatedoc --language=Japanese <input_files_and_or_urls>

カレントディレクトリにファイル名.Source.txtファイル名.Japanese.txtが生成される。

詳細は translatedoc --help を参照。

応用その1

テキストの抽出と翻訳を別々に行うこともできる。

translatedoc-step1 <input_files_and_or_urls>
translatedoc-step2 --language=Japanese <input_text_files>

応用その2

Pythonライブラリとしても使用可能。

import os
import translatedoc

result_text: str = translatedoc.extract_text("path_or_url.pdf")

model = "gpt-3.5-turbo-1106"
openai_client = openai.OpenAI(base_url=os.environ.get("OPENAI_API_BASE"))

chunks: list[str] = translatedoc.partition(result_text, model)

translated = "\n\n".join(
    translatedoc.translate(chunk, model, "Japanese", openai_client) for chunk in chunks
)

環境変数

環境変数名 概要 デフォルト値
OPENAI_API_BASE OpenAI APIのベースURL https://api.openai.com/v1
OPENAI_API_KEY OpenAI APIのキー -
TRANSLATEDOC_MODEL 翻訳に使用するモデル gpt-4o-mini
TRANSLATEDOC_STRATEGY ドキュメントのパーティション化に使用する戦略 hi_res

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

translatedoc-1.5.2.tar.gz (140.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

translatedoc-1.5.2-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file translatedoc-1.5.2.tar.gz.

File metadata

  • Download URL: translatedoc-1.5.2.tar.gz
  • Upload date:
  • Size: 140.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.12

File hashes

Hashes for translatedoc-1.5.2.tar.gz
Algorithm Hash digest
SHA256 02d29565b023a190abdd570253e2708e49fd75ed7df4929cc341fe0f2882aefb
MD5 e8b40452366fcd65ca28d9d8b958960d
BLAKE2b-256 6c95e4e7f78a14f8a3d8194b7e2c00815bb2ec90a5ee44997526c14fced1a608

See more details on using hashes here.

File details

Details for the file translatedoc-1.5.2-py3-none-any.whl.

File metadata

File hashes

Hashes for translatedoc-1.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 57977d4e44860f57f8b69c44db122e085c86fbe48c30942ae1d5805d89bd14f3
MD5 61fc29f97c90163ce9a4b8172f70402b
BLAKE2b-256 c39203e1c89fef76f2d825567750dffa6e9a5a90c1256a381161666cb707838d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page