-
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathtranslate_readme.py
More file actions
43 lines (35 loc) · 1.46 KB
/
translate_readme.py
File metadata and controls
43 lines (35 loc) · 1.46 KB
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
from openai import OpenAI
import os
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
def translate_text(text, target_language):
completion = client.chat.completions.create(
model="gpt-4o-mini", # 使用するモデルの指定
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": f"Translate the following text to {target_language}: {text}"}
]
)
# メッセージのコンテンツに直接アクセスする
return completion.choices[0].message.content
def main():
# Read the original README
with open('README.md', 'r', encoding='utf-8') as file:
original_text = file.read()
# Translate to English
translated_en = translate_text(original_text, 'English')
with open('README_EN.md', 'w', encoding='utf-8') as file:
file.write(translated_en)
# Translate to Chinese
translated_cn = translate_text(original_text, 'Chinese')
with open('README_CN.md', 'w', encoding='utf-8') as file:
file.write(translated_cn)
# Translate to Spanish
translated_es = translate_text(original_text, 'Spanish')
with open('README_ES.md', 'w', encoding='utf-8') as file:
file.write(translated_es)
# Translate to French
translated_fr = translate_text(original_text, 'French')
with open('README_FR.md', 'w', encoding='utf-8') as file:
file.write(translated_fr)
if __name__ == "__main__":
main()