sathwikabhavaraju2005 commited on
Commit
a7abe70
Β·
verified Β·
1 Parent(s): 1e691eb

Update utils/quiz_offline.py

Browse files
Files changed (1) hide show
  1. utils/quiz_offline.py +12 -22
utils/quiz_offline.py CHANGED
@@ -1,28 +1,18 @@
1
  from transformers import T5Tokenizer, T5ForConditionalGeneration
2
 
3
- # Load the model and tokenizer
4
- model_name = "t5-base" # lightweight and works offline
5
  tokenizer = T5Tokenizer.from_pretrained(model_name)
6
  model = T5ForConditionalGeneration.from_pretrained(model_name)
7
 
8
- def generate_mcqs(text, num_questions=3):
9
- input_text = f"generate questions: {text}"
10
- input_ids = tokenizer.encode(input_text, return_tensors="pt", max_length=512, truncation=True)
 
 
 
 
11
 
12
- outputs = model.generate(
13
- input_ids=input_ids,
14
- max_length=256,
15
- num_return_sequences=1,
16
- temperature=0.7
17
- )
18
-
19
- decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
20
- return decoded.strip()
21
-
22
- # πŸ”½ TEST EXAMPLE πŸ”½
23
- if __name__ == "__main__":
24
- sample = """
25
- 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.
26
- """
27
- print("πŸ“˜ Quiz Output:\n")
28
- print(generate_mcqs(sample))
 
1
  from transformers import T5Tokenizer, T5ForConditionalGeneration
2
 
3
+ model_name = "valhalla/t5-small-qg-hl"
 
4
  tokenizer = T5Tokenizer.from_pretrained(model_name)
5
  model = T5ForConditionalGeneration.from_pretrained(model_name)
6
 
7
+ def generate_question(context, answer):
8
+ highlighted = context.replace(answer, f"<hl> {answer} <hl>")
9
+ prompt = f"generate question: {highlighted}"
10
+ input_ids = tokenizer.encode(prompt, return_tensors="pt")
11
+ output = model.generate(input_ids, max_length=128)
12
+ question = tokenizer.decode(output[0], skip_special_tokens=True)
13
+ return question
14
 
15
+ # Example
16
+ context = "The mitochondria is responsible for producing energy in the cell."
17
+ answer = "mitochondria"
18
+ print(generate_question(context, answer))