Commit
•
1b78324
1
Parent(s):
a19db3b
Delete translator.py
Browse files- translator.py +0 -41
translator.py
DELETED
@@ -1,41 +0,0 @@
|
|
1 |
-
from deep_translator import GoogleTranslator
|
2 |
-
from transformers import pipeline
|
3 |
-
|
4 |
-
|
5 |
-
class MangaTranslator:
|
6 |
-
def __init__(self):
|
7 |
-
self.target = "en"
|
8 |
-
self.source = "ja"
|
9 |
-
|
10 |
-
def translate(self, text, method="google"):
|
11 |
-
"""
|
12 |
-
Translates the given text to the target language using the specified method.
|
13 |
-
|
14 |
-
Args:
|
15 |
-
text (str): The text to be translated.
|
16 |
-
method (str):'google' for Google Translator,
|
17 |
-
'hf' for Helsinki-NLP's opus-mt-ja-en model (HF pipeline)
|
18 |
-
|
19 |
-
Returns:
|
20 |
-
str: The translated text.
|
21 |
-
"""
|
22 |
-
if method == "hf":
|
23 |
-
return self._translate_with_hf(self._preprocess_text(text))
|
24 |
-
elif method == "google":
|
25 |
-
return self._translate_with_google(self._preprocess_text(text))
|
26 |
-
else:
|
27 |
-
raise ValueError("Invalid translation method.")
|
28 |
-
|
29 |
-
def _translate_with_google(self, text):
|
30 |
-
translator = GoogleTranslator(source=self.source, target=self.target)
|
31 |
-
translated_text = translator.translate(text)
|
32 |
-
return translated_text
|
33 |
-
|
34 |
-
def _translate_with_hf(self, text):
|
35 |
-
pipe = pipeline("translation", model=f"Helsinki-NLP/opus-mt-ja-en")
|
36 |
-
translated_text = pipe(text)[0]["translation_text"]
|
37 |
-
return translated_text
|
38 |
-
|
39 |
-
def _preprocess_text(self, text):
|
40 |
-
preprocessed_text = text.replace(".", ".")
|
41 |
-
return preprocessed_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|