'How to run huggingface Helsinki-NLP models

I am trying to use the Helsinki-NLP models from huggingface, but I cannot find any instructions on how to do it. The README files are computer generated and do not contain explanations. Can some one point me to a getting started guide, or show an example of how to run a model like opus-mt-en-es?



Solution 1:[1]

On the model's page here there's a Use in Transformers link that you can use to see the code to load it in their transformers package as shown below:

from transformers import AutoTokenizer, AutoModelForSeq2SeqLM

tokenizer = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-es-en")
model = AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-es-en")

then use it as you would any transformer model:

inp = "Me llamo Wolfgang y vivo en Berlin"
input_ids = tokenizer(inp, return_tensors="pt").input_ids
outputs = model.generate(input_ids=input_ids, num_beams=5, num_return_sequences=3)
print("Generated:", tokenizer.batch_decode(outputs, skip_special_tokens=True))

Output:

Generated: ['My name is Wolfgang and I live in Berlin', 'My name is Wolfgang and I live in Berlin.', "My name's Wolfgang and I live in Berlin."]

Solution 2:[2]

To use on the fly, you can check the huggingFace course here. They provide pipelines that help you run this on the fly, consider:

translator = pipeline("translation", model="Helsinki-NLP/opus-mt-es-en")
translator("your-text-to-translate-here") 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Conrad747