đ T5-Base Fine-Tuned on SQuAD for Question Generation
This project presents a T5-base model fine-tuned on the SQuAD dataset for question generation, offering an effective solution for generating relevant questions given an answer and context.
đ Quick Start
Prerequisites
Ensure you have torch
and transformers
libraries installed in your Python environment.
Code Example
import torch
from transformers import T5Tokenizer, T5ForConditionalGeneration
trained_model_path = 'ZhangCheng/T5-Base-Fine-Tuned-for-Question-Generation'
trained_tokenizer_path = 'ZhangCheng/T5-Base-Fine-Tuned-for-Question-Generation'
class QuestionGeneration:
def __init__(self, model_dir=None):
self.model = T5ForConditionalGeneration.from_pretrained(trained_model_path)
self.tokenizer = T5Tokenizer.from_pretrained(trained_tokenizer_path)
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
self.model = self.model.to(self.device)
self.model.eval()
def generate(self, answer: str, context: str):
input_text = '<answer> %s <context> %s ' % (answer, context)
encoding = self.tokenizer.encode_plus(
input_text,
return_tensors='pt'
)
input_ids = encoding['input_ids']
attention_mask = encoding['attention_mask']
outputs = self.model.generate(
input_ids=input_ids,
attention_mask=attention_mask
)
question = self.tokenizer.decode(
outputs[0],
skip_special_tokens=True,
clean_up_tokenization_spaces=True
)
return {'question': question, 'answer': answer, 'context': context}
if __name__ == "__main__":
context = 'ZhangCheng fine-tuned T5 on SQuAD dataset for question generation.'
answer = 'ZhangCheng'
QG = QuestionGeneration()
qa = QG.generate(answer, context)
print(qa['question'])
⨠Features
- Fine-Tuned on SQuAD: The model is fine-tuned on the SQuAD dataset, which enhances its performance in question generation.
- Easy to Use: With a simple Python class, you can easily generate questions based on given answers and contexts.
đĻ Installation
Since the code mainly depends on torch
and transformers
, you can install them using the following commands:
pip install torch
pip install transformers
đģ Usage Examples
Basic Usage
Advanced Usage
You can modify the generate
method in the QuestionGeneration
class to adjust the generation parameters, such as max_length
, num_beams
, etc., to get different generation results.
import torch
from transformers import T5Tokenizer, T5ForConditionalGeneration
trained_model_path = 'ZhangCheng/T5-Base-Fine-Tuned-for-Question-Generation'
trained_tokenizer_path = 'ZhangCheng/T5-Base-Fine-Tuned-for-Question-Generation'
class QuestionGeneration:
def __init__(self, model_dir=None):
self.model = T5ForConditionalGeneration.from_pretrained(trained_model_path)
self.tokenizer = T5Tokenizer.from_pretrained(trained_tokenizer_path)
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
self.model = self.model.to(self.device)
self.model.eval()
def generate(self, answer: str, context: str, max_length=50, num_beams=4):
input_text = '<answer> %s <context> %s ' % (answer, context)
encoding = self.tokenizer.encode_plus(
input_text,
return_tensors='pt'
)
input_ids = encoding['input_ids']
attention_mask = encoding['attention_mask']
outputs = self.model.generate(
input_ids=input_ids,
attention_mask=attention_mask,
max_length=max_length,
num_beams=num_beams
)
question = self.tokenizer.decode(
outputs[0],
skip_special_tokens=True,
clean_up_tokenization_spaces=True
)
return {'question': question, 'answer': answer, 'context': context}
if __name__ == "__main__":
context = 'ZhangCheng fine-tuned T5 on SQuAD dataset for question generation.'
answer = 'ZhangCheng'
QG = QuestionGeneration()
qa = QG.generate(answer, context, max_length=60, num_beams=5)
print(qa['question'])
đ Documentation
Model Details
- Model Type: T5-Base fine-tuned for question generation.
- Training Data: SQuAD dataset.
Input and Output
- Input: An answer and a context string.
- Output: A dictionary containing the generated question, the original answer, and the context.
đ License
No license information provided in the original document.