georgescutelnicu commited on
Commit
76251ff
·
verified ·
1 Parent(s): 86760cc

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +1 -1
  2. translator.py +13 -8
app.py CHANGED
@@ -52,7 +52,7 @@ demo = gr.Interface(fn=predict,
52
  gr.Dropdown([("Google", "google"),
53
  ("Helsinki-NLP's opus-mt-ja-en model",
54
  "hf"),
55
- ("Baidu", "baidu"),
56
  ("Bing", "bing")],
57
  label="Translation Method",
58
  value="google"),
 
52
  gr.Dropdown([("Google", "google"),
53
  ("Helsinki-NLP's opus-mt-ja-en model",
54
  "hf"),
55
+ ("Sogou", "sogou"),
56
  ("Bing", "bing")],
57
  label="Translation Method",
58
  value="google"),
translator.py CHANGED
@@ -1,6 +1,7 @@
1
  from deep_translator import GoogleTranslator
2
  from transformers import pipeline
3
  import translators as ts
 
4
  import time
5
 
6
 
@@ -11,7 +12,7 @@ class MangaTranslator:
11
  self.translators = {
12
  "google": self._translate_with_google,
13
  "hf": self._translate_with_hf,
14
- "baidu": self._translate_with_baidu,
15
  "bing": self._translate_with_bing
16
  }
17
 
@@ -23,20 +24,21 @@ class MangaTranslator:
23
  text (str): The text to be translated.
24
  method (str):"google" for Google Translator,
25
  "hf" for Helsinki-NLP's opus-mt-ja-en model (HF pipeline)
26
- "baidu" for Baidu Translate
27
  "bing" for Microsoft Bing Translator
28
 
29
  Returns:
30
  str: The translated text.
31
  """
32
  translator_func = self.translators.get(method)
33
-
34
  if translator_func:
35
  return translator_func(self._preprocess_text(text))
36
  else:
37
  raise ValueError("Invalid translation method.")
38
 
39
  def _translate_with_google(self, text):
 
40
  translator = GoogleTranslator(source=self.source, target=self.target)
41
  translated_text = translator.translate(text)
42
  return translated_text if translated_text is not None else text
@@ -46,14 +48,15 @@ class MangaTranslator:
46
  translated_text = pipe(text)[0]["translation_text"]
47
  return translated_text if translated_text is not None else text
48
 
49
- def _translate_with_baidu(self, text):
50
- translated_text = ts.translate_text(text, translator="baidu",
51
- from_language="jp",
 
52
  to_language=self.target)
53
  return translated_text if translated_text is not None else text
54
 
55
  def _translate_with_bing(self, text):
56
- time.sleep(4)
57
  translated_text = ts.translate_text(text, translator="bing",
58
  from_language=self.source,
59
  to_language=self.target)
@@ -61,4 +64,6 @@ class MangaTranslator:
61
 
62
  def _preprocess_text(self, text):
63
  preprocessed_text = text.replace(".", ".")
64
- return preprocessed_text
 
 
 
1
  from deep_translator import GoogleTranslator
2
  from transformers import pipeline
3
  import translators as ts
4
+ import random
5
  import time
6
 
7
 
 
12
  self.translators = {
13
  "google": self._translate_with_google,
14
  "hf": self._translate_with_hf,
15
+ "sogou": self._translate_with_sogou,
16
  "bing": self._translate_with_bing
17
  }
18
 
 
24
  text (str): The text to be translated.
25
  method (str):"google" for Google Translator,
26
  "hf" for Helsinki-NLP's opus-mt-ja-en model (HF pipeline)
27
+ "sogou" for Sogou Translate
28
  "bing" for Microsoft Bing Translator
29
 
30
  Returns:
31
  str: The translated text.
32
  """
33
  translator_func = self.translators.get(method)
34
+
35
  if translator_func:
36
  return translator_func(self._preprocess_text(text))
37
  else:
38
  raise ValueError("Invalid translation method.")
39
 
40
  def _translate_with_google(self, text):
41
+ self._delay()
42
  translator = GoogleTranslator(source=self.source, target=self.target)
43
  translated_text = translator.translate(text)
44
  return translated_text if translated_text is not None else text
 
48
  translated_text = pipe(text)[0]["translation_text"]
49
  return translated_text if translated_text is not None else text
50
 
51
+ def _translate_with_sogou(self, text):
52
+ self._delay()
53
+ translated_text = ts.translate_text(text, translator="sogou",
54
+ from_language=self.source,
55
  to_language=self.target)
56
  return translated_text if translated_text is not None else text
57
 
58
  def _translate_with_bing(self, text):
59
+ self._delay()
60
  translated_text = ts.translate_text(text, translator="bing",
61
  from_language=self.source,
62
  to_language=self.target)
 
64
 
65
  def _preprocess_text(self, text):
66
  preprocessed_text = text.replace(".", ".")
67
+
68
+ def _delay(self):
69
+ time.sleep(random.randint(3, 5))