Sunday, 7 May 2023

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?

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."]
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-es-en")
translator("your-text-to-translate-here") 

No comments:

Post a Comment

Connect broadband

AI:Male vs female four season

  Your question touches on gender roles, biology, health, and lifestyle differences, and it's important to approach this with nuance, a...