File size: 15,216 Bytes
9144bfc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import os
import sys
import json
import argparse
from typing import List, Dict, Tuple, Union, Any
from pathlib import Path
import pandas as pd
from tqdm.auto import tqdm

import torch
from transformers import (
    MBart50Tokenizer,                       #type: ignore
    MBartForConditionalGeneration,          #type: ignore
    MT5ForConditionalGeneration,            #type: ignore
    MT5TokenizerFast,                       #type: ignore
)
from peft import PeftModel, PeftConfig
import evaluate

# Add parent directory to sys.path
sys.path.append(str(Path(__file__).resolve().parent.parent))
from models.rule_based_mt import TransferBasedMT
from models.statistical_mt import SMTExtended, LanguageModel

# Device configuration
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# Load configuration
with open("config.json", "r") as json_file:
    CONFIG = json.load(json_file)


def parse_arguments() -> argparse.Namespace:
    """Parse command-line arguments."""
    parser = argparse.ArgumentParser(description="Evaluate English-Vietnamese Machine Translation Models")
    parser.add_argument("--test_file", type=str, default='data/test_cleaned_dataset.csv', help="Path to test CSV file")
    parser.add_argument("--output_dir", type=str, default="results", help="Directory to save results")
    return parser.parse_args()


class ModelLoader:
    """Handles loading of translation models."""

    @staticmethod
    def load_smt() -> None:
        """Load Statistical Machine Translation model."""
        try:
            smt = SMTExtended()
            model_dir = "checkpoints"
            if os.path.exists(model_dir) and os.path.isfile(os.path.join(model_dir, "phrase_table.pkl")):
                print("Loading existing model...")
                smt.load_model()
            else:
                print("Training new smt...")
                stats = smt.train()
                print(f"Training complete: {stats}")
            print("SMT model loaded successfully!")
            return smt
        except Exception as e:
            raise RuntimeError(f"Failed to load SMT model: {str(e)}")

    @staticmethod
    def load_mbart50() -> Tuple[MBartForConditionalGeneration, MBart50Tokenizer]:
        """Load fine-tuned MBart50 model and tokenizer."""
        try:
            model_config = CONFIG["mbart50"]["paths"]
            model = MBartForConditionalGeneration.from_pretrained(model_config["base_model_name"])
            peft_config = PeftConfig.from_pretrained(model_config["checkpoint_path"])
            model = PeftModel.from_pretrained(model, model_config["checkpoint_path"])
            tokenizer = MBart50Tokenizer.from_pretrained(model_config["checkpoint_path"])
            model.eval()
            print("Fine-tuned MBart50 loaded successfully!")
            return model.to(DEVICE), tokenizer              #type: ignore
        except Exception as e:
            raise RuntimeError(f"Failed to load fine-tuned MBart50 model: {str(e)}")

    @staticmethod
    def load_original_mbart50() -> Tuple[MBartForConditionalGeneration, MBart50Tokenizer]:
        """Load original MBart50 model and tokenizer."""
        try:
            model_name = "facebook/mbart-large-50-many-to-many-mmt"
            model = MBartForConditionalGeneration.from_pretrained(model_name)
            tokenizer = MBart50Tokenizer.from_pretrained(model_name)
            model.eval()
            print("Original MBart50 loaded successfully!")
            return model.to(DEVICE), tokenizer              #type: ignore
        except Exception as e:
            raise RuntimeError(f"Failed to load original MBart50 model: {str(e)}")

    @staticmethod
    def load_mt5() -> Tuple[MT5ForConditionalGeneration, MT5TokenizerFast]:
        """Load fine-tuned MT5 model and tokenizer."""
        try:
            model_config = CONFIG["mt5"]["paths"]
            model = MT5ForConditionalGeneration.from_pretrained(model_config["base_model_name"])
            peft_config = PeftConfig.from_pretrained(model_config["checkpoint_path"])
            model = PeftModel.from_pretrained(model, model_config["checkpoint_path"])
            tokenizer = MT5TokenizerFast.from_pretrained(model_config["checkpoint_path"])
            model.eval()
            print("Fine-tuned MT5 loaded successfully!")
            return model.to(DEVICE), tokenizer              #type: ignore
        except Exception as e:
            raise RuntimeError(f"Failed to load fine-tuned MT5 model: {str(e)}")

    @staticmethod
    def load_original_mt5() -> Tuple[MT5ForConditionalGeneration, MT5TokenizerFast]:
        """Load original MT5 model and tokenizer."""
        try:
            model_name = "google/mt5-base"
            model = MT5ForConditionalGeneration.from_pretrained(model_name)
            tokenizer = MT5TokenizerFast.from_pretrained(model_name)
            model.eval()
            print("Original MT5 loaded successfully!")
            return model.to(DEVICE), tokenizer              #type: ignore
        except Exception as e:
            raise RuntimeError(f"Failed to load original MT5 model: {str(e)}")


