Home > Net >  Creating an object outside before a function and use it in the function
Creating an object outside before a function and use it in the function

Time:11-11

Hello I have a class where I load a huggingface translation model, it also has a function that perform the actual translation:

class Translator:
    def __init__(self, language):
        model = f"Helsinki-NLP/opus-mt-{language}-en"
        self.translator = pipeline("translation", model=model, device=0)

    def translate(self, text): 
        translated_text = self.translator(text)
        return translated_text

I also have a function where I use the Translator object multiple times over and over. My question is: Is right to create an instance of the Translator class outside of the function like this or are there any better ways?

#Example code

from anotherdir import Translator

translator = Translator("de")

def load_text_and_translate(list_of_non_translated_text):
    final_translated_text_list = []

    for text in list_of_non_translated_text:
        translated_text = translator.translate(text)
        final_translated_text_list.append(translated_text)

    return final_translated_text_list

Hopefully, I've made myself understood, I've tried to figure out a better way to do it but haven't been able to come up with something.

CodePudding user response:

It's fine to create a single translator that can be used in many places. But have your function take a translator as an argument rather than relying on a global variable.

def load_text_and_translate(tr, list_of_non_translated_text):
    final_translated_text_list = []

    for text in list_of_non_translated_text:
        translated_text = tr.translate(text)
        final_translated_text_list.append(translated_text)

    return final_translated_text_list

translator1 = Translator("de")
translator2 = Translator("fi")
text = ["hello", "world"]

german_translation = load_text_and_translate(translator1, text)
finnish_translation = load_text_and_translate(translator2, text)
  • Related