Skip to content

Commit

Permalink
Fixed retranslation
Browse files Browse the repository at this point in the history
No wonder it never produced a useful result anymore!
  • Loading branch information
machinewrapped committed May 26, 2023
1 parent ec04ae9 commit d820d09
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
3 changes: 2 additions & 1 deletion PySubtitleGPT/ChatGPTClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ def RequestRetranslation(self, translation : ChatGPTTranslation, errors : list[T

# Let's raise the temperature a little bit
temperature = min(options.get('temperature', 0.0) + 0.1, 1.0)
retranslation = self.SendMessages(prompt.messages, temperature)
gpt_retranslation = self.SendMessages(prompt.messages, temperature)

retranslation = ChatGPTTranslation(gpt_retranslation, prompt)
return retranslation

def SendMessages(self, messages : list[str], temperature : float = None):
Expand Down
2 changes: 1 addition & 1 deletion PySubtitleGPT/ChatGPTTranslationParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from PySubtitleGPT.SubtitleValidator import SubtitleValidator

#template = re.compile(r"<translation\s+start='(?P<start>[\d:,]+)'\s+end='(?P<end>[\d:,]+)'>(?P<body>[\s\S]*?)<\/translation>", re.MULTILINE)
template = re.compile(r"#(?P<number>\d+)(?:[\s\r\n]+Original>[\s\r\n]*(?P<original>[\s\S]*?))?[\s\r\n]*(?:Translation>[\s\r\n]*(?P<body>[\s\S]*?))?(?:\n{2,})", re.MULTILINE)
template = re.compile(r"#(?P<number>\d+)(?:[\s\r\n]+Original>[\s\r\n]+(?P<original>[\s\S]*?))?[\s\r\n]*(?:Translation>(?:[\s\r\n]+(?P<body>[\s\S]*?))?(?:(?=\n{2,})|\Z))", re.MULTILINE)

#TODO: update fallback patterns with start and end groups
fallback_patterns = [
Expand Down
19 changes: 11 additions & 8 deletions PySubtitleGPT/SubtitleTranslator.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,22 +338,25 @@ def RequestRetranslations(self, client : ChatGPTClient, batch : SubtitleBatch, t
"""
Ask ChatGPT to retranslate any missing lines
"""
retranslation = client.RequestRetranslation(translation, batch.errors)
retranslation : ChatGPTTranslation = client.RequestRetranslation(translation, batch.errors)

logging.debug(f"Scene {batch.scene} batch {batch.number} retranslation:\n{retranslation.get('text')}\n")
if not isinstance(retranslation, ChatGPTTranslation):
raise TranslationError("Retranslation is not the expected type")

logging.debug(f"Scene {batch.scene} batch {batch.number} retranslation:\n{retranslation.text}\n")

parser = ChatGPTTranslationParser(self.options)

retranslated = parser.ProcessChatGPTResponse(retranslation)

if not retranslated:
if retranslated:
batch.AddContext('retranslated_lines', [f"{item.key}. {item.text}" for item in retranslated])
logging.info(f"Retranslated {len(retranslated)} of {len(retranslated) + len(batch.untranslated)} lines")
else:
#TODO line-by-line retranslation?
logging.error("Retranslation request did not produce a useful result")
return
# return

batch.AddContext('retranslated_lines', [f"{item.key}. {item.text}" for item in retranslated])
logging.info(f"Retranslated {len(retranslated)} of {len(retranslated) + len(batch.untranslated)} lines")

try:
batch.errors = []

Expand All @@ -363,7 +366,7 @@ def RequestRetranslations(self, client : ChatGPTClient, batch : SubtitleBatch, t

logging.info("Retranslation passed validation")

MergeTranslations(batch.translated, retranslated)
batch.translated = MergeTranslations(batch.translated, retranslated)

except TranslationError as e:
logging.warn(f"Retranslation request did not fix problems:\n{retranslation.get('text')}\n")
Expand Down

0 comments on commit d820d09

Please sign in to comment.