diff --git a/README.md b/README.md index 2d0ede7..3bacc53 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ gst.target_language = "French" gst.input_file = "subtitle.srt" gst.output_file = "subtitle_translated.srt" gst.description = "Translation of subtitle file" -gst.model_name = "gemini-1.5-flash" +gst.model_name = "gemini-2.0-flash" gst.batch_size = 30 gst.free_quota = True diff --git a/gemini_srt_translator/main.py b/gemini_srt_translator/main.py index 9a0b7d8..e604bc0 100644 --- a/gemini_srt_translator/main.py +++ b/gemini_srt_translator/main.py @@ -2,6 +2,7 @@ import typing import json +import json_repair import time import unicodedata as ud @@ -90,7 +91,6 @@ class SubtitleObject(typing.TypedDict): content: str Request: list[SubtitleObject] -Response: list[SubtitleObject] The 'index' key is the index of the subtitle dialog. The 'content' key is the dialog to be translated. @@ -175,7 +175,7 @@ class SubtitleObject(typing.TypedDict): if "Gemini" in e_str: print(e_str) else: - print("An unexpected error has occurred") + print("An unexpected error has occurred: {}".format(e_str)) print("Decreasing batch size to {} and trying again...".format(self.batch_size)) translated_file.write(srt.compose(translated_subtitle)) @@ -219,7 +219,7 @@ def _get_model(self, instruction: str) -> GenerativeModel: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE, }, system_instruction=instruction, - generation_config=genai.GenerationConfig(response_mime_type="application/json", temperature=0) + generation_config=genai.GenerationConfig(response_mime_type="application/json") ) def _process_batch(self, model: GenerativeModel, batch: list[SubtitleObject], previous_message: ContentDict, translated_subtitle: list[Subtitle]) -> ContentDict: @@ -240,7 +240,7 @@ def _process_batch(self, model: GenerativeModel, batch: list[SubtitleObject], pr else: messages = [{"role": "user", "parts": json.dumps(batch, ensure_ascii=False)}] response = model.generate_content(messages) - translated_lines: list[SubtitleObject] = json.loads(response.text) + translated_lines: list[SubtitleObject] = json_repair.loads(response.text) if len(translated_lines) != len(batch): raise Exception("Gemini has returned the wrong number of lines.") diff --git a/setup.py b/setup.py index 93dd565..3d20b17 100644 --- a/setup.py +++ b/setup.py @@ -2,11 +2,12 @@ setup( name="gemini-srt-translator", - version="1.3.3", + version="1.3.4", packages=find_packages(), install_requires=[ "google-generativeai==0.8.4", "srt==3.5.3", + "json-repair==0.39.1" ], author="Matheus Castro", description="A tool to translate subtitles using Google Generative AI",