Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
gpt有时会在翻译结果中加入\n导致对齐错误,加入了replace
  • Loading branch information
Huanshere committed Sep 12, 2024
1 parent e5494b8 commit 7871efc
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions core/translate_once.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ def retry_translation(prompt, model, step_name):
## Step 1: Faithful to the Original Text
prompt1 = get_prompt_faithfulness(lines, shared_prompt)
faith_result = retry_translation(prompt1, step4_2_translate_direct_model, 'faithfulness')

# ! Replace '\n' with ' ' in faith_result[i]["Direct Translation"], sometimes gpt will add '\n'
for i in faith_result:
faith_result[i]["Direct Translation"] = faith_result[i]["Direct Translation"].replace('\n', ' ')

for i in faith_result:
print(f'📄 Original Subtitle: {faith_result[i]["Original Subtitle"]}')
print(f'📚 Direct Translation: {faith_result[i]["Direct Translation"]}')
Expand All @@ -34,11 +37,14 @@ def retry_translation(prompt, model, step_name):
print(f'📄 Original Subtitle: {express_result[i]["Original Subtitle"]}')
print(f'🧠 Free Translation: {express_result[i]["Free Translation"]}')

translate_result = "\n".join([express_result[i]["Free Translation"].strip() for i in express_result])
# ! Replace '\n' with ' ', sometimes gpt will add '\n' in the result and will cause the length of the original text and the translated text to be different
translate_result = "\n".join([express_result[i]["Free Translation"].replace('\n', ' ').strip() for i in express_result])

if len(lines.split('\n')) != len(translate_result.split('\n')):
print(f'❌ Translation of block {index} failed')
print(f'✅ Translation of block {index} completed')
print(f'❌ Translation of block {index} failed, Length Mismatch, Please check `output\gpt_log\translate_expressiveness.json`, expected {len(lines.split("\n"))} lines, but got {len(translate_result.split("\n"))} lines.')
raise ValueError(f'Original ···{lines}···,\nbut got ···{translate_result}···')
else:
print(f'✅ Translation of block {index} completed')

return translate_result, lines

Expand Down

0 comments on commit 7871efc

Please sign in to comment.