syncing scripts
This commit is contained in:
@@ -1,8 +1,61 @@
|
||||
import os
|
||||
import sys
|
||||
import warnings
|
||||
import pysubs2
|
||||
from deep_translator import GoogleTranslator
|
||||
|
||||
# Suppress warnings from google.generativeai about deprecation
|
||||
warnings.filterwarnings("ignore", category=FutureWarning, module="google.generativeai")
|
||||
|
||||
import google.generativeai as genai
|
||||
from tenacity import retry, stop_after_attempt, wait_exponential, retry_if_exception_type
|
||||
|
||||
# ... (retry_policy and _generate_with_retry remain same)
|
||||
|
||||
def translate_fallback_free(source_srt_content, target_language="en"):
|
||||
"""
|
||||
Fallback translation using deep-translator (free Google Translate).
|
||||
|
||||
Args:
|
||||
source_srt_content (str): Content of the source SRT file.
|
||||
target_language (str): Target language code (e.g. 'en', 'fr').
|
||||
|
||||
Returns:
|
||||
str: Translated SRT content, or None if failed.
|
||||
"""
|
||||
print(f" [Free Fallback] Translating via Google Translate (deep-translator)...")
|
||||
try:
|
||||
# Load from string
|
||||
subs = pysubs2.SSAFile.from_string(source_srt_content)
|
||||
translator = GoogleTranslator(source='auto', target=target_language)
|
||||
|
||||
# Simple line-by-line translation
|
||||
for line in subs:
|
||||
text = line.text.strip()
|
||||
if text:
|
||||
# Sanity check: Skip lines that are too long
|
||||
if len(text) > 4000:
|
||||
print(f" Warning: Skipping line with excessive length ({len(text)} chars).")
|
||||
continue
|
||||
|
||||
try:
|
||||
# pysubs2 text can contain \N for newlines.
|
||||
original_text = text.replace(r"\N", " ")
|
||||
translated_text = translator.translate(original_text)
|
||||
if translated_text:
|
||||
line.text = translated_text
|
||||
except Exception as e:
|
||||
print(f" Warning: Failed to translate line: {e}")
|
||||
|
||||
# Return as string
|
||||
return subs.to_string(format_="srt")
|
||||
except Exception as e:
|
||||
print(f" [Free Fallback] Critical Error: {e}")
|
||||
return None
|
||||
|
||||
def get_best_available_model():
|
||||
# ... (rest of file)
|
||||
|
||||
# Define a retry decorator
|
||||
# Waits 2^x * 1 seconds between retries (1s, 2s, 4s, 8s, 16s, 32s...)
|
||||
# With max=60, it will cap at waiting 60s per try.
|
||||
|
||||
Reference in New Issue
Block a user