Spaces:
Running
Running
Update src/Translate.py
Browse files- src/Translate.py +16 -0
src/Translate.py
CHANGED
|
@@ -9,6 +9,22 @@ from transformers import pipeline
|
|
| 9 |
METHOD = "TRANSLATE"
|
| 10 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
def paraphraseTranslateMethod(requestValue: str, model: str):
|
| 13 |
nltk.download('punkt')
|
| 14 |
nltk.download('punkt_tab')
|
|
|
|
| 9 |
METHOD = "TRANSLATE"
|
| 10 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 11 |
|
| 12 |
+
class Translators:
|
| 13 |
+
def __init__(self, model_name: str, sl: str, tl: str, input_text: str):
|
| 14 |
+
self.model_name = model_name
|
| 15 |
+
self.sl, self.tl = sl, tl
|
| 16 |
+
self.input_text = input_text
|
| 17 |
+
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 18 |
+
def HelsinkiNLP_mulroa(self):
|
| 19 |
+
try:
|
| 20 |
+
pipe = pipeline("translation", model=self.model_name, device=self.device)
|
| 21 |
+
iso1to3 = {iso[1]: iso[3] for iso in non_empty_isos} # {'ro': 'ron'}
|
| 22 |
+
iso3tl = iso1to3.get(self.tl) # 'deu', 'ron', 'eng', 'fra'
|
| 23 |
+
translation = pipe(f'>>{iso3tl}<< {self.input_text}')
|
| 24 |
+
return translation[0]['translation_text'], f'Translated from {iso1_to_name[self.sl]} to {iso1_to_name[self.tl]} with {self.model_name}.'
|
| 25 |
+
except Exception as error:
|
| 26 |
+
return f"Error translating with model: {self.model_name}! Try other available language combination.", error
|
| 27 |
+
|
| 28 |
def paraphraseTranslateMethod(requestValue: str, model: str):
|
| 29 |
nltk.download('punkt')
|
| 30 |
nltk.download('punkt_tab')
|