Views
No views yet
1
2from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, GenerationConfig
3
4# Configuration
5CONFIG = {
6"model_name": "lyfeyvutha/nllb_350M_en_km_v10",
7"tokenizer_name": "facebook/nllb-200-distilled-600M",
8"source_lang": "eng_Latn",
9"target_lang": "khm_Khmr",
10"max_length": 128
11}
12
13# Load model and tokenizer
14model = AutoModelForSeq2SeqLM.from_pretrained(CONFIG["model_name"])
15tokenizer = AutoTokenizer.from_pretrained(
16CONFIG["tokenizer_name"],
17src_lang=CONFIG["source_lang"],
18tgt_lang=CONFIG["target_lang"]
19)
20
21# Set up generation configuration
22khm_token_id = tokenizer.convert_tokens_to_ids(CONFIG["target_lang"])
23generation_config = GenerationConfig(
24max_length=CONFIG["max_length"],
25forced_bos_token_id=khm_token_id
26)
27
28# Translate
29text = "Hello, how are you?"
30inputs = tokenizer(text, return_tensors="pt")
31outputs = model.generate(**inputs, generation_config=generation_config)
32translation = tokenizer.decode(outputs, skip_special_tokens=True)
33print(translation)
34| Metric | Value |
|---|---|
| chrF Score | 21.3502 |
| BERTScore F1 | 0.8983 |