class Translator:
    """Handles translation using different models."""

    @staticmethod
    def translate_rbmt(text: str) -> str:
        """Translate using Rule-Based Machine Translation."""
        try:
            translator = TransferBasedMT()
            return translator.translate(text)
        except Exception as e:
            raise RuntimeError(f"RBMT translation failed: {str(e)}")

    @staticmethod
    def translate_smt(text: str, smt) -> str:
        """Translate using Statistical Machine Translation."""
        try: 
            # return smt.translate_sentence(text)
            translation = smt.infer(text)
            return translation
        except Exception as e:
            raise RuntimeError(f"SMT translation failed: {str(e)}")

    @staticmethod
    def translate_mbart50(

        model: MBartForConditionalGeneration, tokenizer: MBart50Tokenizer, text: str

    ) -> str:
        """Translate using MBart50 model (fine-tuned or original)."""
        try:
            model_config = CONFIG["mbart50"]["args"]
            tokenizer.src_lang = model_config["src_lang"]
            inputs = tokenizer(text, return_tensors="pt", padding=True)
            inputs = {key: value.to(DEVICE) for key, value in inputs.items()}

            forced_bos_token_id = tokenizer.lang_code_to_id[model_config["tgt_lang"]]
            translated_tokens = model.generate(
                input_ids=inputs["input_ids"],
                attention_mask=inputs["attention_mask"],
                forced_bos_token_id=forced_bos_token_id,
                max_length=128,
                num_beams=5,
            )
            return tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
        except Exception as e:
            raise RuntimeError(f"MBart50 translation failed: {str(e)}")

    @staticmethod
    def translate_mt5(

        model: MT5ForConditionalGeneration, tokenizer: MT5TokenizerFast, text: str

    ) -> str:
        """Translate using MT5 model (fine-tuned or original)."""
        try:
            prefix = CONFIG["mt5"]["args"]["prefix"]
            text = prefix + text
            inputs = tokenizer(text, return_tensors="pt", padding=True)
            inputs = {key: value.to(DEVICE) for key, value in inputs.items()}

            translated_tokens = model.generate(
                input_ids=inputs["input_ids"],
                attention_mask=inputs["attention_mask"],
                max_length=128,
                num_beams=5,
            )
            return tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
        except Exception as e:
            raise RuntimeError(f"MT5 translation failed: {str(e)}")


