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

fix bug and add openai url config #15

Open
wants to merge 2 commits into
base: main
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
3 changes: 2 additions & 1 deletion settings.cfg.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[option]
#API key for OpenAI API
#API URL and key for OpenAI API
openai-url = https://api.openai.com/v1
openai-apikey = sk-

#Target language for translation, e.g. "English", "Chinese", "Japanese"
Expand Down
28 changes: 22 additions & 6 deletions srt_translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@
config.read_string(config_text)

# 获取openai_apikey和language
openai_url = config.get('option', 'openai-url')
openai_apikey = config.get('option', 'openai-apikey')
language_name = config.get('option', 'target-language')

# 设置openai的API密钥
openai.api_base = openai_url
openai.api_key = openai_apikey
import argparse

Expand Down Expand Up @@ -90,28 +92,42 @@ def split_text(text):
def is_translation_valid(original_text, translated_text):
def get_index_lines(text):
lines = text.split('\n')
index_lines = [line for line in lines if re.match(r'^\d+$', line.strip())]
index_lines = [line.strip() for line in lines if re.match(r'^\d+$', line.strip())]
return index_lines

def get_subtitle_lines(text):
paras = re.split(r'(\n\s*\n)', text.strip())
# 判断每段是否至少三行
subtitle_lines = [para for para in paras if len(para.split('\n')) >= 3]
return subtitle_lines

original_index_lines = get_index_lines(original_text)
translated_index_lines = get_index_lines(translated_text)
lines_match = original_index_lines == translated_index_lines

original_index_para = get_subtitle_lines(original_text)
translated_index_para = get_subtitle_lines(translated_text)
# 判断两个数组个数相等
paras_match = len(original_index_para) == len(translated_index_para)

print(original_text, original_index_lines)
print(translated_text, translated_index_lines)
return lines_match and paras_match

return original_index_lines == translated_index_lines
def translate_text(text):
max_retries = 3
retries = 0

while retries < max_retries:
try:
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
model="gpt-4o-mini",
messages=[
{
"role": "system",
"content": f"Translate the following subtitle text into {language_name}, but keep the subtitle number and timeline unchanged. Keep the format and line breaks. Please pay attention to the context to make whole dialog consistent. Use natural language and avoid word-for-word translation.",
},
{
"role": "user",
"content": f"Translate the following subtitle text into {language_name}, but keep the subtitle number and timeline unchanged: \n{text}",
"content": text,
}
],
)
Expand Down