Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pycon_sprints_add_chatgpt #686

Open
wants to merge 3 commits into
base: 3.12
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .scripts/chatgpt_translate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/sh

WORK_DIR=.scripts
cd $WORK_DIR

source utils/install_poetry.sh

TEMP=tmp.po
TARGET=../$2

poetry lock
poetry install
poetry run bash -c "
python google_translate/main.py $2 $TARGET $3 > $TEMP
pomerge -t $TARGET -i $TEMP -o $TARGET
"
rm $TEMP
24 changes: 24 additions & 0 deletions .scripts/google_translate/chatgpt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import json
import requests

def translate(api_key, ipt):
prompt = f'假設你是一位python工程師,請將下列文件翻譯為繁體中文 “{ipt}”'
response = requests.post(
'https://api.openai.com/v1/completions',
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {api_key}'
},
json = {
'model': 'text-davinci-003',
'prompt': prompt,
'temperature': 0.4,
'max_tokens': 1000
}
)
return response

def Translator(ipt, api_key):
response = translate(api_key, ipt)
opt = json.loads(response.text)['choices'][0]['text']
print(opt)
40 changes: 32 additions & 8 deletions .scripts/google_translate/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import polib
from googletrans import Translator
import chatgpt

from utils import refine_translations

Expand All @@ -25,13 +26,23 @@ def _get_po_paths(path: Path) -> List[Path]:

if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
"translator",
help="the translator to use",
choices=["google", "chatgpt"],
default="google"
)
parser.add_argument(
"path",
help="the path of a PO file or a directory containing PO files"
)
parser.add_argument(
"key",
help="api key for chatGPT use",
default=""
)
args = parser.parse_args()

translator = Translator()
po_files = _get_po_paths(Path(args.path).resolve())
errors = []
for path in po_files:
Expand All @@ -41,11 +52,24 @@ def _get_po_paths(path: Path) -> List[Path]:
errors.append(f"{path} doesn't seem to be a .po file")
continue

for entry in pofile.untranslated_entries()[::-1]:
translation = translator.translate(entry.msgid, src='en', dest='zh-TW')
if args.translator == "google":
translator = Translator()
for entry in pofile.untranslated_entries()[::-1]:
translation = translator.translate(entry.msgid, src='en', dest='zh-TW')

print(
'#, fuzzy\n'
f'msgid "{repr(entry.msgid)[1:-1]}"\n'
f'msgstr "{repr(refine_translations(translation.text))[1:-1]}"\n'
)

elif args.translator == "chatgpt":
api_key = args.key
for entry in pofile.untranslated_entries()[::-1]:
translation = chatgpt.Translator(api_key, entry.msgid)

print(
'#, fuzzy\n'
f'msgid "{repr(entry.msgid)[1:-1]}"\n'
f'msgstr "{repr(refine_translations(translation.text))[1:-1]}"\n'
)
print(
'#, fuzzy\n'
f'msgid "{repr(entry.msgid)[1:-1]}"\n'
f'msgstr "{repr(refine_translations(translation.text))[1:-1]}"\n'
)
1 change: 1 addition & 0 deletions .scripts/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ python = "^3.10"
polib = "1.1.1"
googletrans = "3.1.0a0"
translate-toolkit = "3.8.1"
requests = "2.31.0"


[build-system]
Expand Down