from transformers import T5Tokenizer, T5ForConditionalGeneration | |
# Load the model and tokenizer | |
model_name = "t5-base" # lightweight and works offline | |
tokenizer = T5Tokenizer.from_pretrained(model_name) | |
model = T5ForConditionalGeneration.from_pretrained(model_name) | |
def generate_mcqs(text, num_questions=3): | |
input_text = f"generate questions: {text}" | |
input_ids = tokenizer.encode(input_text, return_tensors="pt", max_length=512, truncation=True) | |
outputs = model.generate( | |
input_ids=input_ids, | |
max_length=256, | |
num_return_sequences=1, | |
temperature=0.7 | |
) | |
decoded = tokenizer.decode(outputs[0], skip_special_tokens=True) | |
return decoded.strip() | |
# π½ TEST EXAMPLE π½ | |
if __name__ == "__main__": | |
sample = """ | |
The process of photosynthesis allows plants to convert sunlight, water, and carbon dioxide into food. This process takes place in the chloroplasts and releases oxygen as a byproduct. | |
""" | |
print("π Quiz Output:\n") | |
print(generate_mcqs(sample)) | |