Skip to content

Commit

Permalink
Added better devider
Browse files Browse the repository at this point in the history
  • Loading branch information
pescheck-bram committed Dec 22, 2023
1 parent d9e3f58 commit bcf6628
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
13 changes: 7 additions & 6 deletions python_gpt_po/po_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
load_dotenv()
logging.basicConfig(level=logging.INFO)

UNIQUE_DIVIDER = "\n\n--- Translation Divider ---\n\n"


class TranslationConfig:
""" Class to hold configuration parameters for the translation service. """
Expand All @@ -44,11 +46,10 @@ def translate_bulk(self, texts, target_language, po_file_path, current_batch):
batch_texts = texts[i:i + self.batch_size]
batch_info = f"File: {po_file_path}, Batch {i}/{self.total_batches}"
batch_info += f" (texts {i + 1}-{min(i + self.batch_size, len(texts))})"
translation_request = (f"Translate the following texts into {target_language} as a single, "
"continuous text. "
"Do not repeat the original texts, just provide the "
"translations:\n\n")
translation_request += "\n\n".join(batch_texts)
translation_request = (f"Translate the following texts into {target_language} as separate segments. "
f"Use the unique divider '{UNIQUE_DIVIDER.strip()}' between each translation. "
"Do not repeat the original texts, just provide the translations:\n\n")
translation_request += f"{UNIQUE_DIVIDER}".join(batch_texts)
retries = 3

while retries:
Expand All @@ -72,7 +73,7 @@ def perform_translation(self, translation_request, translated_texts):
"""Performs the translation and updates the results."""
message = {"role": "user", "content": translation_request}
completion = self.config.client.chat.completions.create(model=self.config.model, messages=[message])
batch_translations = completion.choices[0].message.content.strip().split('\n\n')
batch_translations = completion.choices[0].message.content.strip().split(UNIQUE_DIVIDER)
translated_texts.extend(batch_translations)

def scan_and_process_po_files(self, input_folder, languages):
Expand Down
2 changes: 1 addition & 1 deletion python_gpt_po/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
using the '--version' argument.
"""

__version__ = "0.1.6"
__version__ = "0.1.7"

0 comments on commit bcf6628

Please sign in to comment.