Skip to content

Commit 7871efc

Browse files
committed
fix bug
gpt有时会在翻译结果中加入\n导致对齐错误,加入了replace
1 parent e5494b8 commit 7871efc

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

core/translate_once.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ def retry_translation(prompt, model, step_name):
2121
## Step 1: Faithful to the Original Text
2222
prompt1 = get_prompt_faithfulness(lines, shared_prompt)
2323
faith_result = retry_translation(prompt1, step4_2_translate_direct_model, 'faithfulness')
24-
24+
# ! Replace '\n' with ' ' in faith_result[i]["Direct Translation"], sometimes gpt will add '\n'
25+
for i in faith_result:
26+
faith_result[i]["Direct Translation"] = faith_result[i]["Direct Translation"].replace('\n', ' ')
27+
2528
for i in faith_result:
2629
print(f'📄 Original Subtitle: {faith_result[i]["Original Subtitle"]}')
2730
print(f'📚 Direct Translation: {faith_result[i]["Direct Translation"]}')
@@ -34,11 +37,14 @@ def retry_translation(prompt, model, step_name):
3437
print(f'📄 Original Subtitle: {express_result[i]["Original Subtitle"]}')
3538
print(f'🧠 Free Translation: {express_result[i]["Free Translation"]}')
3639

37-
translate_result = "\n".join([express_result[i]["Free Translation"].strip() for i in express_result])
40+
# ! 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
41+
translate_result = "\n".join([express_result[i]["Free Translation"].replace('\n', ' ').strip() for i in express_result])
3842

3943
if len(lines.split('\n')) != len(translate_result.split('\n')):
40-
print(f'❌ Translation of block {index} failed')
41-
print(f'✅ Translation of block {index} completed')
44+
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.')
45+
raise ValueError(f'Original ···{lines}···,\nbut got ···{translate_result}···')
46+
else:
47+
print(f'✅ Translation of block {index} completed')
4248

4349
return translate_result, lines
4450

0 commit comments

Comments
 (0)