Views
No views yet
indoT5-AMRToTextGenerator-V1.11 from transformers import T5TokenizerFast, AutoModelForSeq2SeqLM
2 import torch
3
4 model_path = "fabhiansan/indoT5-AMRToTextGenerator"
5 tokenizer_path = "fabhiansan/indoT5-AMRToTextGenerator"
6
7 tokenizer = T5TokenizerFast.from_pretrained(tokenizer_path)
8 model = AutoModelForSeq2SeqLM.from_pretrained(model_path)
9
10 device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
11 model.to(device)
12 model.eval() # Set model ke mode evaluasi
13
14 contoh_amr = """
15 (w / want-01
16 :ARG0 (b / boy)
17 :ARG1 (g / go-01
18 :ARG0 b
19 :ARG2 (c / cinema)))
20 """
21 prefix = "translate graph to indonesian: "
22 input_text = prefix + contoh_amr.strip() # Hilangkan spasi berlebih di awal/akhir
23
24 inputs = tokenizer(
25 input_text,
26 return_tensors='pt',
27 padding=True,
28 truncation=True,
29 max_length=512
30 )
31
32 inputs = {k: v.to(device) for k, v in inputs.items()}
33
34 print("Melakukan generasi teks...")
35 with torch.no_grad(): # Tidak perlu menghitung gradien saat inferensi
36 outputs = model.generate(
37 input_ids=inputs['input_ids'],
38 attention_mask=inputs['attention_mask'],
39 max_length=512, # Max length untuk output yang digenerasi
40 num_beams=5, # Contoh parameter beam search
41 repetition_penalty=2.5,
42 length_penalty=1.0,
43 early_stopping=True
44 )
45
46 generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
47
48 print("\nInput AMR:")
49 print(contoh_amr)
50 print("\nGenerated Indonesian Text:")
51 print(generated_text)1@INPROCEEDINGS{9932960,
2 author={Daryanto, Taufiq Husada and Khodra, Masayu Leylia},
3 booktitle={2022 9th International Conference on Advanced Informatics: Concepts, Theory and Applications (ICAICTA)},
4 title={Indonesian AMR-to-Text Generation by Language Model Fine-tuning},
5 year={2022},
6 volume={},
7 number={},
8 pages={1-6},
9 doi={10.1109/ICAICTA56449.2022.9932960}
10 }