voiceoperation / translation.py
zeimoto's picture
fixes and updates
3239a8e
raw
history blame contribute delete
819 Bytes
from transformers import MarianMTModel, MarianTokenizer
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
from resources import set_start, audit_elapsedtime
def translate(text_to_translate: str) -> str:
start = set_start()
print("Initiating translation model...")
text_size = len(text_to_translate)*2
tokenizer = AutoTokenizer.from_pretrained("unicamp-dl/translation-pt-en-t5")
model = AutoModelForSeq2SeqLM.from_pretrained("unicamp-dl/translation-pt-en-t5")
pten_pipeline = pipeline('text2text-generation', model=model, tokenizer=tokenizer)
translated_text = pten_pipeline(text_to_translate, max_new_tokens= text_size)
audit_elapsedtime(function="Finished translation", start=start)
print("Translated text:", translated_text)
return translated_text