Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading