Views
No views yet
facebook/mbart-large-50
using Vietnamese dialect/standard sentence pairs from
Biu3010/ViDia2Std.pip install torch transformers sentencepiece1import torch
2from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
3
4MODEL_ID = "coutMinh/mbart-large-50"
5device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
6
7tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
8tokenizer.src_lang = "vi_VN"
9tokenizer.tgt_lang = "vi_VN"
10
11model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_ID)
12model.to(device).eval()
13
14
15@torch.inference_mode()
16def normalize(text: str) -> str:
17 inputs = tokenizer(
18 text,
19 return_tensors="pt",
20 truncation=True,
21 max_length=128,
22 )
23 inputs.pop("token_type_ids", None)
24 inputs = {key: value.to(device) for key, value in inputs.items()}
25
26 generation_args = {
27 "max_new_tokens": 64,
28 "do_sample": False,
29 "num_beams": 1,
30 "pad_token_id": tokenizer.pad_token_id,
31 "eos_token_id": tokenizer.eos_token_id,
32 }
33 if "vi_VN" in tokenizer.lang_code_to_id:
34 generation_args["forced_bos_token_id"] = (
35 tokenizer.lang_code_to_id["vi_VN"]
36 )
37
38 output = model.generate(**inputs, **generation_args)
39 return tokenizer.decode(output[0], skip_special_tokens=True)
40
41
42print(normalize("mi đi mô rứa mi, tau không biết"))mày đi đâu vậy mày, tao không biết/normalize API endpoint when its runtime is enabled:pip install gradio_client1from gradio_client import Client
2
3client = Client("coutMinh/mbart-large-50-demo")
4result = client.predict(
5 text="mi đi mô rứa mi, tau không biết",
6 api_name="/normalize",
7)
8print(result)| Metric | Score |
|---|---|
| ROUGE-L | 0.9382 |
| BLEU | 0.8152 |
| METEOR | 0.8923 |
| WER | 0.1192 |
| CER | 0.0752 |