diff --git a/PySubtitleGPT/ChatGPTClient.py b/PySubtitleGPT/ChatGPTClient.py index 09c3d42c..94b947f3 100644 --- a/PySubtitleGPT/ChatGPTClient.py +++ b/PySubtitleGPT/ChatGPTClient.py @@ -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): diff --git a/PySubtitleGPT/ChatGPTTranslationParser.py b/PySubtitleGPT/ChatGPTTranslationParser.py index 29d81707..11543268 100644 --- a/PySubtitleGPT/ChatGPTTranslationParser.py +++ b/PySubtitleGPT/ChatGPTTranslationParser.py @@ -9,7 +9,7 @@ from PySubtitleGPT.SubtitleValidator import SubtitleValidator #template = re.compile(r"[\d:,]+)'\s+end='(?P[\d:,]+)'>(?P[\s\S]*?)<\/translation>", re.MULTILINE) -template = re.compile(r"#(?P\d+)(?:[\s\r\n]+Original>[\s\r\n]*(?P[\s\S]*?))?[\s\r\n]*(?:Translation>[\s\r\n]*(?P[\s\S]*?))?(?:\n{2,})", re.MULTILINE) +template = re.compile(r"#(?P\d+)(?:[\s\r\n]+Original>[\s\r\n]+(?P[\s\S]*?))?[\s\r\n]*(?:Translation>(?:[\s\r\n]+(?P[\s\S]*?))?(?:(?=\n{2,})|\Z))", re.MULTILINE) #TODO: update fallback patterns with start and end groups fallback_patterns = [ diff --git a/PySubtitleGPT/SubtitleTranslator.py b/PySubtitleGPT/SubtitleTranslator.py index 58776291..0baac559 100644 --- a/PySubtitleGPT/SubtitleTranslator.py +++ b/PySubtitleGPT/SubtitleTranslator.py @@ -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 = [] @@ -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")