Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0fe7fe7

Browse files
committed
pycon_sprints_add_chatgpt
1 parent149fc0b commit0fe7fe7

File tree

2 files changed

+56
-8
lines changed

2 files changed

+56
-8
lines changed

‎.scripts/google_translate/chatgpt.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
importjson
2+
importrequests
3+
4+
deftranslate(api_key,ipt):
5+
prompt=f'假設你是一位python工程師,請將下列文件翻譯為繁體中文 “{ipt}”'
6+
response=requests.post(
7+
'https://api.openai.com/v1/completions',
8+
headers= {
9+
'Content-Type':'application/json',
10+
'Authorization':f'Bearer{api_key}'
11+
},
12+
json= {
13+
'model':'text-davinci-003',
14+
'prompt':prompt,
15+
'temperature':0.4,
16+
'max_tokens':1000
17+
}
18+
)
19+
returnresponse
20+
21+
defTranslator(ipt,api_key):
22+
response=translate(api_key,ipt)
23+
opt=json.loads(response.text)['choices'][0]['text']
24+
print(opt)

‎.scripts/google_translate/main.py

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
importpolib
77
fromgoogletransimportTranslator
8+
importchatgpt
89

910
fromutilsimportrefine_translations
1011

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

2627
if__name__=='__main__':
2728
parser=argparse.ArgumentParser()
29+
parser.add_argument(
30+
"translator",
31+
help="the translator to use",
32+
choices=["google","chatgpt"],
33+
default="google"
34+
)
2835
parser.add_argument(
2936
"path",
3037
help="the path of a PO file or a directory containing PO files"
3138
)
39+
parser.add_argument(
40+
"key",
41+
help="api key for chatGPT use",
42+
default=""
43+
)
3244
args=parser.parse_args()
3345

34-
translator=Translator()
3546
po_files=_get_po_paths(Path(args.path).resolve())
3647
errors= []
3748
forpathinpo_files:
@@ -41,11 +52,24 @@ def _get_po_paths(path: Path) -> List[Path]:
4152
errors.append(f"{path} doesn't seem to be a .po file")
4253
continue
4354

44-
forentryinpofile.untranslated_entries()[::-1]:
45-
translation=translator.translate(entry.msgid,src='en',dest='zh-TW')
55+
ifargs.translator=="google":
56+
translator=Translator()
57+
forentryinpofile.untranslated_entries()[::-1]:
58+
translation=translator.translate(entry.msgid,src='en',dest='zh-TW')
59+
60+
print(
61+
'#, fuzzy\n'
62+
f'msgid "{repr(entry.msgid)[1:-1]}"\n'
63+
f'msgstr "{repr(refine_translations(translation.text))[1:-1]}"\n'
64+
)
65+
66+
elifargs.translator=="chatgpt":
67+
api_key=args.key
68+
forentryinpofile.untranslated_entries()[::-1]:
69+
translation=chatgpt.Translator(api_key,entry.msgid)
4670

47-
print(
48-
'#, fuzzy\n'
49-
f'msgid "{repr(entry.msgid)[1:-1]}"\n'
50-
f'msgstr "{repr(refine_translations(translation.text))[1:-1]}"\n'
51-
)
71+
print(
72+
'#, fuzzy\n'
73+
f'msgid "{repr(entry.msgid)[1:-1]}"\n'
74+
f'msgstr "{repr(refine_translations(translation.text))[1:-1]}"\n'
75+
)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp