-
Notifications
You must be signed in to change notification settings - Fork 0
/
translator.py
99 lines (91 loc) · 2.41 KB
/
translator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python
import argparse
from model import Model, TOKEN_LIMIT, trace
import prompt
def translate(
text,
source_language,
target_language,
model="gpt-3.5-turbo",
checkpoint="translator.pkl",
**kwargs,
):
model = Model(model, checkpoint, **kwargs)
messages = [
{
"role": "system",
"content": prompt.TRANSLATE.format(
source_language=source_language, target_language=target_language
),
},
{"role": "user", "content": text},
]
model.log("Translating...", force=True)
response = model.get_response(messages)
model.log("Finished translating.", force=True)
return response.choices[0].message.content
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Translate a text file from one language to another."
)
parser.add_argument(
"-i",
"--input",
type=str,
required=True,
help="The path to the text file to translate.",
)
parser.add_argument(
"-o",
"--output",
type=str,
required=True,
help="The path to save the translated text file to.",
)
parser.add_argument(
"-s",
"--source-language",
type=str,
required=True,
help="The language of the text file to translate.",
)
parser.add_argument(
"-t",
"--target-language",
type=str,
required=True,
help="The language to translate the text file to.",
)
parser.add_argument(
"-m",
"--model",
type=str,
default="gpt-3.5-turbo",
help=f'The model to use for translation. Default: gpt-3.5-turbo. Options: {", ".join(list(TOKEN_LIMIT))}',
)
parser.add_argument(
"-c",
"--checkpoint",
type=str,
default="translator.pkl",
help="The checkpoint to use for the model.",
)
parser.add_argument(
"-v",
"--verbose",
action="store_true",
help="Whether to print the logs to stdout.",
)
args = parser.parse_args()
with open(args.input, "r") as f:
text = f.read()
translated_text = translate(
text,
args.source_language,
args.target_language,
args.model,
args.checkpoint,
verbose=args.verbose,
)
with open(args.output, "w") as f:
f.write(translated_text)