Views
No views yet
transformers library. Install dependencies with:pip install transformers torch sentencepiece1import torch
2from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
3
4# --- Setup ---
5MODEL_ID = "halaNeji/NLLBMedievalLatin2Spanish" # Hugging Face model ID
6SRC_LANG = "lat_Latn" # Source language code for Medieval Latin
7TGT_LANG = "spa_Latn" # Target language code for Spanish
8
9print("--- Loading NLLB Translator (Latin -> Spanish) ---")
10print("Loading model... (this may take a moment)")
11
12# --- Load Tokenizer and Model ---
13tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
14tokenizer.src_lang = SRC_LANG
15tokenizer.tgt_lang = TGT_LANG
16
17model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_ID)
18
19# Move model to GPU if available
20device = "cuda" if torch.cuda.is_available() else "cpu"
21model.to(device)
22
23print(f"Model loaded on {device.upper()}.")
24print("Type your Latin sentence and press Enter.")
25print("Type 'exit' to quit.\n")
26
27# --- Translation Function ---
28def translate(text):
29 # Tokenize input text
30 inputs = tokenizer(text, return_tensors="pt").to(device)
31
32 # Get the token ID for the target language
33 forced_id = tokenizer.convert_tokens_to_ids(TGT_LANG)
34
35 # Generate translation
36 translated_tokens = model.generate(
37 **inputs,
38 forced_bos_token_id=forced_id,
39 max_length=256,
40 num_beams=5,
41 early_stopping=True
42 )
43
44 # Decode generated tokens to text
45 result = tokenizer.batch_decode(translated_tokens, skip_special_tokens=True)[0]
46 return result
47
48# --- Interactive Loop ---
49while True:
50 try:
51 user_input = input("LATIN > ")
52
53 # Exit conditions
54 if user_input.lower() in ['exit', 'quit', 'q']:
55 print("Closing translator. Bye!")
56 break
57
58 if not user_input.strip():
59 continue
60
61 translation = translate(user_input)
62 print(f"SPANISH > {translation}\n")
63
64 except KeyboardInterrupt:
65 print("\nClosing...")
66 break
67 except Exception as e:
68 print(f"An error occurred: {e}")1@misc{molinoBench2026,
2 author = {Neji, Hala and Nogueras-Iso, Javier and Lacasta, Javier and Latre, Miguel {\'A}. and Garc{\'i}a-Marco, Francisco J.},
3 title = {MolinoBench},
4 year = {2026},
5 publisher = {Zenodo},
6 doi = {10.5281/zenodo.18272211},
7 url = {https://zenodo.org/records/18272211}
8}