Views
No views yet
csebuetnlp/banglat5| Epoch | Training Loss | Validation Loss |
|---|---|---|
| 1 | 3.7985 | 1.3028 |
| 2 | 1.5408 | 0.7553 |
| 3 | 1.0926 | 0.4264 |
| 4 | 0.8402 | 0.4072 |
| 5 | 0.6662 | 0.3555 |
| 6 | 0.5223 | 0.2869 |
| 7 | 0.4514 | 0.2869 |
| 8 | 0.3983 | 0.2172 |
| 9 | 0.3581 | 0.1853 |
| 10 | 0.3067 | 0.1402 |
| 11 | 0.2754 | 0.1678 |
| 12 | 0.2639 | 0.1041 |
| 13 | 0.2587 | 0.1537 |
| 14 | 0.2415 | 0.0902 |
| 15 | 0.2043 | 0.1247 |
1from transformers import T5Tokenizer, T5ForConditionalGeneration
2import torch
3
4MODEL = T5ForConditionalGeneration.from_pretrained("shaanzeeeee/banglaT5forQnAfinetuned")
5TOKENIZER = T5Tokenizer.from_pretrained("shaanzeeeee/banglaT5forQnAfinetuned")
6DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
7MODEL.to(DEVICE)
8
9def predict_answer(context, question, ref_answer=None):
10 inputs = TOKENIZER(question, context, max_length=Q_LEN, padding="max_length", truncation=True, add_special_tokens=True)
11
12 input_ids = torch.tensor(inputs["input_ids"], dtype=torch.long).to(DEVICE).unsqueeze(0)
13 attention_mask = torch.tensor(inputs["attention_mask"], dtype=torch.long).to(DEVICE).unsqueeze(0)
14
15 outputs = MODEL.generate(input_ids=input_ids, attention_mask=attention_mask)
16
17 predicted_answer = TOKENIZER.decode(outputs.flatten(), skip_special_tokens=True)
18
19 if ref_answer:
20 # Load the Bleu metric
21 #bleu = evaluate.load("google_bleu")
22 #score = bleu.compute(predictions=[predicted_answer],
23 #references=[ref_answer])
24
25 print("Context: \n", context)
26 print("\n")
27 print("Question: \n", question)
28 return {
29 "Reference Answer: ": ref_answer,
30 "Predicted Answer: ": predicted_answer,
31 #"BLEU Score: ": score
32 }
33 else:
34 return predicted_answer
35
36
37context = ""
38question = ""
39ref_answer = ""
40predict_answer(context, question, ref_answer)