Views
No views yet
[MASK] token during training, the model learns to handle scenarios where the answer is partially or completely missing, thereby improving its robustness and versatility.context: [context] answer: [MASK or answer] </s>[MASK] token 30% of the time. This forces the model to generate both the question and the answer even when provided with partial input.question: [question] answer: [answer] </s>| Metric | Question | Answer |
|---|---|---|
| BLEU-1 | 0.3127 | 0.7243 |
| BLEU-2 | 0.2073 | 0.5448 |
| BLEU-3 | 0.1526 | 0.4036 |
| BLEU-4 | 0.1159 | 0.3127 |
1from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
2
3
4model_name = "fares7elsadek/t5-base-finetuned-question-generation"
5tokenizer = AutoTokenizer.from_pretrained(model_name)
6model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
7
8def generate_qa(context, answer="[MASK]", max_length=64):
9 """
10 Generates a question and answer pair from the provided context.
11
12 Args:
13 context (str): The context passage.
14 answer (str): The answer text. Use "[MASK]" to prompt the model to predict the answer.
15 max_length (int): Maximum length of the generated sequence.
16
17 Returns:
18 str: The generated question and answer pair.
19 """
20 input_text = f"context: {context} answer: {answer} </s>"
21 inputs = tokenizer([input_text], return_tensors="pt", truncation=True, padding=True)
22 outputs = model.generate(
23 input_ids=inputs["input_ids"],
24 attention_mask=inputs["attention_mask"],
25 max_length=max_length
26 )
27 return tokenizer.decode(outputs[0], skip_special_tokens=True)
28
29# Example inference:
30context = "The Eiffel Tower was constructed in 1889 for the World's Fair in Paris."
31answer = "The Eiffel Tower" # Alternatively, use "[MASK]" to have the model predict the answer
32print(generate_qa(context, answer))