smart-lms-suite / utils /quiz_offline.py
sathwikabhavaraju2005's picture
Rename utils/quiz_generator.py to utils/quiz_offline.py
e2224e6 verified
raw
history blame
1.03 kB
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))