How to Build a Multilingual Multiple-Choice Question Answering Model

Sep 25, 2021 | Educational

In today’s world of Artificial Intelligence, question-answering systems have become essential. One powerful way to accomplish this is by using a multilingual BERT (mBERT) model specifically designed for multiple-choice questions. In this article, we will walk you through how to run such a model using the Transformers library from Hugging Face.

Setting Up Your Environment

Before diving into the code, ensure you have the required libraries installed. You need Python, PyTorch, and the Transformers library. You can install the necessary packages using pip:

pip install torch transformers

Understanding the Code: A Simplified Analogy

Let’s imagine you are throwing a party and need to decide on the best dish among four options: Pizza, Sushi, Burger, and Salad. Each dish represents a candidate answer and your task is to pick the best one based on a question you have, like “What would everyone like the most?”.

Our model operates similarly where:

  • The question acts as the main theme of your inquiry.
  • The candidates are your options (in this case, Pizza, Sushi, Burger, or Salad).

The model evaluates and uses its learned knowledge (like your tastes and experiences) to determine which candidate best fits the given question.

Step-by-Step Implementation

Now let’s break down the code you need to implement this system:

from typing import List
import torch
from transformers import AutoConfig, AutoModelForMultipleChoice, AutoTokenizer

model_name = "persiannlp/mbert-base-parsinlu-multiple-choice"

tokenizer = AutoTokenizer.from_pretrained(model_name)
config = AutoConfig.from_pretrained(model_name)
model = AutoModelForMultipleChoice.from_pretrained(model_name, config=config)

def run_model(question: str, candicates: List[str]):
    assert len(candicates) == 4, "You need four candidates"
    choices_inputs = []
    for c in candicates:
        text_a = ""  # empty context
        text_b = question + " " + c
        inputs = tokenizer(
            text_a,
            text_b,
            add_special_tokens=True,
            max_length=128,
            padding='max_length',
            truncation=True,
            return_overflowing_tokens=True,
        )
        choices_inputs.append(inputs)
    input_ids = torch.LongTensor([x['input_ids'] for x in choices_inputs])
    output = model(input_ids=input_ids)
    print(output)
    return output

# Example runs
run_model(question="وسیع ترین کشور جهان کدام است؟", candicates=["آمریکا", "کانادا", "روسیه", "چین"])
run_model(question="طامع یعنی ؟", candicates=["آزمند", "خوش شانس", "محتاج", "مطمئن"])
run_model(
    question="زمینی به ۳۱ قطعه متساوی مفروض شده است و هر روز مساحت آماده شده برای احداث، دو برابر مساحت روز قبل است.اگر پس از (۵ روز) تمام زمین آماده شده باشد، در چه روزی یک قطعه زمین آماده شده ؟",
    candicates=["روز اول", "روز دوم", "روز سوم", "هیچکدام"]
)

Running the Model

After setting up the model, you can feed it any question along with four candidates. The example runs provided in the code help demonstrate how to do this effectively.

Troubleshooting

If you encounter any issues while running this model, here are some ideas to help you troubleshoot:

  • Make sure all required libraries are installed and properly imported.
  • Check your question and candidate inputs to ensure that the number of candidates is exactly four.
  • If you’re facing memory issues, try reducing the max length of input or run the model on a machine with more resources.
  • If you want to understand the results better, consider printing the output in a more interpretable format.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox