Update README.md
Browse files
README.md
CHANGED
|
@@ -22,25 +22,44 @@ This is a mbert-based model for multiple-choice question answering.
|
|
| 22 |
Here is an example of how you can run this model:
|
| 23 |
|
| 24 |
```python
|
|
|
|
|
|
|
| 25 |
from transformers import AutoConfig, AutoModelForMultipleChoice, AutoTokenizer
|
| 26 |
|
| 27 |
model_name = "persiannlp/mbert-base-parsinlu-multiple-choice"
|
| 28 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 29 |
config = AutoConfig.from_pretrained(model_name)
|
| 30 |
-
model = AutoModelForMultipleChoice.from_pretrained(model_name, config
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
print(output)
|
| 36 |
return output
|
| 37 |
|
| 38 |
|
| 39 |
-
run_model("وسیع ترین کشور جهان کدام است؟
|
| 40 |
-
run_model("طامع یعنی ؟
|
| 41 |
run_model(
|
| 42 |
-
"زمینی به ۳۱ قطعه متساوی مفروض شده است و هر روز مساحت آماده شده برای احداث، دو برابر مساحت روز قبل است.اگر پس از (۵ روز) تمام زمین آماده شده باشد، در چه روزی یک قطعه زمین آماده شده
|
| 43 |
-
|
| 44 |
```
|
| 45 |
|
| 46 |
|
|
|
|
| 22 |
Here is an example of how you can run this model:
|
| 23 |
|
| 24 |
```python
|
| 25 |
+
from typing import List
|
| 26 |
+
import torch
|
| 27 |
from transformers import AutoConfig, AutoModelForMultipleChoice, AutoTokenizer
|
| 28 |
|
| 29 |
model_name = "persiannlp/mbert-base-parsinlu-multiple-choice"
|
| 30 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 31 |
config = AutoConfig.from_pretrained(model_name)
|
| 32 |
+
model = AutoModelForMultipleChoice.from_pretrained(model_name, config=config)
|
| 33 |
|
| 34 |
+
|
| 35 |
+
def run_model(question: str, candicates: List[str]):
|
| 36 |
+
assert len(candicates) == 4, "you need four candidates"
|
| 37 |
+
choices_inputs = []
|
| 38 |
+
for c in candicates:
|
| 39 |
+
text_a = "" # empty context
|
| 40 |
+
text_b = question + " " + c
|
| 41 |
+
inputs = tokenizer(
|
| 42 |
+
text_a,
|
| 43 |
+
text_b,
|
| 44 |
+
add_special_tokens=True,
|
| 45 |
+
max_length=128,
|
| 46 |
+
padding="max_length",
|
| 47 |
+
truncation=True,
|
| 48 |
+
return_overflowing_tokens=True,
|
| 49 |
+
)
|
| 50 |
+
choices_inputs.append(inputs)
|
| 51 |
+
|
| 52 |
+
input_ids = torch.LongTensor([x["input_ids"] for x in choices_inputs])
|
| 53 |
+
output = model(input_ids=input_ids)
|
| 54 |
print(output)
|
| 55 |
return output
|
| 56 |
|
| 57 |
|
| 58 |
+
run_model(question="وسیع ترین کشور جهان کدام است؟", candicates=["آمریکا", "کانادا", "روسیه", "چین"])
|
| 59 |
+
run_model(question="طامع یعنی ؟", candicates=["آزمند", "خوش شانس", "محتاج", "مطمئن"])
|
| 60 |
run_model(
|
| 61 |
+
question="زمینی به ۳۱ قطعه متساوی مفروض شده است و هر روز مساحت آماده شده برای احداث، دو برابر مساحت روز قبل است.اگر پس از (۵ روز) تمام زمین آماده شده باشد، در چه روزی یک قطعه زمین آماده شده ",
|
| 62 |
+
candicates=["روز اول", "روز دوم", "روز سوم", "هیچکدام"])
|
| 63 |
```
|
| 64 |
|
| 65 |
|