Skip to content

Commit

Permalink
New required library for fixing json errors, bump version to 1.3.4
Browse files Browse the repository at this point in the history
  • Loading branch information
MaKTaiL committed Feb 24, 2025
1 parent e2f3d03 commit 6fd0863
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions gemini_srt_translator/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import typing
import json
import json_repair
import time
import unicodedata as ud

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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:
Expand All @@ -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.")
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 6fd0863

Please sign in to comment.