Update utils/quiz_offline.py
Browse files- utils/quiz_offline.py +12 -22
utils/quiz_offline.py
CHANGED
@@ -1,28 +1,18 @@
|
|
1 |
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
2 |
|
3 |
-
|
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
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
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))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|