Skip to content

Commit a821863

Browse files
hydropixclaude
andcommitted
fix: disable Gemini thinking mode for 6x faster translation + fuzzy tag extraction
- Disable thinking for Gemini 2.5+ models (thinkingBudget: 0) reducing response time from ~10s to ~1.5s per chunk - Add fuzzy closing tag matching to handle malformed tags like </TRANATION> - Fix CLI progress_callback parameter that doesn't exist in translate_file() Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent c99d6f0 commit a821863

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

src/core/llm/providers/gemini.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ def __init__(self, api_key: str, model: str = "gemini-2.0-flash"):
5757
self.api_key = api_key
5858
self.api_endpoint = f"https://generativelanguage.googleapis.com/v1beta/models/{model}:generateContent"
5959

60+
def _is_thinking_model(self) -> bool:
61+
"""Check if the current model supports thinking mode (Gemini 2.5+)."""
62+
return "2.5" in self.model
63+
64+
def _get_thinking_config(self) -> dict:
65+
"""Return thinkingConfig to disable thinking for supported models."""
66+
if self._is_thinking_model():
67+
return {"thinkingConfig": {"thinkingBudget": 0}}
68+
return {}
69+
6070
async def get_available_models(self) -> list[dict]:
6171
"""
6272
Fetch available Gemini models from API, excluding experimental/vision models.
@@ -138,7 +148,8 @@ async def generate(self, prompt: str, timeout: int = REQUEST_TIMEOUT,
138148
}]
139149
}],
140150
"generationConfig": {
141-
"temperature": 0.7
151+
"temperature": 0.7,
152+
**self._get_thinking_config()
142153
}
143154
}
144155

src/core/llm/utils/extraction.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,25 @@ def extract(self, response: str) -> Optional[str]:
110110

111111
return extracted
112112

113+
# FUZZY FALLBACK: Opening tag found but closing tag is malformed
114+
# Some models (e.g. Gemini without thinking) write </TRANATION> instead of </TRANSLATION>
115+
if starts_correctly:
116+
# Extract tag name from closing tag (e.g. "TRANSLATION" from "</TRANSLATION>")
117+
closing_tag_match = re.match(r'</(\w+)>', self._tag_out)
118+
if closing_tag_match:
119+
tag_name = closing_tag_match.group(1)
120+
# Look for any closing tag that starts with the same prefix (at least 3 chars)
121+
prefix = tag_name[:3]
122+
fuzzy_pattern = re.compile(
123+
rf'{re.escape(self._tag_in)}(.*?)</\w*{re.escape(prefix)}\w*>',
124+
re.DOTALL
125+
)
126+
fuzzy_match = fuzzy_pattern.search(response)
127+
if fuzzy_match:
128+
extracted = fuzzy_match.group(1).strip()
129+
print(f"[WARN] Fuzzy tag match: closing tag was malformed, extracted content using prefix '</{prefix}...'")
130+
return extracted
131+
113132
# No tags found at all
114133
return None
115134

translate.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ def stats_callback(stats: dict):
135135
llm_provider=args.provider,
136136
checkpoint_manager=checkpoint_manager,
137137
translation_id=translation_id,
138-
progress_callback=None,
139138
log_callback=log_callback,
140139
stats_callback=stats_callback,
141140
check_interruption_callback=None,

0 commit comments

Comments
 (0)