Skip to content

Commit

Permalink
Fix dependency issues by switching to deep-translator.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelNiklaus committed Dec 22, 2024
1 parent f1ba65c commit ace6e59
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 127 deletions.
114 changes: 0 additions & 114 deletions examples/custom_models/google-translate-requirements-freeze.txt

This file was deleted.

21 changes: 8 additions & 13 deletions examples/custom_models/google_translate_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@
from typing import Optional

import diskcache
import httpcore
import tenacity
from googletrans import Translator
from deep_translator import GoogleTranslator
from tqdm import tqdm
from transformers import AutoTokenizer

Expand Down Expand Up @@ -65,11 +64,8 @@ def __init__(self, config, env_config) -> None:

self._tokenizer = AutoTokenizer.from_pretrained("gpt2") # Use a dummy tokenizer for compatibility

# Needed to fix some googletrans bug
# https://stackoverflow.com/questions/72796594/attributeerror-module-httpcore-has-no-attribute-synchttptransport#comment136664963_77334618
setattr(httpcore, "SyncHTTPTransport", "AsyncHTTPProxy")

self.translator = Translator()
# Deep-translator also supports other translators
self.translator = GoogleTranslator()

# Initialize disk cache
cache_dir = os.path.join(os.getcwd(), ".translation_cache")
Expand Down Expand Up @@ -98,15 +94,14 @@ def _translate_with_cache(self, context: str, src_lang: str, tgt_lang: str) -> s
return self.cache[cache_key]

try:
# If not in cache, translate and store
translation = self.translator.translate(context, src=src_lang, dest=tgt_lang)
result = translation.text
# Updated translation call for deep-translator
self.translator.source = src_lang
self.translator.target = tgt_lang
result = self.translator.translate(context)
self.cache[cache_key] = result
return result
except Exception as e:
logger.warning(f"Translation error: {str(e)}. Retrying...")
# Re-initialize translator on error
self.translator = Translator()
raise # Let tenacity handle the retry

def greedy_until(
Expand Down Expand Up @@ -161,7 +156,7 @@ def tokenizer(self):
return self._tokenizer

def tok_encode(self, text: str):
return self.tokenizer.encode(text)
return text

@property
def add_special_tokens(self) -> bool:
Expand Down

0 comments on commit ace6e59

Please sign in to comment.