-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate.py
48 lines (38 loc) · 1.11 KB
/
translate.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
import sys
from openai import OpenAI
from dotenv import load_dotenv
from datetime import datetime
def main():
if len(sys.argv) > 1:
en_path = f'content/posts/{sys.argv[1]}.md'
pt_path = f'content/posts/{sys.argv[1]}.pt.md'
else:
date = datetime.today().strftime('%Y-%m-%d')
en_path = f'content/posts/{date}.md'
pt_path = f'content/posts/{date}.pt.md'
en_file = open(en_path, 'r+')
load_dotenv()
client = OpenAI()
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{
"role": "system",
"content": "You will be provided with a markdown text in English, and your task is to translate the content to Brazilian Portuguese."
},
{
"role": "user",
"content": en_file.read()
}
],
temperature=0.7,
max_tokens=3000,
top_p=1
)
translated_text = response.choices[0].message.content
pt_file = open(pt_path, 'w')
pt_file.write(translated_text)
pt_file.close()
en_file.close()
if __name__ == "__main__":
main()