Views
No views yet

1import torch
2from transformers import AutoTokenizer, AutoModelForCausalLM
3
4# load the NorMistral tokenizer and model
5tokenizer = AutoTokenizer.from_pretrained("norallm/normistral-11b-translate")
6model = AutoModelForCausalLM.from_pretrained(
7 "norallm/normistral-11b-translate",
8 device_map='auto',
9 torch_dtype=torch.bfloat16
10)
11
12# create a conversation and convert it to token indices using the NorMistral chat template
13messages = [
14 {"role": "system", "content": "nynorsk"}, # Optional message to set the target language for translation; "engelsk" by default, "bokmål" and "nynorsk" are supported
15 {"role": "user", "content": "Hva er hovedstaden i Norge?"}
16]
17input_tokens = tokenizer.apply_chat_template(messages, return_tensors="pt").to(model.device)
18
19# run the generation (customizable via the various parameters)
20output_tokens = model.generate(
21 input_tokens,
22 max_new_tokens=2048, # limit max number of generated tokens
23 do_sample=False # do not randomly sample the outputs
24)
25
26# decode the generated tokens back to text; should return "Kva er hovudstaden i Noreg?"
27output_str = tokenizer.decode(output_tokens[0, input_tokens.size(1):], skip_special_tokens=True).strip()ltg/nob-nno-eng-translation-pairs.1@inproceedings{samuel-etal-2025-small,
2 title = "Small Languages, Big Models: {A} Study of Continual Training on Languages of {Norway}",
3 author = "Samuel, David and
4 Mikhailov, Vladislav and
5 Velldal, Erik and
6 {\O}vrelid, Lilja and
7 Charpentier, Lucas Georges Gabriel and
8 Kutuzov, Andrey and
9 Oepen, Stephan",
10 editor = "Johansson, Richard and
11 Stymne, Sara",
12 booktitle = "Proceedings of the Joint 25th Nordic Conference on Computational Linguistics and 11th Baltic Conference on Human Language Technologies (NoDaLiDa/Baltic-HLT 2025)",
13 month = mar,
14 year = "2025",
15 address = "Tallinn, Estonia",
16 publisher = "University of Tartu Library",
17 url = "https://aclanthology.org/2025.nodalida-1.61/",
18 pages = "573--608",
19 ISBN = "978-9908-53-109-0",
20}