Spaces:
Runtime error
Runtime error
Update backend.py
Browse files- backend.py +35 -17
backend.py
CHANGED
|
@@ -1,34 +1,52 @@
|
|
| 1 |
-
from transformers import
|
| 2 |
-
import torch
|
| 3 |
|
| 4 |
-
#
|
| 5 |
model_name = "./T5base_Question_Generation"
|
| 6 |
tokenizer = T5Tokenizer.from_pretrained(model_name)
|
| 7 |
model = T5ForConditionalGeneration.from_pretrained(model_name)
|
| 8 |
|
| 9 |
-
def
|
| 10 |
"""
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
"""
|
| 13 |
-
#
|
| 14 |
-
|
|
|
|
| 15 |
|
| 16 |
# Tokenize input
|
| 17 |
-
|
| 18 |
|
| 19 |
-
#
|
| 20 |
if num_questions == 1:
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
else:
|
| 23 |
-
|
| 24 |
-
input_ids,
|
| 25 |
-
|
|
|
|
| 26 |
do_sample=True,
|
| 27 |
-
top_k=50,
|
| 28 |
top_p=0.95,
|
| 29 |
-
|
|
|
|
| 30 |
)
|
| 31 |
|
| 32 |
-
# Decode
|
| 33 |
-
questions = [tokenizer.decode(
|
| 34 |
return questions
|
|
|
|
|
|
| 1 |
+
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
|
|
|
| 2 |
|
| 3 |
+
# Loading the fine-tuned model
|
| 4 |
model_name = "./T5base_Question_Generation"
|
| 5 |
tokenizer = T5Tokenizer.from_pretrained(model_name)
|
| 6 |
model = T5ForConditionalGeneration.from_pretrained(model_name)
|
| 7 |
|
| 8 |
+
def get_question(tag, difficulty, context, answer="", num_questions=1, max_length=150):
|
| 9 |
"""
|
| 10 |
+
Generate questions using the fine-tuned T5 model
|
| 11 |
+
|
| 12 |
+
Parameters:
|
| 13 |
+
- tag: Type of question (e.g., "short answer", "multiple choice question", "true or false question")
|
| 14 |
+
- difficulty: "easy", "medium", "hard"
|
| 15 |
+
- context: Supporting context or passage
|
| 16 |
+
- answer: Optional — if you want targeted question generation
|
| 17 |
+
- num_questions: Number of diverse questions to generate
|
| 18 |
+
- max_length: Max token length of generated output
|
| 19 |
+
|
| 20 |
+
Returns:
|
| 21 |
+
- List of generated questions as strings
|
| 22 |
"""
|
| 23 |
+
# Format input text based on whether answer is provided
|
| 24 |
+
answer_part = f"[{answer}]" if answer else ""
|
| 25 |
+
input_text = f"<extra_id_97>{tag} <extra_id_98>{difficulty} <extra_id_99>{answer_part} {context}"
|
| 26 |
|
| 27 |
# Tokenize input
|
| 28 |
+
features = tokenizer([input_text], return_tensors='pt', truncation=True, padding=True)
|
| 29 |
|
| 30 |
+
# Decide generation strategy
|
| 31 |
if num_questions == 1:
|
| 32 |
+
output = model.generate(
|
| 33 |
+
input_ids=features['input_ids'],
|
| 34 |
+
attention_mask=features['attention_mask'],
|
| 35 |
+
max_length=max_length,
|
| 36 |
+
do_sample=False
|
| 37 |
+
)
|
| 38 |
else:
|
| 39 |
+
output = model.generate(
|
| 40 |
+
input_ids=features['input_ids'],
|
| 41 |
+
attention_mask=features['attention_mask'],
|
| 42 |
+
max_length=max_length,
|
| 43 |
do_sample=True,
|
|
|
|
| 44 |
top_p=0.95,
|
| 45 |
+
top_k=50,
|
| 46 |
+
num_return_sequences=num_questions
|
| 47 |
)
|
| 48 |
|
| 49 |
+
# Decode questions
|
| 50 |
+
questions = [tokenizer.decode(out, skip_special_tokens=True) for out in output]
|
| 51 |
return questions
|
| 52 |
+
|