Views
No views yet
1from typing import List
2import torch
3from transformers import AutoConfig, AutoModelForMultipleChoice, AutoTokenizer
4
5model_name = "persiannlp/wikibert-base-parsinlu-multiple-choice"
6tokenizer = AutoTokenizer.from_pretrained(model_name)
7config = AutoConfig.from_pretrained(model_name)
8model = AutoModelForMultipleChoice.from_pretrained(model_name, config=config)
9
10
11def run_model(question: str, candicates: List[str]):
12 assert len(candicates) == 4, "you need four candidates"
13 choices_inputs = []
14 for c in candicates:
15 text_a = "" # empty context
16 text_b = question + " " + c
17 inputs = tokenizer(
18 text_a,
19 text_b,
20 add_special_tokens=True,
21 max_length=128,
22 padding="max_length",
23 truncation=True,
24 return_overflowing_tokens=True,
25 )
26 choices_inputs.append(inputs)
27
28 input_ids = torch.LongTensor([x["input_ids"] for x in choices_inputs])
29 output = model(input_ids=input_ids)
30 print(output)
31 return output
32
33
34run_model(question="وسیع ترین کشور جهان کدام است؟", candicates=["آمریکا", "کانادا", "روسیه", "چین"])
35run_model(question="طامع یعنی ؟", candicates=["آزمند", "خوش شانس", "محتاج", "مطمئن"])
36run_model(
37 question="زمینی به ۳۱ قطعه متساوی مفروض شده است و هر روز مساحت آماده شده برای احداث، دو برابر مساحت روز قبل است.اگر پس از (۵ روز) تمام زمین آماده شده باشد، در چه روزی یک قطعه زمین آماده شده ",
38 candicates=["روز اول", "روز دوم", "روز سوم", "هیچکدام"])
39