class Evaluator:
    """Handles evaluation of translation models."""

    @staticmethod
    def load_test_data(test_file: str) -> List[Dict[str, str]]:
        """Load test data from CSV file."""
        try:
            df = pd.read_csv(test_file)
            df = df[:100000]  # Limit to 100,000 rows
            return [{"source": row["en"], "reference": row["vi"]} for _, row in df.iterrows()]
        except Exception as e:
            raise RuntimeError(f"Failed to load test data: {str(e)}")

    @staticmethod
    def compute_metrics(hypotheses: List[str], references: List[str], sources: List[str]) -> Dict[str, float]:
        """Compute translation evaluation metrics."""
        try:
            metrics = {}
            bleu_metric = evaluate.load("sacrebleu")
            meteor_metric = evaluate.load("meteor")
            rouge_metric = evaluate.load("rouge")
            comet_metric = evaluate.load("comet")
            bertscore_metric = evaluate.load("bertscore")
            
            # BLEU
            metrics["SacreBLEU"] = bleu_metric.compute(predictions=hypotheses, references=references)["score"] / 100        #type: ignore 

            # METEOR
            metrics["METEOR"] = meteor_metric.compute(predictions=hypotheses, references=references)["meteor"]            #type: ignore

            # ROUGE
            rouge_results = rouge_metric.compute(
                predictions=hypotheses, references=references, rouge_types=["rouge1", "rouge2", "rougeL"], use_stemmer=True
            )
            metrics["ROUGE-1"] = rouge_results["rouge1"]          #type: ignore
            metrics["ROUGE-2"] = rouge_results["rouge2"]          #type: ignore
            metrics["ROUGE-L"] = rouge_results["rougeL"]          #type: ignore

            # BERTScore
            bertscore_results = bertscore_metric.compute(
                predictions=hypotheses, references=references, model_type="bert-base-multilingual-cased", lang="vi"
            )
            metrics["BERTScore"] = sum(bertscore_results["f1"]) / len(bertscore_results["f1"])            #type: ignore

            # COMET
            comet_results = comet_metric.compute(predictions=hypotheses, references=references, sources=sources)
            metrics["COMET"] = sum(comet_results["scores"]) / len(comet_results["scores"])                #type: ignore

            return metrics
        except Exception as e:
            raise RuntimeError(f"Failed to compute metrics: {str(e)}")

    @staticmethod
    def evaluate_model(

        model_type: str, test_data: List[Dict[str, str]]

    ) -> Tuple[List[str], List[str], Dict[str, float]]:
        """Evaluate a translation model on test data."""
        hypotheses, references, sources = [], [], []

        try:
            if model_type == "rbmt":
                for item in tqdm(test_data, desc="Translating with RBMT"):
                    translation = Translator.translate_rbmt(item["source"])
                    hypotheses.append(translation)
                    references.append(item["reference"])
                    sources.append(item["source"])

            elif model_type == "smt":
                for item in tqdm(test_data, desc="Translating with SMT"):
                    smt = ModelLoader.load_smt()
                    translation = Translator.translate_smt(item["source"], smt)
                    hypotheses.append(translation)
                    references.append(item["reference"])
                    sources.append(item["source"])

            elif model_type == "mbart50":
                model, tokenizer = ModelLoader.load_mbart50()
                for item in tqdm(test_data, desc="Translating with fine-tuned mBART50"):
                    translation = Translator.translate_mbart50(model, tokenizer, item["source"])
                    hypotheses.append(translation)
                    references.append(item["reference"])
                    sources.append(item["source"])

            elif model_type == "original_mbart50":
                model, tokenizer = ModelLoader.load_original_mbart50()
                for item in tqdm(test_data, desc="Translating with original mBART50"):
                    translation = Translator.translate_mbart50(model, tokenizer, item["source"])
                    hypotheses.append(translation)
                    references.append(item["reference"])
                    sources.append(item["source"])

            elif model_type == "mt5":
                model, tokenizer = ModelLoader.load_mt5()
                for item in tqdm(test_data, desc="Translating with fine-tuned MT5"):
                    translation = Translator.translate_mt5(model, tokenizer, item["source"])
                    hypotheses.append(translation)
                    references.append(item["reference"])
                    sources.append(item["source"])

            elif model_type == "original_mt5":
                model, tokenizer = ModelLoader.load_original_mt5()
                for item in tqdm(test_data, desc="Translating with original MT5"):
                    translation = Translator.translate_mt5(model, tokenizer, item["source"])
                    hypotheses.append(translation)
                    references.append(item["reference"])
                    sources.append(item["source"])

            return hypotheses, references, Evaluator.compute_metrics(hypotheses, references, sources) if hypotheses else {}
        except Exception as e:
            raise RuntimeError(f"Evaluation failed for {model_type}: {str(e)}")


def main():
    """Main function to run model evaluation."""
    args = parse_arguments()
    Path(args.output_dir).mkdir(parents=True, exist_ok=True)

    try:
        test_data = Evaluator.load_test_data(args.test_file)
        model_types = ["rbmt", "smt" "mbart50", "original_mbart50", "mt5", "original_mt5"]
        all_results = {}

        for model_type in model_types:
            print(f"\nEvaluating {model_type}...")
            hypotheses, references, metrics = Evaluator.evaluate_model(model_type, test_data)

            if metrics:
                all_results[model_type] = metrics
                print(f"Metrics for {model_type}:")
                for metric, value in metrics.items():
                    print(f"{metric}: {value:.4f}")

                # Save translations
                translations = [
                    {"source": item["source"], "reference": ref, "hypothesis": hyp}
                    for item, ref, hyp in zip(test_data, references, hypotheses)
                ]
                with open(
                    Path(args.output_dir) / f"{model_type}_translations.json", "w", encoding="utf-8"
                ) as f:
                    json.dump(translations, f, ensure_ascii=False, indent=2)

        # Save all metrics
        with open(Path(args.output_dir) / "metrics.json", "w", encoding="utf-8") as f:
            json.dump(all_results, f, indent=2)

    except Exception as e:
        print(f"Error: {str(e)}", file=sys.stderr)
        sys.exit(1)


if __name__ == "__main__":
    main()