Avinash250325 commited on
Commit
e7d3536
·
verified ·
1 Parent(s): 62f90c5

Update backend.py

Browse files
Files changed (1) hide show
  1. backend.py +35 -17
backend.py CHANGED
@@ -1,34 +1,52 @@
1
- from transformers import T5ForConditionalGeneration, T5Tokenizer
2
- import torch
3
 
4
- # Load model and tokenizer once
5
  model_name = "./T5base_Question_Generation"
6
  tokenizer = T5Tokenizer.from_pretrained(model_name)
7
  model = T5ForConditionalGeneration.from_pretrained(model_name)
8
 
9
- def generate_questions(context, question_type, difficulty, num_questions=1):
10
  """
11
- Generates questions using T5 model based on the provided context, question type, and difficulty.
 
 
 
 
 
 
 
 
 
 
 
12
  """
13
- # Build the prompt
14
- prompt = f"Generate {difficulty} {question_type} question based on: {context}"
 
15
 
16
  # Tokenize input
17
- input_ids = tokenizer.encode(prompt, return_tensors="pt", truncation=True)
18
 
19
- # Generate output
20
  if num_questions == 1:
21
- outputs = model.generate(input_ids, max_length=64)
 
 
 
 
 
22
  else:
23
- outputs = model.generate(
24
- input_ids,
25
- max_length=64,
 
26
  do_sample=True,
27
- top_k=50,
28
  top_p=0.95,
29
- num_return_sequences=num_questions,
 
30
  )
31
 
32
- # Decode and return questions
33
- questions = [tokenizer.decode(output, skip_special_tokens=True) for output in outputs]
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
+