T5 Base finetuned for Question Generation

ZhangCheng

Introduction
The T5-Base Fine-tuned for Question Generation model by ZhangCheng is designed for generating questions based on a given context and answer. It utilizes the T5 architecture fine-tuned on the SQuAD dataset, making it adept at transforming input text into relevant questions.

Architecture
This model is based on the T5 (Text-to-Text Transfer Transformer) architecture. It employs a sequence-to-sequence approach, where both input and output are treated as text sequences. The model has been fine-tuned specifically for the task of question generation, leveraging the capabilities of the T5 architecture to understand and generate text in a coherent manner.

Training
The model was fine-tuned on the SQuAD dataset, which is widely recognized for question-answering tasks. This dataset provides a wide array of questions and context pairs, enabling the model to learn the nuances of question formation effectively. Fine-tuning involved adjusting the pre-trained T5 model weights on this specific dataset to optimize its performance for question generation tasks.

Guide: Running Locally
To run the model locally:

  1. Install Dependencies: Make sure you have transformers and torch libraries installed.

    pip install transformers torch
    
  2. Load the Model: Use the following Python script to load the model and generate questions.

    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):
            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'])
    
  3. Cloud GPUs: For better performance, especially with large datasets or batch processing, consider using cloud GPU services like AWS, Google Cloud, or Azure to accelerate computation.

License
For specific licensing details, please refer to the model card or the repository from which the model is sourced. It is important to comply with any usage restrictions or requirements specified therein.

More Related APIs in Text2text Generation