File size: 1,031 Bytes
e2224e6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
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))
|