Views
No views yet
google-bert/bert-base-multilingual-cased and fine-tuned for extractive question answering on Kazakh text. The model can answer questions based on a given context by predicting the start and end positions of the answer span.1from transformers import AutoTokenizer, AutoModelForQuestionAnswering
2import torch
3
4model_name = "r3iwan/kazakh-question-answering"
5try:
6 tokenizer = AutoTokenizer.from_pretrained(model_name)
7except:
8 tokenizer = AutoTokenizer.from_pretrained("google-bert/bert-base-multilingual-cased")
9
10model = AutoModelForQuestionAnswering.from_pretrained(model_name)
11model.eval()
12
13context = "Қазақстан - Орталық Азиядағы мемлекет. Оның астанасы Астана қаласы."
14question = "Қазақстанның астанасы қайда?"
15
16inputs = tokenizer(
17 question,
18 context,
19 return_tensors="pt",
20 padding=True,
21 truncation="only_second",
22 max_length=512
23)
24
25# Get predictions
26with torch.no_grad():
27 outputs = model(**inputs)
28 start_scores = outputs.start_logits[0]
29 end_scores = outputs.end_logits[0]
30
31num_candidates = 20
32max_span_length = 30
33
34# Get top start and end candidates
35start_candidates = torch.topk(start_scores, num_candidates)
36end_candidates = torch.topk(end_scores, num_candidates)
37
38best_score = float('-inf')
39best_start = 0
40best_end = 0
41
42for start_idx in start_candidates.indices:
43 for end_idx in end_candidates.indices:
44 if start_idx <= end_idx and (end_idx - start_idx) <= max_span_length:
45 score = start_scores[start_idx].item() + end_scores[end_idx].item()
46 if score > best_score:
47 best_score = score
48 best_start = start_idx.item()
49 best_end = end_idx.item()
50
51# Extract answer tokens
52answer_tokens = inputs["input_ids"][0][best_start:best_end+1]
53answer = tokenizer.decode(answer_tokens, skip_special_tokens=True)
54
55print(f"Question: {question}")
56print(f"Answer: {answer}")google-bert/bert-base-multilingual-cased| Epoch | Training Loss | Validation Loss |
|---|---|---|
| 1 | - | 2.760520 |
| 2 | 2.372000 | 2.854473 |
| 3 | 1.347700 | 3.108089 |
google/mt5-smallgoogle-bert/bert-base-multilingual-cased1torch>=2.1.0
2transformers==5.0.0rc1
3datasets>=2.14.0,<3.0.0
4accelerate>=0.24.0
5tiktoken>=0.1.0
6sentencepiece>=0.1.99
7numpy>=1.25.2,<2.0.0
8pandas>=2.0.0
9tqdm>=4.66.0pip install torch transformers datasets accelerate tiktoken sentencepiece numpy pandas tqdmpip install -r requirements.txt1@inproceedings{yeshpanov-etal-2024-kazqad,
2 title = "{K}az{QAD}: {K}azakh Open-Domain Question Answering Dataset",
3 author = "Yeshpanov, Rustem and
4 Efimov, Pavel and
5 Boytsov, Leonid and
6 Shalkarbayuli, Ardak and
7 Braslavski, Pavel",
8 editor = "Calzolari, Nicoletta and
9 Kan, Min-Yen and
10 Hoste, Veronique and
11 Lenci, Alessandro and
12 Sakti, Sakriani and
13 Xue, Nianwen",
14 booktitle = "Proceedings of the 2024 Joint International Conference on Computational Linguistics, Language Resources and Evaluation (LREC-COLING 2024)",
15 month = may,
16 year = "2024",
17 address = "Torino, Italia",
18 publisher = "ELRA and ICCL",
19 url = "https://aclanthology.org/2024.lrec-main.843",
20 pages = "9645--9656"
21}